G6.7.1 Johnson Counter with Self-Correction

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Make a 5-bit Johnson counter correct itself, for example by changing one stage, from all 22 illegal states.

Hardware context: A motor driver that steps through ten phases with a 5-bit Johnson counter and no reset. Make a 5-bit Johnson counter correct itself from all 22 illegal states.

A motor driver steps through ten phases with a 5-bit Johnson counter. It has no reset, and a power glitch can leave it in any of the 32 register values. In 22 of them the plain counter loops forever and the motor stalls.

This is an Implement problem. Change johnson5 so that every illegal state joins the legal loop within 10 edges. Every legal state must still go to its usual next state.

Signals in the Timeline

SignalWidthRole
start5input of each check
Full specification
The module (keep the name and the port)
module johnson5 (
  input  logic       clk,
  output logic [4:0] q      // the state itself: q is the register, written in always_ff
);
  ...
endmodule
Legal loop (q4 ... q0)
00000 → 00001 → 00011 → 00111 → 01111 → 11111 → 11110 → 11100 → 11000 → 10000→ back to 00000

The bench loads a start state straight into q between edges, so q must be the register itself. Each case is one start state. A legal start must follow the loop exactly. An illegal start must reach the loop within 10 edges and then follow it.

Examples

Example 1

Start at 00000
Input
start state 00000
Output
q_after_edge1 = 'b00001, q_after_edge2 = 'b00011, q_after_edge3 = 'b00111, q_after_edge4 = 'b01111, q_after_edge5 = 'b11111, q_after_edge6 = 'b11110, q_after_edge7 = 'b11100, q_after_edge8 = 'b11000, q_after_edge9 = 'b10000, q_after_edge10 = 'b00000, q_after_edge11 = 'b00001, q_after_edge12 = 'b00011, reached_the_loop_within_10_edges = yes, then_followed_the_loop = yes

A legal start: the counter must fill with 1s, 00001 to 11111, then empty, exactly as a plain Johnson counter.

Example 2

Start at 01111
Input
start state 01111
Output
q_after_edge1 = 'b11111, q_after_edge2 = 'b11110, q_after_edge3 = 'b11100, q_after_edge4 = 'b11000, q_after_edge5 = 'b10000, q_after_edge6 = 'b00000, q_after_edge7 = 'b00001, q_after_edge8 = 'b00011, q_after_edge9 = 'b00111, q_after_edge10 = 'b01111, q_after_edge11 = 'b11111, q_after_edge12 = 'b11110, reached_the_loop_within_10_edges = yes, then_followed_the_loop = yes

Another legal start, entered in the middle of the loop.

Example 3

Start at 00101
Input
start state 00101 (illegal)
Output
q_after_edge1 = 'b00000, q_after_edge2 = 'b00000, q_after_edge3 = 'b00000, q_after_edge4 = 'b00000, q_after_edge5 = 'b00000, q_after_edge6 = 'b00000, q_after_edge7 = 'b00000, q_after_edge8 = 'b00000, q_after_edge9 = 'b00000, q_after_edge10 = 'b00000, q_after_edge11 = 'b00000, q_after_edge12 = 'b00000, reached_the_loop_within_10_edges = yes, then_followed_the_loop = yes

An illegal start: two separate 1s. The plain counter keeps it out of the loop forever. Yours must reach a legal state within 10 edges.

Constraints and Contract

  • Keep the module name and the port. Assign q in one always_ff block. The design must synthesize with no latches.

Hints

Hint 1

In a legal state the bits form one block of 1s and one block of 0s. Any three neighbouring bits are 000, 001, 011, 111, 110 or 100.

Hint 2

Try changing the input of one middle stage. In a legal state it must still copy its lower neighbour; elsewhere it may differ.

Hint 3

Look for a function of three neighbouring bits that equals the middle bit whenever they are not 010 or 101. Then check every illegal state.

What the grader checks: All 32 start states, loaded straight into q. A legal start is compared with the loop after each of 12 edges. An illegal start must reach the loop within 10 edges and then take the next legal state at every later edge. Synthesis must report 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.

32 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

Start at 00000

Input
start state 00000
Expected output
q_after_edge1 = 'b00001, q_after_edge2 = 'b00011, q_after_edge3 = 'b00111, q_after_edge4 = 'b01111, q_after_edge5 = 'b11111, q_after_edge6 = 'b11110, q_after_edge7 = 'b11100, q_after_edge8 = 'b11000, q_after_edge9 = 'b10000, q_after_edge10 = 'b00000, q_after_edge11 = 'b00001, q_after_edge12 = 'b00011, reached_the_loop_within_10_edges = yes, then_followed_the_loop = yes

A legal start: the counter must fill with 1s, 00001 to 11111, then empty, exactly as a plain Johnson counter.

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

Icarus Verilog + Yosys · server