Hardware interview practice
Write an eight-bit binary-to-Gray converter
An asynchronous-FIFO pointer helper needs reflected Gray code with zero latency. Write the combinational converter.
Starting point
Question code
module bin_to_gray8(
input logic [7:0] bin, output logic [7:0] gray
);Reviewed example
Work through one case
Input
Case 1: bin=8'h00
Case 2: bin=8'h0B
Case 3: bin=8'hFFExpected output
Case 1: Produces gray=8'h00.
Case 2: Produces gray=8'h0E.
Case 3: Produces gray=8'h80.The shown result follows by applying this rule: A single XOR of the input with its one-bit logical right shift implements the converter. The cases also demonstrate this requirement: For consecutive two-state binary counts, the two Gray outputs must differ in exactly one bit, including 8'hFF to 8'h00 wraparound.
What to cover
Requirements
- Implement reflected Gray conversion as gray = bin ^ (bin >> 1).
- Use purely combinational logic with no clock, reset, latch, or retained state.
- Preserve all eight bits, including the most-significant bit and leading zeros.
- For consecutive two-state binary counts, the two Gray outputs must differ in exactly one bit, including 8'hFF to 8'h00 wraparound.
