Actually randomize testing
This commit is contained in:
@@ -22,8 +22,16 @@ class TB:
|
|||||||
self.log = logging.getLogger("cocotb.tb")
|
self.log = logging.getLogger("cocotb.tb")
|
||||||
self.log.setLevel(logging.INFO)
|
self.log.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
self.input_queue = Queue()
|
||||||
|
|
||||||
|
self.expected_queue = Queue()
|
||||||
|
self.output_queue = Queue()
|
||||||
|
|
||||||
cocotb.start_soon(Clock(self.dut.i_clk, CLK_PERIOD, units="ns").start())
|
cocotb.start_soon(Clock(self.dut.i_clk, CLK_PERIOD, units="ns").start())
|
||||||
|
|
||||||
|
cocotb.start_soon(self.run_input())
|
||||||
|
cocotb.start_soon(self.run_output())
|
||||||
|
|
||||||
async def cycle_reset(self):
|
async def cycle_reset(self):
|
||||||
await self._cycle_reset(self.dut.i_rst, self.dut.i_clk)
|
await self._cycle_reset(self.dut.i_rst, self.dut.i_clk)
|
||||||
|
|
||||||
@@ -38,25 +46,46 @@ class TB:
|
|||||||
await RisingEdge(clk)
|
await RisingEdge(clk)
|
||||||
await RisingEdge(clk)
|
await RisingEdge(clk)
|
||||||
|
|
||||||
|
async def write_input(self, value: int, shift_amount: int):
|
||||||
|
await self.input_queue.put((value, shift_amount))
|
||||||
|
await self.expected_queue.put((value << (shift_amount*26)) % PRIME)
|
||||||
|
|
||||||
|
async def run_input(self):
|
||||||
|
while True:
|
||||||
|
value, shift_amount = await self.input_queue.get()
|
||||||
|
self.dut.i_valid.value = 1
|
||||||
|
self.dut.i_val.value = value
|
||||||
|
self.dut.i_shift_amount.value = shift_amount
|
||||||
|
await RisingEdge(self.dut.i_clk)
|
||||||
|
self.dut.i_valid.value = 0
|
||||||
|
self.dut.i_shift_amount.value = 0
|
||||||
|
self.dut.i_val.value = 0
|
||||||
|
|
||||||
|
async def run_output(self):
|
||||||
|
while True:
|
||||||
|
await RisingEdge(self.dut.i_clk)
|
||||||
|
if self.dut.o_valid.value:
|
||||||
|
await self.output_queue.put(self.dut.o_result.value.integer)
|
||||||
|
|
||||||
@cocotb.test
|
@cocotb.test
|
||||||
async def test_sanity(dut):
|
async def test_sanity(dut):
|
||||||
tb = TB(dut)
|
tb = TB(dut)
|
||||||
|
|
||||||
await tb.cycle_reset()
|
await tb.cycle_reset()
|
||||||
|
|
||||||
value_a = random.randint(1,2**(130+16))
|
count = 1024
|
||||||
|
|
||||||
# value_a = PRIME + 1000000
|
for _ in range(count):
|
||||||
tb.dut.i_valid.value = 1
|
await tb.write_input(random.randint(1,2**(130+16)), random.randint(0, 4))
|
||||||
tb.dut.i_val.value = value_a
|
|
||||||
await RisingEdge(tb.dut.i_clk)
|
|
||||||
tb.dut.i_valid.value = 0
|
|
||||||
tb.dut.i_val.value = 0
|
|
||||||
|
|
||||||
await RisingEdge(tb.dut.o_valid)
|
fail = False
|
||||||
value = tb.dut.o_result.value.integer
|
|
||||||
|
|
||||||
print(value_a % PRIME)
|
for _ in range(count):
|
||||||
print(value)
|
sim_val = await tb.expected_queue.get()
|
||||||
|
dut_val = await tb.output_queue.get()
|
||||||
|
|
||||||
await Timer(1, "us")
|
if sim_val != dut_val:
|
||||||
|
tb.log.info(f"{sim_val:x} -> {dut_val:x}")
|
||||||
|
fail = True
|
||||||
|
|
||||||
|
assert not fail
|
||||||
Reference in New Issue
Block a user