Foundation Project 1 | Beginner | 1-2 hours

AND/OR Gate Testbench

Build a self-checking Verilog testbench that tests every input combination, reports failures automatically, and gives you waveform evidence.

What you will build

You will verify two combinational gates:

  • An AND gate, whose output is 1 only when both inputs are 1.
  • An OR gate, whose output is 1 when at least one input is 1.

The testbench will apply all four possible two-bit input combinations. For each combination it will calculate the expected result, compare it with the DUT output, print the result, and call $error if the DUT is wrong.

Important idea: a testbench is self-checking when it decides whether the DUT is correct by itself. You should not need to inspect every line of output manually to discover a failure.

Step 1: Write the truth tables

Before writing code, write down the behavior you expect. This table is the specification for the project.

ABAND expectedOR expected
0000
0101
1001
1111

There are $2^2 = 4$ input combinations because there are two one-bit inputs. Testing all combinations is practical here, so there is no reason to use random stimulus yet.

Step 2: Create the design under test

The DUT is the logic you want to verify. Keep it simple so the testbench is the main learning focus.

module and_or_gates (
    input  logic a,
    input  logic b,
    output logic and_y,
    output logic or_y
);
    assign and_y = a && b;
    assign or_y  = a || b;
endmodule

What this code means

  1. module and_or_gates begins a reusable hardware module.
  2. a and b are one-bit inputs.
  3. and_y and or_y are the outputs to observe.
  4. assign describes combinational behavior, so outputs update when inputs change.
  5. && and || are logical operators that produce a one-bit truth result.
Verification boundary: the DUT produces outputs. The testbench supplies inputs and decides whether the outputs match the truth table.

Step 3: Declare the testbench signals

The testbench needs signals to drive the DUT inputs and wires to observe its outputs.

module tb_and_or_gates;
    logic a;
    logic b;
    logic and_y;
    logic or_y;

    and_or_gates dut (
        .a(a),
        .b(b),
        .and_y(and_y),
        .or_y(or_y)
    );
endmodule

The DUT instance connects testbench signals to module ports. The names on the left are testbench signals; the names on the right are DUT ports.

SignalDirection in testbenchPurpose
adrivenfirst input combination bit
bdrivensecond input combination bit
and_yobservedAND result from the DUT
or_yobservedOR result from the DUT

Step 4: Add a directed test task

A task lets us apply one input combination and check both outputs in one place.

task automatic check_combination(
    input logic test_a,
    input logic test_b
);
    logic expected_and;
    logic expected_or;

    a = test_a;
    b = test_b;
    #1;

    expected_and = test_a && test_b;
    expected_or  = test_a || test_b;

    if (and_y !== expected_and)
        $error("AND failed: a=%0b b=%0b expected=%0b actual=%0b",
               test_a, test_b, expected_and, and_y);

    if (or_y !== expected_or)
        $error("OR failed: a=%0b b=%0b expected=%0b actual=%0b",
               test_a, test_b, expected_or, or_y);

    $display("a=%0b b=%0b AND=%0b OR=%0b PASS",
             test_a, test_b, and_y, or_y);
endtask

Line-by-line explanation

  1. automatic gives each task call its own local storage.
  2. test_a and test_b are the values for this test case.
  3. The testbench drives the inputs before calculating expected values.
  4. #1 allows combinational assignments to update before sampling outputs.
  5. The expected values are calculated independently from the observed DUT outputs.
  6. !== detects mismatches involving unknown values such as X or Z.
  7. $error reports a failure but lets the remaining combinations run.
  8. $display creates a readable result for each case.

Step 5: Test every combination

Now call the task four times. This is a directed test plan derived directly from the truth table.

initial begin
    check_combination(1'b0, 1'b0);
    check_combination(1'b0, 1'b1);
    check_combination(1'b1, 1'b0);
    check_combination(1'b1, 1'b1);

    $display("AND/OR gate test complete");
    $finish;
end

Do not remove a case because the logic looks obvious. The test plan should make coverage visible: every row of the truth table has a corresponding test call.

Complete working code

Paste this single file into EDA Playground. Select a SystemVerilog-compatible simulator and run it.

module and_or_gates (
    input  logic a,
    input  logic b,
    output logic and_y,
    output logic or_y
);
    assign and_y = a && b;
    assign or_y  = a || b;
endmodule

module tb_and_or_gates;
    logic a;
    logic b;
    logic and_y;
    logic or_y;

    and_or_gates dut (
        .a(a), .b(b), .and_y(and_y), .or_y(or_y)
    );

    task automatic check_combination(
        input logic test_a,
        input logic test_b
    );
        logic expected_and;
        logic expected_or;

        a = test_a;
        b = test_b;
        #1;

        expected_and = test_a && test_b;
        expected_or  = test_a || test_b;

        if (and_y !== expected_and)
            $error("AND failed: a=%0b b=%0b expected=%0b actual=%0b",
                   test_a, test_b, expected_and, and_y);
        if (or_y !== expected_or)
            $error("OR failed: a=%0b b=%0b expected=%0b actual=%0b",
                   test_a, test_b, expected_or, or_y);

        $display("a=%0b b=%0b AND=%0b OR=%0b PASS",
                 test_a, test_b, and_y, or_y);
    endtask

    initial begin
        check_combination(1'b0, 1'b0);
        check_combination(1'b0, 1'b1);
        check_combination(1'b1, 1'b0);
        check_combination(1'b1, 1'b1);
        $display("AND/OR gate test complete");
        $finish;
    end
endmodule

Expected simulator output

a=0 b=0 AND=0 OR=0 PASS
a=0 b=1 AND=0 OR=1 PASS
a=1 b=0 AND=0 OR=1 PASS
a=1 b=1 AND=1 OR=1 PASS
AND/OR gate test complete

You should see four passing cases. If you see an ERROR, do not only change the expected value. First determine whether the error is in the DUT, the port connection, the timing delay, or the test calculation.

Completion signal: four input combinations tested, zero errors, and a final completion message printed.

Step 6: Add waveform evidence

Text output tells you whether a check passed. A waveform lets you inspect when signals changed and whether the testbench sampled them at the correct time.

initial begin
    $dumpfile("and_or_gates.vcd");
    $dumpvars(0, tb_and_or_gates);

    check_combination(1'b0, 1'b0);
    check_combination(1'b0, 1'b1);
    check_combination(1'b1, 1'b0);
    check_combination(1'b1, 1'b1);

    $finish;
end

In EDA Playground, open the waveform viewer after simulation. Inspect each input pair and confirm that the outputs settle after the inputs change. The waveform should support the text checks, not replace them.

Common mistakes and fixes

SymptomLikely mistakeFix
Outputs show old valuesNo delay before samplingUse a small timing step such as #1.
Only one input combination is testedTest plan was not derived from the truth tableCall the checking task for all four rows.
Unknown output passes unexpectedlyUsed == instead of case comparisonUse !== for self-checking.
AND or OR output is disconnectedIncorrect named-port connectionCompare every instance port with the DUT declaration.
Simulation never endsMissing $finishFinish after all directed cases complete.
Waveform is emptyDump commands missing or wrong scopeCall $dumpfile and $dumpvars before the test.

Project challenge

Improve the testbench without changing the DUT.

  1. Replace the four explicit task calls with a loop over a two-bit test vector.
  2. Add an integer counter for total checks and failed checks.
  3. Print a final summary such as 4 checks, 0 failures.
  4. Temporarily introduce a bug into the DUT and confirm that the testbench catches it.
  5. Restore the DUT and save the passing output and waveform as project evidence.
Acceptance criteria: all four truth-table rows execute; the testbench calculates expected values independently; failures use $error; unknown outputs are detected; the waveform is available; and the final summary proves the number of checks and failures.

UVM extension: why use UVM for two gates?

The direct testbench above is the fastest way to learn the logic. In a real project, however, the same verification pattern must scale to larger blocks. UVM separates the transaction, stimulus, signal driving, observation, and checking responsibilities so that the test can change without rewriting the driver or monitor.

and_or_test
    |
    v
and_or_env
    |
    +-- and_or_agent
          |
          +-- sequencer  ->  driver  ->  DUT
          |
          +-- monitor  ->  scoreboard
UVM componentResponsibility in this project
Sequence itemStores inputs a and b, plus expected outputs.
SequenceGenerates the four truth-table combinations.
DriverCopies transaction inputs onto the virtual interface.
MonitorSamples inputs and DUT outputs and publishes an observed transaction.
ScoreboardCalculates expected outputs independently and compares them with observations.
AgentPackages the sequencer, driver, and monitor.
Environment and testConstruct the hierarchy, start the sequence, and control objections.
UVM lesson: the DUT is intentionally tiny. The learning target is the reusable verification architecture, not complicated gate logic.

UVM Step 1: define the transaction

A sequence item represents one meaningful test operation. It should contain transaction-level information, not handles to drivers or virtual interfaces.

class and_or_item extends uvm_sequence_item;
    rand bit a;
    rand bit b;
            bit and_y;
            bit or_y;

    `uvm_object_utils_begin(and_or_item)
        `uvm_field_int(a, UVM_DEFAULT)
        `uvm_field_int(b, UVM_DEFAULT)
                `uvm_field_int(and_y, UVM_DEFAULT)
                `uvm_field_int(or_y, UVM_DEFAULT)
    `uvm_object_utils_end

    function new(string name = "and_or_item");
        super.new(name);
    endfunction
endclass

rand allows the sequence to generate inputs. The factory and field macros allow normal UVM creation, printing, copying, and comparison. For this project, the complete input space is only four combinations, so the sequence will use directed values instead of randomization.

UVM Step 2: create the sequence

The sequence owns scenario intent. It does not assign DUT pins directly. The sequencer sends each item to the driver.

class all_combinations_seq extends uvm_sequence #(and_or_item);
    `uvm_object_utils(all_combinations_seq)

    function new(string name = "all_combinations_seq");
        super.new(name);
    endfunction

    task body();
        for (int value = 0; value < 4; value++) begin
            and_or_item item = and_or_item::type_id::create("item");
            start_item(item);
            item.a = value[1];
            item.b = value[0];
            finish_item(item);
        end
    endtask
endclass

What happens here?

  1. start_item waits for the sequencer and driver handshake.
  2. The loop generates 00, 01, 10, and 11.
  3. finish_item makes the completed item available to the driver.
  4. The sequence is reusable because it knows the scenario, not signal timing.

UVM Step 3: define the virtual interface

The virtual interface is the controlled bridge between class-based UVM code and RTL signals.

interface and_or_if;
    logic a;
    logic b;
    logic and_y;
    logic or_y;
endinterface

The driver writes a and b. The monitor reads all four signals and stores the observed outputs in the transaction. Keeping this access inside the driver and monitor prevents sequences and scoreboards from reaching directly into DUT pins.

UVM Step 4: write the driver

class and_or_driver extends uvm_driver #(and_or_item);
    `uvm_component_utils(and_or_driver)
    virtual and_or_if vif;

    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction

    task run_phase(uvm_phase phase);
        forever begin
            seq_item_port.get_next_item(req);
            vif.a <= req.a;
            vif.b <= req.b;
            #1;
            seq_item_port.item_done();
        end
    endtask
endclass

The driver performs pin-level work. The #1 delay gives combinational logic time to settle before the monitor samples it. In a clocked interface, use the protocol clock instead of a delay.

Common mistake: putting the expected AND and OR calculations in the driver. That couples stimulus and checking. The scoreboard must calculate expected behavior independently.

UVM Step 5: write the monitor

class and_or_monitor extends uvm_monitor;
    `uvm_component_utils(and_or_monitor)
    virtual and_or_if vif;
    uvm_analysis_port #(and_or_item) analysis_port;

    function new(string name, uvm_component parent);
        super.new(name, parent);
        analysis_port = new("analysis_port", this);
    endfunction

    task run_phase(uvm_phase phase);
        forever begin
            and_or_item observed = and_or_item::type_id::create("observed");
            #1;
            observed.a = vif.a;
            observed.b = vif.b;
            observed.and_y = vif.and_y;
            observed.or_y = vif.or_y;
            analysis_port.write(observed);
        end
    endtask
endclass

The monitor is passive: it observes but never drives. In a production interface, replace the simple delay with a clocking block or protocol event so one transaction is published exactly once.

UVM Step 6: build the scoreboard

The scoreboard is the self-checking part of the UVM environment. It calculates expected values from observed inputs and compares them with observed outputs.

class and_or_scoreboard extends uvm_scoreboard;
    `uvm_component_utils(and_or_scoreboard)
    uvm_analysis_imp #(and_or_item, and_or_scoreboard) analysis_export;

    function new(string name, uvm_component parent);
        super.new(name, parent);
        analysis_export = new("analysis_export", this);
    endfunction

    function void write(and_or_item item);
        bit expected_and = item.a && item.b;
        bit expected_or = item.a || item.b;

        if (item.and_y !== expected_and)
            `uvm_error("AND_CHECK", $sformatf(
                "a=%0b b=%0b expected=%0b actual=%0b",
                item.a, item.b, expected_and, item.and_y))
        if (item.or_y !== expected_or)
            `uvm_error("OR_CHECK", $sformatf(
                "a=%0b b=%0b expected=%0b actual=%0b",
                item.a, item.b, expected_or, item.or_y))
    endfunction
endclass

Notice that the scoreboard does not receive the driver's request. It receives the monitor's observation. This preserves the most important verification principle: check what the DUT actually produced.

UVM Step 7: connect the agent and environment

class and_or_agent extends uvm_agent;
    `uvm_component_utils(and_or_agent)
    uvm_sequencer #(and_or_item) sequencer;
    and_or_driver driver;
    and_or_monitor monitor;

    function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        sequencer = new("sequencer", this);
        driver = and_or_driver::type_id::create("driver", this);
        monitor = and_or_monitor::type_id::create("monitor", this);
    endfunction

    function void connect_phase(uvm_phase phase);
        driver.seq_item_port.connect(sequencer.seq_item_export);
    endfunction
endclass

class and_or_env extends uvm_env;
    `uvm_component_utils(and_or_env)
    and_or_agent agent;
    and_or_scoreboard scoreboard;

    function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        agent = and_or_agent::type_id::create("agent", this);
        scoreboard = and_or_scoreboard::type_id::create("scoreboard", this);
    endfunction

    function void connect_phase(uvm_phase phase);
        agent.monitor.analysis_port.connect(scoreboard.analysis_export);
    endfunction
endclass

The agent packages protocol components. The environment packages verification components. This hierarchy means a future test can reuse the agent while changing only the scenario.

UVM Step 8: configure and run the test

class and_or_test extends uvm_test;
    `uvm_component_utils(and_or_test)
    and_or_env env;

    function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        env = and_or_env::type_id::create("env", this);
    endfunction

    task run_phase(uvm_phase phase);
        all_combinations_seq sequence;
        phase.raise_objection(this);
        sequence = all_combinations_seq::type_id::create("sequence");
        sequence.start(env.agent.sequencer);
        #2;
        phase.drop_objection(this);
    endtask
endclass

module top;
    and_or_if vif();
    and_or_gates dut(vif.a, vif.b, vif.and_y, vif.or_y);

    initial begin
        uvm_config_db#(virtual and_or_if)::set(
            null, "uvm_test_top.env.agent.*", "vif", vif);
        run_test("and_or_test");
    end
endmodule

In a complete environment, the driver and monitor retrieve the virtual interface from the configuration database during build_phase. Add those get() calls before running. The test raises an objection while the sequence runs and drops it after the monitor has time to publish the final observation.

UVM expected result

UVM_INFO [UVM/RELNOTES] UVM library initialized
UVM_INFO [UVM_TEST] Starting all_combinations_seq
UVM_INFO [AND_OR_SB] a=0 b=0 and=0 or=0 PASS
UVM_INFO [AND_OR_SB] a=0 b=1 and=0 or=1 PASS
UVM_INFO [AND_OR_SB] a=1 b=0 and=0 or=1 PASS
UVM_INFO [AND_OR_SB] a=1 b=1 and=1 or=1 PASS
UVM_INFO [TEST_DONE] 4 transactions checked, 0 errors

Your exact simulator messages may differ. The important evidence is four observed transactions, zero scoreboard errors, and a clean UVM report summary.

UVM project checklist

  1. Keep the gate DUT unchanged.
  2. Create the interface and connect it to the DUT.
  3. Implement the item, sequence, sequencer, driver, monitor, and scoreboard.
  4. Pass the virtual interface through uvm_config_db.
  5. Connect monitor analysis output to the scoreboard.
  6. Run all four combinations through the sequence.
  7. Break one gate assignment intentionally and confirm the scoreboard reports the mismatch.
  8. Restore the DUT and save the clean log and waveform.
UVM acceptance criteria: the sequence generates four items; the driver drives only interface inputs; the monitor publishes observed outputs; the scoreboard calculates expected values independently; a deliberate DUT bug is detected; and the clean test finishes with zero UVM errors.

What you learned

  • A verification plan can begin with a truth table.
  • A self-checking testbench compares expected behavior with observed DUT behavior.
  • Directed tests are appropriate when the complete input space is small.
  • !== is useful when unknown values should be treated as failures.
  • Console checks and waveforms provide complementary evidence.
  • The same structure grows into a reference model, scoreboard, and eventually a UVM monitor/checker flow.