2020-08-23 09:52:08 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteDRAM.
|
|
|
|
#
|
2021-09-21 13:23:36 -04:00
|
|
|
# Copyright (c) 2019-2021 Florent Kermarrec <florent@enjoy-digital.fr>
|
2020-08-23 09:52:08 -04:00
|
|
|
# Copyright (c) 2020 Antmicro <www.antmicro.com>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2020-01-07 09:40:09 -05:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import random
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
|
|
|
|
from litex.soc.interconnect.stream import *
|
|
|
|
|
|
|
|
from litedram.common import LiteDRAMNativeWritePort
|
|
|
|
from litedram.common import LiteDRAMNativeReadPort
|
2020-03-26 07:23:25 -04:00
|
|
|
from litedram.frontend.fifo import LiteDRAMFIFO, _LiteDRAMFIFOCtrl
|
|
|
|
from litedram.frontend.fifo import _LiteDRAMFIFOWriter, _LiteDRAMFIFOReader
|
2020-01-07 09:40:09 -05:00
|
|
|
|
|
|
|
from test.common import *
|
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
|
|
|
|
class FIFODUT(Module):
|
2021-09-21 13:23:36 -04:00
|
|
|
def __init__(self, base, depth, data_width=8, address_width=32, with_bypass=False):
|
2021-09-23 12:57:00 -04:00
|
|
|
port_data_width = data_width if not with_bypass else 4*data_width
|
|
|
|
self.write_port = LiteDRAMNativeWritePort(address_width=32, data_width=port_data_width)
|
|
|
|
self.read_port = LiteDRAMNativeReadPort(address_width=32, data_width=port_data_width)
|
2020-06-03 10:13:28 -04:00
|
|
|
self.submodules.fifo = LiteDRAMFIFO(
|
|
|
|
data_width = data_width,
|
|
|
|
base = base,
|
|
|
|
depth = depth,
|
|
|
|
write_port = self.write_port,
|
|
|
|
read_port = self.read_port,
|
2021-09-21 13:23:36 -04:00
|
|
|
with_bypass = with_bypass
|
2020-06-03 10:13:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
margin = 8
|
2021-09-23 12:57:00 -04:00
|
|
|
self.memory = DRAMMemory(port_data_width, base + depth + margin)
|
2020-06-03 10:13:28 -04:00
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
yield self.fifo.sink.valid.eq(1)
|
|
|
|
yield self.fifo.sink.data.eq(data)
|
|
|
|
yield
|
|
|
|
while not (yield self.fifo.sink.ready):
|
|
|
|
yield
|
|
|
|
yield self.fifo.sink.valid.eq(0)
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
while not (yield self.fifo.source.valid):
|
2020-03-24 09:08:53 -04:00
|
|
|
yield
|
2020-06-03 10:13:28 -04:00
|
|
|
yield self.fifo.source.ready.eq(1)
|
|
|
|
data = (yield self.fifo.source.data)
|
|
|
|
yield
|
|
|
|
yield self.fifo.source.ready.eq(0)
|
|
|
|
yield
|
|
|
|
return data
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
|
|
|
|
class TestFIFO(unittest.TestCase):
|
2020-03-24 09:08:53 -04:00
|
|
|
# _LiteDRAMFIFOCtrl ----------------------------------------------------------------------------
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2020-03-25 06:33:52 -04:00
|
|
|
def test_fifo_ctrl_address_changes(self):
|
2020-04-14 15:40:51 -04:00
|
|
|
# Verify FIFOCtrl address changes.
|
2020-06-03 10:13:28 -04:00
|
|
|
dut = _LiteDRAMFIFOCtrl(base=0, depth=16)
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2020-03-24 09:08:53 -04:00
|
|
|
def main_generator():
|
|
|
|
self.assertEqual((yield dut.write_address), 0)
|
|
|
|
self.assertEqual((yield dut.read_address), 0)
|
|
|
|
|
2020-03-26 07:23:25 -04:00
|
|
|
# Write address
|
2020-03-24 09:08:53 -04:00
|
|
|
yield dut.write.eq(1)
|
|
|
|
yield
|
2020-03-26 07:23:25 -04:00
|
|
|
# Write_address gets updated 1 cycle later
|
2020-03-24 09:08:53 -04:00
|
|
|
for i in range(24 - 1):
|
|
|
|
self.assertEqual((yield dut.write_address), i % 16)
|
|
|
|
yield
|
|
|
|
yield dut.write.eq(0)
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield dut.write_address), 24 % 16)
|
|
|
|
|
2020-03-26 07:23:25 -04:00
|
|
|
# Read address
|
2020-03-24 09:08:53 -04:00
|
|
|
yield dut.read.eq(1)
|
|
|
|
yield
|
|
|
|
for i in range(24 - 1):
|
|
|
|
self.assertEqual((yield dut.read_address), i % 16)
|
|
|
|
yield
|
|
|
|
yield dut.read.eq(0)
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield dut.read_address), 24 % 16)
|
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
run_simulation(dut, main_generator())
|
2020-03-25 06:33:52 -04:00
|
|
|
|
|
|
|
def test_fifo_ctrl_level_changes(self):
|
2020-04-14 15:40:51 -04:00
|
|
|
# Verify FIFOCtrl level changes.
|
2020-06-03 10:13:28 -04:00
|
|
|
dut = _LiteDRAMFIFOCtrl(base=0, depth=16)
|
2020-03-25 06:33:52 -04:00
|
|
|
|
|
|
|
def main_generator():
|
|
|
|
self.assertEqual((yield dut.level), 0)
|
|
|
|
|
2020-03-24 09:08:53 -04:00
|
|
|
def check_level_diff(write, read, diff):
|
|
|
|
level = (yield dut.level)
|
|
|
|
yield dut.write.eq(write)
|
|
|
|
yield dut.read.eq(read)
|
|
|
|
yield
|
|
|
|
yield dut.write.eq(0)
|
|
|
|
yield dut.read.eq(0)
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield dut.level), level + diff)
|
|
|
|
|
|
|
|
check_level_diff(write=1, read=0, diff=+1)
|
2020-03-25 06:33:52 -04:00
|
|
|
check_level_diff(write=1, read=0, diff=+1)
|
|
|
|
check_level_diff(write=1, read=1, diff=+0)
|
2020-03-24 09:08:53 -04:00
|
|
|
check_level_diff(write=1, read=1, diff=+0)
|
|
|
|
check_level_diff(write=0, read=1, diff=-1)
|
2020-03-25 06:33:52 -04:00
|
|
|
check_level_diff(write=0, read=1, diff=-1)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
run_simulation(dut, main_generator())
|
2020-03-24 09:08:53 -04:00
|
|
|
|
|
|
|
# _LiteDRAMFIFOWriter --------------------------------------------------------------------------
|
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
def fifo_writer_test(self, depth, sequence_len, consume=False):
|
2020-03-24 09:08:53 -04:00
|
|
|
class DUT(Module):
|
|
|
|
def __init__(self):
|
|
|
|
self.port = LiteDRAMNativeWritePort(address_width=32, data_width=32)
|
2020-06-03 10:13:28 -04:00
|
|
|
ctrl = _LiteDRAMFIFOCtrl(base=8, depth=depth)
|
2020-03-24 09:08:53 -04:00
|
|
|
self.submodules.ctrl = ctrl
|
2020-03-26 07:23:25 -04:00
|
|
|
writer = _LiteDRAMFIFOWriter(data_width=32, port=self.port, ctrl=ctrl)
|
2020-03-24 09:08:53 -04:00
|
|
|
self.submodules.writer = writer
|
|
|
|
|
|
|
|
self.memory = DRAMMemory(32, 128)
|
|
|
|
assert 8 + sequence_len <= len(self.memory.mem)
|
|
|
|
|
|
|
|
write_data = [seed_to_data(i) for i in range(sequence_len)]
|
|
|
|
|
|
|
|
def generator(dut):
|
|
|
|
for data in write_data:
|
|
|
|
yield dut.writer.sink.valid.eq(1)
|
|
|
|
yield dut.writer.sink.data.eq(data)
|
|
|
|
yield
|
|
|
|
while (yield dut.writer.sink.ready) == 0:
|
|
|
|
yield
|
|
|
|
yield dut.writer.sink.valid.eq(0)
|
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
if consume:
|
|
|
|
yield dut.ctrl.read.eq(1)
|
|
|
|
|
2020-03-24 09:08:53 -04:00
|
|
|
for _ in range(16):
|
|
|
|
yield
|
|
|
|
|
|
|
|
dut = DUT()
|
|
|
|
generators = [
|
|
|
|
generator(dut),
|
|
|
|
dut.memory.write_handler(dut.port),
|
|
|
|
timeout_generator(1500),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
mem_expected = [0] * len(dut.memory.mem)
|
|
|
|
for i, data in enumerate(write_data):
|
|
|
|
mem_expected[8 + i%depth] = data
|
|
|
|
self.assertEqual(dut.memory.mem, mem_expected)
|
|
|
|
|
|
|
|
def test_fifo_writer_sequence(self):
|
2020-04-14 15:40:51 -04:00
|
|
|
# Verify simple FIFOWriter sequence.
|
2020-06-03 10:13:28 -04:00
|
|
|
self.fifo_writer_test(sequence_len=48, depth=64)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
def test_fifo_writer_stops_when_full(self):
|
|
|
|
# Verify FIFOWriter won't continue writing if noone reads the data.
|
2020-03-24 09:08:53 -04:00
|
|
|
with self.assertRaises(TimeoutError):
|
2020-06-03 10:13:28 -04:00
|
|
|
self.fifo_writer_test(sequence_len=48, depth=32)
|
|
|
|
|
|
|
|
def test_fifo_writer_address_wraps(self):
|
|
|
|
# Verify FIFOWriter address wraps.
|
|
|
|
self.fifo_writer_test(sequence_len=48, depth=32, consume=True)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
|
|
|
# _LiteDRAMFIFOReader --------------------------------------------------------------------------
|
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
def fifo_reader_test(self, depth, sequence_len, inital_writes=0):
|
2020-03-24 09:08:53 -04:00
|
|
|
memory_data = [seed_to_data(i) for i in range(128)]
|
2020-03-26 07:23:25 -04:00
|
|
|
read_data = []
|
2020-03-24 09:08:53 -04:00
|
|
|
|
|
|
|
class DUT(Module):
|
|
|
|
def __init__(self):
|
|
|
|
self.port = LiteDRAMNativeReadPort(address_width=32, data_width=32)
|
2020-06-03 10:13:28 -04:00
|
|
|
ctrl = _LiteDRAMFIFOCtrl(base=8, depth=depth)
|
2020-03-24 09:08:53 -04:00
|
|
|
reader = _LiteDRAMFIFOReader(data_width=32, port=self.port, ctrl=ctrl)
|
|
|
|
self.submodules.ctrl = ctrl
|
|
|
|
self.submodules.reader = reader
|
|
|
|
|
|
|
|
self.memory = DRAMMemory(32, len(memory_data), init=memory_data)
|
|
|
|
assert 8 + sequence_len <= len(self.memory.mem)
|
|
|
|
|
|
|
|
def reader(dut):
|
2020-03-26 07:23:25 -04:00
|
|
|
# Fake writing to fifo
|
2020-03-24 09:08:53 -04:00
|
|
|
yield dut.ctrl.write.eq(1)
|
|
|
|
for _ in range(inital_writes):
|
|
|
|
yield
|
|
|
|
yield dut.ctrl.write.eq(0)
|
|
|
|
yield
|
|
|
|
|
|
|
|
for _ in range(sequence_len):
|
2020-03-26 07:23:25 -04:00
|
|
|
# Fake single write
|
2020-03-24 09:08:53 -04:00
|
|
|
yield dut.ctrl.write.eq(1)
|
|
|
|
yield
|
|
|
|
yield dut.ctrl.write.eq(0)
|
|
|
|
|
|
|
|
while (yield dut.reader.source.valid) == 0:
|
|
|
|
yield
|
|
|
|
read_data.append((yield dut.reader.source.data))
|
|
|
|
yield dut.reader.source.ready.eq(1)
|
|
|
|
yield
|
|
|
|
yield dut.reader.source.ready.eq(0)
|
|
|
|
yield
|
|
|
|
|
|
|
|
dut = DUT()
|
|
|
|
generators = [
|
|
|
|
reader(dut),
|
|
|
|
dut.memory.read_handler(dut.port),
|
|
|
|
timeout_generator(1500),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
read_data_expected = [memory_data[8 + i%depth] for i in range(sequence_len)]
|
|
|
|
self.assertEqual(read_data, read_data_expected)
|
|
|
|
|
|
|
|
def test_fifo_reader_sequence(self):
|
2020-04-14 15:40:51 -04:00
|
|
|
# Verify simple FIFOReader sequence.
|
2020-06-03 10:13:28 -04:00
|
|
|
self.fifo_reader_test(sequence_len=48, depth=64)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
|
|
|
def test_fifo_reader_address_wraps(self):
|
2020-04-14 15:40:51 -04:00
|
|
|
# Verify FIFOReader sequence with address wraps.
|
2020-06-03 10:13:28 -04:00
|
|
|
self.fifo_reader_test(sequence_len=48, depth=32)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
|
|
|
# LiteDRAMFIFO ---------------------------------------------------------------------------------
|
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
def fifo_continuous_stream_short_test(self, with_bypass):
|
2020-06-03 10:13:28 -04:00
|
|
|
# Verify FIFO operation with continuous writes and reads without wrapping
|
|
|
|
def generator(dut):
|
|
|
|
for i in range(64):
|
|
|
|
yield from dut.write(10 + i)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
def checker(dut):
|
|
|
|
for i in range(64):
|
|
|
|
data = (yield from dut.read())
|
|
|
|
self.assertEqual(data, 10 + i)
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
dut = FIFODUT(base=16, depth=128, with_bypass=with_bypass)
|
2020-06-03 10:13:28 -04:00
|
|
|
generators = [
|
|
|
|
generator(dut),
|
|
|
|
checker(dut),
|
|
|
|
dut.memory.write_handler(dut.write_port),
|
|
|
|
dut.memory.read_handler(dut.read_port),
|
|
|
|
timeout_generator(1500),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
def test_fifo_continuous_stream_short(self):
|
|
|
|
self.fifo_continuous_stream_short_test(with_bypass=False)
|
|
|
|
|
2021-09-23 17:22:51 -04:00
|
|
|
def test_fifo_continuous_stream_short_with_bypass(self):
|
|
|
|
self.fifo_continuous_stream_short_test(with_bypass=True)
|
2021-09-21 13:23:36 -04:00
|
|
|
|
|
|
|
def fifo_continuous_stream_long_test(self, with_bypass):
|
2020-06-03 10:13:28 -04:00
|
|
|
# Verify FIFO operation with continuous writes and reads with wrapping
|
|
|
|
def generator(dut):
|
|
|
|
for i in range(64):
|
|
|
|
yield from dut.write(10 + i)
|
2020-03-24 09:08:53 -04:00
|
|
|
|
2020-06-03 10:13:28 -04:00
|
|
|
def checker(dut):
|
|
|
|
for i in range(64):
|
|
|
|
data = (yield from dut.read())
|
|
|
|
self.assertEqual(data, 10 + i)
|
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
dut = FIFODUT(base=16, depth=32, with_bypass=with_bypass)
|
2020-06-03 10:13:28 -04:00
|
|
|
generators = [
|
|
|
|
generator(dut),
|
|
|
|
checker(dut),
|
|
|
|
dut.memory.write_handler(dut.write_port),
|
|
|
|
dut.memory.read_handler(dut.read_port),
|
|
|
|
timeout_generator(1500),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
def test_fifo_continuous_stream_long(self):
|
|
|
|
self.fifo_continuous_stream_long_test(with_bypass=False)
|
|
|
|
|
2021-09-23 17:22:51 -04:00
|
|
|
def test_fifo_continuous_stream_long_with_bypass(self):
|
|
|
|
self.fifo_continuous_stream_long_test(with_bypass=True)
|
2021-09-21 13:23:36 -04:00
|
|
|
|
|
|
|
def fifo_delayed_reader_test(self, with_bypass):
|
2020-06-03 10:13:28 -04:00
|
|
|
# Verify FIFO works correctly when reader starts reading only after writer is full
|
|
|
|
def generator(dut):
|
2021-09-21 13:23:36 -04:00
|
|
|
for i in range(128):
|
2020-06-03 10:13:28 -04:00
|
|
|
yield from dut.write(10 + i)
|
|
|
|
|
|
|
|
def checker(dut):
|
|
|
|
# Wait until both the internal writer FIFO and our in-memory FIFO are full
|
2021-09-23 12:57:00 -04:00
|
|
|
for i in range(256):
|
2020-01-07 09:40:09 -05:00
|
|
|
yield
|
2021-09-21 13:23:36 -04:00
|
|
|
for i in range(128):
|
2020-06-03 10:13:28 -04:00
|
|
|
data = (yield from dut.read())
|
|
|
|
self.assertEqual(data, 10 + i)
|
2021-09-21 13:23:36 -04:00
|
|
|
for i in range(32):
|
|
|
|
yield
|
2020-01-07 09:40:09 -05:00
|
|
|
|
2021-09-21 13:23:36 -04:00
|
|
|
dut = FIFODUT(base=16, depth=32, with_bypass=with_bypass)
|
2020-01-07 09:40:09 -05:00
|
|
|
generators = [
|
|
|
|
generator(dut),
|
|
|
|
checker(dut),
|
|
|
|
dut.memory.write_handler(dut.write_port),
|
2020-06-03 10:13:28 -04:00
|
|
|
dut.memory.read_handler(dut.read_port),
|
|
|
|
timeout_generator(1500),
|
2020-01-07 09:40:09 -05:00
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
2021-09-21 13:23:36 -04:00
|
|
|
|
|
|
|
def test_fifo_delayed_reader(self):
|
|
|
|
self.fifo_delayed_reader_test(with_bypass=False)
|
|
|
|
|
|
|
|
def test_fifo_delayed_reader_with_bypass(self):
|
|
|
|
self.fifo_delayed_reader_test(with_bypass=True)
|