S351 of 55
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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?

Name it

The design

  • A memory of D=2n 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), and rng.random() < wprob: store the next word at mem[wb % D], advance wb (mod 2n+1) and drive the 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)) and rng.random() < rprob, read mem[rb % D], advance rb, drive its crossing; then clock the write-to-read crossing.
  • Track the largest occupancy, (wb−rb)mod2n+1. 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
  1. Occupancy. The FIFO holds 11−3=8 words, its depth.
  2. Write pointer in Gray. 11 = 1011 becomes 1011⊕0101=1110
  3. Read pointer in Gray. 3 = 0011 becomes 0011⊕0001=0010
  4. Compare. Inverting the top two bits of 0010 gives 1110, equal to the write pointer's Gray code, so the flag reports full.
  5. 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
  1. Depth 16. The model gives 3020 time units: 302 cycles, one word per cycle after a 2-cycle start-up.
  2. Depth 4. It takes 3760 units, 376 cycles, so the throughput is 300376=80% the round-trip limit.
  3. 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.
  4. 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?

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.