**4-bit Binary to Gray Code Converter:** Why is Gray code extensively use…
**4-bit Binary to Gray Code Converter:** Why is Gray code extensively used across asynchronous clock domains and FIFO pointers? Explain the algorithm to convert an $N$-bit binary number to Gray code, and write synthesizable Verilog RTL code and its testbench.
Model answer
### 1. Working Principle & CDC Relevance Gray code is a **unit-distance code** where only a single bit toggles between successive values. In **Asynchronous FIFO write/read pointers**, multi-bit counter transitions across clock domain boundaries would risk metastability and multi-bit skew without Gray encoding. **Conversion Algorithm:** - The Most Significant Bit (MSB) remains identical: $G_3 = B_3$ - Each subsequent Gray bit is the XOR of adjacent binary bits: $G_i = B_{i+1} \oplus B_i$ {rl} G_3 &= B_3 \\ G_2 &= B_3 \oplus B_2 \\ G_1 &= B_2 \oplus B_1 \\ G_0 &= B_1 \oplus B_0 ### 2. Verilog RTL Implementation ### 3. Verilog Testbench