2021-12-09 22:39:50 -07:00
|
|
|
# amaranth: UnusedElaboratable=no
|
2019-10-26 00:36:54 -06:00
|
|
|
|
2021-12-09 22:39:50 -07:00
|
|
|
from amaranth.hdl import *
|
|
|
|
from amaranth.sim import *
|
|
|
|
from amaranth.lib.cdc 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
|
|
|
|
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)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 0)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 0)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 1)
|
2024-02-08 09:53:17 -07:00
|
|
|
sim.add_process(process)
|
2019-11-22 01:32:41 -07:00
|
|
|
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)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 1)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 1)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield o), 0)
|
2024-02-08 09:53:17 -07:00
|
|
|
sim.add_process(process)
|
2019-11-22 01:32:41 -07:00
|
|
|
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
|
|
|
|
2020-10-27 17:41:01 -06:00
|
|
|
def test_width_wrong(self):
|
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^AsyncFFSynchronizer input width must be 1, not 2$"):
|
|
|
|
AsyncFFSynchronizer(Signal(2), Signal(), o_domain="sync")
|
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
r"^AsyncFFSynchronizer output width must be 1, not 2$"):
|
|
|
|
AsyncFFSynchronizer(Signal(), Signal(2), o_domain="sync")
|
|
|
|
|
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)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
|
|
|
|
yield i.eq(1)
|
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
|
|
|
sim.add_testbench(process)
|
2020-03-08 15:37:40 -06:00
|
|
|
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)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
|
|
|
|
yield i.eq(0)
|
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2020-03-08 15:37:40 -06:00
|
|
|
self.assertEqual((yield o), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
|
|
|
sim.add_testbench(process)
|
2020-03-08 15:37:40 -06:00
|
|
|
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)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
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)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield arst.eq(0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 1)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
2019-11-22 01:32:41 -07:00
|
|
|
self.assertEqual((yield s), 0)
|
2020-10-19 19:54:24 -06:00
|
|
|
yield Tick()
|
|
|
|
sim.add_testbench(process)
|
2019-11-22 01:32:41 -07:00
|
|
|
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):
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2020-02-15 23:51:53 -07:00
|
|
|
# Make sure no pulses are generated in quiescent state
|
|
|
|
for n in range(3):
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2020-02-15 23:51:53 -07:00
|
|
|
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)
|
2024-02-08 09:53:17 -07:00
|
|
|
yield Tick()
|
2020-02-15 23:51:53 -07:00
|
|
|
accum += yield ps.o
|
|
|
|
self.assertEqual(accum, 4)
|
2024-02-08 09:53:17 -07:00
|
|
|
sim.add_process(process)
|
2020-02-15 23:51:53 -07:00
|
|
|
sim.run()
|