Practice

Gray Code Bridge

A parts audit flagged one chip on the palletizer, the machine that stacks finished boxes onto pallets. A converter links the encoder on the lift shaft to the motion controller, but it is no longer manufactured and no stock remains anywhere. The encoder reports its angle in Gray code: a binary numbering where neighboring positions differ in exactly 1 bit. That matters because the controller samples the lines on its own clock, at its own moments. The encoder drives the lines directly from a register, and the wires are matched so their signals arrive together. So even when the Gray lines are sampled during a change, their value is at most 1 step away. The conversion now moves into your FPGA.

While you are in there, the sensor vendor wants the return path too. The telemetry link that echoes position back to the diagnostics port expects the sensor's native Gray code.

Your task: build two independent lanes. Decode Gray to binary for the controller. Encode binary to Gray for telemetry. Keep the two strictly apart. The block sits in a servo sampling path, so it must be pure combinational logic, with no register to soak up a mistake.

Interface

PortDirectionTypeDescription
bin_in_iin4-bit vectorBinary value to encode
gray_in_iin4-bit vectorGray-coded value to decode
gray_out_oout4-bit vectorGray encoding of bin_in_i
bin_out_oout4-bit vectorBinary decoding of gray_in_i

Behavior

  • gray_out_o is the Gray encoding of bin_in_i: adjacent binary values must produce Gray codes that differ in exactly one bit
  • bin_out_o is the binary decoding of gray_in_i: for every value v, decoding the encoding of v returns v
  • The two paths are independent: gray_out_o depends only on bin_in_i, and bin_out_o depends only on gray_in_i

What the bench checks

  • The testbench drives all 256 combinations of the two inputs

Constraints

TIMING: purely combinational: gray_out_o and bin_out_o are functions of the inputs alone, with no clock and no state.

SCOPE: this models the two 4-bit code conversions and nothing else. The encoder, the controller and its sampling, and the telemetry link are out of scope.

INTERFACE: purely combinational: no clock, no registers, no latches.

Both outputs must be valid for all 16 input values in each direction.

Do not add ports.

Loading editor...

Click Run to execute your code. Output will appear here.