← All problems

Asynchronous FIFO

Hard · Sequential · SystemVerilog · VHDL

A premium problem. Subscribe to open it in the editor, run the testbench and see where your design lands.

Design an asynchronous (dual-clock) first-in-first-out buffer that stores up to DEPTH entries of DATA_WIDTH bits each, safely transferring data from an independent write clock domain to an independent read clock domain.

The FIFO has two completely separate clock domains. All write-side operations are synchronous to the rising edge of wr_clk. All read-side operations are synchronous to the rising edge of rd_clk. The two clocks may run at different frequencies and have no phase relationship.

Write domain

On each rising edge of wr_clk:

- If wr_en is asserted and the FIFO is not full, wr_data is written to the tail of the FIFO and the write pointer advances. - If wr_en is asserted but wr_full is already asserted, the write is silently discarded — no data is corrupted and the write pointer does not advance.

wr_rst_n is an asynchronous active-low reset for the write domain. When wr_rst_n is driven low, the write pointer and wr_full reset immediately, without waiting for a clock edge.

wr_full asserts when the FIFO cannot accept any more entries. Because the write side must observe the read pointer from the read clock domain, wr_full is a conservative flag: it may remain asserted for one or more extra wr_clk cycles after a read has occurred, until the updated read pointer has propagated across the clock-domain boundary.

Read domain

On each rising edge of rd_clk:

- If rd_en is asserted and the FIFO is not empty, the entry at the head of the FIFO is captured into rd_data and the read pointer advances. - If rd_en is asserted but rd_empty is already asserted, the read is a no-op and the read pointer does not advance.

rd_rst_n is an asynchronous active-low reset for the read domain. When rd_rst_n is driven low, the read pointer and rd_empty reset immediately, without waiting for a clock edge. After reset, rd_empty is asserted.

rd_empty asserts when there are no valid entries available to read. Because the read side must observe the write pointer from the write clock domain, rd_empty is also conservative: it may remain asserted for one or more extra rd_clk cycles after a write has occurred, until the updated write pointer has propagated across the clock-domain boundary.

Clock-domain crossing

To safely cross the pointer values between the two clock domains without multi-bit metastability glitches, the write and read pointers are encoded in Gray code before being synchronized. In Gray code, consecutive pointer values differ by exactly one bit, so a two-flop synchronizer captures at most one metastable bit per crossing. The synchronized Gray-code pointer is then decoded back to binary on the receiving side to generate the full/empty flags.

The internal memory array is addressed directly by the binary write and read pointers; it is a simple dual-port RAM shared between the two domains.

Flag timing

The outputs are checked cycle by cycle, so the flags follow one exact timing:

- Each domain keeps its pointer as a registered Gray code, and that register crosses into the other domain through exactly two flip-flops clocked by the receiving clock. - wr_full and rd_empty are registered. On every edge they are computed from the pointer value after that edge's write or read, compared with the synchronized pointer as it was before that edge. - So wr_full rises on the same wr_clk edge as the write that fills the FIFO, and rd_empty rises on the same rd_clk edge as the read that empties it. A write while wr_full = 1 and a read while rd_empty = 1 can therefore never happen: a FIFO of DEPTH entries never accepts a (DEPTH+1)-th write before a read, and never reads past the last valid entry.

Output behavior

rd_data is a registered output. It updates on the rising edge of rd_clk when rd_en is asserted and rd_empty is low, and shows the entry read until the next rd_clk edge. In every other rd_clk cycle it may take any value.

  • FIFOs & memory

Open this challenge in the editor →