2020-08-23 09:52:08 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteDRAM.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2018-2019 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2019-06-23 17:56:50 -04:00
|
|
|
|
2018-08-21 07:39:46 -04:00
|
|
|
import unittest
|
|
|
|
import random
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
|
2018-11-12 07:09:05 -05:00
|
|
|
from litedram.common import *
|
2018-08-21 07:39:46 -04:00
|
|
|
from litedram.frontend.axi import *
|
|
|
|
|
2018-08-28 07:39:11 -04:00
|
|
|
from test.common import *
|
|
|
|
|
2018-08-21 07:39:46 -04:00
|
|
|
from litex.gen.sim import *
|
|
|
|
|
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
# Burst/Beat Helpers -------------------------------------------------------------------------------
|
|
|
|
|
2018-11-12 07:09:05 -05:00
|
|
|
class Burst:
|
2019-04-19 05:57:05 -04:00
|
|
|
def __init__(self, addr, type=BURST_FIXED, len=0, size=0):
|
2018-11-12 07:09:05 -05:00
|
|
|
self.addr = addr
|
|
|
|
self.type = type
|
2020-04-13 13:38:29 -04:00
|
|
|
self.len = len
|
2018-11-12 07:09:05 -05:00
|
|
|
self.size = size
|
|
|
|
|
|
|
|
def to_beats(self):
|
|
|
|
r = []
|
|
|
|
for i in range(self.len + 1):
|
2019-04-19 05:57:05 -04:00
|
|
|
if self.type == BURST_INCR:
|
2018-11-12 07:09:05 -05:00
|
|
|
offset = i*2**(self.size)
|
|
|
|
r += [Beat(self.addr + offset)]
|
2019-04-19 05:57:05 -04:00
|
|
|
elif self.type == BURST_WRAP:
|
2018-11-12 07:09:05 -05:00
|
|
|
offset = (i*2**(self.size))%((2**self.size)*(self.len))
|
|
|
|
r += [Beat(self.addr + offset)]
|
|
|
|
else:
|
|
|
|
r += [Beat(self.addr)]
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class Beat:
|
|
|
|
def __init__(self, addr):
|
|
|
|
self.addr = addr
|
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
# Access Helpers -----------------------------------------------------------------------------------
|
2018-11-12 07:09:05 -05:00
|
|
|
|
|
|
|
class Access(Burst):
|
2022-02-28 12:35:39 -05:00
|
|
|
def __init__(self, addr, data, strb, id, **kwargs):
|
2018-11-12 07:09:05 -05:00
|
|
|
Burst.__init__(self, addr, **kwargs)
|
|
|
|
self.data = data
|
2022-02-28 12:35:39 -05:00
|
|
|
self.strb = strb
|
2020-04-13 13:38:29 -04:00
|
|
|
self.id = id
|
2018-11-12 07:09:05 -05:00
|
|
|
|
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
class Write(Access): pass
|
2018-11-12 07:09:05 -05:00
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
class Read(Access): pass
|
2018-11-12 07:09:05 -05:00
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
def strb_to_mask(strb, data_width):
|
|
|
|
mask = 0
|
|
|
|
for i in reversed(range(data_width//8)):
|
|
|
|
mask <<= 8
|
|
|
|
if (strb >> i) & 0x1:
|
|
|
|
mask |= 0xff
|
|
|
|
return mask
|
2018-11-12 07:09:05 -05:00
|
|
|
|
2022-02-28 12:35:39 -05:00
|
|
|
# Test AXI -----------------------------------------------------------------------------------------
|
2018-11-12 07:09:05 -05:00
|
|
|
|
2018-08-27 10:21:12 -04:00
|
|
|
class TestAXI(unittest.TestCase):
|
2018-11-30 03:37:42 -05:00
|
|
|
def _test_axi2native(self,
|
2018-12-03 02:01:33 -05:00
|
|
|
naccesses=16, simultaneous_writes_reads=False,
|
2020-04-13 13:38:29 -04:00
|
|
|
# Random: 0: min (no random), 100: max.
|
|
|
|
# Burst randomness
|
2018-11-30 03:37:42 -05:00
|
|
|
id_rand_enable = False,
|
|
|
|
len_rand_enable = False,
|
|
|
|
data_rand_enable = False,
|
2020-04-13 13:38:29 -04:00
|
|
|
# Flow valid randomness
|
2019-07-23 15:14:17 -04:00
|
|
|
aw_valid_random = 0,
|
|
|
|
w_valid_random = 0,
|
|
|
|
ar_valid_random = 0,
|
|
|
|
r_valid_random = 0,
|
2020-04-13 13:38:29 -04:00
|
|
|
# Flow ready randomness
|
2019-07-23 15:14:17 -04:00
|
|
|
w_ready_random = 0,
|
|
|
|
b_ready_random = 0,
|
|
|
|
r_ready_random = 0
|
2018-11-30 03:37:42 -05:00
|
|
|
):
|
|
|
|
|
2018-11-12 06:58:19 -05:00
|
|
|
def writes_cmd_generator(axi_port, writes):
|
2019-07-23 15:14:17 -04:00
|
|
|
prng = random.Random(42)
|
2018-08-28 07:39:11 -04:00
|
|
|
for write in writes:
|
2019-07-23 15:14:17 -04:00
|
|
|
while prng.randrange(100) < aw_valid_random:
|
|
|
|
yield
|
2020-04-13 13:38:29 -04:00
|
|
|
# Send command
|
2018-08-27 10:21:12 -04:00
|
|
|
yield axi_port.aw.valid.eq(1)
|
2018-08-28 07:39:11 -04:00
|
|
|
yield axi_port.aw.addr.eq(write.addr<<2)
|
2018-11-12 11:16:27 -05:00
|
|
|
yield axi_port.aw.burst.eq(write.type)
|
|
|
|
yield axi_port.aw.len.eq(write.len)
|
|
|
|
yield axi_port.aw.size.eq(write.size)
|
2018-08-28 07:39:11 -04:00
|
|
|
yield axi_port.aw.id.eq(write.id)
|
2018-11-12 11:16:27 -05:00
|
|
|
yield
|
2018-08-27 12:39:36 -04:00
|
|
|
while (yield axi_port.aw.ready) == 0:
|
2018-08-27 10:21:12 -04:00
|
|
|
yield
|
2018-11-09 04:10:07 -05:00
|
|
|
yield axi_port.aw.valid.eq(0)
|
2018-11-12 06:58:19 -05:00
|
|
|
|
|
|
|
def writes_data_generator(axi_port, writes):
|
2019-07-23 15:14:17 -04:00
|
|
|
prng = random.Random(42)
|
2018-11-12 06:58:19 -05:00
|
|
|
for write in writes:
|
2022-02-28 12:35:39 -05:00
|
|
|
for i, (data, strb) in enumerate(zip(write.data, write.strb)):
|
2019-07-23 15:14:17 -04:00
|
|
|
while prng.randrange(100) < w_valid_random:
|
|
|
|
yield
|
2020-04-13 13:38:29 -04:00
|
|
|
# Send data
|
2018-11-12 11:16:27 -05:00
|
|
|
yield axi_port.w.valid.eq(1)
|
|
|
|
if (i == (len(write.data) - 1)):
|
|
|
|
yield axi_port.w.last.eq(1)
|
|
|
|
else:
|
|
|
|
yield axi_port.w.last.eq(0)
|
|
|
|
yield axi_port.w.data.eq(data)
|
2022-02-28 12:35:39 -05:00
|
|
|
yield axi_port.w.strb.eq(strb)
|
2018-08-27 10:21:12 -04:00
|
|
|
yield
|
2018-11-12 11:16:27 -05:00
|
|
|
while (yield axi_port.w.ready) == 0:
|
|
|
|
yield
|
|
|
|
yield axi_port.w.valid.eq(0)
|
2018-11-30 04:40:45 -05:00
|
|
|
axi_port.reads_enable = True
|
2018-08-28 07:43:58 -04:00
|
|
|
|
|
|
|
def writes_response_generator(axi_port, writes):
|
2019-07-23 15:14:17 -04:00
|
|
|
prng = random.Random(42)
|
2018-08-28 07:43:58 -04:00
|
|
|
self.writes_id_errors = 0
|
|
|
|
for write in writes:
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait response
|
2018-11-30 03:37:42 -05:00
|
|
|
yield axi_port.b.ready.eq(0)
|
|
|
|
yield
|
2018-08-28 07:39:11 -04:00
|
|
|
while (yield axi_port.b.valid) == 0:
|
|
|
|
yield
|
2019-07-23 15:14:17 -04:00
|
|
|
while prng.randrange(100) < b_ready_random:
|
|
|
|
yield
|
2018-11-30 03:37:42 -05:00
|
|
|
yield axi_port.b.ready.eq(1)
|
|
|
|
yield
|
2018-08-28 07:39:11 -04:00
|
|
|
if (yield axi_port.b.id) != write.id:
|
|
|
|
self.writes_id_errors += 1
|
2018-08-27 12:39:36 -04:00
|
|
|
|
2018-08-28 07:43:58 -04:00
|
|
|
def reads_cmd_generator(axi_port, reads):
|
2019-07-23 15:14:17 -04:00
|
|
|
prng = random.Random(42)
|
2018-11-30 04:40:45 -05:00
|
|
|
while not axi_port.reads_enable:
|
|
|
|
yield
|
2018-08-28 07:39:11 -04:00
|
|
|
for read in reads:
|
2019-07-23 15:14:17 -04:00
|
|
|
while prng.randrange(100) < ar_valid_random:
|
|
|
|
yield
|
2020-04-13 13:38:29 -04:00
|
|
|
# Send command
|
2018-08-27 10:21:12 -04:00
|
|
|
yield axi_port.ar.valid.eq(1)
|
2018-08-28 07:39:11 -04:00
|
|
|
yield axi_port.ar.addr.eq(read.addr<<2)
|
2018-11-12 11:16:27 -05:00
|
|
|
yield axi_port.ar.burst.eq(read.type)
|
|
|
|
yield axi_port.ar.len.eq(read.len)
|
|
|
|
yield axi_port.ar.size.eq(read.size)
|
2018-08-28 07:39:11 -04:00
|
|
|
yield axi_port.ar.id.eq(read.id)
|
2018-08-27 12:39:36 -04:00
|
|
|
yield
|
|
|
|
while (yield axi_port.ar.ready) == 0:
|
2018-08-27 10:21:12 -04:00
|
|
|
yield
|
|
|
|
yield axi_port.ar.valid.eq(0)
|
2018-08-28 07:43:58 -04:00
|
|
|
|
|
|
|
def reads_response_data_generator(axi_port, reads):
|
2019-07-23 15:14:17 -04:00
|
|
|
prng = random.Random(42)
|
2018-08-28 07:43:58 -04:00
|
|
|
self.reads_data_errors = 0
|
2020-04-13 13:38:29 -04:00
|
|
|
self.reads_id_errors = 0
|
2018-09-06 05:11:17 -04:00
|
|
|
self.reads_last_errors = 0
|
2018-11-30 04:40:45 -05:00
|
|
|
while not axi_port.reads_enable:
|
|
|
|
yield
|
2018-08-28 07:43:58 -04:00
|
|
|
for read in reads:
|
2022-02-28 12:35:39 -05:00
|
|
|
for i, (data, strb) in enumerate(zip(read.data, read.strb)):
|
2020-04-13 13:38:29 -04:00
|
|
|
# Wait data / response
|
2018-11-30 03:37:42 -05:00
|
|
|
yield axi_port.r.ready.eq(0)
|
|
|
|
yield
|
2018-11-12 11:16:27 -05:00
|
|
|
while (yield axi_port.r.valid) == 0:
|
|
|
|
yield
|
2019-07-23 15:14:17 -04:00
|
|
|
while prng.randrange(100) < r_ready_random:
|
|
|
|
yield
|
2018-11-30 03:37:42 -05:00
|
|
|
yield axi_port.r.ready.eq(1)
|
|
|
|
yield
|
2022-02-28 12:35:39 -05:00
|
|
|
data_ref = data & strb_to_mask(strb, axi_port.data_width)
|
|
|
|
data_cur = (yield axi_port.r.data) & strb_to_mask(strb, axi_port.data_width)
|
|
|
|
if data_ref != data_cur:
|
|
|
|
print(f"ref: {data_ref:08x} vs cur: {data_cur:08x}")
|
2018-11-12 11:16:27 -05:00
|
|
|
self.reads_data_errors += 1
|
|
|
|
if (yield axi_port.r.id) != read.id:
|
|
|
|
self.reads_id_errors += 1
|
|
|
|
if i == (len(read.data) - 1):
|
|
|
|
if (yield axi_port.r.last) != 1:
|
|
|
|
self.reads_last_errors += 1
|
|
|
|
else:
|
|
|
|
if (yield axi_port.r.last) != 0:
|
|
|
|
self.reads_last_errors += 1
|
2018-08-21 07:39:46 -04:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# DUT
|
|
|
|
axi_port = LiteDRAMAXIPort(32, 32, 8)
|
2018-08-27 10:21:12 -04:00
|
|
|
dram_port = LiteDRAMNativePort("both", 32, 32)
|
2022-02-28 12:45:46 -05:00
|
|
|
dut = LiteDRAMAXI2Native(axi_port, dram_port, with_read_modify_write=True)
|
2020-04-13 13:38:29 -04:00
|
|
|
mem = DRAMMemory(32, 1024)
|
2018-08-28 07:39:11 -04:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Generate writes/reads
|
|
|
|
prng = random.Random(42)
|
2018-08-28 07:39:11 -04:00
|
|
|
writes = []
|
2018-12-03 02:01:33 -05:00
|
|
|
offset = 1
|
2018-11-30 04:40:45 -05:00
|
|
|
for i in range(naccesses):
|
2022-02-28 12:35:39 -05:00
|
|
|
_id = prng.randrange(2**8) if id_rand_enable else i
|
|
|
|
_len = prng.randrange(32) if len_rand_enable else i
|
|
|
|
_data = [prng.randrange(2**32) if data_rand_enable else j for j in range(_len + 1)]
|
|
|
|
_strb = [prng.randrange(2**(32//8)) if data_rand_enable else 0xff for j in range(_len + 1)]
|
|
|
|
writes.append(Write(offset, _data, _strb, _id, type=BURST_INCR, len=_len, size=log2_int(32//8)))
|
2018-11-30 04:40:45 -05:00
|
|
|
offset += _len + 1
|
2020-04-13 13:38:29 -04:00
|
|
|
# Dummy reads to ensure datas have been written before the effective reads start.
|
2022-02-28 12:35:39 -05:00
|
|
|
dummy_reads = [Read(1023, [0], [2**(32//8)-1], 0, type=BURST_FIXED, len=0, size=log2_int(32//8)) for _ in range(32)]
|
2018-12-03 02:01:33 -05:00
|
|
|
reads = dummy_reads + writes
|
2018-08-27 10:21:12 -04:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Simulation
|
2018-11-30 04:40:45 -05:00
|
|
|
if simultaneous_writes_reads:
|
|
|
|
axi_port.reads_enable = True
|
|
|
|
else:
|
2020-04-13 13:38:29 -04:00
|
|
|
axi_port.reads_enable = False # Will be set by writes_data_generator
|
2018-08-27 12:39:36 -04:00
|
|
|
generators = [
|
2018-11-12 06:58:19 -05:00
|
|
|
writes_cmd_generator(axi_port, writes),
|
|
|
|
writes_data_generator(axi_port, writes),
|
2018-08-28 07:43:58 -04:00
|
|
|
writes_response_generator(axi_port, writes),
|
|
|
|
reads_cmd_generator(axi_port, reads),
|
|
|
|
reads_response_data_generator(axi_port, reads),
|
2019-07-23 15:14:17 -04:00
|
|
|
mem.read_handler(dram_port, rdata_valid_random=r_valid_random),
|
|
|
|
mem.write_handler(dram_port, wdata_ready_random=w_ready_random)
|
2018-08-27 12:39:36 -04:00
|
|
|
]
|
2022-02-28 12:35:39 -05:00
|
|
|
run_simulation(dut, generators, vcd_name="sim.vcd")
|
2018-08-28 07:43:58 -04:00
|
|
|
#mem.show_content()
|
2018-08-28 07:39:11 -04:00
|
|
|
self.assertEqual(self.writes_id_errors, 0)
|
|
|
|
self.assertEqual(self.reads_data_errors, 0)
|
|
|
|
self.assertEqual(self.reads_id_errors, 0)
|
2018-09-06 05:11:17 -04:00
|
|
|
self.assertEqual(self.reads_last_errors, 0)
|
2018-11-30 03:37:42 -05:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Test with no randomness
|
2018-11-30 04:40:45 -05:00
|
|
|
def test_axi2native_writes_then_reads_no_random(self):
|
|
|
|
self._test_axi2native(simultaneous_writes_reads=False)
|
|
|
|
|
|
|
|
def test_axi2native_writes_and_reads_no_random(self):
|
2018-12-03 02:01:33 -05:00
|
|
|
self._test_axi2native(simultaneous_writes_reads=True)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Test randomness one parameter at a time
|
2018-12-03 02:01:33 -05:00
|
|
|
def test_axi2native_writes_then_reads_random_bursts(self):
|
2018-11-30 04:40:45 -05:00
|
|
|
self._test_axi2native(
|
2020-04-13 13:38:29 -04:00
|
|
|
simultaneous_writes_reads = False,
|
|
|
|
id_rand_enable = True,
|
|
|
|
len_rand_enable = True,
|
|
|
|
data_rand_enable = True)
|
2018-12-03 02:01:33 -05:00
|
|
|
|
|
|
|
def test_axi2native_writes_and_reads_random_bursts(self):
|
|
|
|
self._test_axi2native(
|
2020-04-13 13:38:29 -04:00
|
|
|
simultaneous_writes_reads = True,
|
|
|
|
id_rand_enable = True,
|
|
|
|
len_rand_enable = True,
|
|
|
|
data_rand_enable = True)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
2018-11-30 05:22:04 -05:00
|
|
|
def test_axi2native_random_w_ready(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(w_ready_random=90)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
2018-11-30 05:22:04 -05:00
|
|
|
def test_axi2native_random_b_ready(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(b_ready_random=90)
|
2018-11-30 05:22:04 -05:00
|
|
|
|
|
|
|
def test_axi2native_random_r_ready(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(r_ready_random=90)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
|
|
|
def test_axi2native_random_aw_valid(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(aw_valid_random=90)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
|
|
|
def test_axi2native_random_w_valid(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(w_valid_random=90)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
|
|
|
def test_axi2native_random_ar_valid(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(ar_valid_random=90)
|
2018-11-30 04:40:45 -05:00
|
|
|
|
2018-11-30 05:22:04 -05:00
|
|
|
def test_axi2native_random_r_valid(self):
|
2019-07-23 15:14:17 -04:00
|
|
|
self._test_axi2native(r_valid_random=90)
|
2018-11-30 05:22:04 -05:00
|
|
|
|
2020-04-13 13:38:29 -04:00
|
|
|
# Now let's stress things a bit... :)
|
2018-11-30 04:40:45 -05:00
|
|
|
def test_axi2native_random_all(self):
|
|
|
|
self._test_axi2native(
|
|
|
|
simultaneous_writes_reads=True,
|
2020-04-13 13:38:29 -04:00
|
|
|
id_rand_enable = True,
|
|
|
|
len_rand_enable = True,
|
|
|
|
aw_valid_random = 50,
|
|
|
|
w_ready_random = 50,
|
|
|
|
b_ready_random = 50,
|
|
|
|
w_valid_random = 50,
|
|
|
|
ar_valid_random = 90,
|
|
|
|
r_valid_random = 90,
|
|
|
|
r_ready_random = 90
|
2018-11-30 04:40:45 -05:00
|
|
|
)
|