from migen.fhdl.std import * from migen.genlib.misc import optree class CRCEngine(Module): """Cyclic Redundancy Check Engine Compute next CRC value from last CRC value and data input using an optimized asynchronous LFSR. Parameters ---------- width : int Width of the data bus and CRC. polynom : int Polynom of the CRC (ex: 0x04C11DB7 for IEEE 802.3 CRC) Attributes ---------- d : in Data input. last : in last CRC value. next : next CRC value. """ def __init__(self, width, polynom): self.d = Signal(width) self.last = Signal(width) self.next = Signal(width) ### def _optimize_eq(l): """ Replace even numbers of XORs in the equation with an equivalent XOR """ d = {} for e in l: if e in d: d[e] += 1 else: d[e] = 1 r = [] for key, value in d.items(): if value%2 != 0: r.append(key) return r new = Signal(32) self.comb += new.eq(self.last ^ self.d) # compute and optimize CRC's LFSR curval = [[("new", i)] for i in range(width)] for i in range(width): feedback = curval.pop() curval.insert(0, feedback) for j in range(1, width-1): if (polynom&(1<