compat.genlib.cdc: add GrayCounter and GrayDecoder shims.
authorwhitequark <whitequark@whitequark.org>
Sun, 20 Jan 2019 02:29:08 +0000 (02:29 +0000)
committerwhitequark <whitequark@whitequark.org>
Sun, 20 Jan 2019 02:29:08 +0000 (02:29 +0000)
doc/COMPAT_SUMMARY.md
nmigen/compat/genlib/cdc.py

index 6cbfa35a42dc5e091799b437a0ef3b19ec41003b..ab58ae803a0dae369dbd802f2a219929f6b46296 100644 (file)
@@ -126,8 +126,9 @@ Compatibility summary
       - (⊕) `MultiReg` id
       - (−) `PulseSynchronizer` ?
       - (−) `BusSynchronizer` ?
-      - (−) `GrayCounter` ?
-      - (−) `GrayDecoder` ?
+      - (⊕) `GrayCounter` **obs** → `.lib.coding.GrayEncoder`
+      - (⊕) `GrayDecoder` **obs** → `.lib.coding.GrayDecoder`
+        <br>Note: `.lib.coding.GrayEncoder` and `.lib.coding.GrayDecoder` are purely combinatorial.
       - (−) `ElasticBuffer` ?
       - (−) `lcm` ?
       - (−) `Gearbox` ?
index 2405df29b8794da3fc03391c0ccc58cf5ca3be87..9e25c68aac10b46d90346a807a72394d8f876b7f 100644 (file)
@@ -1,4 +1,47 @@
+from ...tools import deprecated
 from ...lib.cdc import MultiReg
+from ...hdl.ast import *
+from ..fhdl.module import CompatModule
 
 
-__all__ = ["MultiReg"]
+__all__ = ["MultiReg", "GrayCounter", "GrayDecoder"]
+
+
+@deprecated("instead of `migen.genlib.cdc.GrayCounter`, use `nmigen.lib.coding.GrayEncoder`")
+class GrayCounter(CompatModule):
+    def __init__(self, width):
+        self.ce = Signal()
+        self.q = Signal(width)
+        self.q_next = Signal(width)
+        self.q_binary = Signal(width)
+        self.q_next_binary = Signal(width)
+
+        ###
+
+        self.comb += [
+            If(self.ce,
+                self.q_next_binary.eq(self.q_binary + 1)
+            ).Else(
+                self.q_next_binary.eq(self.q_binary)
+            ),
+            self.q_next.eq(self.q_next_binary ^ self.q_next_binary[1:])
+        ]
+        self.sync += [
+            self.q_binary.eq(self.q_next_binary),
+            self.q.eq(self.q_next)
+        ]
+
+
+@deprecated("instead of `migen.genlib.cdc.GrayDecoder`, use `nmigen.lib.coding.GrayDecoder`")
+class GrayDecoder(CompatModule):
+    def __init__(self, width):
+        self.i = Signal(width)
+        self.o = Signal(width, reset_less=True)
+
+        # # #
+
+        o_comb = Signal(width)
+        self.comb += o_comb[-1].eq(self.i[-1])
+        for i in reversed(range(width-1)):
+            self.comb += o_comb[i].eq(o_comb[i+1] ^ self.i[i])
+        self.sync += self.o.eq(o_comb)