Binary to Gray Code Datapath
Crossing clock domains safely requires multi-bit buses to transition exactly one bit at a time to prevent intermediate sampling errors. Binary to Gray code converters are the standard mechanism for preparing pointers or counters before they cross asynchronous boundaries.
The module takes an $N$-bit binary input and computes the corresponding $N$-bit Gray code output. The most significant bit (MSB) of the Gray code is identical to the MSB of the binary input. Each subsequent bit is the XOR of the current binary bit and the next higher binary bit. This ensures that any two consecutive numerical values will differ by only a single bit in their Gray code representation.
This is a purely combinational circuit. There is no clock or reset, and the output reflects the input immediately.
| Signal | Direction | Width | Description | |--------|-----------|-------|-------------| | bin | input | WIDTH | Binary input value | | gray | output | WIDTH | Gray code output value |
Constraints
- The module must be purely combinational.
- Output
graymust reflect changes inbinimmediately with 0-delay. - The design must support a parameterised
WIDTH, defaulting to 4. - The testbench will instantiate the module with
WIDTHoverridden to 8.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against a real testbench — you get the waveform back, not a stored answer.
Free to solve. A Codiode account keeps your progress.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.