VLSI CDC · FIFO Pointers · RTL Generator

Gray Code Converter & Visualizer

Interactive multi-bit Binary ↔ Gray Code converter, live bitwise XOR transformation solver, Clock Domain Crossing (CDC) multi-bit skew race simulator, and synthesizable Verilog RTL generator for asynchronous FIFOs.

Interactive Converter

4 Bits · 16 States
Word Width:
Toggle Bits Directly Hex: 0x7
Standard Binary
0111
Weights: [2³ 2² 2¹ 2⁰]
Reflected Gray Code
0100
G = B ^ (B >> 1)
✓
Hamming Distance = 1 Guaranteed: Transition from previous state (0101) flips exactly Bit 0.
Step-by-Step Bitwise XOR Derivation

VLSI Presets & Corner Cases

Design Patterns

Load classic semiconductor corner cases to inspect multi-bit toggling hazards and single-bit Gray transitions.

Verilog One-Liner Formulas
// Binary to Gray:
assign gray = bin ^ (bin >> 1);

// Gray to Binary (Bitwise Loop):
for (i = 0; i < WIDTH; i = i + 1)
  bin[i] = ^(gray >> i);

Clock Domain Crossing (CDC) Multi-Bit Race & Glitch Simulator

Simulate what happens when asynchronous clock domains sample a multi-bit counter during a transition with non-zero physical routing skews. See why binary counters cause catastrophic FIFO pointer corruption, while Gray code guarantees single-bit safety.

Binary Counter Domain (Unsafe!)
Sampled: 0010 (2)
CORRUPT GHOST STATE

Because 4 bits toggled simultaneously across physical routing wires with different RC delays, the destination clock sampled a false transient value (0010 = 2). This corrupts FIFO read/write pointers!

Gray Code Domain (Glitch-Free Safety)
Sampled: 0100 (7)
VALID LEGAL STATE

Because only ONE bit toggled (Bit 3), there is zero multi-bit wire skew. The destination clock samples either the old state (7) or the new state (8) — NEVER a phantom state!

Full Truth Table & Hamming Distance Table

Exhaustive enumeration for the current bit width. Click any row to load into the simulator.

Dec Hex Binary Representation Reflected Gray Code Bit Changed Hamming Dist Action

Synthesizable Verilog & SystemVerilog RTL Generator

Production-grade, parameterizable RTL templates for Binary-to-Gray converters, Gray-to-Binary decoders, and Asynchronous FIFO Gray counters.

bin2gray.v
// Verilog source will render here

Engineering Theory: Gray Code in Modern VLSI & ASIC Design

In-depth physical and architectural principles behind single-bit transitions, CDC synchronization, and FIFO pointers.

1. Frank Gray's Reflected Binary Theorem

Introduced by Bell Labs researcher Frank Gray in his 1953 patent, a reflected binary code organizes $N$-bit numbers such that every consecutive codeword differs in exactly one bit position ($\text{Hamming distance} \equiv 1$). The sequence is formed recursively by taking the $(N-1)$-bit sequence, reflecting it backwards, and prefixing '0' to the first half and '1' to the second half.

2. The Multi-Bit CDC Synchronization Hazard

When multi-bit buses cross asynchronous clock boundaries, standard 2-flip-flop synchronizers cannot be used directly on binary signals. Due to physical place-and-route delays, wire length skews, and clock arrival differences, each bit transitions at a slightly different picosecond. If the capture clock edge lands during this window, the synchronizer latches an illegal transient state. Gray coding guarantees that only a single bit toggles, eliminating intermediate states entirely.

3. FIFO Pointer Wrap-Around & Full/Empty Logic

In an async FIFO with depth $2^N$, pointers use an $(N+1)$-bit Gray code. The extra MSB disambiguates whether write and read pointers are in the same or opposite FIFO buffer laps:
• FIFO Empty: wptr_gray == rptr_gray (identical pointers).
• FIFO Full: wptr[N] != rptr[N] && wptr[N-1] != rptr[N-1] && wptr[N-2:0] == rptr[N-2:0] (the 2 MSBs invert while lower bits match).

4. Non-Power-of-2 FIFO Depths

Standard Gray code sequences naturally loop with Hamming distance 1 only when total depth is an exact power of 2 ($2^N$). If a non-power-of-2 depth (e.g., 24 entries) is needed, designers must symmetrically truncate the Gray code sequence around the center reflection axis to preserve the single-bit transition property when the pointer wraps from max back to 0.

5. Hardware Cost: Binary vs Gray

Binary-to-Gray conversion costs only $(N-1)$ parallel XOR gates with a logic depth of 1 gate ($\mathcal{O}(1)$ latency). Conversely, Gray-to-Binary conversion requires a cascaded XOR chain with a critical path delay of $(N-1)$ XOR gates ($\mathcal{O}(N)$ latency) or a parallel-prefix tree ($\mathcal{O}(\log N)$ latency). In high-speed ASIC flows, Gray counters increment in binary internally and register the Gray code output to prevent combinational glitches.

6. Rotary Optical Encoders

Beyond silicon CDC, Gray code tracks on mechanical and optical rotary encoder discs prevent sensor reading glitches. With physical contact imperfections, mechanical vibrations cannot produce wild jump errors because physical movement only ever toggles one track sensor at a time.

Frequently Asked Questions (VLSI Interview Prep)

Why can't we use a 2-FF synchronizer on a multi-bit binary bus? ▼
A multi-bit binary bus can toggle several bits simultaneously (e.g. from 0111 to 1000, four bits flip). Physical routing wires have different RC time constants and buffer delays. If the receiving clock captures the bus while some bits have switched and others have not, the synchronizer will latch a garbage intermediate state (like 0010 or 1111) that neither exists in the sender nor makes physical sense. Gray code guarantees only 1 bit flips, so the receiver captures either the previous legal value or the new legal value, never garbage.
How does Gray-to-Binary conversion work mathematically? ▼
The MSB remains unchanged: B[N-1] = G[N-1]. Every subsequent binary bit is obtained by XORing the previously computed binary bit with the current Gray bit: B[i] = B[i+1] ^ G[i]. Alternatively, each binary bit B[i] is the odd parity (reduction XOR) of all Gray bits from index i up to N-1: B[i] = ^(G >> i).
Why are the two MSBs inverted in the Async FIFO Full condition? ▼
In an (N+1)-bit Gray code pointer, the upper half (when MSB=1) is a mirror reflection of the lower half (when MSB=0). Because of this reflection, when the write pointer is one full lap ahead of the read pointer, bit N is inverted (showing lap difference) and bit N-1 must also be inverted to match the reflected position in the second lap. All remaining lower bits [N-2:0] are identical.
What is the difference between a Gray counter and a Binary-to-Gray registered counter? ▼
A purely combinational Gray counter computes next Gray code from current Gray code, which requires complex Karnaugh map logic as bit width grows. The industry-standard approach is a registered binary-to-gray counter: keep an internal synchronous binary counter (trivial incrementer bin_next = bin + 1), compute gray_next = bin_next ^ (bin_next >> 1), and register both values into flip-flops on the clock edge. This guarantees glitch-free registered outputs before sending across the CDC boundary.