S3 · Lesson 51 · Asynchronous FIFO project
A cycle model with Gray-code pointers and synchronizers, checked by a constrained-random testbench
The asynchronous FIFO is the standard way to stream data between two clock domains, and it brings together several ideas from this course: a dual-port memory (lesson 12), pointers that cross clock domains through synchronizers (lesson 24), Gray codes that keep a sampled pointer either old or new, and full and empty flags that must never be optimistic. In this project you'll build a cycle-accurate model of one in Python, with a synchronizer that models metastable sampling, and verify it the way a verification engineer would (lesson 25): a constrained-random testbench that throws thousands of words through random depths, clock ratios, traffic patterns and metastability windows, and checks every word.
Predict
Both clocks run at the same frequency, and the FIFO's full flag reaches the writer through a two-flop synchronizer. Why might a 4-deep FIFO fail to keep up with a writer that sends every cycle, even though the reader takes every cycle too?
The writer sees the read pointer only after it crosses two flip-flops, and the reader sees the write pointer just as late. From a write to the moment the writer learns that word has been read takes about five cycles. A 4-deep FIFO lets only 4 words be outstanding in that time, so the writer idles about one cycle in five: 80% throughput. You'll measure this in the project. It's the credit argument from lesson 36: the depth must cover the round trip.
Name it
The design
- A memory of words, written in the write domain and read in the read domain.
- Pointers of n + 1 bits: the low n bits address the memory; the extra bit tells a full FIFO (pointers differ by D) from an empty one (pointers equal).
- Each pointer is kept in Gray code and passed through two flip-flops into the other domain.
- Empty (read domain): the read pointer equals the synchronized write pointer.
- Full (write domain): the write pointer equals the synchronized read pointer with its top two bits inverted and the rest equal. In Gray code, adding D to an (n + 1)-bit pointer flips exactly the top two bits.
Both flags are computed from a pointer that's two cycles stale. That makes them pessimistic: the FIFO may report full when a slot has just freed, or empty when a word has just arrived, but never the other way round, so no word is ever lost or invented.
Metastable sampling
The Crossing helper (given in step 3) models one synchronized pointer. drive(value, t) records a change in the source domain; clock(t) is a sampling edge in the destination domain, after which q2 holds the synchronized value. If the edge comes less than window after the last change, each bit that changed resolves to 0 or 1 at random. With Gray code only one bit ever changes, so the sample is always the old or the new pointer.
The cycle model
simulate(n, Tw, Tr, data, wprob, rprob, seed, window):
- Write edges at Tw, 2Tw, …; read edges at Tr, 2Tr, …; on a tie, the write edge first.
- At a write edge, if words remain, the FIFO isn't full by
is_full(gray(wb), r_to_w.q2, n), andrng.random() < wprob: store the next word atmem[wb % D], advancewb(mod ) anddrivethe new Gray pointer into the write-to-read crossing. Then clock the read-to-write crossing. - At a read edge, symmetrically: if not empty by
is_empty(w_to_r.q2, gray(rb))andrng.random() < rprob, readmem[rb % D], advancerb, drive its crossing; then clock the write-to-read crossing. - Track the largest occupancy, . Stop when every word has been read (or a generous time limit passes) and return
(words read, largest occupancy, time of the last edge).
The testbench
The tests generate 50 random configurations (depth 2 to 16, clock periods 4 to 17, traffic probabilities, metastability windows) and check that every word comes out once, in order, unchanged, that occupancy never exceeds the depth, that a slow reader lets the FIFO fill, and that a 16-deep FIFO with equal clocks runs at full rate.
Worked example
A FIFO of depth 8 has 4-bit pointers. The write pointer is 11 and the read pointer is 3. Show that the Gray comparison reports full.
Show the solution
- Occupancy. The FIFO holds words, its depth.
- Write pointer in Gray. 11 = 1011 becomes
- Read pointer in Gray. 3 = 0011 becomes
- Compare. Inverting the top two bits of 0010 gives 1110, equal to the write pointer's Gray code, so the flag reports full.
- Why the extra bit matters. A comparison of only the low three bits would have called it empty.
Answer the read pointer's Gray code with its top two bits inverted, 1110, matches the write pointer's, so the FIFO reports full with 8 words.
Worked example
With equal clocks of period 10 and full traffic, how long do 300 words take through a 16-deep FIFO, and through a 4-deep one?
Show the solution
- Depth 16. The model gives 3020 time units: 302 cycles, one word per cycle after a 2-cycle start-up.
- Depth 4. It takes 3760 units, 376 cycles, so the throughput is the round-trip limit.
- What Gray coding adds here. In this model a reader that takes one word per edge would also survive a mis-sampled binary pointer, because every change is an increment and a wrong sample is used for a single one-word decision.
- Where Gray coding matters. Real designs that compute a fill level, read in bursts or raise an almost-full flag from the synchronized pointer would act on a value that was never on the counter, so the Gray code is what makes those safe.
Answer 3020 time units (302 cycles) through the 16-deep FIFO, and 3760 (376 cycles, 80% throughput) through the 4-deep one.
Check
Step 1. Write gray(b) and from_gray(g).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write is_empty(wg, rg) and is_full(wg, rg, n) on Gray pointers of n + 1 bits.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write simulate(...), the cycle model of the asynchronous FIFO.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why are an asynchronous FIFO's full and empty flags safe even though each is computed from a pointer that's two cycles out of date?
Each domain only ever sees the other domain's pointer as it was a couple of cycles ago, and pointers only move forward. The read domain's copy of the write pointer can be behind the truth but never ahead of it, so when the reader decides the FIFO isn't empty, the words it thinks exist really have been written. Likewise the writer's copy of the read pointer can only lag, so space it thinks is free really has been read. Staleness can only make a flag pessimistic: the FIFO may look full or empty for a cycle or two longer than it is, costing a little throughput, but it can never let a word be read before it's written or overwritten before it's read. The Gray code guarantees each synchronized copy is a value the pointer really had, which keeps that argument true.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.