2020-08-23 09:52:08 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteDRAM.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2020 Antmicro <www.antmicro.com>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2020-04-03 06:49:07 -04:00
|
|
|
|
|
|
|
import random
|
|
|
|
import unittest
|
|
|
|
import itertools
|
|
|
|
import collections
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
|
|
|
|
from litex.soc.interconnect import stream
|
|
|
|
|
|
|
|
from litedram.common import *
|
|
|
|
from litedram.core.bandwidth import Bandwidth
|
|
|
|
|
|
|
|
from test.common import timeout_generator, CmdRequestRWDriver
|
|
|
|
|
|
|
|
|
|
|
|
class BandwidthDUT(Module):
|
|
|
|
def __init__(self, data_width=8, **kwargs):
|
|
|
|
a, ba = 13, 3
|
|
|
|
self.cmd = stream.Endpoint(cmd_request_rw_layout(a, ba))
|
|
|
|
self.submodules.bandwidth = Bandwidth(self.cmd, data_width, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class CommandDriver:
|
|
|
|
def __init__(self, cmd, cmd_options=None):
|
|
|
|
self.cmd = cmd
|
|
|
|
self.driver = CmdRequestRWDriver(cmd)
|
|
|
|
self.cmd_counts = collections.defaultdict(int)
|
|
|
|
|
|
|
|
@passive
|
|
|
|
def random_generator(self, random_ready_max=20, commands=None):
|
|
|
|
commands = commands or ["read", "write"]
|
|
|
|
prng = random.Random(42)
|
|
|
|
|
|
|
|
while True:
|
2020-04-13 13:38:29 -04:00
|
|
|
# Generate random command
|
2020-04-03 06:49:07 -04:00
|
|
|
command = prng.choice(commands)
|
|
|
|
yield from getattr(self.driver, command)()
|
|
|
|
yield
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait some times before it becomes ready
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(prng.randint(0, random_ready_max)):
|
|
|
|
yield
|
|
|
|
yield self.cmd.ready.eq(1)
|
|
|
|
yield
|
|
|
|
self.cmd_counts[command] += 1
|
|
|
|
yield self.cmd.ready.eq(0)
|
2020-04-13 13:38:29 -04:00
|
|
|
# Disable command
|
2020-04-03 06:49:07 -04:00
|
|
|
yield from self.driver.nop()
|
|
|
|
yield
|
|
|
|
|
|
|
|
@passive
|
|
|
|
def timeline_generator(self, timeline):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Timeline: an iterator of tuples (cycle, command)
|
2020-04-03 06:49:07 -04:00
|
|
|
sim_cycle = 0
|
|
|
|
for cycle, command in timeline:
|
|
|
|
assert cycle >= sim_cycle
|
|
|
|
while sim_cycle != cycle:
|
|
|
|
sim_cycle += 1
|
|
|
|
yield
|
2020-04-13 13:38:29 -04:00
|
|
|
# Set the command
|
2020-04-03 06:49:07 -04:00
|
|
|
yield from getattr(self.driver, command)()
|
|
|
|
yield self.cmd.ready.eq(1)
|
|
|
|
self.cmd_counts[command] += 1
|
2020-04-13 13:38:29 -04:00
|
|
|
# Advance 1 cycle
|
2020-04-03 06:49:07 -04:00
|
|
|
yield
|
|
|
|
sim_cycle += 1
|
2020-04-13 13:38:29 -04:00
|
|
|
# Clear state
|
2020-04-03 06:49:07 -04:00
|
|
|
yield self.cmd.ready.eq(0)
|
|
|
|
yield from self.driver.nop()
|
|
|
|
|
|
|
|
|
|
|
|
class TestBandwidth(unittest.TestCase):
|
|
|
|
def test_can_read_status_data_width(self):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Verify that data width can be read from a CSR.
|
2020-04-03 06:49:07 -04:00
|
|
|
def test(data_width):
|
|
|
|
def main_generator(dut):
|
|
|
|
yield
|
|
|
|
self.assertEqual((yield dut.bandwidth.data_width.status), data_width)
|
|
|
|
|
|
|
|
dut = BandwidthDUT(data_width=data_width)
|
|
|
|
run_simulation(dut, main_generator(dut))
|
|
|
|
|
|
|
|
for data_width in [8, 16, 32, 64]:
|
|
|
|
with self.subTest(data_width=data_width):
|
|
|
|
test(data_width)
|
|
|
|
|
|
|
|
def test_requires_update_to_copy_the_data(self):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Verify that command counts are copied to CSRs only after `update`.
|
2020-04-03 06:49:07 -04:00
|
|
|
def main_generator(dut):
|
2020-04-14 15:57:40 -04:00
|
|
|
nreads = (yield from dut.bandwidth.nreads.read())
|
2020-04-03 06:49:07 -04:00
|
|
|
nwrites = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
self.assertEqual(nreads, 0)
|
|
|
|
self.assertEqual(nwrites, 0)
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait enough for the period to end
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(2**6):
|
|
|
|
yield
|
|
|
|
|
2020-04-14 15:57:40 -04:00
|
|
|
nreads = (yield from dut.bandwidth.nreads.read())
|
2020-04-03 06:49:07 -04:00
|
|
|
nwrites = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
self.assertEqual(nreads, 0)
|
|
|
|
self.assertEqual(nwrites, 0)
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Update register values
|
2020-04-03 06:49:07 -04:00
|
|
|
yield from dut.bandwidth.update.write(1)
|
|
|
|
|
2020-04-14 15:57:40 -04:00
|
|
|
nreads = (yield from dut.bandwidth.nreads.read())
|
2020-04-03 06:49:07 -04:00
|
|
|
nwrites = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
self.assertNotEqual((nreads, nwrites), (0, 0))
|
|
|
|
|
|
|
|
dut = BandwidthDUT(period_bits=6)
|
|
|
|
cmd_driver = CommandDriver(dut.cmd)
|
|
|
|
generators = [
|
|
|
|
main_generator(dut),
|
|
|
|
cmd_driver.random_generator(),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
def test_correct_read_write_counts(self):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Verify that the number of registered READ/WRITE commands is correct.
|
2020-04-03 06:49:07 -04:00
|
|
|
results = {}
|
|
|
|
|
|
|
|
def main_generator(dut):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait for the first period to end
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(2**8):
|
|
|
|
yield
|
|
|
|
yield from dut.bandwidth.update.write(1)
|
|
|
|
yield
|
2020-04-14 15:57:40 -04:00
|
|
|
results["nreads"] = (yield from dut.bandwidth.nreads.read())
|
2020-04-03 06:49:07 -04:00
|
|
|
results["nwrites"] = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
|
|
|
|
dut = BandwidthDUT(period_bits=8)
|
|
|
|
cmd_driver = CommandDriver(dut.cmd)
|
|
|
|
generators = [
|
|
|
|
main_generator(dut),
|
|
|
|
cmd_driver.random_generator(),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
self.assertEqual(results["nreads"], cmd_driver.cmd_counts["read"])
|
|
|
|
|
|
|
|
def test_counts_read_write_only(self):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Verify that only READ and WRITE commands are registered.
|
2020-04-03 06:49:07 -04:00
|
|
|
results = {}
|
|
|
|
|
|
|
|
def main_generator(dut):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait for the first period to end
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(2**8):
|
|
|
|
yield
|
|
|
|
yield from dut.bandwidth.update.write(1)
|
|
|
|
yield
|
|
|
|
results["nreads"] = (yield from dut.bandwidth.nreads.read())
|
|
|
|
results["nwrites"] = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
|
|
|
|
dut = BandwidthDUT(period_bits=8)
|
|
|
|
cmd_driver = CommandDriver(dut.cmd)
|
2020-04-14 15:57:40 -04:00
|
|
|
commands = ["read", "write", "activate", "precharge", "refresh"]
|
2020-04-03 06:49:07 -04:00
|
|
|
generators = [
|
|
|
|
main_generator(dut),
|
|
|
|
cmd_driver.random_generator(commands=commands),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
self.assertEqual(results["nreads"], cmd_driver.cmd_counts["read"])
|
|
|
|
|
|
|
|
def test_correct_period_length(self):
|
2020-04-14 15:57:40 -04:00
|
|
|
# Verify that period length is correct by measuring time between CSR changes.
|
2020-04-03 06:49:07 -04:00
|
|
|
period_bits = 5
|
|
|
|
period = 2**period_bits
|
|
|
|
|
|
|
|
n_per_period = {0: 3, 1: 6, 2: 9}
|
|
|
|
timeline = {}
|
|
|
|
for p, n in n_per_period.items():
|
|
|
|
for i in range(n):
|
|
|
|
margin = 10
|
|
|
|
timeline[period*p + margin + i] = "write"
|
|
|
|
|
|
|
|
def main_generator(dut):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Keep the values always up to date
|
2020-04-03 06:49:07 -04:00
|
|
|
yield dut.bandwidth.update.re.eq(1)
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait until we have the data from 1st period
|
2020-04-03 06:49:07 -04:00
|
|
|
while (yield dut.bandwidth.nwrites.status) != 3:
|
|
|
|
yield
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Count time to next period
|
2020-04-03 06:49:07 -04:00
|
|
|
cycles = 0
|
|
|
|
while (yield dut.bandwidth.nwrites.status) != 6:
|
|
|
|
cycles += 1
|
|
|
|
yield
|
|
|
|
|
|
|
|
self.assertEqual(cycles, period)
|
|
|
|
|
|
|
|
dut = BandwidthDUT(period_bits=period_bits)
|
|
|
|
cmd_driver = CommandDriver(dut.cmd)
|
|
|
|
generators = [
|
|
|
|
main_generator(dut),
|
|
|
|
cmd_driver.timeline_generator(timeline.items()),
|
|
|
|
timeout_generator(period * 3),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|
|
|
|
|
|
|
|
def test_not_missing_commands_on_period_boundary(self):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Verify that no data is lost in the cycle when new period starts.
|
2020-04-03 06:49:07 -04:00
|
|
|
period_bits = 5
|
|
|
|
period = 2**period_bits
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Start 10 cycles before period ends, end 10 cycles after it ends
|
2020-04-03 06:49:07 -04:00
|
|
|
base = period - 10
|
|
|
|
nwrites = 20
|
|
|
|
timeline = {base + i: "write" for i in range(nwrites)}
|
|
|
|
|
|
|
|
def main_generator(dut):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait until 1st period ends (+ some margin)
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(period + 10):
|
|
|
|
yield
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Read the count from 1st period
|
2020-04-03 06:49:07 -04:00
|
|
|
yield from dut.bandwidth.update.write(1)
|
|
|
|
yield
|
|
|
|
nwrites_registered = (yield from dut.bandwidth.nwrites.read())
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait until 2nd period ends
|
2020-04-03 06:49:07 -04:00
|
|
|
for _ in range(period):
|
|
|
|
yield
|
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Read the count from 1st period
|
2020-04-03 06:49:07 -04:00
|
|
|
yield from dut.bandwidth.update.write(1)
|
|
|
|
yield
|
|
|
|
nwrites_registered += (yield from dut.bandwidth.nwrites.read())
|
|
|
|
|
|
|
|
self.assertEqual(nwrites_registered, nwrites)
|
|
|
|
|
|
|
|
dut = BandwidthDUT(period_bits=period_bits)
|
|
|
|
cmd_driver = CommandDriver(dut.cmd)
|
|
|
|
generators = [
|
|
|
|
main_generator(dut),
|
|
|
|
cmd_driver.timeline_generator(timeline.items()),
|
|
|
|
]
|
|
|
|
run_simulation(dut, generators)
|