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 ---------- dat_width : int Width of the data bus. width : int Width of the 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, dat_width, width, polynom): self.d = Signal(dat_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 # compute and optimize CRC's LFSR curval = [[("state", i)] for i in range(width)] for i in range(dat_width): feedback = curval.pop() + [("din", i)] curval.insert(0, feedback) for j in range(1, width-1): if (polynom&(1<