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 wclk edge, if winc is 1 and wfull is 0, wdata is stored. A push request while wfull is 1 is ignored.
  • Pop. On an rclk edge, if rinc is 1 and rempty is 0, the oldest entry is removed. A pop request while rempty is 1 is ignored. rdata always shows the oldest entry (a combinational read of the memory), and it is meaningful whenever rempty is 0. Entries come out in the order they went in.
  • Flags. wfull and rempty are registered, each in its own clock domain, and never lie in the dangerous direction: wfull is 0 only if there is room for one more entry, and rempty is 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, rempty is 1 exactly when the FIFO is empty and wfull is 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_n and rrst_n are asynchronous and active low, and both are asserted together at the start. In reset the FIFO is empty: rempty = 1 and wfull = 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.