lib.fifo.AsyncFFSynchronizer: check input and output signal width
authorRobin Ole Heinemann <robin.ole.heinemann@t-online.de>
Tue, 27 Oct 2020 23:41:01 +0000 (00:41 +0100)
committerwhitequark <whitequark@whitequark.org>
Wed, 28 Oct 2020 00:08:38 +0000 (00:08 +0000)
nmigen/lib/cdc.py
tests/test_lib_cdc.py

index 233483a801c537d96cc3254056b6ead0e4ce14ac..c4333908d94722f9e223701d7fe9e71bdf414aaf 100644 (file)
@@ -128,16 +128,24 @@ class AsyncFFSynchronizer(Elaboratable):
     def __init__(self, i, o, *, o_domain="sync", stages=2, async_edge="pos", max_input_delay=None):
         _check_stages(stages)
 
+        if len(i) != 1:
+            raise ValueError("AsyncFFSynchronizer input width must be 1, not {}"
+                             .format(len(i)))
+        if len(o) != 1:
+            raise ValueError("AsyncFFSynchronizer output width must be 1, not {}"
+                             .format(len(o)))
+
+        if async_edge not in ("pos", "neg"):
+            raise ValueError("AsyncFFSynchronizer async edge must be one of 'pos' or 'neg', "
+                             "not {!r}"
+                             .format(async_edge))
+
         self.i = i
         self.o = o
 
         self._o_domain = o_domain
         self._stages = stages
 
-        if async_edge not in ("pos", "neg"):
-            raise ValueError("AsyncFFSynchronizer async edge must be one of 'pos' or 'neg', "
-                             "not {!r}"
-                             .format(async_edge))
         self._edge = async_edge
 
         self._max_input_delay = max_input_delay
index 1ada46d2df331fc3e0eb501ec343c75ade4cb86c..8243636748262fdfc9e08f8e59d733e58e58e547 100644 (file)
@@ -69,6 +69,14 @@ class AsyncFFSynchronizerTestCase(FHDLTestCase):
                 r"^AsyncFFSynchronizer async edge must be one of 'pos' or 'neg', not 'xxx'$"):
             AsyncFFSynchronizer(Signal(), Signal(), o_domain="sync", async_edge="xxx")
 
+    def test_width_wrong(self):
+        with self.assertRaisesRegex(ValueError,
+                r"^AsyncFFSynchronizer input width must be 1, not 2$"):
+            AsyncFFSynchronizer(Signal(2), Signal(), o_domain="sync")
+        with self.assertRaisesRegex(ValueError,
+                r"^AsyncFFSynchronizer output width must be 1, not 2$"):
+            AsyncFFSynchronizer(Signal(), Signal(2), o_domain="sync")
+
     def test_pos_edge(self):
         i = Signal()
         o = Signal()