Home › Practice › Design Verification › cocotb first steps 1: Test an AND gate

cocotb first steps 1: Test an AND gate

EasycocotbPythonDV

cocotb lets you test a circuit with a few lines of Python. A test is an async def with @cocotb.test() above it.

cocotb cheat sheet
dut.a.value = 1                  # set an input
await Timer(1, units="ns")       # let the circuit settle
int(dut.y.value)                 # read an output as a number
assert int(dut.y.value) == 1     # fail the test if it is wrong

Worked example. This tests an OR gate for one input combination:

import cocotb
from cocotb.triggers import Timer

@cocotb.test()
async def test_or(dut):
    dut.a.value = 1
    dut.b.value = 0
    await Timer(1, units="ns")
    assert int(dut.y.value) == 1

Your task. Test this AND gate (module and2 (input a, b, output y)) for all 4 input combinations. Your test must pass on the correct gate and catch two hidden buggy gates.

Solve in your browser →

No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.

More in this track

← One-hot state machine: exactly one bit setcocotb first steps 2: Test a D flip-flop with reset →