2014-09-24 16:48:36 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.genlib.fsm import FSM, NextState
|
|
|
|
from migen.genlib.record import *
|
|
|
|
from migen.genlib.misc import chooser
|
|
|
|
from migen.genlib.crc import *
|
|
|
|
from migen.flow.actor import Sink, Source
|
2014-12-17 03:22:08 -05:00
|
|
|
from migen.actorlib.fifo import SyncFIFO
|
2014-09-24 16:48:36 -04:00
|
|
|
|
|
|
|
class CRCInserter(Module):
|
|
|
|
"""CRC Inserter
|
|
|
|
|
|
|
|
Append a CRC at the end of each packet.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
layout : layout
|
|
|
|
Layout of the dataflow.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
sink : in
|
|
|
|
Packets input without CRC.
|
|
|
|
source : out
|
|
|
|
Packets output with CRC.
|
|
|
|
"""
|
|
|
|
def __init__(self, crc_class, layout):
|
2014-11-21 01:31:56 -05:00
|
|
|
self.sink = sink = Sink(layout)
|
|
|
|
self.source = source = Source(layout)
|
2014-09-24 16:48:36 -04:00
|
|
|
self.busy = Signal()
|
|
|
|
|
|
|
|
###
|
|
|
|
|
2014-10-31 07:56:03 -04:00
|
|
|
dw = flen(sink.d)
|
2014-12-17 03:22:08 -05:00
|
|
|
crc = crc_class(dw)
|
|
|
|
fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += crc, fsm
|
2014-09-24 16:48:36 -04:00
|
|
|
|
|
|
|
fsm.act("IDLE",
|
2014-12-17 03:22:08 -05:00
|
|
|
crc.reset.eq(1),
|
2014-10-31 07:56:03 -04:00
|
|
|
sink.ack.eq(1),
|
|
|
|
If(sink.stb & sink.sop,
|
|
|
|
sink.ack.eq(0),
|
2014-09-24 16:48:36 -04:00
|
|
|
NextState("COPY"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("COPY",
|
2014-12-17 03:22:08 -05:00
|
|
|
crc.ce.eq(sink.stb & source.ack),
|
|
|
|
crc.d.eq(sink.d),
|
2014-10-31 07:56:03 -04:00
|
|
|
Record.connect(sink, source),
|
|
|
|
source.eop.eq(0),
|
|
|
|
If(sink.stb & sink.eop & source.ack,
|
2014-09-24 16:48:36 -04:00
|
|
|
NextState("INSERT"),
|
|
|
|
)
|
|
|
|
)
|
2014-12-17 03:22:08 -05:00
|
|
|
ratio = crc.width//dw
|
|
|
|
if ratio > 1:
|
|
|
|
cnt = Signal(max=ratio, reset=ratio-1)
|
|
|
|
cnt_done = Signal()
|
|
|
|
fsm.act("INSERT",
|
|
|
|
source.stb.eq(1),
|
|
|
|
chooser(crc.value, cnt, source.d, reverse=True),
|
|
|
|
If(cnt_done,
|
|
|
|
source.eop.eq(1),
|
|
|
|
If(source.ack, NextState("IDLE"))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.comb += cnt_done.eq(cnt == 0)
|
|
|
|
self.sync += \
|
|
|
|
If(fsm.ongoing("IDLE"),
|
|
|
|
cnt.eq(cnt.reset)
|
|
|
|
).Elif(fsm.ongoing("INSERT") & ~cnt_done,
|
|
|
|
cnt.eq(cnt - source.ack)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
fsm.act("INSERT",
|
|
|
|
source.stb.eq(1),
|
2014-10-31 07:56:03 -04:00
|
|
|
source.eop.eq(1),
|
2014-12-17 03:22:08 -05:00
|
|
|
source.d.eq(crc.value),
|
2014-10-31 07:56:03 -04:00
|
|
|
If(source.ack, NextState("IDLE"))
|
2014-09-24 16:48:36 -04:00
|
|
|
)
|
|
|
|
self.comb += self.busy.eq(~fsm.ongoing("IDLE"))
|
|
|
|
|
|
|
|
class CRC32Inserter(CRCInserter):
|
|
|
|
def __init__(self, layout):
|
|
|
|
CRCInserter.__init__(self, CRC32, layout)
|
|
|
|
|
|
|
|
class CRCChecker(Module):
|
|
|
|
"""CRC Checker
|
|
|
|
|
|
|
|
Check CRC at the end of each packet.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
layout : layout
|
|
|
|
Layout of the dataflow.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
sink : in
|
|
|
|
Packets input with CRC.
|
|
|
|
source : out
|
2014-12-17 03:22:08 -05:00
|
|
|
Packets output without CRC and "error" set to 0
|
2014-10-31 07:56:03 -04:00
|
|
|
on eop when CRC OK / set to 1 when CRC KO.
|
2014-09-24 16:48:36 -04:00
|
|
|
"""
|
|
|
|
def __init__(self, crc_class, layout):
|
2014-11-21 01:31:56 -05:00
|
|
|
self.sink = sink = Sink(layout)
|
|
|
|
self.source = source = Source(layout)
|
2014-09-24 16:48:36 -04:00
|
|
|
self.busy = Signal()
|
|
|
|
|
|
|
|
###
|
|
|
|
|
2014-10-31 07:56:03 -04:00
|
|
|
dw = flen(sink.d)
|
2014-12-17 03:22:08 -05:00
|
|
|
crc = crc_class(dw)
|
|
|
|
self.submodules += crc
|
|
|
|
ratio = crc.width//dw
|
2014-09-24 16:48:36 -04:00
|
|
|
|
2014-12-17 03:22:08 -05:00
|
|
|
error = Signal()
|
|
|
|
fifo = InsertReset(SyncFIFO(layout, ratio + 1))
|
|
|
|
self.submodules += fifo
|
|
|
|
|
|
|
|
fsm = FSM(reset_state="RESET")
|
2014-09-24 16:48:36 -04:00
|
|
|
self.submodules += fsm
|
|
|
|
|
2014-12-17 03:22:08 -05:00
|
|
|
fifo_in = Signal()
|
|
|
|
fifo_out = Signal()
|
|
|
|
fifo_full = Signal()
|
|
|
|
|
|
|
|
self.comb += [
|
|
|
|
fifo_full.eq(fifo.fifo.level == ratio),
|
|
|
|
fifo_in.eq(sink.stb & (~fifo_full | fifo_out)),
|
|
|
|
fifo_out.eq(source.stb & source.ack),
|
|
|
|
|
|
|
|
Record.connect(sink, fifo.sink),
|
|
|
|
fifo.sink.stb.eq(fifo_in),
|
|
|
|
self.sink.ack.eq(fifo_in),
|
|
|
|
|
|
|
|
source.stb.eq(sink.stb & fifo_full),
|
|
|
|
source.sop.eq(fifo.source.sop),
|
|
|
|
source.eop.eq(sink.eop),
|
|
|
|
fifo.source.ack.eq(fifo_out),
|
|
|
|
source.payload.eq(fifo.source.payload),
|
|
|
|
|
|
|
|
source.error.eq(sink.error | crc.error),
|
|
|
|
]
|
|
|
|
|
|
|
|
fsm.act("RESET",
|
|
|
|
crc.reset.eq(1),
|
|
|
|
fifo.reset.eq(1),
|
|
|
|
NextState("IDLE"),
|
2014-10-31 07:56:03 -04:00
|
|
|
)
|
|
|
|
fsm.act("IDLE",
|
2014-12-17 03:22:08 -05:00
|
|
|
crc.d.eq(sink.d),
|
|
|
|
If(sink.stb & sink.sop & sink.ack,
|
|
|
|
crc.ce.eq(1),
|
2014-09-24 16:48:36 -04:00
|
|
|
NextState("COPY")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("COPY",
|
2014-12-17 03:22:08 -05:00
|
|
|
crc.d.eq(sink.d),
|
|
|
|
If(sink.stb & sink.ack,
|
|
|
|
crc.ce.eq(1),
|
|
|
|
If(sink.eop,
|
|
|
|
NextState("RESET")
|
|
|
|
)
|
2014-09-24 16:48:36 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.comb += self.busy.eq(~fsm.ongoing("IDLE"))
|
|
|
|
|
|
|
|
class CRC32Checker(CRCChecker):
|
|
|
|
def __init__(self, layout):
|
|
|
|
CRCChecker.__init__(self, CRC32, layout)
|