Digital Design / Module 5: Counters and frequency dividers
28. Ring, Johnson and Gray counters
Pick the right count sequence.
See it move
The idea
Not every counter needs to count in binary. Different sequences trade flops against simplicity and how many bits change at each step.
| Type | States from N flops | Bits changing per step | Decoding |
|---|---|---|---|
| Binary | 2^N | Up to N | Needs gates |
| Ring | N | 2 | One flop per state |
| Johnson | 2N | 1 | Two inputs per state |
| Gray | 2^N | 1 | Needs gates |
Ring counter
A with its end fed back to its start, holding a single 1. It is by nature, so each flop directly marks a state. It suits sequencers, where each step enables one block.
always_ff @(posedge clk)
if (rst) q <= 4'b0001;
else q <= {q[2:0], q[3]};Reset must load exactly one 1. All zeros gets stuck at zero forever.
Johnson counter
The same chain, feeding back the inverted end. It fills with ones, then with zeros, giving twice the states with the same flops, and only one bit changes per step.
always_ff @(posedge clk)
if (rst) q <= 4'b0000;
else q <= {q[2:0], ~q[3]};A 4-bit Johnson counter has eight states: 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000.
Illegal cycles
A 4-bit Johnson counter uses 8 of the 16 possible values. The other 8 form a separate loop. If a bit upset lands the counter in that loop, it stays there, and ring counters have the same issue. Robust designs add logic to detect illegal values and recover.
Common mistake
Choosing a Gray counter because it has fewer glitches, without asking whether anything will ever sample it asynchronously. Gray's main benefit is safe crossing between . Inside a single domain, plain binary is usually simpler.
Key ideas
- N-flop ring has N states and is one-hot.
- N-flop Johnson has 2N states with one-bit changes.
- Gray has 2^N states with one-bit changes.
Your exercise
Find the bug
This code has one deliberate bug. Read the symptom, then click the line you think is wrong.
The fix
else q <= {q[2:0], ~q[3]};Why it failed
Feeding back q[3] unchanged makes a ring counter, and a ring counter holding all zeros stays there. A Johnson counter feeds back the inverted top bit, so it fills with ones and then with zeros, giving 2N states.
Interview questions
coreStates in a 4-bit Johnson?
Eight.
basicYou need a 10-state sequence. How many flops does a ring counter, a Johnson counter and a binary counter need?
A ring counter has one flop per state, so 10. A Johnson counter has 2N states from N flops, so 5. A binary counter needs $clog2(10) = 4. More flops buy simpler decoding: the ring needs no decode, the Johnson needs a 2-input gate per state, and binary needs a full compare.
basicList the states of a 4-bit Johnson counter that starts at 0000, and say how many bits change at each step.
0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, then back to 0000: eight states. It fills with ones, then with zeros, because the inverted top bit is fed back. Exactly one bit changes at every step, including the wrap.
coreIn the 4-bit Johnson sequence 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, which two bits uniquely identify the state 0011?
q[1] = 1 and q[2] = 0, so the decode is q[1] & ~q[2]. Of the eight legal states, only 0011 has a 1 in bit 1 and a 0 in bit 2. Every state can be decoded with one 2-input gate looking at the boundary between the ones and the zeros.
coreThis ring counter never produces any output pulses. What is wrong?
always @(posedge clk)
if (rst) q <= 4'b0000;
else q <= {q[2:0], q[3]};It is reset to all zeros, and rotating zeros only gives zeros, so it stays at 0000 forever. A ring counter must be reset with exactly one 1, for example 4'b0001, so that a single hot bit circulates. Reset is what gives it a legal state to start from.
coreWhy does it matter that only one bit changes per step when a counter value is read by logic in another clock domain?
The reader samples all the bits at an edge that has no fixed relationship to the counter's changes. If several bits change at once, some may be caught before the change and some after, giving a value that never existed. With one bit changing per step, the worst case is reading the old value or the new one, both of which are valid. That is why Gray coding is used for pointers that cross clock domains.
coreWhy might a designer choose a ring counter to sequence four steps rather than a 2-bit binary counter plus a decoder?
Each state is a flop output, so each step's enable comes straight from a flop with no decode logic, which means no decode delay and no glitches on the enables. The cost is more flops, four instead of two, which matters little for short sequences. It also makes the waveform easy to read, as one bit is high per step.
advancedA bit upset puts a 4-bit Johnson counter into the state 0010. What happens next?
q <= {q[2:0], ~q[3]};It does not return to the normal sequence. 0010 is one of the eight unused values, and they form their own loop: 0010, 0101, 1011, 0110, 1101, 1010, 0100, 1001, and back to 0010. The counter keeps cycling through illegal states, and all its decoded outputs are wrong until it is reset.
advancedHow would you make a Johnson counter recover by itself from an illegal state?
Add a check that the value is one of the legal states and, if it is not, load a legal value such as 0000 on the next clock. For the 4-bit form, a legal value has its bits change at most once from bit 3 down to bit 0, and that test can be built from a few gates. The cost is a little extra logic; the benefit is that a single upset causes a short glitch in the sequence instead of a permanent fault.