Skip to content

Commit

Permalink
lib.fifo: fix level on fifo full
Browse files Browse the repository at this point in the history
  • Loading branch information
anuejn committed Nov 3, 2020
1 parent 781a3aa commit c2e4567
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions nmigen/lib/fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ def elaborate(self, platform):
r_empty.eq(consume_r_gry == produce_r_gry),
]

m.d[self._w_domain] += self.w_level.eq((produce_w_bin - consume_w_bin)[:self._ctr_bits-1])
m.d.comb += self.r_level.eq((produce_r_bin - consume_r_bin)[:self._ctr_bits-1])
m.d[self._w_domain] += self.w_level.eq((produce_w_bin - consume_w_bin))
m.d.comb += self.r_level.eq((produce_r_bin - consume_r_bin))

storage = Memory(width=self.width, depth=self.depth)
w_port = m.submodules.w_port = storage.write_port(domain=self._w_domain)
Expand Down
33 changes: 32 additions & 1 deletion tests/test_lib_fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,35 @@ def testbench():
simulator = Simulator(fifo)
simulator.add_clock(100e-6)
simulator.add_sync_process(testbench)
simulator.run()
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)

0 comments on commit c2e4567

Please sign in to comment.