test/support,signed,sort: use new simulator

This commit is contained in:
Sebastien Bourdeauducq 2015-09-12 16:28:21 +08:00
parent 8ee361ffe2
commit 047d1f48b5
3 changed files with 28 additions and 39 deletions

View File

@ -1,24 +1,14 @@
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.sim.generic import run_simulation from migen.sim import Simulator
from migen.fhdl import verilog 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: class SimCase:
TestBench = SimBench
def setUp(self, *args, **kwargs): def setUp(self, *args, **kwargs):
self.tb = self.TestBench(*args, **kwargs) self.tb = self.TestBench(*args, **kwargs)
def test_to_verilog(self): def test_to_verilog(self):
verilog.convert(self.tb) verilog.convert(self.tb)
def run_with(self, cb, ncycles=None): def run_with(self, generator):
self.tb.callback = cb Simulator(self.tb, generator).run()
run_simulation(self.tb, ncycles=ncycles)

View File

@ -1,11 +1,11 @@
import unittest import unittest
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.test.support import SimCase, SimBench from migen.test.support import SimCase
class SignedCase(SimCase, unittest.TestCase): class SignedCase(SimCase, unittest.TestCase):
class TestBench(SimBench): class TestBench(Module):
def __init__(self): def __init__(self):
self.a = Signal((3, True)) self.a = Signal((3, True))
self.b = Signal((4, True)) self.b = Signal((4, True))
@ -27,20 +27,16 @@ class SignedCase(SimCase, unittest.TestCase):
self.vals.append((asign, bsign, f, r, r0.op)) self.vals.append((asign, bsign, f, r, r0.op))
def test_comparisons(self): def test_comparisons(self):
values = range(-4, 4) def gen():
agen = iter(values) for i in range(-4, 4):
bgen = iter(values) yield self.tb.a, i
def cb(tb, tbp): yield self.tb.b, i
try: a = yield self.tb.a
tbp.a = next(agen) b = yield self.tb.b
tbp.b = next(bgen) for asign, bsign, f, r, op in self.tb.vals:
except StopIteration: r, r0 = (yield r), f(asign*a, bsign*b)
raise StopSimulation self.assertEqual(r, int(r0),
a = tbp.a "got {}, want {}*{} {} {}*{} = {}".format(
b = tbp.b r, asign, a, op, bsign, b, r0))
for asign, bsign, f, r, op in self.tb.vals: yield
r, r0 = tbp.simulator.rd(r), f(asign*a, bsign*b) self.run_with(gen())
self.assertEqual(r, int(r0),
"got {}, want {}*{} {} {}*{} = {}".format(
r, asign, a, op, bsign, b, r0))
self.run_with(cb)

View File

@ -4,11 +4,11 @@ from random import randrange
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.genlib.sort 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 BitonicCase(SimCase, unittest.TestCase):
class TestBench(SimBench): class TestBench(Module):
def __init__(self): def __init__(self):
self.submodules.dut = BitonicSort(8, 4, ascending=True) 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) self.assertEqual(flen(self.tb.dut.o[i]), 4)
def test_sort(self): def test_sort(self):
def cb(tb, tbp): def gen():
for i in tb.dut.i: for repeat in range(20):
tbp.simulator.wr(i, randrange(1<<flen(i))) for i in self.tb.dut.i:
self.assertEqual(sorted(list(tbp.dut.i)), list(tbp.dut.o)) yield i, randrange(1<<flen(i))
self.run_with(cb, 20) yield
self.assertEqual(sorted((yield self.tb.dut.i)),
(yield self.tb.dut.o))
self.run_with(gen())