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 *
|
|
|
|
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.fifo import *
|
|
|
|
|
2019-10-13 12:53:38 -06:00
|
|
|
from .utils import *
|
2019-01-16 10:19:46 -07:00
|
|
|
|
|
|
|
|
2019-09-23 04:57:30 -06:00
|
|
|
class FIFOTestCase(FHDLTestCase):
|
|
|
|
def test_depth_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^FIFO width must be a non-negative integer, not -1$"):
|
2019-09-23 05:03:50 -06:00
|
|
|
FIFOInterface(width=-1, depth=8, fwft=True)
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(TypeError,
|
|
|
|
r"^FIFO depth must be a non-negative integer, not -1$"):
|
2019-09-23 06:27:59 -06:00
|
|
|
FIFOInterface(width=8, depth=-1, fwft=True)
|
|
|
|
|
|
|
|
def test_sync_depth(self):
|
|
|
|
self.assertEqual(SyncFIFO(width=8, depth=0).depth, 0)
|
|
|
|
self.assertEqual(SyncFIFO(width=8, depth=1).depth, 1)
|
|
|
|
self.assertEqual(SyncFIFO(width=8, depth=2).depth, 2)
|
|
|
|
|
|
|
|
def test_sync_buffered_depth(self):
|
|
|
|
self.assertEqual(SyncFIFOBuffered(width=8, depth=0).depth, 0)
|
|
|
|
self.assertEqual(SyncFIFOBuffered(width=8, depth=1).depth, 1)
|
|
|
|
self.assertEqual(SyncFIFOBuffered(width=8, depth=2).depth, 2)
|
2019-09-23 04:57:30 -06:00
|
|
|
|
|
|
|
def test_async_depth(self):
|
2019-09-23 06:27:59 -06:00
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=0 ).depth, 0)
|
2019-09-23 04:57:30 -06:00
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=1 ).depth, 1)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=2 ).depth, 2)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=3 ).depth, 4)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=4 ).depth, 4)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=15).depth, 16)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=16).depth, 16)
|
|
|
|
self.assertEqual(AsyncFIFO(width=8, depth=17).depth, 32)
|
|
|
|
|
|
|
|
def test_async_depth_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
(r"^AsyncFIFO only supports depths that are powers of 2; "
|
|
|
|
r"requested exact depth 15 is not$")):
|
2019-09-23 04:57:30 -06:00
|
|
|
AsyncFIFO(width=8, depth=15, exact_depth=True)
|
|
|
|
|
|
|
|
def test_async_buffered_depth(self):
|
2019-09-23 06:27:59 -06:00
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=0 ).depth, 0)
|
2019-09-23 04:57:30 -06:00
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=1 ).depth, 2)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=2 ).depth, 2)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=3 ).depth, 3)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=4 ).depth, 5)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=15).depth, 17)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=16).depth, 17)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=17).depth, 17)
|
|
|
|
self.assertEqual(AsyncFIFOBuffered(width=8, depth=18).depth, 33)
|
|
|
|
|
|
|
|
def test_async_buffered_depth_wrong(self):
|
2020-07-28 13:35:25 -06:00
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
(r"^AsyncFIFOBuffered only supports depths that are one higher than powers of 2; "
|
|
|
|
r"requested exact depth 16 is not$")):
|
2019-09-23 04:57:30 -06:00
|
|
|
AsyncFIFOBuffered(width=8, depth=16, exact_depth=True)
|
|
|
|
|
2019-04-21 02:52:57 -06:00
|
|
|
class FIFOModel(Elaboratable, FIFOInterface):
|
2019-01-19 01:57:18 -07:00
|
|
|
"""
|
|
|
|
Non-synthesizable first-in first-out queue, implemented naively as a chain of registers.
|
|
|
|
"""
|
2019-09-23 05:18:01 -06:00
|
|
|
def __init__(self, *, width, depth, fwft, r_domain, w_domain):
|
|
|
|
super().__init__(width=width, depth=depth, fwft=fwft)
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
self.r_domain = r_domain
|
|
|
|
self.w_domain = w_domain
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-10-11 07:07:42 -06:00
|
|
|
self.level = Signal(range(self.depth + 1))
|
2020-08-15 02:40:56 -06:00
|
|
|
self.r_level = Signal(range(self.depth + 1))
|
|
|
|
self.w_level = Signal(range(self.depth + 1))
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
def elaborate(self, platform):
|
2019-01-19 01:57:18 -07:00
|
|
|
m = Module()
|
|
|
|
|
2019-09-23 05:18:01 -06:00
|
|
|
storage = Memory(width=self.width, depth=self.depth)
|
2019-09-12 13:51:01 -06:00
|
|
|
w_port = m.submodules.w_port = storage.write_port(domain=self.w_domain)
|
|
|
|
r_port = m.submodules.r_port = storage.read_port (domain="comb")
|
2019-01-19 02:27:13 -07:00
|
|
|
|
2019-10-11 07:07:42 -06:00
|
|
|
produce = Signal(range(self.depth))
|
|
|
|
consume = Signal(range(self.depth))
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += self.r_rdy.eq(self.level > 0)
|
|
|
|
m.d.comb += r_port.addr.eq((consume + 1) % self.depth)
|
2019-01-19 02:27:13 -07:00
|
|
|
if self.fwft:
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += self.r_data.eq(r_port.data)
|
|
|
|
with m.If(self.r_en & self.r_rdy):
|
2019-01-19 02:27:13 -07:00
|
|
|
if not self.fwft:
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d[self.r_domain] += self.r_data.eq(r_port.data)
|
|
|
|
m.d[self.r_domain] += consume.eq(r_port.addr)
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += self.w_rdy.eq(self.level < self.depth)
|
|
|
|
m.d.comb += w_port.data.eq(self.w_data)
|
|
|
|
with m.If(self.w_en & self.w_rdy):
|
|
|
|
m.d.comb += w_port.addr.eq((produce + 1) % self.depth)
|
|
|
|
m.d.comb += w_port.en.eq(1)
|
|
|
|
m.d[self.w_domain] += produce.eq(w_port.addr)
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
with m.If(ResetSignal(self.r_domain) | ResetSignal(self.w_domain)):
|
2019-01-21 09:02:46 -07:00
|
|
|
m.d.sync += self.level.eq(0)
|
|
|
|
with m.Else():
|
|
|
|
m.d.sync += self.level.eq(self.level
|
2019-09-12 13:51:01 -06:00
|
|
|
+ (self.w_rdy & self.w_en)
|
|
|
|
- (self.r_rdy & self.r_en))
|
2019-01-21 09:02:46 -07:00
|
|
|
|
2020-08-15 02:40:56 -06:00
|
|
|
m.d.comb += [
|
|
|
|
self.r_level.eq(self.level),
|
|
|
|
self.w_level.eq(self.level),
|
|
|
|
]
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += Assert(ResetSignal(self.r_domain) == ResetSignal(self.w_domain))
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
return m
|
2019-01-19 01:57:18 -07:00
|
|
|
|
|
|
|
|
2019-04-21 02:52:57 -06:00
|
|
|
class FIFOModelEquivalenceSpec(Elaboratable):
|
2019-01-19 01:57:18 -07:00
|
|
|
"""
|
|
|
|
The first-in first-out queue model equivalence specification: for any inputs and control
|
|
|
|
signals, the behavior of the implementation under test exactly matches the ideal model,
|
|
|
|
except for behavior not defined by the model.
|
|
|
|
"""
|
2019-09-12 13:51:01 -06:00
|
|
|
def __init__(self, fifo, r_domain, w_domain):
|
2019-01-19 01:57:18 -07:00
|
|
|
self.fifo = fifo
|
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
self.r_domain = r_domain
|
|
|
|
self.w_domain = w_domain
|
2019-01-21 09:02:46 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
def elaborate(self, platform):
|
2019-01-19 01:57:18 -07:00
|
|
|
m = Module()
|
|
|
|
m.submodules.dut = dut = self.fifo
|
2019-09-23 05:18:01 -06:00
|
|
|
m.submodules.gold = gold = FIFOModel(width=dut.width, depth=dut.depth, fwft=dut.fwft,
|
2019-09-12 13:51:01 -06:00
|
|
|
r_domain=self.r_domain, w_domain=self.w_domain)
|
2019-01-19 01:57:18 -07:00
|
|
|
|
|
|
|
m.d.comb += [
|
2019-09-12 13:51:01 -06:00
|
|
|
gold.r_en.eq(dut.r_rdy & dut.r_en),
|
|
|
|
gold.w_en.eq(dut.w_en),
|
|
|
|
gold.w_data.eq(dut.w_data),
|
2019-01-19 01:57:18 -07:00
|
|
|
]
|
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += Assert(dut.r_rdy.implies(gold.r_rdy))
|
|
|
|
m.d.comb += Assert(dut.w_rdy.implies(gold.w_rdy))
|
2020-08-15 02:40:56 -06:00
|
|
|
m.d.comb += Assert(dut.r_level == gold.r_level)
|
|
|
|
m.d.comb += Assert(dut.w_level == gold.w_level)
|
2019-01-21 09:02:46 -07:00
|
|
|
|
2019-01-19 01:57:18 -07:00
|
|
|
if dut.fwft:
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += Assert(dut.r_rdy
|
|
|
|
.implies(dut.r_data == gold.r_data))
|
2019-01-19 01:57:18 -07:00
|
|
|
else:
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += Assert((Past(dut.r_rdy, domain=self.r_domain) &
|
|
|
|
Past(dut.r_en, domain=self.r_domain))
|
|
|
|
.implies(dut.r_data == gold.r_data))
|
2019-01-19 01:57:18 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
return m
|
2019-01-19 01:57:18 -07:00
|
|
|
|
|
|
|
|
2019-04-21 02:52:57 -06:00
|
|
|
class FIFOContractSpec(Elaboratable):
|
2019-01-19 01:57:18 -07:00
|
|
|
"""
|
|
|
|
The first-in first-out queue contract specification: if two elements are written to the queue
|
|
|
|
consecutively, they must be read out consecutively at some later point, no matter all other
|
|
|
|
circumstances, with the exception of reset.
|
|
|
|
"""
|
2019-09-23 05:18:01 -06:00
|
|
|
def __init__(self, fifo, *, r_domain, w_domain, bound):
|
|
|
|
self.fifo = fifo
|
2019-09-12 13:51:01 -06:00
|
|
|
self.r_domain = r_domain
|
|
|
|
self.w_domain = w_domain
|
2019-09-23 05:18:01 -06:00
|
|
|
self.bound = bound
|
2019-01-18 17:52:56 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
def elaborate(self, platform):
|
2019-01-18 17:52:56 -07:00
|
|
|
m = Module()
|
|
|
|
m.submodules.dut = fifo = self.fifo
|
|
|
|
|
2019-01-21 09:02:46 -07:00
|
|
|
m.domains += ClockDomain("sync")
|
2019-01-18 17:52:56 -07:00
|
|
|
m.d.comb += ResetSignal().eq(0)
|
2019-09-12 13:51:01 -06:00
|
|
|
if self.w_domain != "sync":
|
|
|
|
m.domains += ClockDomain(self.w_domain)
|
|
|
|
m.d.comb += ResetSignal(self.w_domain).eq(0)
|
|
|
|
if self.r_domain != "sync":
|
|
|
|
m.domains += ClockDomain(self.r_domain)
|
|
|
|
m.d.comb += ResetSignal(self.r_domain).eq(0)
|
2019-01-21 09:02:46 -07:00
|
|
|
|
2019-01-18 17:52:56 -07:00
|
|
|
entry_1 = AnyConst(fifo.width)
|
|
|
|
entry_2 = AnyConst(fifo.width)
|
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
with m.FSM(domain=self.w_domain) as write_fsm:
|
2019-01-18 17:52:56 -07:00
|
|
|
with m.State("WRITE-1"):
|
2019-09-12 13:51:01 -06:00
|
|
|
with m.If(fifo.w_rdy):
|
2019-01-18 17:52:56 -07:00
|
|
|
m.d.comb += [
|
2019-09-12 13:51:01 -06:00
|
|
|
fifo.w_data.eq(entry_1),
|
|
|
|
fifo.w_en.eq(1)
|
2019-01-18 17:52:56 -07:00
|
|
|
]
|
|
|
|
m.next = "WRITE-2"
|
|
|
|
with m.State("WRITE-2"):
|
2019-09-12 13:51:01 -06:00
|
|
|
with m.If(fifo.w_rdy):
|
2019-01-18 17:52:56 -07:00
|
|
|
m.d.comb += [
|
2019-09-12 13:51:01 -06:00
|
|
|
fifo.w_data.eq(entry_2),
|
|
|
|
fifo.w_en.eq(1)
|
2019-01-18 17:52:56 -07:00
|
|
|
]
|
|
|
|
m.next = "DONE"
|
2020-02-06 11:10:15 -07:00
|
|
|
with m.State("DONE"):
|
|
|
|
pass
|
2019-01-18 17:52:56 -07:00
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
with m.FSM(domain=self.r_domain) as read_fsm:
|
2019-01-18 17:52:56 -07:00
|
|
|
read_1 = Signal(fifo.width)
|
|
|
|
read_2 = Signal(fifo.width)
|
|
|
|
with m.State("READ"):
|
2019-09-12 13:51:01 -06:00
|
|
|
m.d.comb += fifo.r_en.eq(1)
|
2019-01-21 09:02:46 -07:00
|
|
|
if fifo.fwft:
|
2019-09-12 13:51:01 -06:00
|
|
|
r_rdy = fifo.r_rdy
|
2019-01-21 09:02:46 -07:00
|
|
|
else:
|
2019-09-12 13:51:01 -06:00
|
|
|
r_rdy = Past(fifo.r_rdy, domain=self.r_domain)
|
|
|
|
with m.If(r_rdy):
|
2019-01-18 17:52:56 -07:00
|
|
|
m.d.sync += [
|
|
|
|
read_1.eq(read_2),
|
2019-09-12 13:51:01 -06:00
|
|
|
read_2.eq(fifo.r_data),
|
2019-01-18 17:52:56 -07:00
|
|
|
]
|
|
|
|
with m.If((read_1 == entry_1) & (read_2 == entry_2)):
|
|
|
|
m.next = "DONE"
|
2020-02-06 11:10:15 -07:00
|
|
|
with m.State("DONE"):
|
|
|
|
pass
|
2019-01-18 17:52:56 -07:00
|
|
|
|
2019-08-14 20:53:07 -06:00
|
|
|
with m.If(Initial()):
|
2019-01-18 17:52:56 -07:00
|
|
|
m.d.comb += Assume(write_fsm.ongoing("WRITE-1"))
|
|
|
|
m.d.comb += Assume(read_fsm.ongoing("READ"))
|
2019-08-14 20:53:07 -06:00
|
|
|
with m.If(Past(Initial(), self.bound - 1)):
|
2019-01-18 23:02:04 -07:00
|
|
|
m.d.comb += Assert(read_fsm.ongoing("DONE"))
|
2019-01-18 17:52:56 -07:00
|
|
|
|
2020-03-14 17:26:07 -06:00
|
|
|
with m.If(ResetSignal(domain=self.w_domain)):
|
|
|
|
m.d.comb += Assert(~fifo.r_rdy)
|
|
|
|
|
2019-09-12 13:51:01 -06:00
|
|
|
if self.w_domain != "sync" or self.r_domain != "sync":
|
|
|
|
m.d.comb += Assume(Rose(ClockSignal(self.w_domain)) |
|
|
|
|
Rose(ClockSignal(self.r_domain)))
|
2019-01-21 09:02:46 -07:00
|
|
|
|
2019-01-25 19:31:12 -07:00
|
|
|
return m
|
2019-01-18 17:52:56 -07:00
|
|
|
|
|
|
|
|
2019-01-16 22:26:54 -07:00
|
|
|
class FIFOFormalCase(FHDLTestCase):
|
2019-01-21 09:02:46 -07:00
|
|
|
def check_sync_fifo(self, fifo):
|
2019-09-12 13:51:01 -06:00
|
|
|
self.assertFormal(FIFOModelEquivalenceSpec(fifo, r_domain="sync", w_domain="sync"),
|
2019-01-19 02:27:13 -07:00
|
|
|
mode="bmc", depth=fifo.depth + 1)
|
2019-09-12 13:51:01 -06:00
|
|
|
self.assertFormal(FIFOContractSpec(fifo, r_domain="sync", w_domain="sync",
|
2019-01-21 09:02:46 -07:00
|
|
|
bound=fifo.depth * 2 + 1),
|
2019-01-18 17:52:56 -07:00
|
|
|
mode="hybrid", depth=fifo.depth * 2 + 1)
|
2019-01-16 22:26:54 -07:00
|
|
|
|
2019-01-18 17:52:56 -07:00
|
|
|
def test_sync_fwft_pot(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFO(width=8, depth=4, fwft=True))
|
2019-01-18 17:52:56 -07:00
|
|
|
|
2019-01-16 22:26:54 -07:00
|
|
|
def test_sync_fwft_npot(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFO(width=8, depth=5, fwft=True))
|
2019-01-16 22:26:54 -07:00
|
|
|
|
|
|
|
def test_sync_not_fwft_pot(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFO(width=8, depth=4, fwft=False))
|
2019-01-16 22:26:54 -07:00
|
|
|
|
|
|
|
def test_sync_not_fwft_npot(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFO(width=8, depth=5, fwft=False))
|
2019-01-16 22:26:54 -07:00
|
|
|
|
|
|
|
def test_sync_buffered_pot(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFOBuffered(width=8, depth=4))
|
2019-01-16 22:26:54 -07:00
|
|
|
|
|
|
|
def test_sync_buffered_potp1(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFOBuffered(width=8, depth=5))
|
2019-01-16 22:26:54 -07:00
|
|
|
|
|
|
|
def test_sync_buffered_potm1(self):
|
2019-01-21 09:02:46 -07:00
|
|
|
self.check_sync_fifo(SyncFIFOBuffered(width=8, depth=3))
|
|
|
|
|
|
|
|
def check_async_fifo(self, fifo):
|
|
|
|
# TODO: properly doing model equivalence checking on this likely requires multiclock,
|
|
|
|
# which is not really documented nor is it clear how to use it.
|
2019-09-12 13:51:01 -06:00
|
|
|
# self.assertFormal(FIFOModelEquivalenceSpec(fifo, r_domain="read", w_domain="write"),
|
2019-01-21 09:02:46 -07:00
|
|
|
# mode="bmc", depth=fifo.depth * 3 + 1)
|
2019-09-12 13:51:01 -06:00
|
|
|
self.assertFormal(FIFOContractSpec(fifo, r_domain="read", w_domain="write",
|
2019-01-21 09:02:46 -07:00
|
|
|
bound=fifo.depth * 4 + 1),
|
|
|
|
mode="hybrid", depth=fifo.depth * 4 + 1)
|
|
|
|
|
|
|
|
def test_async(self):
|
|
|
|
self.check_async_fifo(AsyncFIFO(width=8, depth=4))
|
|
|
|
|
|
|
|
def test_async_buffered(self):
|
2019-09-23 04:57:30 -06:00
|
|
|
self.check_async_fifo(AsyncFIFOBuffered(width=8, depth=4))
|
2020-10-24 08:58:23 -06:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncFIFOSimCase(FHDLTestCase):
|
|
|
|
def test_async_fifo_r_level_latency(self):
|
|
|
|
fifo = AsyncFIFO(width=32, depth=10, r_domain="sync", w_domain="sync")
|
|
|
|
|
|
|
|
ff_syncronizer_latency = 2
|
|
|
|
|
|
|
|
def testbench():
|
|
|
|
for i in range(10):
|
|
|
|
yield fifo.w_data.eq(i)
|
|
|
|
yield fifo.w_en.eq(1)
|
|
|
|
yield
|
|
|
|
|
|
|
|
if (i - ff_syncronizer_latency) > 0:
|
|
|
|
self.assertEqual((yield fifo.r_level), i - ff_syncronizer_latency)
|
|
|
|
else:
|
|
|
|
self.assertEqual((yield fifo.r_level), 0)
|
|
|
|
|
|
|
|
simulator = Simulator(fifo)
|
|
|
|
simulator.add_clock(100e-6)
|
|
|
|
simulator.add_sync_process(testbench)
|
2020-11-03 02:10:07 -07:00
|
|
|
simulator.run()
|
|
|
|
|
|
|
|
def check_async_fifo_level(self, fifo, fill_in, expected_level):
|
|
|
|
write_done = Signal()
|
|
|
|
|
|
|
|
def write_process():
|
|
|
|
for i in range(fill_in):
|
|
|
|
yield fifo.w_data.eq(i)
|
|
|
|
yield fifo.w_en.eq(1)
|
|
|
|
yield
|
|
|
|
yield fifo.w_en.eq(0)
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield fifo.w_level), expected_level)
|
|
|
|
yield write_done.eq(1)
|
|
|
|
|
|
|
|
def read_process():
|
|
|
|
while not (yield write_done):
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield fifo.r_level), expected_level)
|
|
|
|
|
|
|
|
simulator = Simulator(fifo)
|
|
|
|
simulator.add_clock(100e-6, domain="write")
|
|
|
|
simulator.add_sync_process(write_process, domain="write")
|
|
|
|
simulator.add_clock(50e-6, domain="read")
|
|
|
|
simulator.add_sync_process(read_process, domain="read")
|
|
|
|
with simulator.write_vcd("test.vcd"):
|
|
|
|
simulator.run()
|
|
|
|
|
|
|
|
def test_async_fifo_level_full(self):
|
|
|
|
fifo = AsyncFIFO(width=32, depth=8, r_domain="read", w_domain="write")
|
|
|
|
self.check_async_fifo_level(fifo, fill_in=10, expected_level=8)
|