What Is A Testbench
Part of the Fundamentals section of Coddy's Verilog journey — lesson 73 of 90.
A testbench is a special Verilog module used to test another module. It provides inputs to your design and checks if the outputs are correct.
Why Do We Need a Testbench?
When you build a module, you need to make sure it works correctly. A testbench allows you to:
- Apply different input values to your module
- Observe the outputs
- Check if the outputs match what you expect
- Do this automatically without manual testing
Testbench vs Design Module
| Design Module | Testbench | |
|---|---|---|
| Purpose | Implements hardware | Tests the design module |
| Has ports? | Yes (inputs and outputs) | No (self-contained) |
| Synthesizable? | Yes | No (simulation only) |
Simple Testbench Example
module testbench; // No ports!
// The inputs and outputs come from the module we are testing (the DUT).
reg a, b; // reg for inputs
wire c; // wire for output
// This is module instantiation — it creates a copy of the and_gate module and names it dut
and_gate dut ( // Instantiate DUT
.a(a),
.b(b),
.c(c)
);
// This is an initial block that applies test values to the inputs of the module being tested.
initial begin // Apply test values
a = 0; b = 0; #10;
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
$finish;
end
endmoduleKey Points
- Testbench has no ports
regis used for signals that change (inputs to DUT)wireis used for signals from DUT (outputs)
- The module being tested is called DUT (Design Under Test)
$finishends the simulation
We will cover creating stimulus, displaying results, and other testbench features in the following lessons.
Challenge
You are given an AND gate module. Your task is to add the missing parts to its testbench.
What to do:
Add the following parts to the testbench:
- Declare
regfor inputsaandb - Declare
wirefor outputc - Instantiate
and_gatewith namedutand connect the ports
Cheat sheet
A testbench is a Verilog module used to test another module (the DUT - Design Under Test). It has no ports and is simulation-only.
module testbench; // No ports!
reg a, b; // reg for inputs (signals that change)
wire c; // wire for DUT outputs
and_gate dut ( // Instantiate DUT
.a(a),
.b(b),
.c(c)
);
initial begin // Apply test values
a = 0; b = 0; #10;
a = 1; b = 1; #10;
$finish; // End simulation
end
endmodule- Use
regfor signals driven ininitialblocks (inputs to DUT) - Use
wirefor signals coming from the DUT (outputs) $finishends the simulation
Try it yourself
module and_gate (
input a,
input b,
output c
);
assign c = a & b;
endmodule
module testbench;
// Task 1: Declare reg for inputs a and b
// Task 2: Declare wire for output c
// Task 3: Instantiate and_gate with name dut
// Connect .a(a), .b(b), .c(c)
initial begin
a = 0; b = 0; #10 $display("%d & %d = %d", a, b, c);
a = 0; b = 1; #10 $display("%d & %d = %d", a, b, c);
a = 1; b = 0; #10 $display("%d & %d = %d", a, b, c);
a = 1; b = 1; #10 $display("%d & %d = %d", a, b, c);
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge14Testbench Basics
What Is A TestbenchCreating StimulusDisplay And MonitorDumpfile And DumpvarsUsing System TasksRecap - Full Testbench3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic