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

The Asynchronous FIFO

Modern integrated circuits contain multiple independent clock domains. A PCIe controller operates on a 250 MHz reference clock, an external memory controller runs at 400 MHz, and an application processor core runs at 1 GHz. Streaming multi-byte data between asynchronous clock domains requires an asynchronous FIFO. The asynchronous FIFO is the standard Clock Domain Crossing (CDC) circuit used in production System-on-Chip (SoC) architectures.

Dual-Clock Architecture and Bidirectional Synchronization

The dual-port memory array sits at the physical center of the FIFO. The write port operates synchronously to wr_clk, and the read port operates synchronously to rd_clk. Because memory storage locations do not cross clock boundaries directly, data words require no synchronizer logic. Instead, read and write pointers cross between clock domains:

Async FIFO: memory in the middle, Gray pointers synchronized in both directions
Figure 1: Asynchronous FIFO structure

Both pointers cross domain boundaries:

  • The write pointer wr_ptr crosses into the rd_clk domain to evaluate the empty condition.
  • The read pointer rd_ptr crosses into the wr_clk domain to evaluate the full condition.

Each crossing converts the local binary pointer to Gray code before passing through a two-flop synchronizer.

Evaluating Full and Empty Conditions in Gray Code

Binary arithmetic comparators cannot evaluate Gray-coded vectors directly. However, equivalent structural rules distinguish full and empty states:

  • Empty Flag (Evaluated in rd_clk domain): The local read pointer matches the synchronized write pointer bit-for-bit: rd_ptr_gray == wr_ptr_gray_sync.
  • Full Flag (Evaluated in wr_clk domain): The local write pointer matches the synchronized read pointer in its lower address bits, but the two most-significant bits must both be inverted:
wire full = (wr_ptr_gray[PTR_WIDTH-1]   != rd_ptr_gray_sync[PTR_WIDTH-1]) &&
            (wr_ptr_gray[PTR_WIDTH-2]   != rd_ptr_gray_sync[PTR_WIDTH-2]) &&
            (wr_ptr_gray[PTR_WIDTH-3:0] == rd_ptr_gray_sync[PTR_WIDTH-3:0]);

In Gray code representations, wrapping around buffer boundaries inverts both the MSB and the second MSB. Checking this two-bit inversion confirms that the write pointer has wrapped around the buffer once more than the read pointer.

Complete Asynchronous FIFO Implementation

`timescale 1ns / 1ps
`default_nettype none

module async_fifo #(
    parameter DATA_WIDTH = 8,
    parameter DEPTH      = 16,
    parameter ADDR_WIDTH = $clog2(DEPTH)
) (
    // Write interface
    input  wire                   wr_clk, wr_rst_n,
    input  wire                   wr_en,
    input  wire [DATA_WIDTH-1:0]  wdata,
    output wire                   full,
    // Read interface
    input  wire                   rd_clk, rd_rst_n,
    input  wire                   rd_en,
    output reg  [DATA_WIDTH-1:0]  rdata,
    output wire                   empty
);
    reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];

    reg [ADDR_WIDTH:0] wr_ptr_bin, wr_ptr_gray;
    reg [ADDR_WIDTH:0] rd_ptr_bin, rd_ptr_gray;

    // Two-flop synchronizer registers
    reg [ADDR_WIDTH:0] rd_ptr_gray_s1, rd_ptr_gray_sync;   // Synchronized into wr_clk domain
    reg [ADDR_WIDTH:0] wr_ptr_gray_s1, wr_ptr_gray_sync;   // Synchronized into rd_clk domain

    // ---- Write Clock Domain ----
    always @(posedge wr_clk or negedge wr_rst_n) begin
        if (!wr_rst_n) begin
            wr_ptr_bin <= 0; wr_ptr_gray <= 0;
        end else if (wr_en && !full) begin
            mem[wr_ptr_bin[ADDR_WIDTH-1:0]] <= wdata;
            wr_ptr_bin  <= wr_ptr_bin + 1'b1;
            wr_ptr_gray <= (wr_ptr_bin + 1'b1) ^ ((wr_ptr_bin + 1'b1) >> 1);
        end
    end

    // Synchronize read pointer into write clock domain
    always @(posedge wr_clk or negedge wr_rst_n) begin
        if (!wr_rst_n) begin
            rd_ptr_gray_s1 <= 0; rd_ptr_gray_sync <= 0;
        end else begin
            rd_ptr_gray_s1   <= rd_ptr_gray;
            rd_ptr_gray_sync <= rd_ptr_gray_s1;
        end
    end

    assign full = (wr_ptr_gray[ADDR_WIDTH]   != rd_ptr_gray_sync[ADDR_WIDTH]) &&
                  (wr_ptr_gray[ADDR_WIDTH-1] != rd_ptr_gray_sync[ADDR_WIDTH-1]) &&
                  (wr_ptr_gray[ADDR_WIDTH-2:0] == rd_ptr_gray_sync[ADDR_WIDTH-2:0]);

    // ---- Read Clock Domain ----
    always @(posedge rd_clk or negedge rd_rst_n) begin
        if (!rd_rst_n) begin
            rd_ptr_bin <= 0; rd_ptr_gray <= 0; rdata <= {DATA_WIDTH{1'b0}};
        end else if (rd_en && !empty) begin
            rdata      <= mem[rd_ptr_bin[ADDR_WIDTH-1:0]];
            rd_ptr_bin  <= rd_ptr_bin + 1'b1;
            rd_ptr_gray <= (rd_ptr_bin + 1'b1) ^ ((rd_ptr_bin + 1'b1) >> 1);
        end
    end

    // Synchronize write pointer into read clock domain
    always @(posedge rd_clk or negedge rd_rst_n) begin
        if (!rd_rst_n) begin
            wr_ptr_gray_s1 <= 0; wr_ptr_gray_sync <= 0;
        end else begin
            wr_ptr_gray_s1   <= wr_ptr_gray;
            wr_ptr_gray_sync <= wr_ptr_gray_s1;
        end
    end

    assign empty = (rd_ptr_gray == wr_ptr_gray_sync);
endmodule

The design is architecturally symmetric. Neither clock domain samples raw binary pointer data directly. All cross-domain communication uses Gray-coded vectors protected by two-stage synchronizer flip-flops.

Conservative Flag Behavior and Safety Guarantees

Synchronizer latency delays pointer visibility across clock boundaries by two clock periods. Consequently, status flags behave conservatively:

  • The write domain might report full for two clock cycles after a read occurs.
  • The read domain might report empty for two clock cycles after a write occurs.

This latency-induced pessimism operates strictly in the safe direction. The write domain always knows its own write pointer accurately, preventing accidental buffer overflow. The read domain always knows its own read pointer accurately, preventing accidental buffer underflow. A delayed pointer view temporarily delays throughput but guarantees data integrity.

Verification Exercise

Explain why pointer synchronization latency can cause the full flag to remain asserted after a read operation. Explain why this latency never causes buffer data corruption.

View Solution

The write controller evaluates the full flag using rd_ptr_gray_sync. When the consumer reads an entry on rd_clk, the updated read pointer requires two wr_clk clock edges to traverse the synchronizer. During this synchronization interval, the write domain considers the memory entry occupied.

This staleness behaves conservatively. The write controller maintains an exact, real-time count of all writes it executed. Delayed notification of completed reads merely causes the write controller to delay new writes. The FIFO never reports false space availability when full.

Asynchronous FIFOs provide safe, elastic data streaming across clock domains. The next article establishes a self-checking testbench to verify dual-clock FIFO operation.

← Gray Code Pointers Verifying an Asynchronous FIFO →