lib.cdc: avoid interior clock domains in ResetSynchronizer.
authorwhitequark <cz@m-labs.hk>
Fri, 28 Jun 2019 07:34:10 +0000 (07:34 +0000)
committerwhitequark <cz@m-labs.hk>
Fri, 28 Jun 2019 07:34:10 +0000 (07:34 +0000)
Such clock domains will "leak" into the enclosing scope, which is
generally undesirable. Also, this is instructive for a platform
overriding the behavior, since it provides guidance on how to
correctly instantiate platform-specific flops.

I've considered also doing this for MultiReg(), but it is very
challenging in presence of non-reset-less CDC FFs, since Yosys'
$dffsr primitive has separate set and clear inputs, and reshuffling
the reset value for those results in quite a bit of additional logic.

(That said, it might have to be done anyway, precisely because
letting Yosys generate this additional logic might prove too much
for the toolchain to cope with, and again, platform-independent
code should provide guidance to platform-specific code.)

nmigen/lib/cdc.py

index 5d52503d3e248a5d1b092b0b6f4c0e5cd5ab71ef..802a865d0020adbe64cddc95c78d6d4be4b927a6 100644 (file)
@@ -82,12 +82,16 @@ class ResetSynchronizer(Elaboratable):
             return platform.get_reset_sync(self)
 
         m = Module()
-        m.domains += ClockDomain("_reset_sync", async_reset=True)
-        for i, o in zip((0, *self._regs), self._regs):
-            m.d._reset_sync += o.eq(i)
-        m.d.comb += [
-            ClockSignal("_reset_sync").eq(ClockSignal(self.domain)),
-            ResetSignal("_reset_sync").eq(self.arst),
-            ResetSignal(self.domain).eq(self._regs[-1])
-        ]
+        for i, o in zip((Const(0, 1), *self._regs), self._regs):
+            m.submodules += Instance("$adff",
+                p_CLK_POLARITY=1,
+                p_ARST_POLARITY=1,
+                p_ARST_VALUE=Const(1, 1),
+                p_WIDTH=1,
+                i_CLK=ClockSignal(self.domain),
+                i_ARST=self.arst,
+                i_D=i,
+                o_Q=o
+            )
+        m.d.comb += ResetSignal(self.domain).eq(self._regs[-1])
         return m