The Four Parts of a Testbench
Every effective testbench, from a simple gate-level check to a system-on-chip verification suite, divides functionality into four distinct architectural blocks. Structuring testbenches into modular blocks prevents monolithic procedural code that is difficult to maintain and expand.
The Four Core Architectural Blocks
A robust testbench architecture comprises four primary elements (Figure 1):
- Device Under Test (DUT) instance: Connects internal testbench nets to target module ports.
- Stimulus generator: Drives input signals over simulation time.
- Response checker: Samples DUT outputs and compares measured values against reference expectations.
- Summary report generator: Tracks assertion tallies and reports total test passes and failures.
Separating these functional responsibilities ensures modularity. You can modify input stimulus without altering verification checking logic. Similarly, you can refine checking thresholds without modifying stimulus sequences.
Top-Level Module Port Declarations
The top-level testbench module contains an empty port list:
module tb_adder_4bit();
endmodule
The testbench operates at the apex of the simulation hierarchy. No higher-level module connects to it. Declaring external ports on a top-level testbench serves no architectural purpose.
Data Type Selection: reg Versus wire
When connecting signals between the testbench and the DUT (Figure 2):
- Inputs to the DUT are driven procedurally by testbench
initialoralwaysblocks. Declare these signals asreg. - Outputs from the DUT are driven continuously by the instantiated design. Declare these signals as
wire.
Bidirectional (inout) ports require continuous driving through tri-state drivers declared as wire.
Testbench Skeleton Implementation
The following example demonstrates a modular testbench for a 4-bit binary adder:
// adder_4bit.v: Synthesizable Device Under Test
module adder_4bit (
input wire [3:0] a,
input wire [3:0] b,
input wire cin,
output wire [3:0] sum,
output wire cout
);
assign {cout, sum} = a + b + cin;
endmodule
The testbench structure implements the four functional sections:
`timescale 1ns / 1ps
module tb_adder_4bit();
// Signal declarations: reg for inputs, wire for outputs
reg [3:0] a_tb;
reg [3:0] b_tb;
reg cin_tb;
wire [3:0] sum_tb;
wire cout_tb;
// 1. DUT Instantiation using explicit port mapping
adder_4bit dut (
.a (a_tb),
.b (b_tb),
.cin (cin_tb),
.sum (sum_tb),
.cout(cout_tb)
);
// 2. Stimulus Generation
initial begin
$dumpfile("tb_adder_4bit.vcd");
$dumpvars(0, tb_adder_4bit);
a_tb = 4'h0; b_tb = 4'h0; cin_tb = 1'b0; #10;
a_tb = 4'h5; b_tb = 4'h3; cin_tb = 1'b0; #10; // 5 + 3 = 8
a_tb = 4'hF; b_tb = 4'h1; cin_tb = 1'b0; #10; // 15 + 1 = 16 (overflow)
a_tb = 4'h7; b_tb = 4'h8; cin_tb = 1'b1; #10; // 7 + 8 + 1 = 16
// 4. Summary Report
$display("--- Simulation Completed ---");
$finish;
end
// 3. Output Observation Block
always @(a_tb or b_tb or cin_tb) begin
#1; // Allow combinational settling
$display(" %0d + %0d + %0d = %0d (cout=%b)", a_tb, b_tb, cin_tb, sum_tb, cout_tb);
end
endmodule
Compile and run the simulation using Icarus Verilog:
iverilog -o sim_adder tb_adder_4bit.v adder_4bit.v
vvp sim_adder
Always use named port connections (such as .a(a_tb)). Positional connections can silently miswire if module port declarations change order.
The next article examines stimulus driving techniques for combinational and synchronous sequential circuits.