Skip to content

Commit

Permalink
Added a test for all sizes 32/64b and alignments
Browse files Browse the repository at this point in the history
Signed-off-by: Anderson Ignacio <anderson@aignacio.com>
  • Loading branch information
aignacio committed Dec 25, 2023
1 parent 41c5e3a commit a1dbf65
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 91 deletions.
4 changes: 2 additions & 2 deletions cocotbext/ahb/ahb_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License : MIT license <Check LICENSE>
# Author : Anderson I. da Silva (aignacio) <anderson@aignacio.com>
# Date : 08.10.2023
# Last Modified Date: 15.11.2023
# Last Modified Date: 25.12.2023

import cocotb
import logging
Expand Down Expand Up @@ -184,7 +184,7 @@ async def _send_txn(
f"\tID = {index}\n"
f"\tADDR = 0x{txn_addr:x}\n"
f"\tDATA = 0x{value[index+1]:x}\n"
f"\tSIZE = {txn_size}"
f"\tSIZE = {txn_size} bytes"
)
self.bus.hwdata.value = txn_data
if self.bus.hready_in_exist:
Expand Down
123 changes: 86 additions & 37 deletions docs_utils/template_64b.gtkw
Original file line number Diff line number Diff line change
@@ -1,57 +1,106 @@
[*]
[*] GTKWave Analyzer v3.4.0 (w)1999-2022 BSI
[*] Mon Oct 30 18:55:22 2023
[*] Mon Dec 25 19:36:33 2023
[*]
[dumpfile] "run_dir/sim_build_icarus_test_ahb_lite_sram_data_width_64_bits/ahb_template.fst"
[dumpfile_mtime] "Mon Oct 30 18:52:23 2023"
[dumpfile_size] 123189
[dumpfile] "./run_dir/sim_build_icarus_test_ahb_lite_sram_all_sizes_data_width_64_bits/ahb_template.fst"
[dumpfile_mtime] "Mon Dec 25 19:32:54 2023"
[dumpfile_size] 1768
[savefile] "/Users/aignacio/projects/cocotbext-ahb/docs_utils/template_64b.gtkw"
[timestart] 0
[size] 1440 771
[pos] 577 0
*-25.811741 18043200 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[size] 3008 1554
[pos] 1890 802
*-16.359070 103180 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[sst_width] 253
[signals_width] 315
[sst_expanded] 1
[sst_vpaned_height] 252
[sst_vpaned_height] 566
@28
ahb_template.hresetn
ahb_template.hclk
@200
-
-slave
@28
ahb_template.slave_hsel
ahb_template.hsel
@22
ahb_template.slave_haddr[31:0]
ahb_template.haddr[31:0]
@28
ahb_template.slave_hsize[2:0]
ahb_template.slave_htrans[1:0]
ahb_template.slave_hwrite
@23
ahb_template.slave_hwdata[63:0]
@28
ahb_template.slave_hready_in
ahb_template.slave_hready
ahb_template.slave_hresp
@22
ahb_template.slave_hrdata[63:0]
@200
-
-master
ahb_template.hready_in
ahb_template.htrans[1:0]
ahb_template.hsize[2:0]
ahb_template.hwrite
@22
ahb_template.master_haddr[31:0]
ahb_template.hwdata[63:0]
@28
ahb_template.master_hsize[2:0]
ahb_template.master_htrans[1:0]
ahb_template.master_hwrite
@22
ahb_template.master_hwdata[63:0]
@28
ahb_template.master_hready_in
ahb_template.master_hready
ahb_template.master_hresp
@22
ahb_template.master_hrdata[63:0]
ahb_template.hready
ahb_template.hresp
@c00023
ahb_template.hrdata[63:0]
@29
(0)ahb_template.hrdata[63:0]
(1)ahb_template.hrdata[63:0]
(2)ahb_template.hrdata[63:0]
(3)ahb_template.hrdata[63:0]
(4)ahb_template.hrdata[63:0]
(5)ahb_template.hrdata[63:0]
(6)ahb_template.hrdata[63:0]
(7)ahb_template.hrdata[63:0]
(8)ahb_template.hrdata[63:0]
(9)ahb_template.hrdata[63:0]
(10)ahb_template.hrdata[63:0]
(11)ahb_template.hrdata[63:0]
(12)ahb_template.hrdata[63:0]
(13)ahb_template.hrdata[63:0]
(14)ahb_template.hrdata[63:0]
(15)ahb_template.hrdata[63:0]
(16)ahb_template.hrdata[63:0]
(17)ahb_template.hrdata[63:0]
(18)ahb_template.hrdata[63:0]
(19)ahb_template.hrdata[63:0]
(20)ahb_template.hrdata[63:0]
(21)ahb_template.hrdata[63:0]
(22)ahb_template.hrdata[63:0]
(23)ahb_template.hrdata[63:0]
(24)ahb_template.hrdata[63:0]
(25)ahb_template.hrdata[63:0]
(26)ahb_template.hrdata[63:0]
(27)ahb_template.hrdata[63:0]
(28)ahb_template.hrdata[63:0]
(29)ahb_template.hrdata[63:0]
(30)ahb_template.hrdata[63:0]
(31)ahb_template.hrdata[63:0]
(32)ahb_template.hrdata[63:0]
(33)ahb_template.hrdata[63:0]
(34)ahb_template.hrdata[63:0]
(35)ahb_template.hrdata[63:0]
(36)ahb_template.hrdata[63:0]
(37)ahb_template.hrdata[63:0]
(38)ahb_template.hrdata[63:0]
(39)ahb_template.hrdata[63:0]
(40)ahb_template.hrdata[63:0]
(41)ahb_template.hrdata[63:0]
(42)ahb_template.hrdata[63:0]
(43)ahb_template.hrdata[63:0]
(44)ahb_template.hrdata[63:0]
(45)ahb_template.hrdata[63:0]
(46)ahb_template.hrdata[63:0]
(47)ahb_template.hrdata[63:0]
(48)ahb_template.hrdata[63:0]
(49)ahb_template.hrdata[63:0]
(50)ahb_template.hrdata[63:0]
(51)ahb_template.hrdata[63:0]
(52)ahb_template.hrdata[63:0]
(53)ahb_template.hrdata[63:0]
(54)ahb_template.hrdata[63:0]
(55)ahb_template.hrdata[63:0]
(56)ahb_template.hrdata[63:0]
(57)ahb_template.hrdata[63:0]
(58)ahb_template.hrdata[63:0]
(59)ahb_template.hrdata[63:0]
(60)ahb_template.hrdata[63:0]
(61)ahb_template.hrdata[63:0]
(62)ahb_template.hrdata[63:0]
(63)ahb_template.hrdata[63:0]
@1401201
-group_end
[pattern_trace] 1
[pattern_trace] 0
86 changes: 34 additions & 52 deletions tests/test_ahb_lite_sram_all_sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License : MIT license <Check LICENSE>
# Author : Anderson I. da Silva (aignacio) <anderson@aignacio.com>
# Date : 08.10.2023
# Last Modified Date: 18.12.2023
# Last Modified Date: 25.12.2023

import cocotb
import os
Expand All @@ -15,10 +15,21 @@
from cocotb_test.simulator import run
from cocotb.triggers import ClockCycles
from cocotb.clock import Clock
from cocotbext.ahb import AHBBus, AHBLiteMaster, AHBLiteSlaveRAM, AHBResp, AHBMonitor
from cocotbext.ahb import (
AHBBus,
AHBLiteMaster,
AHBLiteSlaveRAM,
AHBResp,
AHBMonitor,
AHBSize,
)
from cocotb.regression import TestFactory


def get_rnd_addr(mem_size_kib: int = 0):
return random.randint(0, ((mem_size_kib - 1) * 1024) - 1) & 0xFFFF_FFF8


def rnd_val(bit: int = 0, zero: bool = True):
if zero is True:
return random.randint(0, (2**bit) - 1)
Expand Down Expand Up @@ -58,13 +69,7 @@ async def run_test(dut, bp_fn=None, pip_mode=False):
ahb_bus_slave = AHBBus.from_entity(dut)

data_width = ahb_bus_slave.data_width

await setup_dut(dut, cfg.RST_CYCLES)

ahb_lite_mon = AHBMonitor(ahb_bus_slave, dut.hclk, dut.hresetn)

# Below is only required bc of flake8 - non-used rule
type(ahb_lite_mon)
n_bytes = data_width // 8

ahb_lite_sram = AHBLiteSlaveRAM(
AHBBus.from_entity(dut),
Expand All @@ -82,49 +87,26 @@ async def run_test(dut, bp_fn=None, pip_mode=False):
AHBBus.from_entity(dut), dut.hclk, dut.hresetn, def_val="Z"
)

# Generate a list of unique addresses with the double of memory size
# to create error responses
address = random.sample(range(0, 2 * mem_size_kib * 1024, 8), N)
# Generate a list of random 32-bit values
value = [rnd_val(data_width) for _ in range(N)]
# Generate a list of random sizes
if data_width == 32:
size = [pick_random_value([1, 2, 4]) for _ in range(N)]
else:
size = [pick_random_value([1, 2, 4, 8]) for _ in range(N)]

# Create the comparison list with expected results
expected = []
for addr, val, sz in zip(address, value, size):
resp, data = 0, 0
if addr >= mem_size_kib * 1024:
resp = AHBResp.ERROR
else:
resp = AHBResp.OKAY
if sz == 1:
data = val & 0xFF
elif sz == 2:
data = val & 0xFFFF
elif sz == 4:
data = val & 0xFFFFFFFF
elif sz == 8:
data = val & 0xFFFFFFFFFFFFFFFF
expected.append({"resp": resp, "data": hex(data)})

# Perform the writes and reads
resp = await ahb_lite_master.write(address, value, size, pip=pip_mode)
resp = await ahb_lite_master.read(address, size, pip=pip_mode)
print(resp)
# Compare all txns
for index, (real, expect) in enumerate(zip(resp, expected)):
if real != expect:
print("------ERROR------")
print(f"Txn ID: {index}")
print("DUT")
print(real)
print("Expected")
print(expect)
assert real == expect, "DUT != Expected"
await setup_dut(dut, cfg.RST_CYCLES)

seq_ops = [1, 2, 4] if data_width == 32 else [1, 2, 4, 8]
mask = [0xFF, 0xFFFF, 0xFFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF]

for index, byte_mode in enumerate(seq_ops):
address_dw_aligned = get_rnd_addr(mem_size_kib)
addr = [address_dw_aligned + i for i in range(0, n_bytes, byte_mode)]
data = [rnd_val(data_width, False) for i in range(len(addr))]
size = [byte_mode for i in range(len(addr))]
resp_wr = await ahb_lite_master.write(addr, data, size, pip=pip_mode)
print(resp_wr)
expect = sum(
(data[i // byte_mode] & mask[index]) << (8 * i)
for i in range(0, n_bytes, byte_mode)
)
resp_rd = await ahb_lite_master.read(address_dw_aligned, pip=pip_mode)
assert (
hex(expect) == resp_rd[0]["data"]
), f"Mismatch between WR/RD {hex(expect)} != {resp_rd[0]['data']}"


if cocotb.SIM_NAME:
Expand Down

0 comments on commit a1dbf65

Please sign in to comment.