Home › Practice › Design Verification › cocotb first steps 4: Random tests for an 8-bit adder
cocotb first steps 4: Random tests for an 8-bit adder
EasycocotbPythonDV
Some bugs hide in a small corner of the input space. Random inputs and a Python model find them without you guessing where they are.
cocotb cheat sheet
import random
a = random.randint(0, 255) # a random whole number from 0 to 255
expected = (a + b) & 0xFF # Python model (keep low 8 bits)
expected_carry = (a + b) >> 8 # the carry bit
for _ in range(200): # repeat 200 times
Worked example. A 4-bit adder, 50 random pairs, checking the sum:
import cocotb, random
from cocotb.triggers import Timer
@cocotb.test()
async def test_add4(dut):
for _ in range(50):
a = random.randint(0, 15)
b = random.randint(0, 15)
dut.a.value = a
dut.b.value = b
await Timer(1, units="ns")
assert int(dut.sum.value) == (a + b) & 0xF
Your task. Test module adder8c (input [7:0] a, b, output [7:0] sum, output cout). Use 200 random pairs and check both sum and cout against your model. Catch two hidden buggy adders.
No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.