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
| Port | Direction | Type | Description |
|---|---|---|---|
bin_in | in | 4-bit vector | Binary value to encode |
gray_in | in | 4-bit vector | Gray-coded value to decode |
gray_out | out | 4-bit vector | Gray encoding of bin_in |
bin_out | out | 4-bit vector | Binary decoding of gray_in |
Behavior
gray_outis the Gray encoding ofbin_in: adjacent binary values must produce Gray codes that differ in exactly one bitbin_outis the binary decoding ofgray_in: for every valuev, decoding the encoding ofvreturnsv- The two paths are independent:
gray_outdepends only onbin_in, andbin_outdepends only ongray_in
What the bench checks
- The testbench drives all 256 combinations of the two inputs
Constraints
purely combinational: gray_out and bin_out are functions of the inputs alone, with no clock and no state.
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.
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.
Click Run to execute your code. Output will appear here.