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

Two Clocks

Production digital systems often operate across multiple clock domains: a processor core operates at high frequency, a memory bus runs at medium frequency, and peripheral interfaces operate at lower frequencies. When verifying multi-clock architectures, testbenches must drive input signals in synchronization with their respective target clock domains.

Generating Independent Clocks

Define independent parameters and generators for each clock domain:

`timescale 1ns / 1ps

module tb_multi_domain();

    parameter SYS_CLK_PERIOD  = 10;    // 100 MHz system clock
    parameter UART_CLK_PERIOD = 40;    // 25 MHz peripheral clock

    parameter SYS_RESET_CYCLES  = 4;
    parameter UART_RESET_CYCLES = 4;

    reg sys_clk  = 1'b0;
    reg uart_clk = 1'b0;

    reg sys_rst_n  = 1'b0;
    reg uart_rst_n = 1'b0;

    always #(SYS_CLK_PERIOD / 2)  sys_clk  = ~sys_clk;
    always #(UART_CLK_PERIOD / 2) uart_clk = ~uart_clk;

Each generator operates independently. Because their period ratios can drift in real hardware, avoid assuming phase alignment between asynchronous clock domains.

Domain-Specific Driving Rules

Follow this strict verification rule: Always drive an input signal using the clock that controls its receiving flip-flop:

// Correct implementation: Driven relative to destination clock domains
@(posedge sys_clk); #2;
sys_data = 8'hA5;

@(posedge uart_clk); #2;
uart_tx_data = 8'h3C;

// Defective implementation: Driving UART signal relative to system clock
@(posedge sys_clk); #2;
uart_tx_data = 8'h3C; // Violates domain timing constraints

Driving signals from mismatched clocks causes random timing phase offsets in simulation, generating false verification failures.

Parallel Multi-Domain Reset Sequences

Each clock domain requires its own reset signal deasserted in phase with its local clock. Use fork and join to execute reset sequences concurrently:

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

    sys_rst_n  = 1'b0;
    uart_rst_n = 1'b0;

    fork
        begin
            repeat (SYS_RESET_CYCLES) @(posedge sys_clk);
            #2;
            sys_rst_n = 1'b1;
            $display("sys_rst_n released at %0t ns", $time);
        end

        begin
            repeat (UART_RESET_CYCLES) @(posedge uart_clk);
            #2;
            uart_rst_n = 1'b1;
            $display("uart_rst_n released at %0t ns", $time);
        end
    join

    repeat (2) @(posedge sys_clk);
    $display("Both clock domains operational at %0t ns", $time);
end

The system reset deasserts at approximately 42 ns. The UART reset deasserts at approximately 162 ns. The join keyword synchronizes simulation until both parallel threads complete.

Synchronizing Across Domains Using Handshakes

Do not use hardcoded time delays to synchronize across clock boundaries:

// Defective implementation: Estimates hardware latency using fixed delays
#150;
@(posedge uart_clk); #2;
uart_rx_read = 1'b1;

Use level-sensitive conditions followed by clock alignment:

// Correct implementation: Waits for hardware status flag and aligns to target clock
@(posedge sys_clk); #2;
sys_start = 1'b1;
@(posedge sys_clk); #2;
sys_start = 1'b0;

wait (dut.sys_done);          // Wait for event completion

@(posedge uart_clk);          // Re-synchronize to target domain clock
#2;
uart_rx_read = 1'b1;
@(posedge uart_clk); #2;
uart_rx_read = 1'b0;

Figure 1 illustrates a handshake spanning two independent clock domains.

A handshake across two clocks
Figure 1: Raised in one domain, consumed in the other

Complete Dual-Clock Domain Testbench Example

`timescale 1ns / 1ps

module tb_uart_bridge();

    parameter SYS_CLK_PERIOD    = 10;
    parameter UART_CLK_PERIOD   = 40;
    parameter SYS_RESET_CYCLES  = 4;
    parameter UART_RESET_CYCLES = 4;

    reg        sys_clk    = 1'b0;
    reg        uart_clk   = 1'b0;
    reg        sys_rst_n  = 1'b0;
    reg        uart_rst_n = 1'b0;

    reg [7:0]  sys_data_in = 8'h00;     // System domain input
    reg        sys_valid   = 1'b0;

    reg        uart_rd_en  = 1'b0;      // UART domain input

    wire [7:0] uart_data_out;
    wire       uart_data_valid;
    wire       sys_ready;

    always #(SYS_CLK_PERIOD / 2)  sys_clk  = ~sys_clk;
    always #(UART_CLK_PERIOD / 2) uart_clk = ~uart_clk;

    // Instantiate Design Under Test:
    // uart_bridge dut ( ... );

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

    initial begin
        sys_data_in = 8'h00;
        sys_valid   = 1'b0;
        uart_rd_en  = 1'b0;

        // Parallel reset deassertion
        fork
            begin
                repeat (SYS_RESET_CYCLES) @(posedge sys_clk);
                #2;
                sys_rst_n = 1'b1;
            end
            begin
                repeat (UART_RESET_CYCLES) @(posedge uart_clk);
                #2;
                uart_rst_n = 1'b1;
            end
        join

        repeat (2) @(posedge sys_clk);
        $display("[%0t ns] Both domains initialized", $time);

        // Drive stimulus in system clock domain
        @(posedge sys_clk); #2;
        sys_data_in = 8'hA5;
        sys_valid   = 1'b1;
        @(posedge sys_clk); #2;
        sys_valid   = 1'b0;

        // Synchronize and sample in UART clock domain
        wait (uart_data_valid);
        @(posedge uart_clk); #2;
        uart_rd_en = 1'b1;
        @(posedge uart_clk); #2;
        uart_rd_en = 1'b0;

        $display("[%0t ns] UART output data: %h (expected A5)", $time, uart_data_out);

        repeat (4) @(posedge uart_clk);
        $display("[%0t ns] Testbench completed", $time);
        $finish;
    end

    initial begin
        #10000;
        $fatal(1, "TIMEOUT: Simulation exceeded allotted duration");
    end

endmodule

Summary of multi-clock testbench rules:

  1. Generate each clock using an independent procedural block.
  2. Drive each input signal synchronized to its local receiving clock domain.
  3. Synchronize cross-domain stimulus using handshakes rather than fixed time delays.
  4. Manage multi-domain resets concurrently using fork and join.

This concludes Section 6.3. Section 6.4 examines self-checking test methodologies, golden reference models, and automated response checking.

← Initialization Sequences in Testbenches Why Checking Has to Be Automatic →