Gray code
Karnaugh maps
Create functions to encode a number to and decode a number from Gray code.

Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).

There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."

Encoding (MSB is bit 0, b is binary, g is Gray code):

if b[i-1] = 1
   g[i] = not b[i]
else
   g[i] = b[i]
Or:

g = b xor (b logically right shifted 1 time)
Decoding (MSB is bit 0, b is binary, g is Gray code):

b[0] = g[0]

for other bits:
b[i] = g[i] xor b[i-1]
Reference
 Converting Between Gray and Binary Codes. It includes step-by-step animations.
 
`timescale 1ns/10ps
`default_nettype wire
 
module graytestbench;
 
localparam aw = 8;
 
function [aw:0] binn_to_gray;
    input  [aw:0] binn;
begin :b2g
    binn_to_gray = binn ^ (binn >> 1); 
end
endfunction
 
function [aw:0] gray_to_binn;
    input [aw:0] gray;
begin :g2b
    reg   [aw:0] binn;
    integer      i;
 
    for(i=0; i <= aw; i = i+1) begin
     binn[i] = ^(gray >> i);
    end
    gray_to_binn = binn;
end
endfunction
 
initial begin :test_graycode
    integer   ii;
    reg[aw:0] gray;
    reg[aw:0] binn;
 
    for(ii=0; ii < 10; ii=ii+1) begin
        gray = binn_to_gray(ii[aw:0]);
        binn = gray_to_binn(gray);    
 
        $display("test_graycode: i:%x gray:%x:%b binn:%x", ii[aw:0], gray, gray, binn);
    end
 
    $stop;
end
 
endmodule
 
`default_nettype none
 
 