RTL Design #72 · 2026-12-04 · Clock-domain crossing FIFO · Hard · about 40 min
Build an asynchronous FIFO with Gray-coded pointers
Build async_fifo: an 8-entry, 8-bit FIFO whose write side and read side run on two unrelated clocks, wclk and rclk.
- Push. On a
wclkedge, ifwincis 1 andwfullis 0,wdatais stored. A push request whilewfullis 1 is ignored. - Pop. On an
rclkedge, ifrincis 1 andremptyis 0, the oldest entry is removed. A pop request whileremptyis 1 is ignored.rdataalways shows the oldest entry (a combinational read of the memory), and it is meaningful wheneverremptyis 0. Entries come out in the order they went in. - Flags.
wfullandremptyare registered, each in its own clock domain, and never lie in the dangerous direction:wfullis 0 only if there is room for one more entry, andremptyis 0 only if the entry is really there. They may be late: after the other side moves, a flag can take a few clock edges to catch up. Once the traffic stops,remptyis 1 exactly when the FIFO is empty andwfullis 1 exactly when 8 entries are stored. - Crossing. A pointer that crosses into the other clock domain must be Gray-coded and pass through two flip-flops in the destination domain. Nothing else crosses.
- Reset.
wrst_nandrrst_nare asynchronous and active low, and both are asserted together at the start. In reset the FIFO is empty:rempty= 1 andwfull= 0.
Timing example (both sides idle, FIFO empty): push one entry at wclk edge 1. rempty falls on an rclk edge a couple of read clocks later, once the new write pointer has gone through the two synchronizer flops. Pop it at an rclk edge with rinc = 1 and rempty = 0: rempty is 1 again after that edge.
Fill in async_fifo.sv. Run pushes five bytes with a 10 ns write clock and a 14 ns read clock and reads them back.
Write and run your answer in the browser. The hidden tests check it, and it’s free.