Skip to content

CoCoTb

Examples

TestFactory

Source

cocotb.regression.TestFactory provides a way to parameterise tests:

async def run_testfactory(dut, a_in, b_in):
    # Initialize and start clock
    clock = Clock(dut.clk, 10, 'ns')
    cocotb.start_soon(clock.start(start_high=False))

    model = test_model(dut)
    cocotb.start_soon(model.start())
    cocotb.start_soon(output_checker(dut, model))

    dut.a_in = a_in
    dut.b_in = b_in

    await ClockCycles(dut.clk, 10, rising=True)


# Read generics
WORDS = int(cocotb.top.words_g.value)
BIT_WIDTH = int(cocotb.top.bit_width_g.value)

MIN_NUM = -1 * (2 ** (WORDS * BIT_WIDTH - 1))
MAX_NUM = (2 ** (WORDS * BIT_WIDTH - 1)) - 1

# Create tests
tf = TestFactory(run_testfactory)
tf.add_option('a_in', [0, min_num, max_num])
tf.add_option('b_in', [0, min_num, max_num])
tf.generate_tests()

Warning

This is changing in v2.0.0.

Troubleshooting

Resolving std_logics that aren't '0' or '1'

Source

You can set COCOTB_RESOLVE_X.

It defines how to resolve bits with a value of 'X', 'Z', 'U' or 'W' when being converted to an integer. Valid settings are:

  • VALUE_ERROR
    • raise a ValueError exception
  • ZEROS
    • resolve to '0'
  • ONES
    • resolve to '1'
  • RANDOM
    • randomly resolve to a '0' or a '1'

Set to VALUE_ERROR by default. ```