The idea: two clocks share one memory

A chip often has parts that run on different clocks: a 100 MHz bus and a 60 MHz video block, say. To pass a stream of words from one to the other, designers use an asynchronous FIFO: a small dual-port memory that one side writes on its clock (wclk) and the other side reads on its own clock (rclk). The writer keeps a write pointer, the reader a read pointer. The hard part is not the memory. It is that each side must know the other side's pointer, to tell whether the FIFO is full or empty, and that pointer changes on a clock it does not share.

Block diagram: the write domain on wclk holds wbin, wptr and wfull and writes into an 8-slot memory; the read domain on rclk holds rbin, rptr and rempty and reads from it; wptr crosses to the read domain through two flip-flops rq1 and rq2, and rptr crosses to the write domain through wq1 and wq2
Each pointer stays in its own clock domain; only a copy crosses, through two flip-flops on the other clock.

On the canvas the clock strip is at the top, the write domain on the left, the memory in the middle and the read domain on the right. Pick how many words to Write and Read and press Run Clocks. Each step is one clock edge that changed something; cells that changed turn orange, a wrong sample turns red. The page follows the design in Cummings' SNUG 2002 paper. Metastability and the 2-flip-flop synchronizer for a single bit are explained on Timing Analysis; this page is about what goes wrong with a multi-bit value.

Two clocks with a fixed ratio and phase

The writer's clock runs at 100 MHz: rising edges at 10, 20, 30 … ns. The reader's runs at 60 MHz (a period of 16.67 ns), with its first edge at 7 ns: 7, 23.67, 40.33, 57 … ns. Five wclk periods equal three rclk periods, so the picture repeats every 50 ns. The edges never meet, but every third rclk edge comes only 0.33 ns after a wclk edge (40.33, 90.33 …). That is the moment when the reader may catch the write pointer in the middle of a change.

The words are named d0, d1, … in the order they are written. The memory has 8 slots, so a word goes to slot wbin mod 8.

Why a binary pointer cannot cross

A 2-flop synchronizer makes each bit settle to a clean 0 or 1. It does not make the bits agree with each other. When a binary counter goes from 3 to 4, three bits change at once: 0011 → 0100. The wires have slightly different delays, and a flip-flop that samples during a change can go either way. In the model bits 3 and 2 arrive 0.2 ns after the edge and bits 1 and 0 after 0.6 ns. At 40.33 ns, 0.33 ns after the write, rq1 gets the new bit 2 but the old bits 1 and 0: 0111 = 7, a value the pointer never had. For one rclk cycle the reader sees 7 words ready when there are 4 (Demo: binary pointers, 0011 → 0100 seen as 0111). A reader that trusted that count for a burst would read slots 4 to 6, which hold no data.

The fix is to cross a Gray-coded pointer. Gray code changes exactly one bit per step (3 → 4 is 0010 → 0110), so a sample taken during a change is either the old value or the new one. Both are values the pointer really had; the old one is only a little late (Demo: Gray pointers, the same edge is safe). How Gray code is built and converted is on Binary Codes: BCD, Excess-3, Gray. Each side still counts in binary (wbin, rbin) to add 1 and to address the memory, and turns the count into Gray (wptr, rptr) before it crosses.

Two rows sampled 0.33 ns after the write edge. Binary 0011 to 0100: bits 3 and 2 have arrived, bits 1 and 0 have not, so the sample is 0111 = 7, never a real value. Gray 0010 to 0110: only bit 2 changes, so the sample is 0110 = 4 or, if bit 2 were late, 0010 = 3; both are real
Caught mid-change, a binary pointer can read as any mix of old and new bits; a Gray pointer reads as the old or the new value.

Full and empty from the extra bit

With 8 slots the address needs 3 bits, but the pointers have 4. The extra top bit flips each time a pointer wraps past slot 7. That is what tells full from empty, because in both cases the write and read addresses are the same:

ConditionBinary pointersGray pointersExample
Emptyall 4 bits equalall 4 bits equalrptr 1100, synced wptr 1100
Fulltop bit differs, low 3 equaltop 2 bits differ, low 2 equalwptr 1100, synced rptr 0000

The Gray rule looks odd but follows from the reflected code: the second half of a 4-bit Gray sequence is the first half mirrored with the top bit set, so the pointer 8 places ahead differs in the top two bits. After 8 writes wbin = 1000 and rbin = 0000 point at the same slot 0 and the FIFO is full; after 8 reads both are 1000 and it is empty (Demo: full and empty at the same address). The writer computes wfull from its own next pointer and the synchronized read pointer wq2; the reader computes rempty from its own next pointer and rq2. Both flags are registers.

Pessimistic flags: on at once, off late

A synchronized pointer is always two edges of the other clock old. So each side's view of the FIFO is a little behind, but always on the safe side:

  • Full goes on at once, because the writer's own pointer makes it full: in the burst of 10, the 8th write at 80 ns sets wfull in the same edge and d8 waits (Demo: burst of 10 fills the FIFO). It goes off late: the reader takes d0 at 123.67 ns, but the writer sees it only at 150 ns, 26.3 ns later (Demo: reader frees space, full clears late).
  • Empty goes on at once when the reader takes the last word, and goes off late: d0 is written at 10 ns, rq1 catches the new pointer at 23.67, rq2 at 40.33, rempty falls at 57 and the reader takes d0 at 73.67 ns (Demo: write 3, read 3, empty clears late).
Time runs down: at 10 ns the writer writes d0 and wptr becomes 0001; at 23.67 ns rq1 catches 0001; at 40.33 ns rq2 gets 0001; at 57 ns rempty falls to 0; at 73.67 ns the reader takes d0, 63.67 ns after it was written
One word in an empty FIFO: four rclk edges pass between the write and the read, because the pointer must go through rq1, rq2 and the registered rempty.

The FIFO can therefore never overflow or underflow; it only wastes a few cycles. The Gray sample can only be old, never ahead, so the writer may think the FIFO is fuller than it is and the reader may think it is emptier, never the other way round. With a 100 MHz writer and a 60 MHz reader, the writer fills the 8 slots and is then held back to the reader's pace: in 12 words wfull goes on 4 times, at 80, 110, 140 and 160 ns, and the reader still gets d0 to d11 in order. At 140.33 ns rq1 samples wptr 0.33 ns after it changed 1111 → 1110 and catches the old value 1111 (Demo: 12 words, 100 MHz writer vs 60 MHz reader).

In RTL

Cummings' design splits the FIFO into a memory, a write-side module and a read-side module, and two synchronizer modules. The write side, in Verilog, is a few lines:

assign wbinnext  = wbin + (winc & ~wfull);
assign wgraynext = (wbinnext >> 1) ^ wbinnext;
assign wfull_val = (wgraynext == {~wq2_rptr[3:2], wq2_rptr[1:0]});
always @(posedge wclk) begin
    {wbin, wptr} <= {wbinnext, wgraynext};
    wfull        <= wfull_val;
    {wq2_rptr, wq1_rptr} <= {wq1_rptr, rptr};
end

The read side mirrors it, with rempty_val = (rgraynext == rq2_wptr) and rempty reset to 1. The pointer goes into the synchronizer straight from a register, never from logic, so it cannot glitch. Gray counting needs a depth that is a power of two. For how such register code is written and simulated, see Verilog FSM Coding. Static timing tools cannot check the crossing paths, so they are marked as false or max-delay paths; see Static Timing Analysis. Bus bridges between clock domains, as in AMBA Bus Protocols, are built from FIFOs like this one. The same full/empty idea in software is the bounded buffer.

See also On-Chip Interconnect for the buses and networks on chip whose blocks often run on separate clocks joined by FIFOs like this one.

What the page leaves out

Real metastability (random settling, MTBF) is replaced by fixed bit delays so the demos repeat. The page leaves out resets and reset synchronizers, almost-full and almost-empty flags, the fill-count outputs of vendor FIFOs, Cummings' second style with asynchronous comparison of the pointers, FIFOs with a depth that is not a power of two, handshake and pulse synchronizers for single events, CDC lint and verification tools, and the timing constraints (max delay, bus skew) that a real crossing needs.

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OLatches and flip-flopsLatches and flip-flopslatch is level, flip-flop is edgeFlip-flops and counter designFlip-flops and counter designexcitation = characteristic backwardsLatch from transistorsLatch from transistorstransmission gates plus inverter loopCMOS layout, DRC, LVSCMOS layout, DRC, LVSDRC: can it be built? LVS: right circuit?Timing analysisTiming analysissetup limits clock, hold limits short pathsAsync FIFO (CDC)Gray pointers cross; flags clear lateStuck-at fault testingStuck-at fault testingActivate, propagate, justify: D-algorithmStatic Timing AnalysisStatic Timing AnalysisSlack = required − arrival, every nodeLogical effort sizingLogical effort sizingd = gh + p; equal effort F^(1/N)Scan chainsScan chainsShift in, capture once, shift outFinite state machinesFinite state machinesMoore: output from state; Mealy: +inputsASMD chart and datapathASMD chart and datapathTransfers land at the edge leaving the state1011 Sequence Detector1011 Sequence DetectorA wrong bit keeps the longest prefixFSM in SystemVerilogFSM in SystemVerilogenum, always_ff, always_comb; default!Shift registers and countersShift registers and countersChained flip-flops shift or count each clockOpen the Computer Architecture mind mapComputer Architecturestate held by a clockSequential logicLatches and flip-flopsLatches and flip-flopslatch is level, flip-flop is edgeFlip-flops and counter designFlip-flops and counter designexcitation = characteristic backwardsLatch from transistorsLatch from transistorstransmission gates plus inverter loopCMOS layout, DRC, LVSCMOS layout, DRC, LVSDRC: can it be built? LVS: right circuit?Timing analysisTiming analysissetup limits clock, hold limits short pathsAsync FIFO (CDC)Gray pointers cross; flags clear lateStuck-at fault testingStuck-at fault testingActivate, propagate, justify: D-algorithmStatic Timing AnalysisStatic Timing AnalysisSlack = required − arrival, every nodeLogical effort sizingLogical effort sizingd = gh + p; equal effort F^(1/N)Scan chainsScan chainsShift in, capture once, shift outFinite state machinesFinite state machinesMoore: output from state; Mealy: +inputsASMD chart and datapathASMD chart and datapathTransfers land at the edge leaving the state1011 Sequence Detector1011 Sequence DetectorA wrong bit keeps the longest prefixFSM in SystemVerilogFSM in SystemVerilogenum, always_ff, always_comb; default!Shift registers and countersShift registers and countersChained flip-flops shift or count each clock
This page on the Computer Architecture mind map · 76 pages in 7 branches · open it to see the whole map and test yourself in recall mode.