test/support,signed,sort: use new simulator
authorSebastien Bourdeauducq <sb@m-labs.hk>
Sat, 12 Sep 2015 08:28:21 +0000 (16:28 +0800)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Sat, 12 Sep 2015 08:28:21 +0000 (16:28 +0800)
migen/test/support.py
migen/test/test_signed.py
migen/test/test_sort.py

index c4a8c7d7cb303a12ac5da58ab287950d8ee65a62..8982079aed3782900537a76e8eca5cfb1b9ae9b6 100644 (file)
@@ -1,24 +1,14 @@
 from migen.fhdl.std import *
-from migen.sim.generic import run_simulation
+from migen.sim import Simulator
 from migen.fhdl import verilog
 
 
-class SimBench(Module):
-    callback = None
-    def do_simulation(self, selfp):
-        if self.callback is not None:
-            return self.callback(self, selfp)
-
-
 class SimCase:
-    TestBench = SimBench
-
     def setUp(self, *args, **kwargs):
         self.tb = self.TestBench(*args, **kwargs)
 
     def test_to_verilog(self):
         verilog.convert(self.tb)
 
-    def run_with(self, cb, ncycles=None):
-        self.tb.callback = cb
-        run_simulation(self.tb, ncycles=ncycles)
+    def run_with(self, generator):
+        Simulator(self.tb, generator).run()
index cdb7a5a08d0d76d11519d1e75f9b8f5d032a99ec..5a4b09b4d6ef37a629bc669e4c0f0afea8ad19f4 100644 (file)
@@ -1,11 +1,11 @@
 import unittest
 
 from migen.fhdl.std import *
-from migen.test.support import SimCase, SimBench
+from migen.test.support import SimCase
 
 
 class SignedCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.a = Signal((3, True))
             self.b = Signal((4, True))
@@ -27,20 +27,16 @@ class SignedCase(SimCase, unittest.TestCase):
                         self.vals.append((asign, bsign, f, r, r0.op))
 
     def test_comparisons(self):
-        values = range(-4, 4)
-        agen = iter(values)
-        bgen = iter(values)
-        def cb(tb, tbp):
-            try:
-                tbp.a = next(agen)
-                tbp.b = next(bgen)
-            except StopIteration:
-                raise StopSimulation
-            a = tbp.a
-            b = tbp.b
-            for asign, bsign, f, r, op in self.tb.vals:
-                r, r0 = tbp.simulator.rd(r), f(asign*a, bsign*b)
-                self.assertEqual(r, int(r0),
-                        "got {}, want {}*{} {} {}*{} = {}".format(
-                            r, asign, a, op, bsign, b, r0))
-        self.run_with(cb)
+        def gen():
+            for i in range(-4, 4):
+                yield self.tb.a, i
+                yield self.tb.b, i
+                a = yield self.tb.a
+                b = yield self.tb.b
+                for asign, bsign, f, r, op in self.tb.vals:
+                    r, r0 = (yield r), f(asign*a, bsign*b)
+                    self.assertEqual(r, int(r0),
+                            "got {}, want {}*{} {} {}*{} = {}".format(
+                                r, asign, a, op, bsign, b, r0))
+                yield
+        self.run_with(gen())
index 83c552b699db5bc4fd60de41488d86ff576029ea..835b4c0718c736bc63d30a382895a145117e2cf5 100644 (file)
@@ -4,11 +4,11 @@ from random import randrange
 from migen.fhdl.std import *
 from migen.genlib.sort import *
 
-from migen.test.support import SimCase, SimBench
+from migen.test.support import SimCase
 
 
 class BitonicCase(SimCase, unittest.TestCase):
-    class TestBench(SimBench):
+    class TestBench(Module):
         def __init__(self):
             self.submodules.dut = BitonicSort(8, 4, ascending=True)
 
@@ -20,8 +20,11 @@ class BitonicCase(SimCase, unittest.TestCase):
             self.assertEqual(flen(self.tb.dut.o[i]), 4)
 
     def test_sort(self):
-        def cb(tb, tbp):
-            for i in tb.dut.i:
-                tbp.simulator.wr(i, randrange(1<<flen(i)))
-            self.assertEqual(sorted(list(tbp.dut.i)), list(tbp.dut.o))
-        self.run_with(cb, 20)
+        def gen():
+            for repeat in range(20):
+                for i in self.tb.dut.i:
+                    yield i, randrange(1<<flen(i))
+                yield
+                self.assertEqual(sorted((yield self.tb.dut.i)),
+                                 (yield self.tb.dut.o))
+        self.run_with(gen())