Module 6: Verilog for Testbench & Simulation · 6.1 Testbench Architecture

Sampling the Outputs

In event-driven simulation, the timing of output observation is critical. If a testbench samples an output port during the identical delta cycle as a clock transition, the verification check can read stale data. Sampling at invalid times generates false error reports for functionally correct designs.

The Observation Race Condition

Section 5.1 examined the Verilog simulation event queue. When a clock edge occurs, nonblocking assignments (<=) schedule state updates for the Nonblocking Assignment (NBA) region. The simulator evaluates all active procedural blocks before applying scheduled updates.

Consequently, if a testbench samples an output using always @(posedge clk), it evaluates before the DUT updates register values:

// Defective observation: prints previous cycle data
always @(posedge clk_tb) begin
    $display("count = %0d", count_tb);
end

The testbench evaluation block and the DUT register update reside in the same active simulation step. The $display task executes before the nonblocking assignment updates signal count_tb.

Safe Output Observation Techniques

Apply two standard methods to observe synchronous outputs safely:

1. Mid-Period Sampling

Delay observation until the middle of the clock period. Waiting half a clock cycle ensures that all internal flip-flop transitions and combinational propagation paths have settled:

localparam CLK_PERIOD = 10;

always @(posedge clk_tb) begin
    #(CLK_PERIOD/2);
    $display("[%0t] count = %0d", $time, count_tb);
end

2. Post-Update Sampling with $strobe

Use the $strobe system task. The simulator defers $strobe execution until the end of the current simulation time step, after all nonblocking assignments and delta events resolve:

always @(posedge clk_tb) begin
    $strobe("[%0t] count = %0d", $time, count_tb);
end

Trace Comparison: $display Versus $strobe

The following testbench illustrates the functional difference between $display and $strobe:

`timescale 1ns / 1ps

module tb_display_vs_strobe();
    reg       clk;
    reg [3:0] q;

    always @(posedge clk)
        q <= q + 1;

    initial begin
        clk = 0; q = 0;
        #1;

        repeat (4) begin
            @(posedge clk);
            $display("$display at the edge: q = %0d", q);
            $strobe ("$strobe at the edge:  q = %0d", q);
        end

        $finish;
    end

    always #5 clk = ~clk;
endmodule

The simulator prints the following output:

$display at the edge: q = 0
$strobe at the edge:  q = 1
$display at the edge: q = 1
$strobe at the edge:  q = 2
$display at the edge: q = 2
$strobe at the edge:  q = 3
$display at the edge: q = 3
$strobe at the edge:  q = 4

Both tasks execute at the identical simulation timestamp. However, $display executes in the Active region before the nonblocking update occurs. In contrast, $strobe executes in the Postponed region after the update completes. Use $strobe to log registered states at clock transitions. Use $display for general procedural status messages.

Clock Period Timing Zones

Figure 1 illustrates the timing zones across a synchronous clock cycle.

The zones of a clock period
Figure 1: Where to drive and where to sample

Cycle Timing Zone Offset From Active Edge Recommended Verification Action
Post-edge offset Edge + 1 ns to 2 ns Drive DUT inputs (stimulus generation)
Settling interval Edge + 2 ns to 4 ns Combinational datapath propagation
Mid-cycle window Edge + 5 ns (half-period) Sample DUT outputs (response checking)
Pre-edge setup Final 2 ns of cycle Maintain signal stability

Sampled Adder Testbench Example

Combinational circuits do not contain nonblocking internal updates. However, waiting for propagation delay ensures reliable sampling:

`timescale 1ns / 1ps

module tb_adder_4bit_sampled();
    reg  [3:0] a_tb, b_tb;
    reg        cin_tb;
    wire [3:0] sum_tb;
    wire       cout_tb;

    adder_4bit dut (
        .a   (a_tb),
        .b   (b_tb),
        .cin (cin_tb),
        .sum (sum_tb),
        .cout(cout_tb)
    );

    integer i, j;

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

        cin_tb = 0;

        for (i = 0; i < 4; i = i + 1) begin
            for (j = 0; j < 4; j = j + 1) begin
                a_tb = i[3:0];
                b_tb = j[3:0];
                #5; // Wait for combinational logic propagation
                $strobe("  %0d + %0d = %0d (cout=%b)", a_tb, b_tb, sum_tb, cout_tb);
                #5;
            end
        end

        $finish;
    end
endmodule

Sampling at mid-cycle or using $strobe eliminates observation race conditions. The next article combines stimulus driving and output sampling into an automated self-checking verification loop.

← Driving the Inputs Automatic Pass and Fail →