G1.7.1 Binary to Gray and Back

EasyImplementIcarus · browser samples / server checksReview the topic

Convert a 6-bit binary number to Gray code with XOR gates, and convert a Gray code word back with a running XOR.

Hardware context: The angle bus of a shaft encoder and the set-point bus of a display, both in Gray code. Convert 6-bit binary to Gray code and Gray code back to binary.

A shaft encoder reports its angle as a 6-bit Gray code. A reading taken while the shaft turns is then off by one step at most. The controller works in binary. It also sends set points back to a display that expects Gray code.

This is an Implement problem. Write gray_convert. The output g_out is the Gray code of b_in. The output b_out is the binary number whose Gray code is g_in. The two halves are independent.

Use the binary-reflected Gray code of the lesson, at six bits.

The check applies six pairs of inputs, one per nanosecond, and records both outputs after each.

Signals in the Timeline

SignalWidthRole
script72input of each check
Full specification
The module (keep the name and the ports)
module gray_convert (
  input  logic [5:0] b_in,    // a binary number
  input  logic [5:0] g_in,    // a Gray code word
  output logic [5:0] g_out,   // the Gray code of b_in
  output logic [5:0] b_out    // the binary number whose Gray code is g_in
);
  // your design
endmodule
OutputRule
g_outNeighbouring binary values give Gray words that differ in exactly one bit. g_out is 0 when b_in is 0.
b_outThe inverse of the encoder: encoding b_out gives g_in again.

The result fields are g_out_step0 and b_out_step0 for the first pair, up to g_out_step5 and b_out_step5. g_out is shown in binary and b_out in decimal.

Examples

Example 1

Counting up from 0
Input
b_in 0, g_in 000000; b_in 1, g_in 000001; b_in 2, g_in 000011; b_in 3, g_in 000010; b_in 4, g_in 000110; b_in 5, g_in 000111
Output
g_out_step0 = 'b000000, b_out_step0 = 0, g_out_step1 = 'b000001, b_out_step1 = 1, g_out_step2 = 'b000011, b_out_step2 = 2, g_out_step3 = 'b000010, b_out_step3 = 3, g_out_step4 = 'b000110, b_out_step4 = 4, g_out_step5 = 'b000111, b_out_step5 = 5

The binary values 0 to 5 give six Gray words that differ in one bit from step to step. On the decoder side, each g_in is the Gray code of the step number, so b_out counts 0 to 5.

Example 2

Single-Bit Binary Values
Input
b_in 1, g_in 000001; b_in 2, g_in 000010; b_in 4, g_in 000100; b_in 8, g_in 001000; b_in 16, g_in 010000; b_in 32, g_in 100000
Output
g_out_step0 = 'b000001, b_out_step0 = 1, g_out_step1 = 'b000011, b_out_step1 = 3, g_out_step2 = 'b000110, b_out_step2 = 7, g_out_step3 = 'b001100, b_out_step3 = 15, g_out_step4 = 'b011000, b_out_step4 = 31, g_out_step5 = 'b110000, b_out_step5 = 63

A binary value with one 1 bit gives a Gray word with two adjacent 1 bits, except at bit 5. A Gray word with one 1 bit decodes to a run of 1s from that bit down.

Example 3

Values with the Top Bit Set
Input
b_in 32, g_in 100000; b_in 33, g_in 100001; b_in 48, g_in 101000; b_in 63, g_in 111111; b_in 40, g_in 111100; b_in 62, g_in 100001
Output
g_out_step0 = 'b110000, b_out_step0 = 63, g_out_step1 = 'b110001, b_out_step1 = 62, g_out_step2 = 'b101000, b_out_step2 = 48, g_out_step3 = 'b100000, b_out_step3 = 42, g_out_step4 = 'b111100, b_out_step4 = 40, g_out_step5 = 'b100001, b_out_step5 = 62

The top Gray bit is the top binary bit, so every g_out here starts with 1. On the decoder side the top bit of b_out also copies straight across.

Constraints and Contract

  • Inputs are known 0/1 values. Each pair is held for one nanosecond and the outputs are sampled at its end.
  • Keep the module name and the ports. The design is combinational.

Hints

Hint 1

For the encoder, write one Gray bit at a time. Which binary bits does each one depend on? What happens at the top bit?

Hint 2

For the decoder, start at the top bit, which needs no work. Then go down one bit at a time.

Hint 3

A lower binary bit depends on every Gray bit above it, not just its neighbour. A loop or a chain of XOR gates carries that.

What the grader checks: Seven directed and eight seeded cases of six steps each run on the block. Both outputs are compared after every step with a model written in JavaScript.

15 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 1 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Counting up from 0

Input
b_in 0, g_in 000000; b_in 1, g_in 000001; b_in 2, g_in 000011; b_in 3, g_in 000010; b_in 4, g_in 000110; b_in 5, g_in 000111
Expected output
g_out_step0 = 'b000000, b_out_step0 = 0, g_out_step1 = 'b000001, b_out_step1 = 1, g_out_step2 = 'b000011, b_out_step2 = 2, g_out_step3 = 'b000010, b_out_step3 = 3, g_out_step4 = 'b000110, b_out_step4 = 4, g_out_step5 = 'b000111, b_out_step5 = 5

The binary values 0 to 5 give six Gray words that differ in one bit from step to step. On the decoder side, each g_in is the Gray code of the step number, so b_out counts 0 to 5.

Run samples executes all 3 public checks. Check all tests runs 15 checks on the server.

Icarus · browser samples / server checks