udpip_tb: able to send valid UDP msg to model
[litex.git] / liteeth / core / udp.py
1 from liteeth.common import *
2 from liteeth.generic.depacketizer import LiteEthDepacketizer
3 from liteeth.generic.packetizer import LiteEthPacketizer
4
5 class LiteEthUDPDepacketizer(LiteEthDepacketizer):
6 def __init__(self):
7 LiteEthDepacketizer.__init__(self,
8 eth_ipv4_user_description(8),
9 eth_udp_description(8),
10 udp_header,
11 udp_header_len)
12
13 class LiteEthUDPPacketizer(LiteEthPacketizer):
14 def __init__(self):
15 LiteEthPacketizer.__init__(self,
16 eth_udp_description(8),
17 eth_ipv4_user_description(8),
18 udp_header,
19 udp_header_len)
20
21 class LiteEthUDPTX(Module):
22 def __init__(self, ip_address):
23 self.sink = Sink(eth_udp_user_description(8))
24 self.source = Source(eth_ipv4_user_description(8))
25 ###
26 packetizer = LiteEthUDPPacketizer()
27 self.submodules += packetizer
28 self.comb += [
29 packetizer.sink.stb.eq(self.sink.stb),
30 packetizer.sink.sop.eq(self.sink.sop),
31 packetizer.sink.eop.eq(self.sink.eop),
32 self.sink.ack.eq(packetizer.sink.ack),
33 packetizer.sink.source_port.eq(self.sink.source_port),
34 packetizer.sink.destination_port.eq(self.sink.destination_port),
35 packetizer.sink.length.eq(self.sink.length + udp_header_len),
36 packetizer.sink.checksum.eq(0),
37 packetizer.sink.data.eq(self.sink.data)
38 ]
39 sink = packetizer.source
40
41 fsm = FSM(reset_state="IDLE")
42 self.submodules += fsm
43 fsm.act("IDLE",
44 sink.ack.eq(1),
45 If(sink.stb & sink.sop,
46 sink.ack.eq(0),
47 NextState("SEND")
48 )
49 )
50 fsm.act("SEND",
51 Record.connect(packetizer.source, self.source),
52 self.source.length.eq(packetizer.sink.length + ipv4_header_len),
53 self.source.protocol.eq(udp_protocol),
54 self.source.ip_address.eq(self.sink.ip_address),
55 If(self.source.stb & self.source.eop & self.source.ack,
56 NextState("IDLE")
57 )
58 )
59
60 class LiteEthUDPRX(Module):
61 def __init__(self, ip_address):
62 self.sink = Sink(eth_ipv4_user_description(8))
63 self.source = source = Source(eth_udp_user_description(8))
64 ###
65 depacketizer = LiteEthUDPDepacketizer()
66 self.submodules += depacketizer
67 self.comb += Record.connect(self.sink, depacketizer.sink)
68 sink = depacketizer.source
69
70 fsm = FSM(reset_state="IDLE")
71 self.submodules += fsm
72 fsm.act("IDLE",
73 sink.ack.eq(1),
74 If(sink.stb & sink.sop,
75 sink.ack.eq(0),
76 NextState("CHECK")
77 )
78 )
79 valid = Signal()
80 self.comb += valid.eq(
81 sink.stb &
82 (self.sink.protocol == udp_protocol) &
83 (self.sink.ip_address == ip_address)
84 )
85
86 fsm.act("CHECK",
87 If(valid,
88 NextState("PRESENT")
89 ).Else(
90 NextState("DROP")
91 )
92 ),
93 fsm.act("PRESENT",
94 source.stb.eq(sink.stb),
95 source.sop.eq(sink.sop),
96 source.eop.eq(sink.eop),
97 sink.ack.eq(source.ack),
98 source.source_port.eq(sink.source_port),
99 source.destination_port.eq(sink.destination_port),
100 source.ip_address.eq(0),
101 source.length.eq(sink.length - udp_header_len),
102 source.data.eq(sink.data),
103 source.error.eq(sink.error),
104 If(source.stb & source.eop & source.ack,
105 NextState("IDLE")
106 )
107 )
108 fsm.act("DROP",
109 sink.ack.eq(1),
110 If(source.stb & source.eop & source.ack,
111 NextState("IDLE")
112 )
113 )
114
115 class LiteEthUDP(Module):
116 def __init__(self, ip, ip_address):
117 self.submodules.tx = LiteEthUDPTX(ip_address)
118 self.submodules.rx = LiteEthUDPRX(ip_address)
119 self.comb += [
120 Record.connect(self.tx.source, ip.sink),
121 Record.connect(ip.source, self.rx.sink)
122 ]
123 self.sink, self.source = self.tx.sink, self.rx.source