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_inin4-bit vectorBinary value to encode
gray_inin4-bit vectorGray-coded value to decode
gray_outout4-bit vectorGray encoding of bin_in
bin_outout4-bit vectorBinary decoding of gray_in

Behavior

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

What the bench checks

  • The testbench drives all 256 combinations of the two inputs

Constraints

TIMING

purely combinational: gray_out and bin_out 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.