|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import logging |
| 4 | +import random |
| 5 | + |
| 6 | +from bus2csr import ( |
| 7 | + FrontBusTestInterface, |
| 8 | + compare_values, |
| 9 | + dword2int, |
| 10 | + get_frontend_bus_if, |
| 11 | + int2bytes, |
| 12 | + int2dword, |
| 13 | +) |
| 14 | +from utils import mask_bits, rand_bits, rand_bits32 |
| 15 | + |
| 16 | +import cocotb |
| 17 | +from cocotb.handle import SimHandleBase |
| 18 | +from cocotb.triggers import ClockCycles, Combine, Event, RisingEdge, Timer, with_timeout |
| 19 | +from cocotb_helpers import reset_n |
| 20 | + |
| 21 | + |
| 22 | +async def timeout_task(timeout): |
| 23 | + await Timer(timeout, "us") |
| 24 | + raise RuntimeError("Test timeout!") |
| 25 | + |
| 26 | + |
| 27 | +async def initialize(dut, timeout=50): |
| 28 | + """ |
| 29 | + Common test initialization routine which sets up environment and starts a timeout coroutine |
| 30 | + to observe whether the test did not fall in infinite loop. |
| 31 | + """ |
| 32 | + |
| 33 | + cocotb.log.setLevel(logging.DEBUG) |
| 34 | + |
| 35 | + # Start the background timeout task |
| 36 | + await cocotb.start(timeout_task(timeout)) |
| 37 | + |
| 38 | + # Initialize inputs |
| 39 | + dut.araddr.value = 0 |
| 40 | + dut.arburst.value = 0 |
| 41 | + dut.arsize.value = 0 |
| 42 | + dut.arlen.value = 0 |
| 43 | + dut.aruser.value = 0 |
| 44 | + dut.arid.value = 0 |
| 45 | + dut.arlock.value = 0 |
| 46 | + dut.arvalid.value = 0 |
| 47 | + dut.rready.value = 0 |
| 48 | + dut.awaddr.value = 0 |
| 49 | + dut.awburst.value = 0 |
| 50 | + dut.awsize.value = 0 |
| 51 | + dut.awlen.value = 0 |
| 52 | + dut.awuser.value = 0 |
| 53 | + dut.awid.value = 0 |
| 54 | + dut.awlock.value = 0 |
| 55 | + dut.awvalid.value = 0 |
| 56 | + dut.wdata.value = 0 |
| 57 | + dut.wstrb.value = 0 |
| 58 | + dut.wlast.value = 0 |
| 59 | + dut.wvalid.value = 0 |
| 60 | + dut.bready.value = 0 |
| 61 | + |
| 62 | + # Configure testbench |
| 63 | + tb = get_frontend_bus_if()(dut) |
| 64 | + tb.log = dut._log |
| 65 | + await tb.register_test_interfaces() |
| 66 | + await ClockCycles(tb.clk, 20) |
| 67 | + await reset_n(tb.clk, tb.rst_n, cycles=5) |
| 68 | + |
| 69 | + data_len = random.randint(10, 100) |
| 70 | + test_data = [random.randint(0, 2**32 - 1) for _ in range(data_len)] |
| 71 | + |
| 72 | + return tb, data_len, test_data |
| 73 | + |
| 74 | + |
| 75 | +@cocotb.test() |
| 76 | +async def test_collision_with_write(dut): |
| 77 | + tb, data_len, test_data = await initialize(dut) |
| 78 | + |
| 79 | + fifo_addr = tb.reg_map.I3C_EC.SECFWRECOVERYIF.INDIRECT_FIFO_DATA.base_addr |
| 80 | + |
| 81 | + tb.log.info(f"Generated {data_len} dwords to transfer.") |
| 82 | + |
| 83 | + async def writer(): |
| 84 | + # Write sequence should just write data |
| 85 | + for d in test_data: |
| 86 | + await tb.write_csr(fifo_addr, int2bytes(d)) |
| 87 | + |
| 88 | + async def reader(return_data): |
| 89 | + # Read sequence should read data on each write data |
| 90 | + for _ in test_data: |
| 91 | + # Awaiting `awvalid` causes reading simultaneously with write data channel activity |
| 92 | + await RisingEdge(dut.awvalid) |
| 93 | + return_data.append(dword2int(await tb.read_csr(fifo_addr))) |
| 94 | + |
| 95 | + received_data = [] |
| 96 | + w = cocotb.start_soon(writer()) |
| 97 | + r = cocotb.start_soon(reader(received_data)) |
| 98 | + |
| 99 | + await Combine(w, r) |
| 100 | + |
| 101 | + assert received_data == test_data, "Recieved data does not match sent data!" |
| 102 | + |
| 103 | + tb.log.info("Test finished!") |
| 104 | + |
| 105 | + |
| 106 | +@cocotb.test(skip=True) |
| 107 | +async def test_collision_with_read(dut): |
| 108 | + tb, data_len, test_data = await initialize(dut) |
| 109 | + |
| 110 | + fifo_addr = tb.reg_map.I3C_EC.SECFWRECOVERYIF.INDIRECT_FIFO_DATA.base_addr |
| 111 | + |
| 112 | + tb.log.info(f"Generated {data_len} dwords to transfer.") |
| 113 | + |
| 114 | + async def writer(): |
| 115 | + # Write sequence should write data on each read data |
| 116 | + for i, d in enumerate(test_data): |
| 117 | + # Awaiting `arvalid` causes writing simultaneously with read data channel activity |
| 118 | + if i >= 2: |
| 119 | + await RisingEdge(dut.arvalid) |
| 120 | + await tb.write_csr(fifo_addr, int2bytes(d)) |
| 121 | + |
| 122 | + async def reader(return_data): |
| 123 | + # Wait until there is data in FIFO |
| 124 | + while dut.fifo_depth_o.value < 2: |
| 125 | + continue |
| 126 | + |
| 127 | + # Read sequence should just read data |
| 128 | + for i in range(data_len): |
| 129 | + wvalid = RisingEdge(dut.wvalid) |
| 130 | + wready = RisingEdge(dut.wready) |
| 131 | + await Combine(wvalid, wready) |
| 132 | + return_data.append(dword2int(await tb.read_csr(fifo_addr))) |
| 133 | + |
| 134 | + received_data = [] |
| 135 | + w = cocotb.start_soon(writer()) |
| 136 | + r = cocotb.start_soon(reader(received_data)) |
| 137 | + |
| 138 | + await Combine(w, r) |
| 139 | + |
| 140 | + assert received_data == test_data, "Recieved data does not match sent data!" |
| 141 | + |
| 142 | + tb.log.info("Test finished!") |
| 143 | + |
| 144 | + |
| 145 | +@cocotb.test(skip=True) |
| 146 | +async def test_write_burst(dut): |
| 147 | + tb = await initialize(dut) |
| 148 | + |
| 149 | + |
| 150 | +@cocotb.test(skip=True) |
| 151 | +async def test_read_burst(dut): |
| 152 | + tb = await initialize(dut) |
| 153 | + |
| 154 | + |
| 155 | +@cocotb.test(skip=True) |
| 156 | +async def test_read_burst_collision_with_write(dut): |
| 157 | + tb = await initialize(dut) |
| 158 | + |
| 159 | + |
| 160 | +@cocotb.test(skip=True) |
| 161 | +async def test_write_burst_collision_with_read(dut): |
| 162 | + tb = await initialize(dut) |
0 commit comments