Generic Gray Code Counter

Status
Not open for further replies.


Counters are not COMBINATIONAL circuits
 

I whipped up this synchronous Gray counter without using any intermediate counter or tedious 'case' statement. The 'bin' value is combinatorial. The number of bits is easily adjusted.
Code:
module top (clk, gray);
  parameter             bits = 8;
  input                 clk;
  reg        [bits-1:0] bin = 0;
  output reg [bits-1:0] gray = 0;

  integer i;
  always @ (gray)
    for (i = 0; i < bits; i = i + 1)
      bin[i] <= ^(gray >> i);

  always @ (posedge clk)
    gray <= ((bin + 1'd1) >> 1) ^ (bin + 1'd1);  // to count down, change both + to -
endmodule
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…