RTL Problems / Level 4 · Sequential Logic
What You Will Learn
By solving Gray Code Counter — Gray Encoding, you will acquire core industry skills:
- Synthesizable hardware modeling in standard Verilog-2001 / SystemVerilog.
- Correct handling of interface port contracts, edge-triggered clocks, and active-low/high resets.
- Simulation waveform interpretation and debugging using timing diagrams (VCD traces).
- Architectural avoidance of timing violations, glitching hazards, and synthesis warnings.
Problem Statement & Requirements
Implement a 4-bit Gray code counter that advances by exactly one bit per transition: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100...
Hardware design teams at leading semiconductor firms evaluate these exact port contracts, race-condition assertions, and corner cases during technical design reviews and ASIC engineer interviews.
Prerequisites: Binary counters, XOR logic, CDC pointer synchronization.
Port List & Interface Timing
The hidden testbench instantiates top_module. Module port names and bit-widths must match the interface contract exactly:
| Name | Dir | Width | Description |
|---|---|---|---|
clk | input | 1 | Clock signal |
areset_n | input | 1 | Active-low asynchronous reset |
gray | output | 4 | 4-bit Gray code count value |
Timing Constraints & Synthesizable RTL Rules
Timing Contract: Transitions on `posedge clk`. Hamming distance between consecutive output words is strictly 1.
Synthesis Constraints: 4-bit output vector `gray[3:0]`. Modulo-16 cyclic sequence.
Architectural Deep Dive & Implementation Walkthrough
Common RTL Designer Pitfalls
['Converting to Gray code through unclocked combinational gates before crossing domains: logic glitches defeat the purpose of Gray coding.', 'Using non-power-of-2 modulo counts with Gray code without folding: rolling over from an arbitrary count back to 0 breaks the 1-bit Hamming distance guarantee.']
Silicon & Hardware Engineering Notes
Gray coding is mandatory for all asynchronous FIFO read/write pointer synchronizers across chips, networking ASICs, and GPUs.
Starter RTL Code
module top_module(
input clk,
input areset_n,
output reg [3:0] gray
);
// 4-bit Gray code counter
endmodule
Verified Reference Solution
🔒 Reveal Verified Reference RTL Solution
module top_module(
input clk,
input areset_n,
output reg [3:0] gray
);
reg [3:0] bin_cnt;
always @(posedge clk or negedge areset_n) begin
if (!areset_n) begin
bin_cnt <= 4'b0000;
gray <= 4'b0000;
end else begin
bin_cnt <= bin_cnt + 1'b1;
gray <= (bin_cnt + 1'b1) ^ ((bin_cnt + 1'b1) >> 1);
end
end
endmodule
Related RTL Problems in Level 4
- Two-Flop Synchronizer — Single-Bit CDC — Design a 2-flip-flop synchronizer to safely pass a single-bit control signal from an asynchronous source into the destination clock domain clk_dst.
- Pulse Synchronizer — Clock-Domain Crossing — Design a Toggle-based Pulse Synchronizer to transfer a single-cycle pulse from a fast clock domain (clk_src) to a slow destination domain (clk_dst).
- Asynchronous FIFO Pointer Synchronization — Multi-Clock Design — Convert a 4-bit write pointer to Gray code in clk_wr, synchronize it across clock domains into clk_rd using a 2-flop synchronizer, and generate a safe rd_empty flag.
Connected EcrioniX Learning Resources
Accelerate your VLSI engineering knowledge with these curated learning materials:
Progress through step-by-step verified modules from fundamentals to advanced silicon design.
Benchmark your hardware knowledge across 10 semiconductor domains and receive study roadmaps.
Explore foundational circuit theory, waveform timing, and physical hardware implementation.
Frequently Asked Questions
What does Gray Code Counter — Gray Encoding require?
Implement a 4-bit Gray code counter that advances by exactly one bit per transition: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100... Implement it as Verilog module top_module matching the listed ports.
Is this design combinational or sequential?
Tags: cdc, industry-rtl, arbiters, clock-domain-crossing, hard, gray-codes. If a clock signal is present in the port table, implement it as sequential logic; otherwise, use continuous assignments or combinational blocks.
How is my solution graded?
A hidden SystemVerilog testbench simulates your module in the cloud judge. You pass when all directed corner cases match the golden model and the VCD timing waveform matches the contract.
How does this challenge help with semiconductor interviews?
Technical interviewers at NVIDIA, Qualcomm, Apple, and Intel routinely ask candidates to code, debug, and explain the timing waveforms of Gray Code Counter — Gray Encoding to assess real-world synthesizability and race-condition awareness.