Every technique so far has moved a single bit, or a value guarded by a handshake. An asynchronous FIFO needs something different: two multi-bit counters — a write pointer and a read pointer — each constantly incrementing in its own clock domain, each needing to be read safely from the other domain, continuously, with no handshake slowing it down. A plain binary counter cannot survive that crossing. Gray code is the reason it can.
You already met this failure mode in Lesson 3.1: independently synchronize each bit of a multi-bit bus, and the receiver can briefly see a combination that was never actually driven. A counter makes this worse, not better — because counters roll over constantly, and some of those rollovers flip every bit at once.
Watch what happens at 0111 → 1000 (7 → 8). All four bits change simultaneously. In real
silicon, "simultaneously" is never perfectly true — different bit paths have slightly different delays. If a
receiving clock edge lands inside that tiny window, some bits might already reflect the new value while
others still show the old one. The bits synchronizer produces isn't 7 or 8 — it could be 1111
(15), 0000 (0), or any other combination of old and new bits.
This is exactly the mechanism behind classic FIFO corruption bugs. A momentarily wrong pointer value can make a FIFO believe it's full when it's empty, or read stale data as if it were valid — bugs that show up rarely, under specific timing, and are notoriously painful to reproduce in the lab.
Gray code is a way of numbering the same 16 values a 4-bit counter would produce, but reordering which bit pattern represents which count — so that every step to the next value changes exactly one bit. Nothing else about "counting" changes: it still goes through 16 distinct values before repeating.
| Decimal | Binary | Bits changed | Gray Code | Bits changed |
|---|
The Binary column routinely changes 2, 3, even all 4 bits between consecutive rows (highlighted amber). The Gray Code column never changes more than 1 — including the wraparound from 15 back to 0.
Go back to the independent-per-bit synchronizer picture from Section 1. With Gray code, only one bit is ever transitioning at any given moment. So no matter how a receiving clock edge lands relative to that one bit's change — before it, after it, or awkwardly in between — every other bit is guaranteed unchanged.
A synchronizer sampling a Gray-coded value can only ever read the old value or the new value — because those are the only two combinations of bits that exist during the transition. There is no third option.
This doesn't remove metastability from the picture — the single bit that is changing can still go metastable, exactly as in Lesson 2.1, and still needs its own two-flop synchronizer, exactly as in Lesson 3.1. What Gray code removes is the multi-bit tearing problem — a completely different failure mode that two-flop synchronizers were never able to fix on their own.
One straight chain: count in binary (it's what adders are built for), convert to Gray right before crossing, synchronize bit-by-bit, convert back on the other side.
Both directions are simple, cheap, combinational logic — no lookup tables, no iteration cost worth worrying about.
Notice the asymmetry: binary→Gray is a single-level XOR (fast, constant delay regardless of width). Gray→binary is a cascaded XOR chain (delay grows with bit width). This is one reason the Gray-coded value is what actually crosses the domain — the wide-conversion is a one-time combinational cost paid once you're already safely in the receiving domain.
This exact circuit — binary counter, Gray-code the pointer, synchronize it, decode it back — is the standard way real asynchronous FIFOs move their write and read pointers across domains so the "how full is it?" logic on each side can trust what it's comparing. That's the very next lesson.
Race naive binary sync against Gray-code sync on the same counter, live. 👇
A 4-bit counter increments in domain A. Each bit is synchronized into domain B independently — once as plain binary, once as Gray code — with a small, realistic timing skew between bit paths. Each band below is a standard multi-bit waveform: a flat segment holds the decimal value, and an hourglass crossing marks each change. Watch for red segments: moments where the synchronized value briefly isn't the old count or the new count.
Try it yourself — copy this into our Verilog simulator and check the waveform. 👇
module gray_pointer_sync #(
parameter WIDTH = 4
) (
input wire clk_a,
input wire rst_a_n,
input wire increment, // domain A: pulse to advance the counter
input wire clk_b,
input wire rst_b_n,
output wire [WIDTH-1:0] ptr_bin_sync // domain B: safe binary value of the pointer
);
// ---- Domain A: plain binary counter ----
reg [WIDTH-1:0] bin_cnt_a;
always @(posedge clk_a or negedge rst_a_n) begin
if (!rst_a_n) bin_cnt_a <= {WIDTH{1'b0}};
else if (increment) bin_cnt_a <= bin_cnt_a + 1'b1;
end
// ---- Domain A: binary -> Gray (single-level XOR, combinational) ----
wire [WIDTH-1:0] gray_cnt_a = bin_cnt_a ^ (bin_cnt_a >> 1);
// ---- Domain B: N independent two-flop synchronizers, one per Gray bit ----
(* ASYNC_REG = "TRUE" *) reg [WIDTH-1:0] gray_sync_stage1;
(* ASYNC_REG = "TRUE" *) reg [WIDTH-1:0] gray_sync_b;
always @(posedge clk_b or negedge rst_b_n) begin
if (!rst_b_n) begin
gray_sync_stage1 <= {WIDTH{1'b0}};
gray_sync_b <= {WIDTH{1'b0}};
end else begin
gray_sync_stage1 <= gray_cnt_a;
gray_sync_b <= gray_sync_stage1;
end
end
// ---- Domain B: Gray -> binary (cascaded XOR) ----
genvar i;
wire [WIDTH-1:0] bin_from_gray;
assign bin_from_gray[WIDTH-1] = gray_sync_b[WIDTH-1];
generate
for (i = WIDTH-2; i >= 0; i = i - 1) begin : GRAY2BIN
assign bin_from_gray[i] = bin_from_gray[i+1] ^ gray_sync_b[i];
end
endgenerate
assign ptr_bin_sync = bin_from_gray;
endmodule
`timescale 1ns/1ps
module tb_gray_pointer_sync;
reg clk_a = 0, clk_b = 0;
reg rst_a_n = 0, rst_b_n = 0;
reg increment = 0;
wire [3:0] ptr_bin_sync;
// Two unrelated clocks
always #5 clk_a = ~clk_a; // 100 MHz
always #3.5 clk_b = ~clk_b; // ~143 MHz
gray_pointer_sync #(.WIDTH(4)) dut (
.clk_a(clk_a), .rst_a_n(rst_a_n), .increment(increment),
.clk_b(clk_b), .rst_b_n(rst_b_n), .ptr_bin_sync(ptr_bin_sync)
);
initial begin
$dumpfile("gray_pointer_sync.vcd");
$dumpvars(0, tb_gray_pointer_sync);
#20 rst_a_n = 1; rst_b_n = 1;
// Free-run the counter through a full wraparound, including 7->8 and 15->0
repeat (20) begin
#10 increment = 1;
#5 increment = 0;
end
#60 $finish;
end
endmodule