2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.common import *
|
|
|
|
from misoclib.liteeth.generic import *
|
2015-01-28 18:02:50 -05:00
|
|
|
|
|
|
|
def _decode_header(h_dict, h_signal, obj):
|
|
|
|
r = []
|
|
|
|
for k, v in sorted(h_dict.items()):
|
|
|
|
start = v.byte*8+v.offset
|
|
|
|
end = start+v.width
|
2015-01-30 04:48:56 -05:00
|
|
|
r.append(getattr(obj, k).eq(reverse_bytes(h_signal[start:end])))
|
2015-01-28 18:02:50 -05:00
|
|
|
return r
|
|
|
|
|
2015-01-28 18:25:55 -05:00
|
|
|
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)
|
2015-02-04 13:35:38 -05:00
|
|
|
self.header = Signal(header_length*8)
|
2015-01-28 18:02:50 -05:00
|
|
|
###
|
2015-02-10 03:25:36 -05:00
|
|
|
dw = flen(sink.data)
|
|
|
|
|
2015-02-11 10:21:06 -05:00
|
|
|
header_words = (header_length*8)//dw
|
|
|
|
|
2015-01-28 18:02:50 -05:00
|
|
|
shift = Signal()
|
2015-02-11 10:21:06 -05:00
|
|
|
counter = Counter(max=max(header_words, 2))
|
2015-01-28 18:02:50 -05:00
|
|
|
self.submodules += counter
|
|
|
|
|
2015-02-11 10:21:06 -05:00
|
|
|
if header_words == 1:
|
|
|
|
self.sync += \
|
|
|
|
If(shift,
|
|
|
|
self.header.eq(sink.data)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.sync += \
|
|
|
|
If(shift,
|
|
|
|
self.header.eq(Cat(self.header[dw:], sink.data))
|
|
|
|
)
|
2015-01-30 04:48:56 -05:00
|
|
|
|
2015-01-28 18:02:50 -05:00
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
|
2015-02-11 10:21:06 -05:00
|
|
|
if header_words == 1:
|
|
|
|
idle_next_state = "COPY"
|
|
|
|
else:
|
|
|
|
idle_next_state = "RECEIVE_HEADER"
|
|
|
|
|
2015-01-28 18:02:50 -05:00
|
|
|
fsm.act("IDLE",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
counter.reset.eq(1),
|
|
|
|
If(sink.stb,
|
|
|
|
shift.eq(1),
|
2015-02-11 10:21:06 -05:00
|
|
|
NextState(idle_next_state)
|
2015-01-28 18:02:50 -05:00
|
|
|
)
|
|
|
|
)
|
2015-02-11 10:21:06 -05:00
|
|
|
if header_words != 1:
|
|
|
|
fsm.act("RECEIVE_HEADER",
|
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb,
|
|
|
|
counter.ce.eq(1),
|
|
|
|
shift.eq(1),
|
|
|
|
If(counter.value == header_words-2,
|
|
|
|
NextState("COPY")
|
|
|
|
)
|
2015-01-28 18:02:50 -05:00
|
|
|
)
|
|
|
|
)
|
2015-02-11 08:33:17 -05:00
|
|
|
no_payload = Signal()
|
2015-01-28 18:02:50 -05:00
|
|
|
self.sync += \
|
|
|
|
If(fsm.before_entering("COPY"),
|
2015-02-11 08:33:17 -05:00
|
|
|
source.sop.eq(1),
|
|
|
|
no_payload.eq(sink.eop)
|
2015-01-28 18:02:50 -05:00
|
|
|
).Elif(source.stb & source.ack,
|
|
|
|
source.sop.eq(0)
|
|
|
|
)
|
|
|
|
self.comb += [
|
2015-02-11 08:33:17 -05:00
|
|
|
source.eop.eq(sink.eop | no_payload),
|
2015-01-28 18:02:50 -05:00
|
|
|
source.data.eq(sink.data),
|
|
|
|
source.error.eq(sink.error),
|
2015-02-04 13:35:38 -05:00
|
|
|
_decode_header(header_type, self.header, source)
|
2015-01-28 18:02:50 -05:00
|
|
|
]
|
|
|
|
fsm.act("COPY",
|
|
|
|
sink.ack.eq(source.ack),
|
2015-02-11 08:33:17 -05:00
|
|
|
source.stb.eq(sink.stb | no_payload),
|
2015-01-28 18:02:50 -05:00
|
|
|
If(source.stb & source.ack & source.eop,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|