2019-10-26 00:36:54 -06:00
|
|
|
# nmigen: UnusedElaboratable=no
|
|
|
|
|
tests: move out of the main package.
Compared to tests in the repository root, tests in the package have
many downsides:
* Unless explicitly excluded in find_packages(), tests and their
support code effectively become a part of public API.
This, unfortunately, happened with FHDLTestCase, which was never
intended for downstream use.
* Even if explicitly excluded from the setuptools package, using
an editable install, or setting PYTHONPATH still allows accessing
the tests.
* Having a sub-package that is present in the source tree but not
exported (or, worse, exported only sometimes) is confusing.
* The name `nmigen.test` cannot be used for anything else, such as
testing utilities that *are* intended for downstream use.
2020-08-26 18:33:31 -06:00
|
|
|
from nmigen.hdl import *
|
sim: split into base, core, and engines.
Before this commit, each simulation engine (which is only pysim at
the moment, but also cxxsim soon) was a subclass of SimulatorCore,
and every simulation engine module would essentially duplicate
the complete structure of a simulator, with code partially shared.
This was a really bad idea: it was inconvenient to use, with
downstream code having to branch between e.g. PySettle and CxxSettle;
it had no well-defined external interface; it had multiple virtually
identical entry points; and it had no separation between simulation
algorithms and glue code.
This commit completely rearranges simulation code.
1. sim._base defines internal simulation interfaces. The clarity of
these internal interfaces is important because simulation
engines mix and match components to provide a consistent API
regardless of the chosen engine.
2. sim.core defines the external simulation interface: the commands
and the simulator facade. The facade provides a single entry
point and, when possible, validates or lowers user input.
It also imports built-in simulation engines by their symbolic
name, avoiding eager imports of pyvcd or ctypes.
3. sim.xxxsim (currently, only sim.pysim) defines the simulator
implementation: time and state management, process scheduling,
and waveform dumping.
The new simulator structure has none of the downsides of the old one.
See #324.
2020-08-27 04:17:02 -06:00
|
|
|
from nmigen.sim import *
|
tests: move out of the main package.
Compared to tests in the repository root, tests in the package have
many downsides:
* Unless explicitly excluded in find_packages(), tests and their
support code effectively become a part of public API.
This, unfortunately, happened with FHDLTestCase, which was never
intended for downstream use.
* Even if explicitly excluded from the setuptools package, using
an editable install, or setting PYTHONPATH still allows accessing
the tests.
* Having a sub-package that is present in the source tree but not
exported (or, worse, exported only sometimes) is confusing.
* The name `nmigen.test` cannot be used for anything else, such as
testing utilities that *are* intended for downstream use.
2020-08-26 18:33:31 -06:00
|
|
|
from nmigen.lib.cdc import *
|
|
|
|
|
2019-10-13 12:53:38 -06:00
|
|
|
from .utils import *
|
2018-12-26 05:58:30 -07:00
|
|
|
|
|
|
|
|
2019-09-23 08:17:44 -06:00
|
|
|
class FFSynchronizerTestCase(FHDLTestCase):
|
2019-09-23 13:38:21 -06:00
|
|
|
def test_stages_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^Synchronization stage count must be a positive integer, not 0$"):
|
2019-09-23 13:38:21 -06:00
|
|
|
FFSynchronizer(Signal(), Signal(), stages=0)
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^Synchronization stage count may not safely be less than 2$"):
|
2019-09-23 13:38:21 -06:00
|
|
|
FFSynchronizer(Signal(), Signal(), stages=1)
|
|
|
|
|
2018-12-26 05:58:30 -07:00
|
|
|
def test_basic(self):
|
|
|
|
i = Signal()
|
|
|
|
o = Signal()
|
2019-09-23 08:17:44 -06:00
|
|
|
frag = FFSynchronizer(i, o)
|
2019-11-22 01:32:41 -07:00
|
|
|
|
|
|
|
sim = Simulator(frag)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield i.eq(1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
sim.add_process(process)
|
|
|
|
sim.run()
|
2018-12-26 05:58:30 -07:00
|
|
|
|
2019-01-26 11:07:59 -07:00
|
|
|
def test_reset_value(self):
|
2018-12-26 05:58:30 -07:00
|
|
|
i = Signal(reset=1)
|
|
|
|
o = Signal()
|
2019-09-23 08:17:44 -06:00
|
|
|
frag = FFSynchronizer(i, o, reset=1)
|
2019-11-22 01:32:41 -07:00
|
|
|
|
|
|
|
sim = Simulator(frag)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
sim.add_process(process)
|
|
|
|
sim.run()
|
2019-01-26 11:07:59 -07:00
|
|
|
|
|
|
|
|
2020-03-08 15:37:40 -06:00
|
|
|
class AsyncFFSynchronizerTestCase(FHDLTestCase):
|
|
|
|
def test_stages_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^Synchronization stage count must be a positive integer, not 0$"):
|
2020-03-08 15:37:40 -06:00
|
|
|
ResetSynchronizer(Signal(), stages=0)
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^Synchronization stage count may not safely be less than 2$"):
|
2020-03-08 15:37:40 -06:00
|
|
|
ResetSynchronizer(Signal(), stages=1)
|
|
|
|
|
|
|
|
def test_edge_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^AsyncFFSynchronizer async edge must be one of 'pos' or 'neg', not 'xxx'$"):
|
2020-08-25 21:19:13 -06:00
|
|
|
AsyncFFSynchronizer(Signal(), Signal(), o_domain="sync", async_edge="xxx")
|
2020-03-08 15:37:40 -06:00
|
|
|
|
|
|
|
def test_pos_edge(self):
|
|
|
|
i = Signal()
|
|
|
|
o = Signal()
|
|
|
|
m = Module()
|
|
|
|
m.domains += ClockDomain("sync")
|
|
|
|
m.submodules += AsyncFFSynchronizer(i, o)
|
|
|
|
|
|
|
|
sim = Simulator(m)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
# initial reset
|
|
|
|
self.assertEqual((yield i), 0)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
|
|
|
|
yield i.eq(1)
|
|
|
|
yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
sim.add_process(process)
|
|
|
|
with sim.write_vcd("test.vcd"):
|
|
|
|
sim.run()
|
|
|
|
|
|
|
|
def test_neg_edge(self):
|
|
|
|
i = Signal(reset=1)
|
|
|
|
o = Signal()
|
|
|
|
m = Module()
|
|
|
|
m.domains += ClockDomain("sync")
|
|
|
|
m.submodules += AsyncFFSynchronizer(i, o, async_edge="neg")
|
|
|
|
|
|
|
|
sim = Simulator(m)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
# initial reset
|
|
|
|
self.assertEqual((yield i), 1)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
|
|
|
|
yield i.eq(0)
|
|
|
|
yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
sim.add_process(process)
|
|
|
|
with sim.write_vcd("test.vcd"):
|
|
|
|
sim.run()
|
|
|
|
|
|
|
|
|
2019-01-26 11:07:59 -07:00
|
|
|
class ResetSynchronizerTestCase(FHDLTestCase):
|
2019-09-23 13:38:21 -06:00
|
|
|
def test_stages_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^Synchronization stage count must be a positive integer, not 0$"):
|
2019-09-23 13:38:21 -06:00
|
|
|
ResetSynchronizer(Signal(), stages=0)
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^Synchronization stage count may not safely be less than 2$"):
|
2019-09-23 13:38:21 -06:00
|
|
|
ResetSynchronizer(Signal(), stages=1)
|
|
|
|
|
2019-01-26 11:07:59 -07:00
|
|
|
def test_basic(self):
|
|
|
|
arst = Signal()
|
|
|
|
m = Module()
|
|
|
|
m.domains += ClockDomain("sync")
|
|
|
|
m.submodules += ResetSynchronizer(arst)
|
|
|
|
s = Signal(reset=1)
|
|
|
|
m.d.sync += s.eq(0)
|
|
|
|
|
2019-11-22 01:32:41 -07:00
|
|
|
sim = Simulator(m)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
# initial reset
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
2019-01-26 11:07:59 -07:00
|
|
|
|
2019-11-22 01:32:41 -07:00
|
|
|
yield arst.eq(1)
|
|
|
|
yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield arst.eq(0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
sim.add_process(process)
|
|
|
|
with sim.write_vcd("test.vcd"):
|
2019-01-26 11:07:59 -07:00
|
|
|
sim.run()
|
2020-02-15 23:51:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: test with distinct clocks
|
|
|
|
class PulseSynchronizerTestCase(FHDLTestCase):
|
2020-06-27 23:17:33 -06:00
|
|
|
def test_stages_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^Synchronization stage count must be a positive integer, not 0$"):
|
2020-06-27 23:17:33 -06:00
|
|
|
PulseSynchronizer("w", "r", stages=0)
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^Synchronization stage count may not safely be less than 2$"):
|
2020-06-27 23:17:33 -06:00
|
|
|
PulseSynchronizer("w", "r", stages=1)
|
2020-02-15 23:51:53 -07:00
|
|
|
|
|
|
|
def test_smoke(self):
|
|
|
|
m = Module()
|
|
|
|
m.domains += ClockDomain("sync")
|
|
|
|
ps = m.submodules.dut = PulseSynchronizer("sync", "sync")
|
|
|
|
|
|
|
|
sim = Simulator(m)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
yield ps.i.eq(0)
|
|
|
|
# TODO: think about reset
|
|
|
|
for n in range(5):
|
|
|
|
yield Tick()
|
|
|
|
# Make sure no pulses are generated in quiescent state
|
|
|
|
for n in range(3):
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield ps.o), 0)
|
|
|
|
# Check conservation of pulses
|
|
|
|
accum = 0
|
|
|
|
for n in range(10):
|
|
|
|
yield ps.i.eq(1 if n < 4 else 0)
|
|
|
|
yield Tick()
|
|
|
|
accum += yield ps.o
|
|
|
|
self.assertEqual(accum, 4)
|
|
|
|
sim.add_process(process)
|
|
|
|
sim.run()
|