From 454351e4873b80de4a17cd2d1b4666e00a42bf33 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 20 Jan 2019 02:29:08 +0000 Subject: [PATCH] compat.genlib.cdc: add GrayCounter and GrayDecoder shims. --- doc/COMPAT_SUMMARY.md | 5 +++-- nmigen/compat/genlib/cdc.py | 45 ++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/doc/COMPAT_SUMMARY.md b/doc/COMPAT_SUMMARY.md index 6cbfa35..ab58ae8 100644 --- a/doc/COMPAT_SUMMARY.md +++ b/doc/COMPAT_SUMMARY.md @@ -126,8 +126,9 @@ Compatibility summary - (⊕) `MultiReg` id - (−) `PulseSynchronizer` ? - (−) `BusSynchronizer` ? - - (−) `GrayCounter` ? - - (−) `GrayDecoder` ? + - (⊕) `GrayCounter` **obs** → `.lib.coding.GrayEncoder` + - (⊕) `GrayDecoder` **obs** → `.lib.coding.GrayDecoder` +
Note: `.lib.coding.GrayEncoder` and `.lib.coding.GrayDecoder` are purely combinatorial. - (−) `ElasticBuffer` ? - (−) `lcm` ? - (−) `Gearbox` ? diff --git a/nmigen/compat/genlib/cdc.py b/nmigen/compat/genlib/cdc.py index 2405df2..9e25c68 100644 --- a/nmigen/compat/genlib/cdc.py +++ b/nmigen/compat/genlib/cdc.py @@ -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) -- 2.30.2