2020-07-28 15:02:01 -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
|
|
|
|
2020-07-28 15:02:01 -06:00
|
|
|
import unittest
|
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 *
|
|
|
|
from nmigen.asserts 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.scheduler import *
|
|
|
|
|
2020-07-28 15:02:01 -06:00
|
|
|
from .utils import *
|
|
|
|
|
|
|
|
|
|
|
|
class RoundRobinTestCase(unittest.TestCase):
|
|
|
|
def test_count(self):
|
|
|
|
dut = RoundRobin(count=32)
|
|
|
|
self.assertEqual(dut.count, 32)
|
|
|
|
self.assertEqual(len(dut.requests), 32)
|
|
|
|
self.assertEqual(len(dut.grant), 5)
|
|
|
|
|
|
|
|
def test_wrong_count(self):
|
|
|
|
with self.assertRaisesRegex(ValueError, r"Count must be a non-negative integer, not 'foo'"):
|
|
|
|
dut = RoundRobin(count="foo")
|
|
|
|
with self.assertRaisesRegex(ValueError, r"Count must be a non-negative integer, not -1"):
|
|
|
|
dut = RoundRobin(count=-1)
|
|
|
|
|
|
|
|
|
|
|
|
class RoundRobinSimulationTestCase(unittest.TestCase):
|
|
|
|
def test_count_one(self):
|
|
|
|
dut = RoundRobin(count=1)
|
|
|
|
sim = Simulator(dut)
|
|
|
|
def process():
|
|
|
|
yield dut.requests.eq(0)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 0)
|
|
|
|
self.assertFalse((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(1)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 0)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
sim.add_sync_process(process)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
with sim.write_vcd("test.vcd"):
|
|
|
|
sim.run()
|
|
|
|
|
|
|
|
def test_transitions(self):
|
|
|
|
dut = RoundRobin(count=3)
|
|
|
|
sim = Simulator(dut)
|
|
|
|
def process():
|
|
|
|
yield dut.requests.eq(0b111)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 1)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b110)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 2)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b010)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 1)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b011)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 0)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b001)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 0)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b101)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 2)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b100)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 2)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b000)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertFalse((yield dut.valid))
|
|
|
|
|
|
|
|
yield dut.requests.eq(0b001)
|
|
|
|
yield; yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield dut.grant), 0)
|
|
|
|
self.assertTrue((yield dut.valid))
|
|
|
|
sim.add_sync_process(process)
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
with sim.write_vcd("test.vcd"):
|
|
|
|
sim.run()
|