G6.8.1 Gray Counter Without a Binary Register

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Build an N-bit Gray-code counter whose only register is the Gray value itself, for any width from 2 to 16.

Hardware context: The write pointer of an asynchronous FIFO, sent to the read clock domain in Gray code. Build a Gray-code counter of any width whose only register is the Gray value.

An asynchronous FIFO sends its write pointer to the read clock domain in Gray code. The pointer must come straight from flip-flops, and the area budget allows N of them, not 2N.

This is an Implement problem. The starter counts in binary and converts the count, so its output passes through XOR gates. Rewrite gray_counter so that g is the register and the next value is worked out from g.

Signals in the Timeline

SignalWidthRole
width_n5input of each check
loaded1input of each check
start16input of each check
Full specification
The module (keep the name, the parameter and the ports)
module gray_counter #(parameter int N = 4) (
  input  logic         clk,
  input  logic         rst,   // synchronous reset, active high: g becomes 0 (beats en)
  input  logic         en,    // 1: count one step at the edge; 0: hold
  output logic [N-1:0] g      // the count in Gray code: g is the register itself
);
  ...
endmodule
RuleRequired
CountAt a rising edge with en = 1, g takes the next value of the N-bit Gray sequence. After the top value 10...0 it wraps to 0.
HoldWith en = 0, g keeps its value.
ResetWith rst = 1, g becomes 0 at the edge, whatever en is.
Registerg is the state: a value loaded straight into g between edges is counted on from at the next edge.

The check builds counters with N = 2, 3, 4, 5, 8, 11 and 16. A load case forces a value onto g between edges and releases it, then reads g after each edge. A cycle case counts through all 2^N values from reset.

Examples

Example 1

Count from Reset, 4 Bits
Input
N = 4, from reset, edges count count count count count count count count count count count count
Output
g_after_edge1 = 'b0001, g_after_edge2 = 'b0011, g_after_edge3 = 'b0010, g_after_edge4 = 'b0110, g_after_edge5 = 'b0111, g_after_edge6 = 'b0101, g_after_edge7 = 'b0100, g_after_edge8 = 'b1100, g_after_edge9 = 'b1101, g_after_edge10 = 'b1111, g_after_edge11 = 'b1110, g_after_edge12 = 'b1010

Twelve counting edges from 0000. Each edge changes exactly one bit of g.

Example 2

Load 1010 and Count Through the Wrap
Input
N = 4, load g = 1010, edges count count count count hold hold count count
Output
g_after_edge1 = 'b1011, g_after_edge2 = 'b1001, g_after_edge3 = 'b1000, g_after_edge4 = 'b0000, g_after_edge5 = 'b0000, g_after_edge6 = 'b0000, g_after_edge7 = 'b0001, g_after_edge8 = 'b0011

The check loads 1010 straight into g. Your counter must go on from there: 1011, 1001, 1000, then 0000. A counter that keeps its count elsewhere carries on from its own value.

Example 3

A Full Cycle, 3 Bits
Input
N = 3, 8 counting edges from reset
Output
values_that_were_not_the_gray_count = 0, edges_that_changed_exactly_one_bit = 8, edges_until_g_was_0_again = 8

Eight edges from reset. Every value must be the next Gray code and every edge must change one bit. g is 0 again after exactly 8 edges.

Constraints and Contract

  • Keep the module name, the parameter and the ports. Assign g in one always_ff block, so that every bit of g comes straight from a flip-flop. The design must synthesize with no latches.

Hints

Hint 1

g holds everything the counter knows. So the next value must be a function of g alone.

Hint 2

One way: undo the Gray code inside the next-state logic, step, and redo it. The binary value then lives in wires, not in flip-flops.

Hint 3

Another way is the flip rule from the lesson. Either way, try the top value 10...0 for N = 2 and N = 16.

What the grader checks: Widths 2, 3, 4, 5, 8, 11 and 16. Full cycles for N = 2, 3, 5 and 8. Loads at the top value, at 010...0 and at random values, with holds and resets. Synthesis (N = 4) must show g straight from flip-flops and no latches.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

26 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 1 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Count from Reset, 4 Bits

Input
N = 4, from reset, edges count count count count count count count count count count count count
Expected output
g_after_edge1 = 'b0001, g_after_edge2 = 'b0011, g_after_edge3 = 'b0010, g_after_edge4 = 'b0110, g_after_edge5 = 'b0111, g_after_edge6 = 'b0101, g_after_edge7 = 'b0100, g_after_edge8 = 'b1100, g_after_edge9 = 'b1101, g_after_edge10 = 'b1111, g_after_edge11 = 'b1110, g_after_edge12 = 'b1010

Twelve counting edges from 0000. Each edge changes exactly one bit of g.

Run samples executes all 3 public checks. Check all tests runs 26 checks on the server.

Icarus Verilog + Yosys · server