Add support for using non-compat Elaboratable instances with compat.fhdl.verilog...
authorJacob Lifshay <programmerjake@gmail.com>
Thu, 2 Apr 2020 02:38:14 +0000 (19:38 -0700)
committerwhitequark <whitequark@whitequark.org>
Thu, 2 Apr 2020 02:46:44 +0000 (02:46 +0000)
Fixes #344

nmigen/compat/fhdl/verilog.py
nmigen/compat/sim/__init__.py
nmigen/test/compat/test_run_simulation.py [new file with mode: 0644]

index 35a1ca59a11bb45969a6a4860ef74d1183cfb710..3773cc6cce3211b988d97b196df3b696bceb0d64 100644 (file)
@@ -4,6 +4,7 @@ from ...hdl.ir import Fragment
 from ...hdl.cd import ClockDomain
 from ...back import verilog
 from .conv_output import ConvOutput
+from .module import Module
 
 
 def convert(fi, ios=None, name="top", special_overrides=dict(),
@@ -17,11 +18,14 @@ def convert(fi, ios=None, name="top", special_overrides=dict(),
                       DeprecationWarning, stacklevel=1)
     # TODO: attr_translate
 
+    if isinstance(fi, Module):
+        fi = fi.get_fragment()
+
     def missing_domain(name):
         if create_clock_domains:
             return ClockDomain(name)
     v_output = verilog.convert(
-        elaboratable=fi.get_fragment(),
+        elaboratable=fi,
         name=name,
         ports=ios or (),
         missing_domain=missing_domain
index c6a89e348dd84498f5563aa535972a0cc8d320e7..b2cc87e9f519c904bece329c582b4225d2333b6f 100644 (file)
@@ -3,6 +3,7 @@ import inspect
 from collections.abc import Iterable
 from ...hdl.cd import ClockDomain
 from ...back.pysim import *
+from ...hdl.ir import Fragment
 
 
 __all__ = ["run_simulation", "passive"]
@@ -17,9 +18,12 @@ def run_simulation(fragment_or_module, generators, clocks={"sync": 10}, vcd_name
     else:
         fragment = fragment_or_module
 
+    fragment = Fragment.get(fragment, platform=None)
+
     if not isinstance(generators, dict):
         generators = {"sync": generators}
-        fragment.domains += ClockDomain("sync")
+        if "sync" not in fragment.domains:
+            fragment.add_domains(ClockDomain("sync"))
 
     sim = Simulator(fragment)
     for domain, period in clocks.items():
diff --git a/nmigen/test/compat/test_run_simulation.py b/nmigen/test/compat/test_run_simulation.py
new file mode 100644 (file)
index 0000000..0c33413
--- /dev/null
@@ -0,0 +1,26 @@
+import unittest
+from ... import Signal, Module, Elaboratable
+from .support import SimCase
+
+
+class RunSimulation(SimCase, unittest.TestCase):
+    """ test for https://github.com/nmigen/nmigen/issues/344 """
+
+    class TestBench(Elaboratable):
+        def __init__(self):
+            self.a = Signal()
+
+        def elaborate(self, platform):
+            m = Module()
+            m.d.sync += self.a.eq(~self.a)
+            return m
+
+    def test_run_simulation(self):
+        def gen():
+            yield
+            for i in range(10):
+                yield
+                a = (yield self.tb.a)
+                self.assertEqual(a, i % 2)
+
+        self.run_with(gen())