Home › Practice › Design Verification › cocotb 1: Test a 2-bit ALU

cocotb 1: Test a 2-bit ALU

EasycocotbPythonDVEarly

Concept first. In cocotb, every test is a Python async def decorated with @cocotb.test(). You drive inputs with dut.<port>.value = ..., advance simulation time with await Timer(1, units="ns"), and check outputs with plain assert.

Write a Python testbench for this 2-bit combinational ALU (op: 0 = ADD, 1 = AND, 2 = OR, 3 = XOR; zero = (y == 0); eq = (a == b)).

module alu2 (
  input  [1:0] a, b, op,
  output [1:0] y,
  output       zero, eq
);

Your tests must pass on the correct design and catch hidden buggy versions (for example, a wrong op or a stuck flag).

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

← cocotb first steps 4: Random tests for an 8-bit addercocotb 2: Verify a PWM controller →