add helper routine for creating latched registers
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 11 May 2019 04:16:06 +0000 (05:16 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 11 May 2019 04:24:08 +0000 (05:24 +0100)
src/nmutil/latch.py

index d845a954331be955681adac188d7e4f05c9ba4b5..6cf5ab7ec93e5de0752cdfd2cf00f56c4edf7d8c 100644 (file)
@@ -21,6 +21,15 @@ always @ (posedge c)
 endmodule
 """
 
+def latchregister(m, incoming, outgoing, settrue):
+    reg = Signal.like(incoming) # make register same as input. reset is OK.
+    with m.If(settrue):
+        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)
+
+
 class SRLatch(Elaboratable):
     def __init__(self, sync=True):
         self.sync = sync