The idea: two clocks share one memory
A chip often has parts that run on different clocks: a 100 MHz bus and a 60 MHz video block, say. To pass a stream of words from one to the other, designers use an asynchronous FIFO: a small dual-port memory that one side writes on its clock (wclk) and the other side reads on its own clock (rclk). The writer keeps a write pointer, the reader a read pointer. The hard part is not the memory. It is that each side must know the other side's pointer, to tell whether the FIFO is full or empty, and that pointer changes on a clock it does not share.
On the canvas the clock strip is at the top, the write domain on the left, the memory in the middle and the read domain on the right. Pick how many words to Write and Read and press Run Clocks. Each step is one clock edge that changed something; cells that changed turn orange, a wrong sample turns red. The page follows the design in Cummings' SNUG 2002 paper. Metastability and the 2-flip-flop synchronizer for a single bit are explained on Timing Analysis; this page is about what goes wrong with a multi-bit value.
Two clocks with a fixed ratio and phase
The writer's clock runs at 100 MHz: rising edges at 10, 20, 30 … ns. The reader's runs at 60 MHz (a period of 16.67 ns), with its first edge at 7 ns: 7, 23.67, 40.33, 57 … ns. Five wclk periods equal three rclk periods, so the picture repeats every 50 ns. The edges never meet, but every third rclk edge comes only 0.33 ns after a wclk edge (40.33, 90.33 …). That is the moment when the reader may catch the write pointer in the middle of a change.
The words are named d0, d1, … in the order they are written. The memory has 8 slots, so a word goes to slot wbin mod 8.
Why a binary pointer cannot cross
A 2-flop synchronizer makes each bit settle to a clean 0 or 1. It does not make the bits agree with each other. When a binary counter goes from 3 to 4, three bits change at once: 0011 → 0100. The wires have slightly different delays, and a flip-flop that samples during a change can go either way. In the model bits 3 and 2 arrive 0.2 ns after the edge and bits 1 and 0 after 0.6 ns. At 40.33 ns, 0.33 ns after the write, rq1 gets the new bit 2 but the old bits 1 and 0: 0111 = 7, a value the pointer never had. For one rclk cycle the reader sees 7 words ready when there are 4 (Demo: binary pointers, 0011 → 0100 seen as 0111). A reader that trusted that count for a burst would read slots 4 to 6, which hold no data.
The fix is to cross a Gray-coded pointer. Gray code changes exactly one bit per step (3 → 4 is 0010 → 0110), so a sample taken during a change is either the old value or the new one. Both are values the pointer really had; the old one is only a little late (Demo: Gray pointers, the same edge is safe). How Gray code is built and converted is on Binary Codes: BCD, Excess-3, Gray. Each side still counts in binary (wbin, rbin) to add 1 and to address the memory, and turns the count into Gray (wptr, rptr) before it crosses.
Full and empty from the extra bit
With 8 slots the address needs 3 bits, but the pointers have 4. The extra top bit flips each time a pointer wraps past slot 7. That is what tells full from empty, because in both cases the write and read addresses are the same:
| Condition | Binary pointers | Gray pointers | Example |
|---|---|---|---|
| Empty | all 4 bits equal | all 4 bits equal | rptr 1100, synced wptr 1100 |
| Full | top bit differs, low 3 equal | top 2 bits differ, low 2 equal | wptr 1100, synced rptr 0000 |
The Gray rule looks odd but follows from the reflected code: the second half of a 4-bit Gray sequence is the first half mirrored with the top bit set, so the pointer 8 places ahead differs in the top two bits. After 8 writes wbin = 1000 and rbin = 0000 point at the same slot 0 and the FIFO is full; after 8 reads both are 1000 and it is empty (Demo: full and empty at the same address). The writer computes wfull from its own next pointer and the synchronized read pointer wq2; the reader computes rempty from its own next pointer and rq2. Both flags are registers.
Pessimistic flags: on at once, off late
A synchronized pointer is always two edges of the other clock old. So each side's view of the FIFO is a little behind, but always on the safe side:
- Full goes on at once, because the writer's own pointer makes it full: in the burst of 10, the 8th write at 80 ns sets wfull in the same edge and d8 waits (Demo: burst of 10 fills the FIFO). It goes off late: the reader takes d0 at 123.67 ns, but the writer sees it only at 150 ns, 26.3 ns later (Demo: reader frees space, full clears late).
- Empty goes on at once when the reader takes the last word, and goes off late: d0 is written at 10 ns, rq1 catches the new pointer at 23.67, rq2 at 40.33, rempty falls at 57 and the reader takes d0 at 73.67 ns (Demo: write 3, read 3, empty clears late).
The FIFO can therefore never overflow or underflow; it only wastes a few cycles. The Gray sample can only be old, never ahead, so the writer may think the FIFO is fuller than it is and the reader may think it is emptier, never the other way round. With a 100 MHz writer and a 60 MHz reader, the writer fills the 8 slots and is then held back to the reader's pace: in 12 words wfull goes on 4 times, at 80, 110, 140 and 160 ns, and the reader still gets d0 to d11 in order. At 140.33 ns rq1 samples wptr 0.33 ns after it changed 1111 → 1110 and catches the old value 1111 (Demo: 12 words, 100 MHz writer vs 60 MHz reader).
In RTL
Cummings' design splits the FIFO into a memory, a write-side module and a read-side module, and two synchronizer modules. The write side, in Verilog, is a few lines:
assign wbinnext = wbin + (winc & ~wfull);
assign wgraynext = (wbinnext >> 1) ^ wbinnext;
assign wfull_val = (wgraynext == {~wq2_rptr[3:2], wq2_rptr[1:0]});
always @(posedge wclk) begin
{wbin, wptr} <= {wbinnext, wgraynext};
wfull <= wfull_val;
{wq2_rptr, wq1_rptr} <= {wq1_rptr, rptr};
end
The read side mirrors it, with rempty_val = (rgraynext == rq2_wptr) and rempty reset to 1. The pointer goes into the synchronizer straight from a register, never from logic, so it cannot glitch. Gray counting needs a depth that is a power of two. For how such register code is written and simulated, see Verilog FSM Coding. Static timing tools cannot check the crossing paths, so they are marked as false or max-delay paths; see Static Timing Analysis. Bus bridges between clock domains, as in AMBA Bus Protocols, are built from FIFOs like this one. The same full/empty idea in software is the bounded buffer.
See also On-Chip Interconnect for the buses and networks on chip whose blocks often run on separate clocks joined by FIFOs like this one.
What the page leaves out
Real metastability (random settling, MTBF) is replaced by fixed bit delays so the demos repeat. The page leaves out resets and reset synchronizers, almost-full and almost-empty flags, the fill-count outputs of vendor FIFOs, Cummings' second style with asynchronous comparison of the pointers, FIFOs with a depth that is not a power of two, handshake and pulse synchronizers for single events, CDC lint and verification tools, and the timing constraints (max delay, bus skew) that a real crossing needs.
On the mind map
References
David Money Harris and Sarah L. Harris, Digital Design and Computer Architecture, 2nd ed., section 3.5.5 (Synchronizers)