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.
Step 1: Write the truth tables
Before writing code, write down the behavior you expect. This table is the specification for the project.
| A | B | AND expected | OR expected |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
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
module and_or_gatesbegins a reusable hardware module.aandbare one-bit inputs.and_yandor_yare the outputs to observe.assigndescribes combinational behavior, so outputs update when inputs change.&&and||are logical operators that produce a one-bit truth result.
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.
| Signal | Direction in testbench | Purpose |
|---|---|---|
a | driven | first input combination bit |
b | driven | second input combination bit |
and_y | observed | AND result from the DUT |
or_y | observed | OR 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
automaticgives each task call its own local storage.test_aandtest_bare the values for this test case.- The testbench drives the inputs before calculating expected values.
#1allows combinational assignments to update before sampling outputs.- The expected values are calculated independently from the observed DUT outputs.
!==detects mismatches involving unknown values such asXorZ.$errorreports a failure but lets the remaining combinations run.$displaycreates 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.
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
| Symptom | Likely mistake | Fix |
|---|---|---|
| Outputs show old values | No delay before sampling | Use a small timing step such as #1. |
| Only one input combination is tested | Test plan was not derived from the truth table | Call the checking task for all four rows. |
| Unknown output passes unexpectedly | Used == instead of case comparison | Use !== for self-checking. |
| AND or OR output is disconnected | Incorrect named-port connection | Compare every instance port with the DUT declaration. |
| Simulation never ends | Missing $finish | Finish after all directed cases complete. |
| Waveform is empty | Dump commands missing or wrong scope | Call $dumpfile and $dumpvars before the test. |
Project challenge
Improve the testbench without changing the DUT.
- Replace the four explicit task calls with a loop over a two-bit test vector.
- Add an integer counter for total checks and failed checks.
- Print a final summary such as
4 checks, 0 failures. - Temporarily introduce a bug into the DUT and confirm that the testbench catches it.
- Restore the DUT and save the passing output and waveform as project evidence.
$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 component | Responsibility in this project |
|---|---|
| Sequence item | Stores inputs a and b, plus expected outputs. |
| Sequence | Generates the four truth-table combinations. |
| Driver | Copies transaction inputs onto the virtual interface. |
| Monitor | Samples inputs and DUT outputs and publishes an observed transaction. |
| Scoreboard | Calculates expected outputs independently and compares them with observations. |
| Agent | Packages the sequencer, driver, and monitor. |
| Environment and test | Construct the hierarchy, start the sequence, and control objections. |
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?
start_itemwaits for the sequencer and driver handshake.- The loop generates
00,01,10, and11. finish_itemmakes the completed item available to the driver.- 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.
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
- Keep the gate DUT unchanged.
- Create the interface and connect it to the DUT.
- Implement the item, sequence, sequencer, driver, monitor, and scoreboard.
- Pass the virtual interface through
uvm_config_db. - Connect monitor analysis output to the scoreboard.
- Run all four combinations through the sequence.
- Break one gate assignment intentionally and confirm the scoreboard reports the mismatch.
- Restore the DUT and save the clean log and waveform.
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.