back.pysim: implement sim.add_clock(if_exists=True).
authorwhitequark <cz@m-labs.hk>
Fri, 23 Aug 2019 08:53:48 +0000 (08:53 +0000)
committerwhitequark <cz@m-labs.hk>
Fri, 23 Aug 2019 08:53:48 +0000 (08:53 +0000)
nmigen/back/pysim.py
nmigen/test/test_sim.py

index 8471b51806ded606ac4a68a774a6be22fba6528f..3c73eaee9ebcc4e3ee629f5e5ed5917b9908f6a7 100644 (file)
@@ -438,7 +438,7 @@ class Simulator:
         sync_process = sync_process()
         self.add_process(sync_process)
 
-    def add_clock(self, period, phase=None, domain="sync"):
+    def add_clock(self, period, *, phase=None, domain="sync", if_exists=False):
         if self._fastest_clock == self._epsilon or period < self._fastest_clock:
             self._fastest_clock = period
         if domain in self._all_clocks:
@@ -453,8 +453,11 @@ class Simulator:
                 clk = domain_obj.clk
                 break
         else:
-            raise ValueError("Domain '{}' is not present in simulation"
-                             .format(domain))
+            if if_exists:
+                return
+            else:
+                raise ValueError("Domain '{}' is not present in simulation"
+                                 .format(domain))
         def clk_process():
             yield Passive()
             yield Delay(phase)
index c9cdcb79ec969aae319145805e3ca662642f3e7c..585bbeb170a2d7b28c41e528c266bfb4a2d4f0c1 100644 (file)
@@ -403,7 +403,7 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
                         "a generator function"):
                 sim.add_process(1)
 
-    def test_add_clock_wrong(self):
+    def test_add_clock_wrong_twice(self):
         m = Module()
         s = Signal()
         m.d.sync += s.eq(0)
@@ -413,13 +413,18 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
                     msg="Domain 'sync' already has a clock driving it"):
                 sim.add_clock(1)
 
-    def test_add_clock_wrong(self):
+    def test_add_clock_wrong_missing(self):
         m = Module()
         with self.assertSimulation(m) as sim:
             with self.assertRaises(ValueError,
                     msg="Domain 'sync' is not present in simulation"):
                 sim.add_clock(1)
 
+    def test_add_clock_if_exists(self):
+        m = Module()
+        with self.assertSimulation(m) as sim:
+            sim.add_clock(1, if_exists=True)
+
     def test_eq_signal_unused_wrong(self):
         self.setUp_lhs_rhs()
         self.s = Signal()