lib.cdc: add tests for MultiReg.
authorwhitequark <cz@m-labs.hk>
Wed, 26 Dec 2018 12:58:30 +0000 (12:58 +0000)
committerwhitequark <cz@m-labs.hk>
Wed, 26 Dec 2018 12:58:30 +0000 (12:58 +0000)
nmigen/back/pysim.py
nmigen/test/test_lib_cdc.py [new file with mode: 0644]

index dd8f24a1546a3458a8bc5df059f4c11f81338ecb..27bed924820c620915b520f970049ff3c385db82 100644 (file)
@@ -7,6 +7,7 @@ from vcd.gtkw import GTKWSave
 
 from ..tools import flatten
 from ..hdl.ast import *
+from ..hdl.ir import *
 from ..hdl.xfrm import ValueVisitor, StatementVisitor
 
 
@@ -359,6 +360,9 @@ class Simulator:
         self._gtkw_file       = gtkw_file
         self._traces          = traces
 
+        while not isinstance(self._fragment, Fragment):
+            self._fragment = self._fragment.get_fragment(platform=None)
+
     @staticmethod
     def _check_process(process):
         if inspect.isgeneratorfunction(process):
diff --git a/nmigen/test/test_lib_cdc.py b/nmigen/test/test_lib_cdc.py
new file mode 100644 (file)
index 0000000..b65c04e
--- /dev/null
@@ -0,0 +1,40 @@
+from .tools import *
+from ..hdl.ast import *
+from ..back.pysim import *
+from ..lib.cdc import *
+
+
+class MultiRegTestCase(FHDLTestCase):
+    def test_basic(self):
+        i = Signal()
+        o = Signal()
+        frag = MultiReg(i, o)
+        with Simulator(frag) as sim:
+            sim.add_clock(1e-6)
+            def process():
+                self.assertEqual((yield o), 0)
+                yield i.eq(1)
+                yield Tick()
+                self.assertEqual((yield o), 0)
+                yield Tick()
+                self.assertEqual((yield o), 0)
+                yield Tick()
+                self.assertEqual((yield o), 1)
+            sim.add_process(process)
+
+    def test_basic(self):
+        i = Signal(reset=1)
+        o = Signal()
+        frag = MultiReg(i, o, reset=1)
+        with Simulator(frag) as sim:
+            sim.add_clock(1e-6)
+            def process():
+                self.assertEqual((yield o), 1)
+                yield i.eq(0)
+                yield Tick()
+                self.assertEqual((yield o), 1)
+                yield Tick()
+                self.assertEqual((yield o), 1)
+                yield Tick()
+                self.assertEqual((yield o), 0)
+            sim.add_process(process)