X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fnmutil%2Flatch.py;h=f4c03ab9f5784f7c1365bc3c8454c12fc6e95140;hb=a6bd87ab95d929f7532f7f7fab0b5be226674e3e;hp=7d6a1efe22c881585a626e397590337186f6ef1b;hpb=4e8677e306016a7f04721242385d4703569c6cb3;p=nmutil.git diff --git a/src/nmutil/latch.py b/src/nmutil/latch.py index 7d6a1ef..f4c03ab 100644 --- a/src/nmutil/latch.py +++ b/src/nmutil/latch.py @@ -1,6 +1,13 @@ +""" + This work is funded through NLnet under Grant 2019-02-012 + + License: LGPLv3+ + + +""" from nmigen.compat.sim import run_simulation from nmigen.cli import verilog, rtlil -from nmigen import Record, Signal, Module, Const, Elaboratable +from nmigen import Record, Signal, Module, Const, Elaboratable, Mux """ jk latch @@ -21,47 +28,59 @@ always @ (posedge c) endmodule """ + def latchregister(m, incoming, outgoing, settrue, name=None): + """latchregister + + based on a conditon, "settrue", incoming data will be "latched" + into a register and passed out on "outgoing". + + * if "settrue" is ASSERTED, outgoing is COMBINATORIALLY equal to incoming + * on the same cycle that settrue is DEASSERTED, outgoing REMAINS + equal (indefinitely) to the incoming value + """ # make reg same as input. reset OK. if isinstance(incoming, Record): reg = Record.like(incoming, name=name) else: reg = Signal.like(incoming, name=name) + m.d.comb += outgoing.eq(Mux(settrue, incoming, reg)) with m.If(settrue): # pass in some kind of expression/condition here m.d.sync += reg.eq(incoming) # latch input into register - m.d.comb += outgoing.eq(incoming) # return input (combinatorial) - with m.Else(): - m.d.comb += outgoing.eq(reg) # return input (combinatorial) + return reg + def mkname(prefix, suffix): if suffix is None: return prefix return "%s_%s" % (prefix, suffix) + class SRLatch(Elaboratable): def __init__(self, sync=True, llen=1, name=None): self.sync = sync self.llen = llen s_n, r_n = mkname("s", name), mkname("r", name) q_n, qn_n = mkname("q", name), mkname("qn", name) + qint = mkname("qint", name) qlq_n = mkname("qlq", name) self.s = Signal(llen, name=s_n, reset=0) self.r = Signal(llen, name=r_n, reset=(1<