From 5b37068393ac17781e0b7f1f7e42b62e49637c8c Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 29 Jan 2015 00:25:55 +0100 Subject: [PATCH] make packetizer/depacketizer generic and use it for all layers --- liteeth/arp/__init__.py | 19 +++++++++++++++++++ liteeth/common.py | 3 ++- liteeth/generic/__init__.py | 0 liteeth/{mac/core => generic}/depacketizer.py | 18 ++++++++---------- liteeth/{mac/core => generic}/packetizer.py | 18 +++++++++--------- liteeth/ip/__init__.py | 19 +++++++++++++++++++ liteeth/mac/__init__.py | 18 ++++++++++++++++++ liteeth/test/mac_core_tb.py | 8 ++++---- liteeth/udp/__init__.py | 19 +++++++++++++++++++ 9 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 liteeth/generic/__init__.py rename liteeth/{mac/core => generic}/depacketizer.py (73%) rename liteeth/{mac/core => generic}/packetizer.py (74%) diff --git a/liteeth/arp/__init__.py b/liteeth/arp/__init__.py index e69de29bb..3e09b0ede 100644 --- a/liteeth/arp/__init__.py +++ b/liteeth/arp/__init__.py @@ -0,0 +1,19 @@ +from liteeth.common import * +from liteeth.generic.depacketizer import LiteEthDepacketizer +from liteeth.generic.packetizer import LiteEthPacketizer + +class LiteEthARPDepacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_mac_description(8), + eth_arp_description(8), + arp_header, + arp_header_length) + +class LiteEthARPPacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_arp_description(8), + eth_mac_description(8), + arp_header, + arp_header_length) diff --git a/liteeth/common.py b/liteeth/common.py index 88433e5c1..2fc2660f7 100644 --- a/liteeth/common.py +++ b/liteeth/common.py @@ -1,7 +1,7 @@ from collections import OrderedDict from migen.fhdl.std import * -from migen.fhdl.std import * +from migen.fhdl.decorators import ModuleDecorator from migen.genlib.resetsync import AsyncResetSynchronizer from migen.genlib.record import * from migen.genlib.fsm import FSM, NextState @@ -160,3 +160,4 @@ class BufferizeEndpoints(ModuleDecorator): self.submodules += buf self.comb += Record.connect(source, buf.d) setattr(self, name, buf.q) + diff --git a/liteeth/generic/__init__.py b/liteeth/generic/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/liteeth/mac/core/depacketizer.py b/liteeth/generic/depacketizer.py similarity index 73% rename from liteeth/mac/core/depacketizer.py rename to liteeth/generic/depacketizer.py index b0e1a08e7..ff7308a47 100644 --- a/liteeth/mac/core/depacketizer.py +++ b/liteeth/generic/depacketizer.py @@ -1,5 +1,3 @@ -import math - from liteeth.common import * def _decode_header(h_dict, h_signal, obj): @@ -10,14 +8,14 @@ def _decode_header(h_dict, h_signal, obj): r.append(getattr(obj, k).eq(h_signal[start:end])) return r -class LiteEthMACDepacketizer(Module): - def __init__(self): - self.sink = sink = Sink(eth_mac_description(8)) - self.source = source = Source(eth_phy_description(8)) +class LiteEthDepacketizer(Module): + def __init__(self, sink_description, source_description, header_type, header_length): + self.sink = sink = Sink(sink_description) + self.source = source = Source(source_description) ### shift = Signal() - header = Signal(mac_header_length*8) - counter = Counter(max=mac_header_length) + header = Signal(header_length*8) + counter = Counter(max=header_length) self.submodules += counter fsm = FSM(reset_state="IDLE") @@ -36,7 +34,7 @@ class LiteEthMACDepacketizer(Module): If(sink.stb, counter.ce.eq(1), shift.eq(1), - If(counter.value == mac_header_length-2, + If(counter.value == header_length-2, NextState("COPY") ) ) @@ -52,7 +50,7 @@ class LiteEthMACDepacketizer(Module): source.eop.eq(sink.eop), source.data.eq(sink.data), source.error.eq(sink.error), - _decode_header(mac_header, header, source) + _decode_header(header_type, header, source) ] fsm.act("COPY", sink.ack.eq(source.ack), diff --git a/liteeth/mac/core/packetizer.py b/liteeth/generic/packetizer.py similarity index 74% rename from liteeth/mac/core/packetizer.py rename to liteeth/generic/packetizer.py index 66600216a..c10ff9366 100644 --- a/liteeth/mac/core/packetizer.py +++ b/liteeth/generic/packetizer.py @@ -8,19 +8,19 @@ def _encode_header(h_dict, h_signal, obj): r.append(h_signal[start:end].eq(getattr(obj, k))) return r -class LiteEthMACPacketizer(Module): - def __init__(self): - self.sink = sink = Sink(eth_phy_description(8)) - self.source = source = Source(eth_mac_description(8)) +class LiteEthPacketizer(Module): + def __init__(self, sink_description, source_description, header_type, header_length): + self.sink = sink = Sink(sink_description) + self.source = source = Source(source_description) ### - header = Signal(mac_header_length*8) - header_reg = Signal(mac_header_length*8) + header = Signal(header_length*8) + header_reg = Signal(header_length*8) load = Signal() shift = Signal() - counter = Counter(max=mac_header_length) + counter = Counter(max=header_length) self.submodules += counter - self.comb += header.eq(_encode_header(mac_header, header, sink)) + self.comb += header.eq(_encode_header(header_type, header, sink)) self.sync += [ If(load, header_reg.eq(header) @@ -53,7 +53,7 @@ class LiteEthMACPacketizer(Module): source.data.eq(header_reg[8:16]), If(source.stb & source.ack, sink.ack.eq(1), - If(counter == mac_header_length-2, + If(counter == header_length-2, NextState("COPY") ) ) diff --git a/liteeth/ip/__init__.py b/liteeth/ip/__init__.py index e69de29bb..3dbc7c016 100644 --- a/liteeth/ip/__init__.py +++ b/liteeth/ip/__init__.py @@ -0,0 +1,19 @@ +from liteeth.common import * +from liteeth.generic.depacketizer import LiteEthDepacketizer +from liteeth.generic.packetizer import LiteEthPacketizer + +class LiteEthIPV4Depacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_mac_description(8), + eth_ipv4_description(8), + ipv4_header, + ipv4_header_length) + +class LiteEthIPV4Packetizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_ipv4_description(8), + eth_mac_description(8), + ipv4_header, + ipv4_header_length) diff --git a/liteeth/mac/__init__.py b/liteeth/mac/__init__.py index 7676f38a4..a983c301c 100644 --- a/liteeth/mac/__init__.py +++ b/liteeth/mac/__init__.py @@ -1,6 +1,24 @@ from liteeth.common import * from liteeth.mac.core import LiteEthMACCore from liteeth.mac.frontend import wishbone +from liteeth.generic.depacketizer import LiteEthDepacketizer +from liteeth.generic.packetizer import LiteEthPacketizer + +class LiteEthMACDepacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_phy_description(8), + eth_mac_description(8), + mac_header, + mac_header_length) + +class LiteEthMACPacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_mac_description(8), + eth_phy_description(8), + mac_header, + mac_header_length) class LiteEthMAC(Module, AutoCSR): def __init__(self, phy, dw, interface="core", endianness="be", diff --git a/liteeth/test/mac_core_tb.py b/liteeth/test/mac_core_tb.py index 810390dc9..fe1e42853 100644 --- a/liteeth/test/mac_core_tb.py +++ b/liteeth/test/mac_core_tb.py @@ -15,11 +15,11 @@ class TB(Module): self.submodules.hostmac = mac.MAC(self.hostphy, debug=False, loopback=True) self.submodules.ethmac = LiteEthMAC(phy=self.hostphy, dw=32, interface="core", with_hw_preamble_crc=True) - self.submodules.streamer = PacketStreamer(eth_mac_description(32), last_be=1) - self.submodules.streamer_randomizer = AckRandomizer(eth_mac_description(32), level=50) + self.submodules.streamer = PacketStreamer(eth_phy_description(32), last_be=1) + self.submodules.streamer_randomizer = AckRandomizer(eth_phy_description(32), level=50) - self.submodules.logger_randomizer = AckRandomizer(eth_mac_description(32), level=50) - self.submodules.logger = PacketLogger(eth_mac_description(32)) + self.submodules.logger_randomizer = AckRandomizer(eth_phy_description(32), level=50) + self.submodules.logger = PacketLogger(eth_phy_description(32)) # use sys_clk for each clock_domain self.clock_domains.cd_eth_rx = ClockDomain() diff --git a/liteeth/udp/__init__.py b/liteeth/udp/__init__.py index e69de29bb..9f8b5dafc 100644 --- a/liteeth/udp/__init__.py +++ b/liteeth/udp/__init__.py @@ -0,0 +1,19 @@ +from liteeth.common import * +from liteeth.generic.depacketizer import LiteEthDepacketizer +from liteeth.generic.packetizer import LiteEthPacketizer + +class LiteEthUDPDepacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_ipv4_description(8), + eth_udp_description(8), + udp_header, + udp_header_length) + +class LiteEthUDPPacketizer(LiteEthDepacketizer): + def __init__(self): + LiteEthDepacketizer.__init__(self, + eth_udp_description(8), + eth_ipv4_description(8), + udp_header, + udp_header_length)