Module 7: Advanced RTL Design Techniques · 7.4 FIFOs — Synchronous & Asynchronous

Gray Code Pointers

Module 3 introduced the two-flop synchronizer to resolve metastability on single-bit asynchronous signals. A single-bit synchronizer guarantees that a metastable signal resolves cleanly to logic 0 or logic 1. However, attempting to synchronize a multi-bit binary counter using parallel two-flop synchronizers causes severe functional failures.

Multi-Bit Synchronization Hazards

Consider a 4-bit binary counter transitioning from 7 (4'b0111) to 8 (4'b1000). All four bits change state simultaneously:

$$0111 \rightarrow 1000$$

Because physical routing paths introduce minor trace skew, bit transitions do not arrive at the destination flip-flops at the exact same picosecond. If an asynchronous clock edge samples during this transition window, destination flip-flops capture an invalid intermediate state:

Binary 0111 to 1000 sampled mid-transition gives 1101
Figure 1: Sampling a multi-bit binary transition from an unrelated clock

The receiving domain can sample corrupted values such as 4'b1100 or 4'b0000. The counter never held these values. Two-flop synchronizers resolve electrical metastability on individual bits, but they cannot enforce temporal coherence across multi-bit binary vectors.

Gray Code Characteristics

Gray code encodes numerical sequences such that adjacent values differ by exactly one bit:

Decimal Value Standard Binary Reflected Gray Code
0 000 000
1 001 001
2 010 011
3 011 010
4 100 110
5 101 111
6 110 101
7 111 100

Examine the transition from decimal 3 to decimal 4. Binary transitions from 011 to 100, toggling all three bits. Gray code transitions from 010 to 110, toggling only the most-significant bit. Because only one bit transitions at any step, an asynchronous clock edge samples either the old value or the new value. The receiving domain never captures corrupted intermediate states.

Binary to Gray Code Conversion

Convert standard binary vectors to Gray code using bitwise right-shift and XOR operations:

$$\text{gray} = \text{binary} \oplus (\text{binary} \gg 1)$$

function [WIDTH-1:0] bin_to_gray;
    input [WIDTH-1:0] bin;
    begin
        bin_to_gray = bin ^ (bin >> 1);
    end
endfunction

Gray Code to Binary Conversion

Converting Gray code back to binary uses a cascaded parity chain:

function [WIDTH-1:0] gray_to_bin;
    input [WIDTH-1:0] gray;
    integer i;
    reg [WIDTH-1:0] bin;
    begin
        bin[WIDTH-1] = gray[WIDTH-1];
        for (i = WIDTH-2; i >= 0; i = i - 1)
            bin[i] = bin[i+1] ^ gray[i];
        gray_to_bin = bin;
    end
endfunction

Receiving logic uses this conversion when calculating remaining buffer capacity. Status flag comparators can evaluate pointers directly in Gray code format.

Dual-Domain Gray Pointer Counter

Asynchronous FIFO controllers maintain both binary and Gray representations locally:

module gray_counter #(parameter WIDTH = 4) (
    input  wire             clk, rst_n, en,
    output reg [WIDTH-1:0]  gray_out
);
    reg [WIDTH-1:0] bin_count;

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            bin_count <= 0;
            gray_out  <= 0;
        end else if (en) begin
            bin_count <= bin_count + 1'b1;
            gray_out  <= (bin_count + 1'b1) ^ ((bin_count + 1'b1) >> 1);
        end
    end
endmodule

The register bin_count provides local memory addressing and increment logic. The output gray_out crosses safely into the asynchronous clock domain through multi-flop synchronizers.

Gray code does not eliminate the need for two-flop synchronizers. Gray code guarantees single-bit transitions, while two-flop synchronizers prevent flip-flop metastability. Robust clock domain crossing requires both mechanisms.

Verification Exercise

Calculate the Gray code representation for decimal 7 and decimal 0 in a 3-bit system. Confirm that wrapping around the sequence limit modifies exactly one bit.

View Solution

Decimal 7 binary equals 3'b111. Compute Gray code:

$$\text{Gray}(7) = 111_2 \oplus 011_2 = 100_2$$

Decimal 0 binary equals 3'b000. Compute Gray code:

$$\text{Gray}(0) = 000_2 \oplus 000_2 = 000_2$$

The wraparound transition from 100_2 to 000_2 modifies only the most-significant bit. The single-bit transition property holds across pointer wraparound boundaries.

Gray-coded pointers prevent race conditions during asynchronous clock sampling. The next article integrates Gray pointers into a full dual-clock asynchronous FIFO.

← The Synchronous FIFO and the Full/Empty Problem The Asynchronous FIFO →