2018-12-18 11:05:37 -07:00
|
|
|
import unittest
|
|
|
|
from itertools import count
|
|
|
|
|
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.compat import *
|
|
|
|
from nmigen.compat.genlib.fsm import FSM
|
2018-12-18 11:05:37 -07:00
|
|
|
|
|
|
|
from .support import SimCase
|
|
|
|
|
|
|
|
|
|
|
|
class FSMCase(SimCase, unittest.TestCase):
|
|
|
|
class TestBench(Module):
|
|
|
|
def __init__(self):
|
|
|
|
self.ctrl = Signal()
|
|
|
|
self.data = Signal()
|
|
|
|
self.status = Signal(8)
|
|
|
|
|
|
|
|
self.submodules.dut = FSM()
|
|
|
|
self.dut.act("IDLE",
|
|
|
|
If(self.ctrl,
|
|
|
|
NextState("START")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.dut.act("START",
|
|
|
|
If(self.data,
|
|
|
|
NextState("SET-STATUS-LOW")
|
|
|
|
).Else(
|
|
|
|
NextState("SET-STATUS")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.dut.act("SET-STATUS",
|
|
|
|
NextValue(self.status, 0xaa),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
self.dut.act("SET-STATUS-LOW",
|
|
|
|
NextValue(self.status[:4], 0xb),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertState(self, fsm, state):
|
|
|
|
self.assertEqual(fsm.decoding[(yield fsm.state)], state)
|
|
|
|
|
|
|
|
def test_next_state(self):
|
|
|
|
def gen():
|
|
|
|
yield from self.assertState(self.tb.dut, "IDLE")
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "IDLE")
|
|
|
|
yield self.tb.ctrl.eq(1)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "IDLE")
|
|
|
|
yield self.tb.ctrl.eq(0)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "START")
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "SET-STATUS")
|
|
|
|
yield self.tb.ctrl.eq(1)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "IDLE")
|
|
|
|
yield self.tb.ctrl.eq(0)
|
|
|
|
yield self.tb.data.eq(1)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "START")
|
|
|
|
yield self.tb.data.eq(0)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "SET-STATUS-LOW")
|
|
|
|
self.run_with(gen())
|
|
|
|
|
|
|
|
def test_next_value(self):
|
|
|
|
def gen():
|
|
|
|
self.assertEqual((yield self.tb.status), 0x00)
|
|
|
|
yield self.tb.ctrl.eq(1)
|
|
|
|
yield
|
|
|
|
yield self.tb.ctrl.eq(0)
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "SET-STATUS")
|
|
|
|
yield self.tb.ctrl.eq(1)
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield self.tb.status), 0xaa)
|
|
|
|
yield self.tb.ctrl.eq(0)
|
|
|
|
yield self.tb.data.eq(1)
|
|
|
|
yield
|
|
|
|
yield self.tb.data.eq(0)
|
|
|
|
yield
|
|
|
|
yield from self.assertState(self.tb.dut, "SET-STATUS-LOW")
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield self.tb.status), 0xab)
|
|
|
|
self.run_with(gen())
|