2013-07-10 15:08:57 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.genlib.misc import optree
|
|
|
|
from migen.fhdl import verilog
|
|
|
|
|
|
|
|
class LFSR(Module):
|
|
|
|
def __init__(self, n_out, n_state=31, taps=[27, 30]):
|
|
|
|
self.ce = Signal()
|
|
|
|
self.o = Signal(n_out)
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
state = Signal(n_state)
|
|
|
|
curval = [state[i] for i in range(n_state)]
|
|
|
|
curval += [0]*(n_out - n_state)
|
|
|
|
for i in range(n_out):
|
2013-07-11 10:23:05 -04:00
|
|
|
nv = ~optree("^", [curval[tap] for tap in taps])
|
2013-07-10 15:08:57 -04:00
|
|
|
curval.insert(0, nv)
|
|
|
|
curval.pop()
|
|
|
|
|
|
|
|
self.sync += If(self.ce,
|
|
|
|
state.eq(Cat(*curval[:n_state])),
|
|
|
|
self.o.eq(Cat(*curval))
|
|
|
|
)
|
|
|
|
|
2013-07-11 10:23:05 -04:00
|
|
|
def _print_lfsr_code():
|
2013-07-10 15:08:57 -04:00
|
|
|
dut = LFSR(3, 4, [3, 2])
|
|
|
|
print(verilog.convert(dut, ios={dut.ce, dut.o}))
|
|
|
|
|
2013-07-11 10:23:05 -04:00
|
|
|
class _LFSRTB(Module):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.submodules.lfsr = LFSR(*args, **kwargs)
|
|
|
|
self.comb += self.lfsr.ce.eq(1)
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
print(s.rd(self.lfsr.o))
|
|
|
|
|
|
|
|
def _sim_lfsr():
|
|
|
|
from migen.sim.generic import Simulator
|
|
|
|
tb = _LFSRTB(3, 4, [3, 2])
|
|
|
|
sim = Simulator(tb)
|
|
|
|
sim.run(20)
|
|
|
|
|
2013-07-10 15:08:57 -04:00
|
|
|
if __name__ == "__main__":
|
2013-07-11 10:23:05 -04:00
|
|
|
_print_lfsr_code()
|
|
|
|
_sim_lfsr()
|