X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fnmutil%2Flatch.py;h=7d6a1efe22c881585a626e397590337186f6ef1b;hb=HEAD;hp=2086fe59695c3aad7b2f47d9b7dd005e4c9c128c;hpb=55d2673359c6a9f007012a8da96f70200895e2d4;p=nmutil.git diff --git a/src/nmutil/latch.py b/src/nmutil/latch.py index 2086fe5..e2d7541 100644 --- a/src/nmutil/latch.py +++ b/src/nmutil/latch.py @@ -1,6 +1,14 @@ +# SPDX-License-Identifier: LGPL-3-or-later +""" + 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 Signal, Module, Const, Elaboratable +from nmigen import Record, Signal, Module, Const, Elaboratable, Mux """ jk latch @@ -21,36 +29,61 @@ always @ (posedge c) endmodule """ + def latchregister(m, incoming, outgoing, settrue, name=None): - reg = Signal.like(incoming, name=name) # make reg same as input. reset OK. - with m.If(settrue): + """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): + def __init__(self, sync=True, llen=1, name=None): self.sync = sync self.llen = llen - self.s = Signal(llen, reset=0) - self.r = Signal(llen, reset=(1<