CDC Course / Module 4: Asynchronous FIFO Design
Lesson 4.1

Gray Code Fundamentals

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.

1

Why a binary counter breaks the moment it crosses

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.

2

Gray code: counting where only one bit ever moves

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.

DecimalBinaryBits changedGray CodeBits changed
Read the table

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.

3

Why one-bit-at-a-time is exactly enough

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.

The entire guarantee, in one sentence

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.

4

The complete circuit

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.

DOMAIN A (clk_a) DOMAIN B (clk_b) COUNTER binary · clk_a BIN2GRAY g = b ^ (b>>1) N × 2-FLOP SYNC 1 per Gray bit · clk_b (Lesson 3.1, repeated N times) GRAY2BIN clk_b ptr_sync clk_a clk_b
The counter itself stays plain binary — Gray code only has to exist for the moment the value crosses. BIN2GRAY and GRAY2BIN are both pure combinational logic; the only registers are the counter and the N independent two-flop synchronizers.
5

The conversion math

Both directions are simple, cheap, combinational logic — no lookup tables, no iteration cost worth worrying about.

gray = binary ^ (binary >> 1)
binary → Gray: XOR each bit with the bit above it
binary[N-1] = gray[N-1]; binary[i] = binary[i+1] ^ gray[i]
Gray → binary: XOR cascades from the top bit down

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.

6

Where this is actually used

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. 👇

🧪

Interactive Lab: Binary Sync vs. Gray-Code Sync

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.

Naive binary sync: —
Gray-code sync: —
glitch: a value that was never actually counted

Try it yourself — copy this into our Verilog simulator and check the waveform. 👇

gray_pointer_sync.v
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
tb_gray_pointer_sync.v
`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

🎯 What to remember

  • A binary counter's bits don't all change at exactly the same instant in real silicon — and independent per-bit synchronizers can catch a torn combination that was never a real count value.
  • Gray code reorders the same count sequence so consecutive values (including the wraparound) differ in exactly one bit — so a synchronizer can only ever read the old value or the new one.
  • Gray code doesn't remove metastability — the one changing bit still needs its own two-flop synchronizer. It removes the separate multi-bit tearing problem.
  • The circuit: count in binary, Gray-code it right before crossing, synchronize bit-by-bit, decode back to binary on the receiving side.
  • This is exactly how real asynchronous FIFOs move read/write pointers across domains — up next.
Advertisement