Module 7: Advanced RTL Design Techniques · 7.4 FIFOs — Synchronous & Asynchronous

Verifying an Asynchronous FIFO

Verifying an asynchronous FIFO requires validating operations across two independent time domains. A single global clock event @(posedge clk) cannot synchronize test operations. The testbench must instantiate two concurrent, independently timed processes to exercise the crossing logic thoroughly.

Generating Asynchronous, Unrelated Clock Signals

To stress synchronizer flip-flops, configure clock generators with non-integer period ratios. Integer clock ratios (such as 10 ns and 20 ns) cause clock edges to align in repetitive phase patterns. Constant phase alignment hides timing race conditions:

`timescale 1ns / 1ps

module tb_async_fifo;
    parameter DATA_WIDTH = 8, DEPTH = 8;

    reg wr_clk = 0, rd_clk = 0;
    reg wr_rst_n, rd_rst_n;
    reg wr_en, rd_en;
    reg [DATA_WIDTH-1:0] wdata;
    wire [DATA_WIDTH-1:0] rdata;
    wire full, empty;

    async_fifo #(.DATA_WIDTH(DATA_WIDTH), .DEPTH(DEPTH)) dut (
        .wr_clk(wr_clk), .wr_rst_n(wr_rst_n), .wr_en(wr_en), .wdata(wdata), .full(full),
        .rd_clk(rd_clk), .rd_rst_n(rd_rst_n), .rd_en(rd_en), .rdata(rdata), .empty(empty)
    );

    // Unrelated clock periods (7 ns and 11 ns) force continuous phase drift
    always #3.5  wr_clk = ~wr_clk;
    always #5.5  rd_clk = ~rd_clk;

Non-integer clock periods cause edges to drift across all possible phase alignments, maximizing test coverage.

Concurrent Stimulus and Scoreboard Verification

The write and read processes operate within separate initial procedural blocks. Both processes drive control signals on falling clock edges so that target registers sample stable signals on rising edges:

    localparam N = 100;
    reg [DATA_WIDTH-1:0] expected_queue [0:N-1];   // Scoreboard array
    integer push_idx = 0, pop_idx = 0;

    // Write process: synchronous to wr_clk
    initial begin
        wr_rst_n = 0; wr_en = 0; wdata = 0;
        repeat (3) @(negedge wr_clk);
        wr_rst_n = 1;

        while (push_idx < N) begin
            @(negedge wr_clk);
            if (!full && ($random % 3 != 0)) begin   // Pseudo-random write pauses
                wdata = push_idx[DATA_WIDTH-1:0];
                wr_en = 1;
                expected_queue[push_idx] = wdata;    // Enqueue expected data
                push_idx = push_idx + 1;
            end else begin
                wr_en = 0;
            end
        end
        @(negedge wr_clk);
        wr_en = 0;
    end

    // Read process: synchronous to rd_clk
    integer errors = 0;

    initial begin
        rd_rst_n = 0; rd_en = 0;
        repeat (3) @(negedge rd_clk);
        rd_rst_n = 1;

        while (pop_idx < N) begin
            @(negedge rd_clk);
            if (rd_en) begin
                if (rdata !== expected_queue[pop_idx]) begin
                    $display("FAIL at read #%0d: expected %0d, got %0d", pop_idx, expected_queue[pop_idx], rdata);
                    errors = errors + 1;
                end
                pop_idx = pop_idx + 1;
            end
            rd_en = (!empty && ($random % 4 != 0));   // Randomize read timing
        end
        rd_en = 0;

        if (errors == 0)
            $display("ALL %0d READS PASSED: order preserved across 7 ns / 11 ns clocks", N);
        else
            $display("%0d ERRORS across %0d reads", errors, N);

        $finish;
    end

endmodule

Key verification strategies used in this testbench:

  1. Intermittent Traffic: The conditions $random % 3 != 0 and $random % 4 != 0 introduce intermittent idle cycles. Continuous transfers mask corner cases that occur at partial buffer fills.
  2. Order-Preserving Scoreboard: The scoreboard verifies strict sequence ordering independent of clock timing.
  3. Falling-Edge Sampling: Evaluating !empty and !full on falling clock edges avoids setup race conditions against target registers.

Verification Exercise

Modify the testbench to sweep the read clock across five different frequencies while keeping the write clock fixed. Reset the FIFO between each frequency pass to verify robustness across various frequency ratios.

View Solution
real rd_half = 5.5;
always begin
    #(rd_half) rd_clk = ~rd_clk;      // Dynamic half-period evaluation
end

real periods [0:4];
integer p;
initial begin
    periods[0] = 4.0; periods[1] = 5.5; periods[2] = 6.3; periods[3] = 8.1; periods[4] = 9.7;
    for (p = 0; p < 5; p = p + 1) begin
        rd_half = periods[p];
        // Reset pointers and scoreboard
        wr_rst_n = 0; rd_rst_n = 0; wr_en = 0; rd_en = 0;
        push_idx = 0; pop_idx = 0; errors = 0;
        repeat (3) @(negedge wr_clk); repeat (3) @(negedge rd_clk);
        wr_rst_n = 1; rd_rst_n = 1;

        // Execute concurrent read and write verification tasks
        $display("rd half-period %.1f ns: %0d errors", rd_half, errors);
    end
    $finish;
end

The dynamic assignment #(rd_half) updates clock timing dynamically without restarting the simulation kernel.

This concludes Section 7.4. Section 7.5 formalizes Clock Domain Crossing (CDC) principles, multi-bit synchronizers, and linting rules.

← The Asynchronous FIFO Metastability and MTBF →