THE VLSI HOMEPAGE

A Practical guide to VLSI Design and Verification..

Gray code counters

Posted in Digital Design by Nigam on the September 17th, 2007

While designing modules with asynchronous clock transfers, one may encounter the problem of transferring multi-bit data bus from one clock domain to another. To dual synchronize these bits and hope that all the bits are latched on the same clock is problematic. To eliminate this problem, we use Gray code counters where only one bit changes during each clock transition.

The most common Gray code is where the lower half of the sequence is exactly the mirror image of first half with only the MSB inverted. We illustrate the 3-bit binary Gray code as an example.

Gray Counter

Gray code counter schematic (from Cliff Cumming's paper)

Gray code to equivalent binary conversion is simple and is as shown below

bin[2] = gray[2];

bin[1] = gray[2] ^ gray[1] (XOR function)

bin[0] = gray[2] ^ gray[1] ^ gray[0]

Verilog module is as below

 

CODE:
  1. module gray2binary_converter (binary, gray);
  2.  
  3.   parameter NUM_BITS = 3;
  4.   output [NUM_BITS-1:0] binary;
  5.   input [NUM_BITS-1:0] gray;
  6.  
  7.   reg [NUM_BITS-1:0] binary;
  8.   integer i;
  9.  
  10.   always @(gray) begin
  11.    for (i=0; i<NUM_bits; i=i+1)
  12.           binary[i] = ^(gray>> i); // Add padded 0's for the significant bits
  13.   end
  14.  
  15. endmodule

Similarly, the Binary to Gray conversion is achieved by

gray[2] = binary[3];

gray[1] = binary[2] ^ binary[1];

gray[0] = binary[0] ^ binary[1];

Verilog code is

CODE:
  1. module binary2gray_converter (gray, binary);
  2.  
  3.   parameter NUM_BITS = 3;
  4.   output [NUM_BITS-1:0] gray;
  5.   input [NUM_BITS-1:0] binary;
  6.  
  7.   assign gray = (binary>> 1) ^ binary; // Right shift binary vector and XOR
  8.  
  9. endmodule

The gray code counter can be implemented using these functions - please refer to Cliff Cumming's excellent paper on asynchronous clock domains.

Sphere: Related Content

Leave a Reply


Close
E-mail It