Explaining async FIFO design with Gray-coded pointers
I’m providing a clear explanation of asynchronous FIFO design using separate read/write pointers in their clock domains, each converted to Gray code before crossing domains through synchronizers to prevent metastability-induced errors. Gray coding ensures only one bit changes per increment, avoiding multi-bit misreads that can cause false empty/full detection, unlike binary pointers that toggle multiple bits simultaneously.
What people normally mean by an “asynchronous FIFO” is a dual‑ported RAM with independent write and read clocks and some logic on either side to keep track of where the next write or read will occur and to flag full and empty. A very common implementation has these elements:
A binary write pointer that lives entirely in the write clock domain and is incremented on each accepted write.
A binary read pointer that lives entirely in the read clock domain and is incremented on each accepted read.
An extra most‑significant bit on each pointer to distinguish between the “first time round” and the “second time round” so that you can tell the difference between full and empty when the addresses are equal.
Combinatorial logic in the write domain that asserts full when the next write pointer would equal the read pointer.
Combinatorial logic in the read domain that asserts empty when the next read pointer would equal the write pointer.
The problem is that those comparisons span the two domains: the write domain needs a copy of the read pointer, and the read domain needs a copy of the write pointer. If you simply pipe a multi‑bit binary counter across an asynchronous boundary and stick it into a two‑flip‑flop synchroniser you can sample a state in which some bits have changed and some have not. For example, a binary counter going from 0111 to 1000 flips all four bits. If the receiving clock samples in the middle of that transition it could see 0100 or 1011, neither of which was ever a valid pointer. That can lead to spurious full/empty flags and corrupted data.
The standard way around this is to convert the pointers to Gray code before you ship them across the boundary and to re‑synchronise the Gray‑coded value with a couple of flip‑flops. An n‑bit Gray code has the property that only one bit changes between successive counts. If you take an n+1‑bit binary counter, transform it to Gray with gray = bin ^ (bin >> 1), and synchronise that bus into the other domain, the receiver will see either the “old” Gray code or the “next” Gray code; there are no illegal intermediate combinations. In the full/empty comparators that off‑by‑one ambiguity is harmless, because you only ever test equality. A typical full detector in the write clock domain, for example, compares the next write pointer in Gray to the synchronised read pointer in Gray with the two MSBs inverted to account for wrap‑around.
So the design flows like this:
verilog
// write pointer and Gray-coded version
always @(posedge wr_clk) if (wr_en && !full) wr_bin <= wr_bin + 1;
assign wr_gray = wr_bin ^ (wr_bin >> 1);
// synchronise read pointer into write domain
always @(posedge wr_clk) rd_gray_sync <= {rd_gray_sync[0], rd_gray};
// full when next wr_gray equals rd_gray_sync with MSBs inverted
assign full = (next_wr_gray == {~rd_gray_sync[1][ADDR_MSB:ADDR_MSB-1], rd_gray_sync[1][ADDR_MSB-2:0]});
and similarly on the read side.
Using Gray‑coded pointers in this way greatly reduces the chance of interpreting a partially updated multi‑bit bus as a valid address, which is why you see it in most robust asynchronous FIFO designs.