Module 6: Verilog for Testbench & Simulation · 6.5 Waveform Analysis

From a Waveform to a Fix

A self-checking testbench reports a verification failure. The waveform viewer displays anomalous signal transitions. Resolving digital design defects requires a disciplined, step-by-step methodology. Following a structured debugging loop prevents engineers from wasting time on symptoms while ignoring the root cause:

The debugging loop
Figure 1: Message, inputs, trace, hypothesis, fix, re-run, regression

Engineers under pressure frequently skip two vital steps: verifying testbench stimulus and running full regression test suites. Failing to verify stimulus leads to debugging correct RTL code. Failing to run regressions introduces new bugs into previously passing features.

Step 1: Read the Diagnostic Error Message

A structured $error message provides essential diagnostic context:

ERROR at 170 ns: count expected 15, got 0

The message identifies the failing signal, the expected value, the received value, and the exact simulation timestamp. Open the waveform viewer and navigate immediately to 170 ns.

Step 2: Verify Input Stimulus

Before modifying RTL code, verify the input signals driving the module under test. At 170 ns, confirm that reset is deasserted. Confirm that the clock operates at the specified frequency. Verify that input transactions match specification requirements. Testbench defects, such as incorrect reset timing or off-by-one loop limits, occur frequently. If input stimulus is incorrect, modify the testbench and rerun simulation before editing the RTL.

Step 3: Trace the Internal Datapath Backward

If input signals are valid, trace internal module signals driving the failing output. Trace backward level by level. For a counter, inspect the adder output and the wrap comparator. Identify the earliest signal that displays an incorrect value. The logic generating that signal contains the underlying defect.

Step 4: Formulate a Specific Hypothesis

Do not make vague assumptions about circuit failure. Formulate a specific, testable hypothesis: “The counter wrap comparator asserts at value 14 instead of 15.” Inspect the corresponding RTL code with this exact condition in mind.

Step 5: Apply the Fix, Retest, and Run Regressions

Modify the RTL code to correct the defect. Run the failing test individually to confirm that it passes. If the test continues to fail, re-evaluate the hypothesis. Once the test passes, run the complete regression test suite. Running the regression suite confirms that the fix does not break other operating modes.

Complete Walkthrough: Debugging an Off-By-One Counter

Consider a modulo-16 counter that counts from 0 to 15 and pulses done for one clock cycle during wrap-around.

`timescale 1ns / 1ps

module counter_4bit (
    input  wire       clk,
    input  wire       rst_n,
    output reg  [3:0] count,
    output reg        done
);
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            count <= 4'd0;
            done  <= 1'b0;
        end else begin
            if (count == 4'd14) begin       // Defective comparison condition
                count <= 4'd0;
                done  <= 1'b1;
            end else begin
                count <= count + 4'd1;
                done  <= 1'b0;
            end
        end
    end
endmodule

The accompanying self-checking testbench verifies counter sequencing:

`timescale 1ns / 1ps

module tb_counter_4bit();
    parameter CLK_PERIOD = 10;

    reg        clk, rst_n;
    wire [3:0] count;
    wire       done;
    integer    error_count;
    reg  [3:0] expected_count;

    counter_4bit dut (.clk(clk), .rst_n(rst_n), .count(count), .done(done));

    initial clk = 0;
    always #(CLK_PERIOD / 2) clk = ~clk;

    initial begin
        $dumpfile("counter_4bit.vcd");
        $dumpvars(0, tb_counter_4bit);
    end

    initial begin
        error_count = 0;
        rst_n = 0;
        repeat (3) @(posedge clk); #2;
        rst_n = 1;

        repeat (20) @(posedge clk); #2;

        if (error_count == 0)
            $display("PASS");
        else
            $display("FAIL: %0d errors.", error_count);
        $finish;
    end

    // Verification check loop
    initial begin
        expected_count = 4'd0;
        @(posedge rst_n);
        @(posedge clk); #2;
        forever begin
            @(posedge clk); #2;
            if (count !== expected_count) begin
                $error("at %0t ns: count expected %0d, got %0d", $time, expected_count, count);
                error_count = error_count + 1;
            end
            expected_count = (expected_count == 4'd15) ? 4'd0 : expected_count + 4'd1;
        end
    end
endmodule

Executing the simulation produces the following log:

ERROR at 170 ns: count expected 15, got 0
ERROR at 180 ns: count expected 0, got 1
ERROR at 340 ns: count expected 15, got 0
ERROR at 350 ns: count expected 0, got 1
FAIL: 4 errors.

At timestamp 170 ns, the checker expected value 15 but sampled value 0. Inspect the waveform: at 160 ns, count equals 4'hE and done is low. At 170 ns, count transitions to 0 and done pulses high. The counter sequences through values 13, 14, and then resets directly to 0. It skips value 15 completely.

Inputs: reset remained deasserted and the clock toggled regularly. The stimulus operates correctly.

Hypothesis: the wrap comparator triggers prematurely on value 14. Review the RTL source code: line 75 checks count == 4'd14. Update the comparison threshold to 15:

            if (count == 4'd15) begin       // Corrected comparison

Rerun the simulation. The log confirms that all checks pass:

PASS

Waveform inspection confirms the counter sequences through values 14, 15, and 0 with done asserting at value 15. Executing the regression suite confirms complete functional compliance.

Never modify testbench checker expectations to match buggy hardware behavior. Fix the underlying RTL implementation to adhere strictly to the engineering specification.

Debugging Best Practices

Step Recommended Practice Practice to Avoid
1 Read error messages to identify signals and timestamps Guess failure causes without reading logs
2 Navigate to the exact failure timestamp in waveforms Inspect arbitrary timeline regions
3 Verify input stimulus driving the design Assume the RTL contains the defect immediately
4 Trace failing outputs backward toward drivers Trace inputs forward through unrelated circuits
5 Formulate a single, testable hypothesis Make ambiguous assumptions about failures
6 Inspect the RTL logic identified by the hypothesis Rewrite unrelated logic blocks
7 Correct the root design defect in RTL Modify checkers to mask RTL bugs
8 Retest the failing test, then run full regressions Skip regression verification after code changes
← Reading Waveforms