How Asynchronous FIFOs Prevent Metastability Across Clock Domains
A FIFO lets two chips, or two blocks of one chip, run on completely different clocks and still pass data safely. The trick is in how the two sides share their pointers.
1 Two clocks that never agree
The writer runs on clk_w, the reader on clk_r. Neither knows when the other clock ticks. To know if the FIFO is full or empty, each side needs a copy of the other side's pointer, so those pointers must cross the clock boundary.
Key idea: you can’t stop metastability. You can only (a) make sure the thing being sampled is safe to get slightly wrong, and (b) give the flop time to settle.
2 Why plain binary pointers break
Count from 3 to 4 in binary: 011 → 100. All three bits change. If the other clock samples at that instant, each bit may be caught old or new independently, so it could read 111 (7) or 000 (0), values the pointer never held. A pointer that is off by up to half the FIFO breaks the full/empty logic.
3 Gray code: one bit at a time
In Gray code, neighbouring numbers differ in exactly one bit. Sampled mid-change, you get either the old value or the new one. Both are valid pointers, and being one step behind only makes the FIFO look slightly fuller or emptier than it is, which is the safe direction.
Convert with gray = bin ^ (bin >> 1). A FIFO of depth 2n uses n + 1 pointer bits: n to address memory and one extra wrap bit to tell full from empty.
4 Play with it
Two clock domains, an 8-slot memory and the real pointer-crossing logic. Run it in Gray mode, then switch to Binary and hit Force collision.
5 How full and empty are decided
- EMPTY (read side): the read pointer equals the synchronized write pointer.
- FULL (write side): the next write pointer equals the synchronized read pointer with its top two Gray bits inverted.
- The synchronized pointers are always a little old, so the flags are pessimistic: they may say “full” a bit early, never “not full” too late. That costs a little throughput, never data.
6 Check yourself
Cheat sheet
Try our Async FIFO Depth calculator to size one, or ask Chippy “why Gray code in async FIFOs?”.