Ring and Johnson Counters

Feed the end of a shift register back to its start. You get a ring with one hot bit, or a Johnson counter with twice the states.

You Will Be Able To

  • Build a ring counter and a Johnson counter from a shift register.
  • State how many states each has and how their outputs are decoded.
  • Find the illegal states of a shift-register counter.
  • Make a ring counter correct itself from any illegal state.
Lesson
G6.7
Time
25 min
Runs on
Your browser
Check
5 questions

A traffic controller needs four phases, one at a time, and each phase output must never glitch. A binary counter plus a decoder can glitch (G6.3). A counter whose bits are the phases cannot.

A Ring Counter and a Johnson Counter on One ClockFrom the lab: the value after each edge.
012345678clkring000100100100100000010010010010000001johnson000000010011011111111110110010000000
Scroll horizontally ↔
  1. Cycle 0

    After reset the ring holds one 1; the Johnson counter holds all 0s.

  2. Cycle 4

    The ring is back at 0001 after 4 edges. The Johnson counter has filled with 1s.

  3. Cycle 8

    The Johnson counter is back at 0000 after 8 edges: twice the states of the ring.

Measured: the values are the lines the lab prints.

The Ring Counter

Feed the last stage of a shift register back to the first, and a single 1 circulates. An N-bit ring counter has N states, one per bit, so its value is one-hot.

Each output is already a decoded state: bit k is 1 in state k only. No decoder is needed, so nothing can glitch. The cost is one flip-flop per state.

One Shift Register, Two FeedbacksFour flip-flops in a chain. The arrow from the last stage back to the first is the feedback.
  • Storage
  • Data
One shift register, two feedbacksFour flip-flops in a chain. The arrow from the last stage back to the first is the feedback. storage q0 stage 0 storage q1 stage 1 storage q2 stage 2 storage q3 stage 3 q3 or NOT q3
  1. storage q0stage 0
    • q1
  2. storage q1stage 1
    • q2
  3. storage q2stage 2
    • q3
  4. storage q3stage 3
    • q3 or NOT q3 → q0
  1. Ring

    q3 goes straight back to q0. A single 1 circulates: 4 states.

  2. Johnson

    NOT q3 goes back to q0. The chain fills with 1s, then with 0s: 8 states.

Read the diagram as text
  • q0: stage 0
  • q1: stage 1
  • q2: stage 2
  • q3: stage 3
  • q0 → q1
  • q1 → q2
  • q2 → q3
  • q3 → q0: q3 or NOT q3

Only the feedback differs: straight for a ring counter, inverted for a Johnson counter.

The Johnson Counter

Feed the last stage back inverted, and the register fills with 1s, then empties. An N-bit Johnson counter has 2N states. Exactly one bit changes at each edge.

Stateq3 q2 q1 q0Decode with
00000q3' q0'
10001q1' q0
20011q2' q1
30111q3' q2
41111q3 q0
51110q1 q0'
61100q2 q1'
71000q3 q2'

Every state is found by a 2-input AND of two neighbouring bits (or the two end bits). Only one input changes at a time, so the decode cannot glitch.

Illegal States

A 4-bit register has 16 values. The Johnson counter uses 8. The other 8 are illegal. If noise or a missing reset puts the counter in one, it follows a second loop and never comes back.

All 16 States of a 4-Bit Johnson Counter
All 16 States of a 4-Bit Johnson Counter01010101010101010101010101010101S00000000legalS00010001legalS00110011legalS01110111legalS11111111legalS11101110legalS11001100legalS10001000legalS00100010illegalS01010101illegalS10111011illegalS01100110illegalS11011101illegalS10101010illegalS01000100illegalS10011001illegal
  1. legal · S00000000
    • ↓0 → S0000
    • ↓1 → S0001
  2. legal · S00010001
    • ↓0 → S0001
    • ↓1 → S0011
  3. legal · S00110011
    • ↓0 → S0011
    • ↓1 → S0111
  4. legal · S01110111
    • ↓0 → S0111
    • ↓1 → S1111
  5. legal · S11111111
    • ↓0 → S1111
    • ↓1 → S1110
  6. legal · S11101110
    • ↓0 → S1110
    • ↓1 → S1100
  7. legal · S11001100
    • ↓0 → S1100
    • ↓1 → S1000
  8. legal · S10001000
    • ↓0 → S1000
    • ↓1 → S0000
  9. illegal · S00100010
    • ↓0 → S0010
    • ↓1 → S0101
  10. illegal · S01010101
    • ↓0 → S0101
    • ↓1 → S1011
  11. illegal · S10111011
    • ↓0 → S1011
    • ↓1 → S0110
  12. illegal · S01100110
    • ↓0 → S0110
    • ↓1 → S1101
  13. illegal · S11011101
    • ↓0 → S1101
    • ↓1 → S1010
  14. illegal · S10101010
    • ↓0 → S1010
    • ↓1 → S0100
  15. illegal · S01000100
    • ↓0 → S0100
    • ↓1 → S1001
  16. illegal · S10011001
    • ↓0 → S1001
    • ↓1 → S0010
StateInputNextOutput in state
S00000S00000000
S00001S00010000
S00010S00010001
S00011S00110001
S00110S00110011
S00111S01110011
S01110S01110111
S01111S11110111
S11110S11111111
S11111S11101111
S11100S11101110
S11101S11001110
S11000S11001100
S11001S10001100
S10000S10001000
S10001S00001000
S00100S00100010
S00101S01010010
S01010S01010101
S01011S10110101
S10110S10111011
S10111S01101011
S01100S01100110
S01101S11010110
S11010S11011101
S11011S10101101
S10100S10101010
S10101S01001010
S01000S01000100
S01001S10010100
S10010S10011001
S10011S00101001

Current state: S0010 · Output: 0010

Sampled input, state and output waveformStepState0S00101S01012S10113S01104S11015S10106S01007S10018S0010en—11111111q3001011010q2010110100q1101101001q0011010010
Scroll horizontally ↔
Sampled waveform: Moore output after each edge; Mealy output on the taken arc.
Step012345678
Input—11111111
StateS0010S0101S1011S0110S1101S1010S0100S1001S0010
Output001001011011011011011010010010010010

  1. Step 1. Start in the illegal state 0010. The next state is 0101, also illegal.
  2. Step 8. After eight edges it is back at 0010. It never reaches the legal loop.

Top loop: the 8 legal states. Bottom loop: the 8 illegal states. en = 1 is a clock edge; en = 0 holds.

Self-Correction

A self-correcting counter changes its feedback so that every illegal state leads back to the legal loop. Legal states move as before.

Plain ring

q <= {q[2:0], q[3]};

Any number of 1s keeps circulating. 0000 and 0110 stay illegal forever.

Self-correcting ring

q <= {q[2:0], ~|q[2:0]};

A 1 enters only when q2, q1 and q0 are all 0. Extra 1s shift out; the legal loop is unchanged.

In a legal state the only 1 reaches q3 exactly when q2 to q0 are 0. So the NOR gives the same bit as before. From an illegal state, extra 1s leave at the top and no new one enters until the low bits are clear.

The same idea works for a Johnson counter. Change the input of one stage so that illegal states fall into the legal loop. G6.7.1 asks you to find such a change.

Asked in interviews
  1. How many states does an N-bit ring counter, Johnson counter and binary counter have?

    N, 2N and 2^N. The ring and Johnson counters trade density for outputs that decode without glitches.

  2. What is a Johnson counter?

    A shift register whose last output is fed back inverted to its input. It fills with 1s and then empties, giving 2N states with one bit changing per edge.

  3. What happens if a ring counter starts in a wrong state?

    It keeps circulating the wrong pattern forever. Reset it to one 1, or use self-correcting feedback.

Try It

The ring visits four states and repeats after edge 4. The Johnson counter visits eight and repeats after edge 8.

In each Johnson step exactly one bit changes. In each ring step one 1 moves, so two bits change.

Change the ring's reset to 4'b0000 and run again: it stays at 0000. Press Synthesize: the Johnson counter is four flip-flops and one inverter.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: edge 0: ring=0001 johnson=0000 edge 1: ring=0010 johnson=0001 edge 2: ring=0100 johnson=0011 edge 3: ring=1000 johnson=0111 edge 4: ring=0001 johnson=1111 edge 5: ring=0010 johnson=1110 edge 6: ring=0100 johnson=1100 edge 7: ring=1000 johnson=1000 edge 8: ring=0001 johnson=0000

Original design · 5 cells · 4 flip-flops · 0 latches · 1 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
// tb.sv: both counters after a reset and eight edges (read-only).
`timescale 1ns/1ns
module tb;
  logic clk = 0, rst = 1;
  wire  [3:0] r, j;
  ring4    u1 (.clk(clk), .rst(rst), .q(r));
  johnson4 u2 (.clk(clk), .rst(rst), .q(j));
  initial begin
    #5 clk = 1; #5 clk = 0; rst = 0;
    $display("edge 0: ring=%b johnson=%b", r, j);
    for (int k = 1; k <= 8; k++) begin
      #5 clk = 1; #5 clk = 0;
      $display("edge %0d: ring=%b johnson=%b", k, r, j);
    end
  end
endmodule

Check Yourself

ConceptHow many states does a 6-bit Johnson counter visit?
ConceptWhy does decoding a Johnson counter state not glitch?
Predict the outputA 4-bit Johnson counter starts in the illegal state 0101. The program prints its state after three edges. Predict the exact line.
module tb;
  logic       clk = 0;
  logic [3:0] q = 4'b0101;
  always @(posedge clk) q <= {q[2:0], ~q[3]};
  initial begin
    repeat (3) begin #1 clk = 1; #1 clk = 0; end
    $display("q=%b", q);
  end
endmodule
ConceptA self-correcting 4-bit ring counter has the feedback NOR(q2, q1, q0). Where does the illegal state 0000 go at the next edge?
ConceptWhich counter gives 8 glitch-free decoded states with the fewest flip-flops?

Common Mistakes

Wrong Reset a ring counter to all zeros.

Rotating 0000 gives 0000 forever: a ring counter needs exactly one 1 to circulate.

Fix Reset it to one 1, such as 0001, or make it self-correcting.

Wrong Assume a Johnson counter can only be in its 2N legal states.

A 4-bit register has 16 states. The other 8 form their own loop, and a noise hit can put the counter there for good.

Fix Reset it, and where it matters, add logic that steers every illegal state back.

Wrong Expect 2^N states from an N-bit ring or Johnson counter.

A ring visits N states and a Johnson counter 2N. A 4-bit Johnson counter counts 8, not 16.

Fix Pick them for their decoding, not their density: use a binary counter when you need many states.

Remember

  • A ring counter circulates one 1: N states from N flip-flops, already decoded.
  • A Johnson counter feeds back the inverted last bit: 2N states, one bit changes per edge.
  • Johnson states decode with 2-input ANDs that cannot glitch.
  • Unused register values are illegal states; self-correcting feedback steers them back to the legal loop.

Practice

Report a problem with this page