Module 6: Verilog for Testbench & Simulation · 6.3 Generating Clocks & Reset Sequences

Reset in the Testbench

A digital design initializes during reset assertion. Before reset, flip-flops hold uninitialized (x) states. A compliant reset sequence drives every internal register into a deterministic state. Applying incorrect reset timing in the testbench causes simulation race conditions and propagates unknown values through logic pipelines.

Determining Reset Pulse Duration

A reset signal must propagate across all clock domains and pipeline stages. In synchronous reset architectures, each register stage requires an active clock edge to absorb the reset state. A multi-stage pipeline requires several consecutive clock cycles.

Define the reset duration as an explicit parameter:

parameter RESET_CYCLES = 4;

Parameterizing reset duration ensures that minimum pulse width requirements remain visible and verifiable.

Synchronous Reset Timing

In synchronous reset architectures, registers sample the reset signal on active clock edges:

`timescale 1ns / 1ps

module tb_sync_reset_example();

    parameter CLK_PERIOD   = 10;
    parameter RESET_CYCLES = 4;

    reg clk;
    reg rst_n;

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

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

        rst_n = 1'b0;                          // Assert active-low reset prior to clock edges

        repeat (RESET_CYCLES) @(posedge clk);  // Hold reset for configured cycles

        #2;                                    // Offset deassertion away from active edge
        rst_n = 1'b1;                          // Release reset

        @(posedge clk);                        // Allow one settling cycle

        $display("Reset released at %0t ns", $time);
        #200;
        $finish;
    end

endmodule

The #2; delay offset is critical. Executing repeat (RESET_CYCLES) @(posedge clk) pauses simulation until the active clock transition. If rst_n deasserts coincident with that edge, simulator race conditions determine whether flip-flops sample 0 or 1. Adding a 2 ns offset guarantees that registers sample the asserted reset state, deasserting cleanly before the next cycle.

Asynchronous Reset Timing

In asynchronous reset architectures (always @(posedge clk or negedge rst_n)), registers clear immediately when rst_n transitions low. However, releasing reset requires careful timing. If rst_n transitions high within the setup or hold window of the clock edge, physical flip-flops violate reset-recovery time ($t_{\text{rec}}$) and experience metastability.

In simulation testbenches, release asynchronous resets using the post-edge offset rule:

rst_n = 1'b0;
repeat (RESET_CYCLES) @(posedge clk);
#2;
rst_n = 1'b1;

This sequence applies to both synchronous and asynchronous designs. Always count clock edges using @(posedge clk) rather than fixed time delays (#50), ensuring correct behavior across clock frequencies.

Verifying Reset Polarity

Verify module reset polarity before writing stimulus:

  • Active-low (rst_n): Default industry convention. Assert with 0 and release with 1.
  • Active-high (rst): Common in educational examples. Assert with 1 and release with 0.
// Active-high reset sequence
rst = 1'b1;
repeat (RESET_CYCLES) @(posedge clk);
#2;
rst = 1'b0;

// Active-low reset sequence
rst_n = 1'b0;
repeat (RESET_CYCLES) @(posedge clk);
#2;
rst_n = 1'b1;

Figure 1 shows the relationship between the clock waveform and the deassertion offset.

Reset release
Figure 1: Assert, hold four edges, release just after the fourth

Standard Testbench Reset Template

`timescale 1ns / 1ps

module tb_dut_template();

    parameter CLK_PERIOD   = 10;
    parameter RESET_CYCLES = 4;

    reg clk;
    reg rst_n;
    // Declare other input drivers as reg, outputs as wire

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

    // Instantiate Design Under Test:
    // your_dut dut (.clk(clk), .rst_n(rst_n), ...);

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

        rst_n = 1'b0;
        // Initialize other input drivers to inactive states

        repeat (RESET_CYCLES) @(posedge clk);
        #2;
        rst_n = 1'b1;

        @(posedge clk);

        $display("Reset deasserted at %0t ns; initiating verification suite", $time);

        // Verification stimulus here

        $display("Simulation completed");
        $finish;
    end

endmodule

The next article examines startup initialization sequences to ensure all input buses achieve defined logic states before normal operation.

← Generating Clocks Initialization Sequences in Testbenches →