Codiode

Parameterized Gray Code Counter

MediumVerilog / SystemVerilogBuild

Async FIFO pointers cross clock domains, so only one bit may change per increment. A Gray code counter guarantees that.

This module implements a parameterizable counter that natively outputs Gray code. It increments when enabled. To achieve this safely, it maintains an internal binary count, converts the next binary value to Gray code, and registers the Gray code output to guarantee glitch-free transitions on the crossing paths.

The circuit operates on the positive edge of clk. It uses an asynchronous, active-low reset rst_n. On reset, both the internal binary state and the gray_out register must be cleared to 0. When en is high, the counter increments. When en is low, the output holds its current value.

Cycle 1: rst_n=0 → internal_bin=0, gray_out=0
Cycle 2: rst_n=1, en=1 → internal_bin=1, gray_out=1
Cycle 3: en=1 → internal_bin=2, gray_out=3
Cycle 4: en=0 → internal_bin=2, gray_out=3 (hold)
Cycle 5: en=1 → internal_bin=3, gray_out=2
Cycle 6: en=1 → internal_bin=4, gray_out=6

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; clears internal state and output to 0
en input 1 Enable signal; increments counter on posedge clk when high
gray_out output WIDTH Registered Gray code output; resets to 0

Constraints

  • Module must be parameterized with WIDTH defaulting to 4.
  • The gray_out output must be registered directly (driven by a flip-flop, not combinational logic) to prevent glitches from propagating across clock domains.
  • Internal binary counter wraps around naturally when it reaches its maximum value.
  • Clock edge is posedge clk and reset is asynchronous negedge rst_n.
  • Reset takes priority over enable when both are asserted simultaneously.

Topics

gray-codecounterscdcparameterizable

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Clock Domain Crossing and Synchronization

Problem 16 of 36 in the Clock Domain Crossing and Synchronization track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.