CodiodeCodiode
Home
Problem Solving
Skill Tracks
My Assignments
Contests
Leaderboard
Community
Settings
Codiode/Problems/Arithmetic

Binary to Gray Code Datapath

EasyVerilog / SystemVerilogBuildFree2 solved

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 gray must reflect changes in bin immediately with 0-delay.
  • The design must support a parameterised WIDTH, defaulting to 4.
  • The testbench will instantiate the module with WIDTH overridden to 8.

Topics

CDCCombinational LogicDatapath

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.

Sign in to solve

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.

Related problems

  • Parameterized ALU with Safe DefaultsMedium
  • Fixed Point Multiplication with SaturationHard
  • 16-bit Arithmetic Barrel ShifterHard
  • 8 bit Basic ALU with FlagsEasy
  • 8-bit Combinational PopcountMedium
  • 8-bit Logical Barrel ShifterMedium
  • Absolute Value with Bit GrowthMedium
  • Count Leading Sign BitsHard

Browse all problems · Learning tracks