back.pysim: check for a clock being added twice.
authorwhitequark <cz@m-labs.hk>
Tue, 11 Jun 2019 03:54:22 +0000 (03:54 +0000)
committerwhitequark <cz@m-labs.hk>
Tue, 11 Jun 2019 03:54:22 +0000 (03:54 +0000)
This commit adds a best-effort error for a common mistake of adding
a clock driving the same domain twice, such as a result of
a copy-paste error.

Fixes #27.

nmigen/back/pysim.py
nmigen/test/test_sim.py

index 530ee7dfc411dd3087219eb480685c5d14d1014f..fee545a88c7ad35411a4cfb00ae292be34ddd9da 100644 (file)
@@ -366,6 +366,7 @@ class Simulator:
         self._delta           = 0.
         self._epsilon         = 1e-10
         self._fastest_clock   = self._epsilon
+        self._all_clocks      = set()         # {str/domain}
         self._state           = _State()
 
         self._processes       = set()         # {process}
@@ -426,6 +427,9 @@ class Simulator:
     def add_clock(self, period, phase=None, domain="sync"):
         if self._fastest_clock == self._epsilon or period < self._fastest_clock:
             self._fastest_clock = period
+        if domain in self._all_clocks:
+            raise ValueError("Domain '{}' already has a clock driving it"
+                             .format(domain))
 
         half_period = period / 2
         if phase is None:
@@ -440,6 +444,7 @@ class Simulator:
                 yield clk.eq(0)
                 yield Delay(half_period)
         self.add_process(clk_process)
+        self._all_clocks.add(domain)
 
     def __enter__(self):
         if self._vcd_file:
index ff3986d751b12121661322fa1ad57a5a217dae8f..52a1f76ecfdfc2ce6e4ff85cca730375c51f78ac 100644 (file)
@@ -388,6 +388,13 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
                         "a generator function"):
                 sim.add_process(1)
 
+    def test_add_clock_wrong(self):
+        with self.assertSimulation(Module()) as sim:
+            sim.add_clock(1)
+            with self.assertRaises(ValueError,
+                    msg="Domain 'sync' already has a clock driving it"):
+                sim.add_clock(1)
+
     def test_eq_signal_unused_wrong(self):
         self.setUp_lhs_rhs()
         self.s = Signal()