Driving the Inputs
Applying input stimulus requires structured timing control. If a testbench updates input signals coincident with an active clock edge, a simulation race condition occurs. Establishing systematic timing relationships between stimulus and the system clock ensures repeatable verification results.
Driving Combinational Circuits Exhaustively
For circuits without sequential clock inputs, drive inputs, allow combinational logic to settle, and observe outputs. A for loop provides exhaustive stimulus coverage:
`timescale 1ns / 1ps
module tb_decoder_3to8();
reg [2:0] sel_tb;
wire [7:0] out_tb;
decoder_3to8 dut (.sel(sel_tb), .out(out_tb));
integer i;
initial begin
$dumpfile("tb_decoder_3to8.vcd");
$dumpvars(0, tb_decoder_3to8);
for (i = 0; i < 8; i = i + 1) begin
sel_tb = i[2:0];
#10; // Allow combinational propagation
$display("sel=%03b out=%08b", sel_tb, out_tb);
end
$finish;
end
endmodule
Unlike synthesizable for loops that unroll into physical hardware, testbench procedural loops execute sequentially in simulation software.
Driving Clocked Circuits: The Post-Edge Rule
Sequential flip-flops sample input data on active clock edges. If an input changes at the identical simulation delta time, setup and hold violations occur.
Follow this standard rule: Apply stimulus slightly after the active clock edge:
@(posedge clk); // Synchronize to the active clock edge
#2; // Offset simulation time into the clock cycle
data_in = new_val; // Drive stable input data
This delay ensures that input data remains stable for the entire setup window before the subsequent clock edge (Figure 1).
The following example demonstrates this timing discipline:
`timescale 1ns / 1ps
module tb_counter();
reg clk_tb;
reg rst_tb;
reg en_tb;
wire [3:0] count_tb;
localparam CLK_PERIOD = 10;
localparam CLK_TO_STIM = 2; // Drive stimulus 2 ns after the active clock edge
counter_4bit dut (
.clk (clk_tb),
.rst (rst_tb),
.en (en_tb),
.count(count_tb)
);
initial clk_tb = 0;
always #(CLK_PERIOD/2) clk_tb = ~clk_tb;
initial begin
$dumpfile("tb_counter.vcd");
$dumpvars(0, tb_counter);
rst_tb = 1; en_tb = 0;
@(posedge clk_tb); #CLK_TO_STIM;
@(posedge clk_tb); #CLK_TO_STIM;
rst_tb = 0; // Deassert reset after two cycles
en_tb = 1;
repeat (12) @(posedge clk_tb); // Enable counting for 12 cycles
#CLK_TO_STIM;
en_tb = 0; // Hold counter state
repeat (3) @(posedge clk_tb);
$finish;
end
endmodule
Parameter CLK_TO_STIM models the clock-to-output propagation delay ($t_{\text{cq}}$) of upstream flip-flops.
The Coincident-Edge Race Condition
Do not apply stimulus changes at the exact moment of clock transitions:
// Incorrect implementation: creates simulator race conditions
always #5 clk = ~clk; // Clock transitions at 5, 15, 25 ns
initial begin
data_in = 8'hAB;
#5;
data_in = 8'hCD; // Coincides exactly with clock edge at 5 ns
end
When stimulus updates coincide with clock edges, event evaluation order depends on internal simulator scheduling. A testbench may pass in one simulator and fail in another. Always use @(posedge clk); #CLK_TO_STIM; to prevent nondeterministic simulation behavior.
Encapsulating Stimulus in Tasks
When applying repeated protocol sequences, encapsulate the operations in reusable Verilog task constructs. Tasks can contain simulation timing delays and event controls:
`timescale 1ns / 1ps
module tb_uart_tx();
reg clk_tb;
reg rst_tb;
reg [7:0] tx_data_tb;
reg tx_valid_tb;
wire tx_ready_tb;
wire tx_serial_tb;
localparam CLK_PERIOD = 10;
localparam CLK_TO_STIM = 2;
uart_tx dut (
.clk (clk_tb),
.rst (rst_tb),
.tx_data (tx_data_tb),
.tx_valid (tx_valid_tb),
.tx_ready (tx_ready_tb),
.tx_serial(tx_serial_tb)
);
initial clk_tb = 0;
always #(CLK_PERIOD/2) clk_tb = ~clk_tb;
// Task: Synchronize to ready, present byte, and pulse valid for one cycle
task send_byte;
input [7:0] byte_val;
begin
@(posedge clk_tb);
while (!tx_ready_tb) @(posedge clk_tb);
#CLK_TO_STIM;
tx_data_tb = byte_val;
tx_valid_tb = 1'b1;
@(posedge clk_tb); #CLK_TO_STIM;
tx_valid_tb = 1'b0;
$display("[%0t] sent 0x%02h", $time, byte_val);
end
endtask
initial begin
$dumpfile("tb_uart_tx.vcd");
$dumpvars(0, tb_uart_tx);
rst_tb = 1;
tx_valid_tb = 0;
tx_data_tb = 8'h00;
repeat (4) @(posedge clk_tb);
#CLK_TO_STIM;
rst_tb = 0;
// Execute procedural bus transactions
send_byte(8'h41);
send_byte(8'h42);
send_byte(8'h43);
repeat (20) @(posedge clk_tb);
$display("done");
$finish;
end
endmodule
Using tasks encapsulates interface handshake protocols, keeps top-level stimulus clean, and simplifies maintenance.
The next article examines output observation and explains how to sample signals at the correct simulation time.