Everything in this module culminates here. An asynchronous FIFO allows one clock domain to stream data in, and a completely asynchronous domain to pull data out—safely, continuously, and without a per-word handshake bottleneck. It is constructed entirely from primitives you have already mastered: a true dual-port RAM, two Gray-coded counters, and a pair of dual-flop synchronizers. This lesson assembles them into the exact, silicon-proven architecture deployed in modern SoCs, derived rigorously and verified against a tape-out grade model.
Strip away the RAM array for a moment. An Async FIFO is fundamentally an exercise in pointer management. You have a Write Pointer tracking the next empty memory slot, and a Read Pointer tracking the next unread slot. Crossing clock domains securely boils down to answering two critical questions, computed simultaneously across asynchronous boundaries:
Answering these questions requires each domain to continuously evaluate its own pointer against a synchronized copy of the remote domain's pointer. Because pointers are multi-bit counters, relying on standard binary encoding would guarantee fatal combinational glitches. This is why Gray Code (Lesson 4.1) is not an optional optimization—it is the foundational mechanism that guarantees structural integrity.
Observe the elegant symmetry: two mirror-image logic cones, a shared dual-port memory core, and two entirely independent synchronizer paths. The data itself never crosses a synchronizer; only the Gray-coded pointers traverse the boundary.
Physical Isolation: Notice what absolutely does not cross the clock domain boundary: the massive, multi-bit data bus. The data payload safely waits inside the dual-port RAM. The only signals that ever experience asynchronous crossings are the two N+1 bit Gray-coded pointers.
If a FIFO is \(2^N\) deep, you might assume an N-bit pointer is sufficient. It is not. If the writer completes exactly one lap around the memory array, its N-bit address rolls over back to 0—becoming numerically identical to a reader that hasn't moved. An N-bit pointer cannot distinguish between a memory array that is completely empty and one that is catastrophically full.
The architectural solution is to append a Virtual MSB. Both pointers are instantiated as N+1 bits wide. The lower N bits map directly to the physical RAM address, natively wrapping via natural binary overflow. The extra MSB tracks the "lap count."
Equal Address Bits + Equal MSB = Pointers are at the exact same physical and virtual state. FIFO is EMPTY.
Equal Address Bits + Opposite MSB = Writer has lapped the reader exactly once. FIFO is FULL.
Because we are operating strictly in the Gray-code domain (to prevent multi-bit synchronization failures), checking if the writer has "lapped" the reader requires translating the N+1 binary logic into Gray space. The industry standard comparator inverts the top two bits of the synchronized read pointer:
Why invert the top two bits? This is a mathematical property of reflected binary code. When a binary counter overflows its MSB (e.g., from 011... to 100...), the corresponding Gray code mirrors across the axis, causing the top two bits to differ from the previous lap, while all lower bits remain identical.
| wptr_bin | 100 (Decimal 4 — Writer has completed one lap) |
| wptr_gray | 110 (= 100 ^ 010) |
| rptr_bin | 000 (Nothing read yet) |
| rptr_gray_sync | 000 |
| ~rptr_gray_sync[2:1] | 11 (Invert the top two bits) |
| Comparator RHS | 110 (Recombined with LSB) |
| Conclusion | 110 == 110 → FULL Asserted ✓ |
Deriving EMPTY is computationally trivial. Because the read pointer can only ever chase the write pointer and never overtake it, "caught up" simply means the two Gray pointers are identical.
The asymmetry between the FULL and EMPTY logic is a deliberate artifact of the architecture. FULL must distinguish "lapped by one" from "exactly caught up," while EMPTY only needs to confirm exact synchronization.
A frequent architectural debate revolves around why we compare Gray vectors directly instead of utilizing a GRAY2BIN module (Lesson 4.1) to decode the synchronized pointer back into binary, and then performing standard arithmetic.
Decoding is mathematically valid, but it carries a severe penalty in physical implementation.
Timing Closure Failure: A GRAY2BIN converter is structurally a cascaded chain of XOR gates. The combinational delay grows linearly with the pointer width. Placing this deep logic cone directly in the path of the FULL or EMPTY flag generation—which are typically the most critical timing paths in a memory interface—is an anti-pattern that causes routing congestion and timing violations at high frequencies.
Direct-Gray comparison (the logic used in Sections 4 and 5) requires only a bitwise equality check and two inverters. It maps instantly to a single stage of LUTs in an FPGA or a minimal delay gate path in an ASIC, ensuring robust timing closure.
If you analyze the synthesizable Verilog provided below, you will observe a subtle but absolute requirement: wptr_gray and rptr_gray are flip-flops, not combinational wires. The Gray value is calculated combinationally, but it is explicitly clocked into a register before traversing the clock domain boundary.
010 to 110 via a transient 111), the destination domain will latch an illegal pointer state. Registering the Gray output isolates the synchronizer from combinational hazards, guaranteeing a clean, single-bit transition.
A rigorous CDC architecture does not strive for "real-time" accuracy. Because the pointers must traverse dual-flop synchronizers, the status flags inherently evaluate a delayed snapshot of the remote domain (lagging by at least two clock cycles).
This pessimism is not a flaw; it is the cornerstone of CDC reliability. The flags err on the side of caution. A write is never allowed to overtake a slow reader, and a reader is never allowed to underflow a slow writer. Trading a microscopic fraction of bandwidth for a mathematically airtight guarantee against data corruption is the hallmark of professional hardware design.
Watch a live 8-deep FIFO fill and drain, verified against the same golden model used to prove this design. 👇
An 8-deep FIFO (N=3) with independent write and read clocks. A producer fills it until FULL
asserts, then a consumer drains it until EMPTY asserts, repeating — so occupancy sweeps cleanly
between 0 and 8. WR_DATA and RD_DATA show the actual values moving through; watch the
same sequence reappear on RD_DATA. Green dots are successful attempts; red dots are attempts
blocked by FULL/EMPTY. Change the clock periods to see how the frequency ratio reshapes the fill/drain slopes
and the synchronizer latency on the flags.
Try it yourself — copy this into our Verilog simulator and check the waveform. 👇
module async_fifo #(
parameter DATA_WIDTH = 8,
parameter ADDR_WIDTH = 3 // FIFO depth = 2**ADDR_WIDTH
) (
// Write domain
input wire clk_a,
input wire rst_a_n,
input wire wr_en,
input wire [DATA_WIDTH-1:0] wr_data,
output wire full,
// Read domain
input wire clk_b,
input wire rst_b_n,
input wire rd_en,
output reg [DATA_WIDTH-1:0] rd_data,
output wire empty
);
localparam DEPTH = 1 << ADDR_WIDTH;
// ---- Shared dual-port memory ----
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
wire wr_now = wr_en && !full;
always @(posedge clk_a) begin
if (wr_now) mem[wptr_bin[ADDR_WIDTH-1:0]] <= wr_data;
end
wire rd_now = rd_en && !empty;
always @(posedge clk_b or negedge rst_b_n) begin
if (!rst_b_n) rd_data <= {DATA_WIDTH{1'b0}};
else if (rd_now) rd_data <= mem[rptr_bin[ADDR_WIDTH-1:0]];
end
// ---- Write domain: Binary counter & registered Gray pointer ----
reg [ADDR_WIDTH:0] wptr_bin;
reg [ADDR_WIDTH:0] wptr_gray;
wire [ADDR_WIDTH:0] wptr_bin_next = wptr_bin + wr_now;
wire [ADDR_WIDTH:0] wptr_gray_next = wptr_bin_next ^ (wptr_bin_next >> 1);
always @(posedge clk_a or negedge rst_a_n) begin
if (!rst_a_n) begin
wptr_bin <= {(ADDR_WIDTH+1){1'b0}};
wptr_gray <= {(ADDR_WIDTH+1){1'b0}};
end else begin
wptr_bin <= wptr_bin_next;
wptr_gray <= wptr_gray_next; // CRITICAL: Gray pointer must be registered before crossing domains to avoid combinational glitches
end
end
// ---- Read domain: Binary counter & registered Gray pointer ----
reg [ADDR_WIDTH:0] rptr_bin;
reg [ADDR_WIDTH:0] rptr_gray;
wire [ADDR_WIDTH:0] rptr_bin_next = rptr_bin + rd_now;
wire [ADDR_WIDTH:0] rptr_gray_next = rptr_bin_next ^ (rptr_bin_next >> 1);
always @(posedge clk_b or negedge rst_b_n) begin
if (!rst_b_n) begin
rptr_bin <= {(ADDR_WIDTH+1){1'b0}};
rptr_gray <= {(ADDR_WIDTH+1){1'b0}};
end else begin
rptr_bin <= rptr_bin_next;
rptr_gray <= rptr_gray_next; // CRITICAL: Register before crossing
end
end
// ---- Synchronize rptr_gray into the write domain (feeds FULL) ----
(* ASYNC_REG = "TRUE" *) reg [ADDR_WIDTH:0] rptr_gray_sync1, rptr_gray_sync2;
always @(posedge clk_a or negedge rst_a_n) begin
if (!rst_a_n) begin
rptr_gray_sync1 <= {(ADDR_WIDTH+1){1'b0}};
rptr_gray_sync2 <= {(ADDR_WIDTH+1){1'b0}};
end else begin
rptr_gray_sync1 <= rptr_gray;
rptr_gray_sync2 <= rptr_gray_sync1;
end
end
// ---- Synchronize wptr_gray into the read domain (feeds EMPTY) ----
(* ASYNC_REG = "TRUE" *) reg [ADDR_WIDTH:0] wptr_gray_sync1, wptr_gray_sync2;
always @(posedge clk_b or negedge rst_b_n) begin
if (!rst_b_n) begin
wptr_gray_sync1 <= {(ADDR_WIDTH+1){1'b0}};
wptr_gray_sync2 <= {(ADDR_WIDTH+1){1'b0}};
end else begin
wptr_gray_sync1 <= wptr_gray;
wptr_gray_sync2 <= wptr_gray_sync1;
end
end
// ---- FULL: write has lapped the synchronized read pointer by one cycle ----
assign full = (wptr_gray == {~rptr_gray_sync2[ADDR_WIDTH:ADDR_WIDTH-1],
rptr_gray_sync2[ADDR_WIDTH-2:0]});
// ---- EMPTY: read pointer has caught up to the synchronized write pointer ----
assign empty = (rptr_gray == wptr_gray_sync2);
endmodule
`timescale 1ns/1ps
module tb_async_fifo;
localparam DATA_WIDTH = 8;
localparam ADDR_WIDTH = 3;
reg clk_a = 0, clk_b = 0;
reg rst_a_n = 0, rst_b_n = 0;
reg wr_en = 0, rd_en = 0;
reg [DATA_WIDTH-1:0] wr_data = 0;
wire full, empty;
wire [DATA_WIDTH-1:0] rd_data;
always #5 clk_a = ~clk_a; // 100 MHz write clock
always #3.5 clk_b = ~clk_b; // ~143 MHz read clock
async_fifo #(.DATA_WIDTH(DATA_WIDTH), .ADDR_WIDTH(ADDR_WIDTH)) dut (
.clk_a(clk_a), .rst_a_n(rst_a_n), .wr_en(wr_en), .wr_data(wr_data), .full(full),
.clk_b(clk_b), .rst_b_n(rst_b_n), .rd_en(rd_en), .rd_data(rd_data), .empty(empty)
);
initial begin
$dumpfile("async_fifo.vcd");
$dumpvars(0, tb_async_fifo);
#20 rst_a_n = 1; rst_b_n = 1;
end
// Write side: push 12 values, backing off whenever FULL
integer i;
initial begin
wr_en = 0;
@(posedge rst_a_n);
for (i = 0; i < 12; i = i + 1) begin
@(posedge clk_a);
while (full) @(posedge clk_a);
#1 wr_en = 1; wr_data = i;
@(posedge clk_a);
#1 wr_en = 0;
end
end
// Read side: pop everything, backing off whenever EMPTY, checking order
integer expected;
initial begin
rd_en = 0;
expected = 0;
@(posedge rst_b_n);
while (expected < 12) begin
@(posedge clk_b);
while (empty) @(posedge clk_b);
#1 rd_en = 1;
@(posedge clk_b);
#1 rd_en = 0;
if (rd_data !== expected)
$display("MISMATCH at time %0t: expected %0d, got %0d", $time, expected, rd_data);
else
$display("OK at time %0t: read %0d", $time, rd_data);
expected = expected + 1;
end
#20 $finish;
end
endmodule
wr_data/rd_data) never crosses domains — only the two Gray-coded pointers do.