Module 6: Verilog for Testbench & Simulation · 6.2 Simulation-Only Constructs

#delay, @(event) and wait()

In event-driven simulation, time advances only when procedural processes yield control to the scheduler. Verilog provides three primary timing control mechanisms:

  1. #delay: Delays execution by a fixed duration of simulation time.
  2. @(event): Suspends execution until a specific signal edge or transition occurs.
  3. wait(condition): Suspends execution until a logical expression evaluates to true.

Using the incorrect construct makes testbenches fragile and introduces simulation race conditions.

Fixed Time Delays (#delay)

The #delay operator advances simulation time by an explicit numeric duration:

#10;
#(CLK_PERIOD * 3);

Use fixed delays for physical time durations:

  • Asserting power-on reset pulses for a specific duration in nanoseconds.
  • Applying short delay offsets after clock transitions to model propagation delays.
  • Setting pulse widths for asynchronous control strobes.

Do not use fixed delays to count synchronous clock cycles. If you write #10 to represent one clock period, the testbench fails when another engineer modifies the clock frequency. Use @(posedge clk) to synchronize with clock cycles.

Edge-Sensitive Event Controls (@(event))

The @ operator suspends execution until a signal transition occurs:

@(posedge clk);                  // Waits for the next rising clock edge
@(negedge clk);                  // Waits for the next falling clock edge
@(data_valid);                   // Waits for any logic level change
@(posedge a or negedge b);       // Waits for either edge transition

To wait for $N$ consecutive clock cycles, use repeat (N) @(posedge clk). This construct remains robust if the clock frequency changes.

Level-Sensitive Conditions (wait())

The wait() statement suspends execution until an expression evaluates to true:

wait (data_valid);
wait (fifo_empty == 0);
wait (ack || timeout);

Compare wait(done) against @(posedge done):

  • @(posedge done) requires an active transition from 0 to 1. If done transitioned to 1 before the testbench reached the statement, @(posedge done) stalls indefinitely.
  • wait(done) evaluates the current logic level immediately. If done is already 1, execution continues without delay.

Use @(posedge clk) for periodic clock synchronization. Use wait() for handshakes and status flags where signals may assert before the check executes.

Proportional Clock Offsets

Do not use fixed nanosecond offsets to drive synchronous inputs:

// Fragile implementation
@(posedge clk);
#1 data_in = new_value;         // Fixed 1 ns offset can cause races at high clock frequencies

// Robust implementation
@(posedge clk);
#(CLK_PERIOD * 0.1);            // Offsets by 10% of the active clock period
data_in = new_value;

Calculating delay offsets as a fraction of CLK_PERIOD ensures that stimulus timing scales automatically across frequencies.

Figure 1 illustrates the operational differences among the three timing control mechanisms.

Three ways to wait
Figure 1: A delay, an edge, and a condition

Integrated Protocol Example

The following testbench integrates all three timing controls to verify a memory controller:

`timescale 1ns / 1ps

module tb_mem_ctrl();

    parameter CLK_PERIOD   = 10;
    parameter RESET_CYCLES = 4;
    parameter MAX_SIM_TIME = 50_000;

    reg        clk, rst_n;
    reg        req;
    reg  [7:0] addr;
    reg  [7:0] wdata;
    wire [7:0] rdata;
    wire       ack;

    // Watchdog timer process
    initial begin
        #MAX_SIM_TIME;
        $fatal(1, "TIMEOUT at %t", $time);
    end

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

    initial begin
        $dumpfile("tb_mem_ctrl.vcd");
        $dumpvars(0, tb_mem_ctrl);
        $timeformat(-9, 0, " ns", 8);

        // 1. Fixed delay: Hold reset low for configured duration
        rst_n = 0;
        req   = 0;
        addr  = 0;
        wdata = 0;
        #(CLK_PERIOD * RESET_CYCLES);

        // 2. Event control: Deassert reset on falling edge away from rising edge
        @(negedge clk);
        rst_n = 1;
        $display("Reset released at %t", $time);

        // 2. Event control: Wait two clock cycles for internal pipeline settling
        repeat (2) @(posedge clk);
        #(CLK_PERIOD * 0.1);

        req  = 1;
        addr = 8'hAB;
        $display("Read request initiated at %t: addr=%h", $time, addr);

        // 3. Condition wait: Wait for DUT handshake acknowledgement
        @(posedge clk);
        wait (ack);
        $display("Acknowledge received at %t: rdata=%h", $time, rdata);

        req = 0;
        @(posedge clk);
        #(CLK_PERIOD * 0.1);

        $display("Test completed at %t", $time);
        $finish;
    end

endmodule

Summary of Timing Control Applications

Synchronization Scenario Recommended Construct
Power-on reset duration or pulse width #delay
Waiting for $N$ clock cycles repeat (N) @(posedge clk)
Driving inputs after active clock edge @(posedge clk); #(CLK_PERIOD * 0.1)
Handshake acknowledgment (ready, done) wait(signal)
Waiting for an upcoming signal transition @(posedge signal)
Waiting for multiple alternate events @(sig_a or sig_b)
Allowing combinational logic to settle Small #delay

This concludes Section 6.2. Section 6.3 examines clock generators, reset initialization protocols, and multi-clock verification environments.

← $readmemh and $readmemb Generating Clocks →