Volume 08 Intermediate 6 sub-modules ~15 min read

Registers, Counters and Shift Registers

A single flip-flop stores one bit. This volume puts them together: registers that store words, shift registers that move bits along, and counters of every kind. It simulates a ripple counter and a synchronous counter gate by gate to show exactly how they differ, then designs a counter for any sequence - and catches the lock-up that the obvious design hides.

You will learn
  • How a register stores a word, and how a load enable works
  • How shift registers move data between serial and parallel form
  • Ring and Johnson counters, and the danger of unused states
  • Why ripple counters are slow and pass through wrong values, and how synchronous counters fix it
  • How to design a counter for any sequence, and how to check it for lock-up
You need
  • Volume 07 of this course (D, JK and T flip-flops and their excitation tables) and the K-maps of Volume 04.

8.1 Registers

A register is a row of D flip-flops sharing one clock: it stores a whole word, and loads a new one at each clock edge. A load-enable input lets it keep its value instead.

One flip-flop stores one bit. Put four side by side, give them the same clock, and they store a 4-bit number - a 4-bit register. At each rising edge, all four capture their D inputs at the same instant.

Loading only when asked

Usually a register should load only sometimes, and hold its value the rest of the time. Put a 2-to-1 multiplexer in front of each flip-flop, choosing between the new data Din and the flip-flop's own output Q. The select input is called LD, for load:


LD·Din + LD'·Q is a 2-to-1 multiplexer choosing Din or Q: True

When LD is 0 the flip-flop reloads its own value, so it holds. When LD is 1 it takes Din. Here is a 4-bit register over five clock edges:

Clock edge LD Din Q before Q after
1 1 1011 0000 1011
2 0 0110 1011 1011
3 0 1111 1011 1011
4 1 0110 1011 0110
5 0 0000 0110 0110

The data on Din changes all the time, but the register only takes it on the edges where LD is 1. The registers inside a processor work exactly like this.

Quick check

A 4-bit register with load enable holds 1011. On the next clock edge LD = 0 and Din = 0110. What does it hold after the edge?

Show the answer

Answer: B. With LD at 0 the multiplexers feed each flip-flop its own output, so the register holds 1011. Din is ignored.

8.2 Shift registers

A shift register passes its bits along one place at every clock edge. It can turn a serial stream - one bit at a time on one wire - into a parallel word, or a word back into a stream.

Chain D flip-flops so that each one's Q drives the next one's D, and give them all the same clock. At every edge, each bit moves one place along, and a new bit enters at the start:

A 4-bit shift register: four D flip-flops in a chain on one clock CLK serial in Q3 Q2 Q1 Q0 D FF CLK D Q D FF CLK D Q D FF CLK D Q D FF CLK D Q
Figure 8.1 - Each flip-flop's output feeds the next one's input. At every rising edge of CLK the bits all move one place to the right, and the serial input enters Q3. The four outputs can be read together as a word.

Send the bits of 1101 in, lowest first:

Clock edge Serial in Q3 Q2 Q1 Q0
0 - 0000
1 1 1000
2 0 0100
3 1 1010
4 1 1101

After four edges the whole word sits in the register, ready to be read at once:


after 4 clocks the whole word is in: 4 bits need 4 clocks in, and 4 more to shift out

That is serial-in, parallel-out. Other shift registers load a word in parallel and shift it out one bit at a time, which is how a serial port sends a byte down a single wire.

Quick check

How many clock edges does it take to shift 8 bits into an empty 8-bit shift register?

Show the answer

Answer: D. A shift register moves one bit per clock edge, so 8 bits need 8 edges.

8.3 Ring and Johnson counters

Feed a shift register's last output back to its input and it counts round a loop of states. A ring counter circulates a single 1. A Johnson counter feeds back the inverse, and gets twice as many states from the same flip-flops.

The ring counter

Load 1000 into a 4-bit shift register and feed Q0 back into Q3. The 1 goes round and round:


ring: 1000 -> 0100 -> 0010 -> 0001 -> 1000

Only one output is 1 at a time, so each output can directly drive one step of a repeating sequence - no decoder needed.

The Johnson counter

Feed back Q0 inverted instead, and the register fills up with 1s, then empties:


Johnson: 0000 -> 1000 -> 1100 -> 1110 -> 1111 -> 0111 -> 0011 -> 0001 -> 0000

with 4 flip-flops: ring counter 4 states, Johnson 8, binary 16

Unused states and lock-up

Four flip-flops have 16 states, and these counters use only some of them. If a counter ever lands in an unused state - at power-up, or after electrical noise - it may never get back:


a ring counter that starts at 0000 stays at 0000 for ever
a Johnson counter started in an unused state: 0101 -> 0010 -> 1001 -> 0100 -> 1010

The ring counter stuck at 0000 has no 1 to pass round. The Johnson counter circles through a second loop of unused states for ever. This trap is called lock-up, and sub-module 8.6 shows how to design it out.

Quick check

How many different states does a 5-bit Johnson counter go through?

Show the answer

Answer: C. A Johnson counter with n flip-flops has 2n states: it fills with n ones, then empties again. Five flip-flops give 10 states.

8.4 Ripple counters

In a ripple counter, each flip-flop is clocked by the one before. It needs no gates at all, but the change ripples through the stages one after another, and on the way the outputs pass through wrong values.

A T flip-flop with T = 1 halves its clock (Volume 07). Chain four of them, each clocked by the inverted output of the one before. Then bit 1 toggles every time bit 0 falls, bit 2 every time bit 1 falls, and so on. That is exactly binary counting:

A 4-bit ripple counter: four T flip-flops, each clocked by the inverted output of the one before CLK 1 1 1 1 Q0 Q1 Q2 Q3 T FF CLK T Qn Q T FF CLK T Qn Q T FF CLK T Qn Q T FF CLK T Qn Q
Figure 8.2 - Every T input is tied to 1, so every flip-flop toggles on its clock. Only the first one sees the real clock; each of the others is clocked when the stage before it falls from 1 to 0.

Watching it ripple

The simulator built this counter from the gate-level flip-flops of Volume 07 and ran it for 18 clock cycles. Here is every value the outputs passed through:


values the outputs pass through: 0 1 0 2 3 2 0 4 5 4 6 7 6 4 0 8 9 8 10 11 10 8 12 13 12 14 15 14 12 8 0 1 0 2

It does count 0, 1, 2, 3 and on up to 15 - but between the counts it visits wrong values. Going from 7 to 8, the change ripples through every stage:


after the edge at step 300 (from 7 to 8): 7 -> 6 -> 4 -> 0 -> 8
after that edge Q0, Q1, Q2, Q3 settle 3, 5, 7, 8 steps later
The ripple counter changing from 7 to 8: each bit changes two steps after the one before CLK Q0 Q1 Q2 Q3
Figure 8.3 - Q0 falls first, which clocks Q1, which falls and clocks Q2, and so on. For several steps the outputs read 6, then 4, then 0, before they finally reach 8.

The delay grows with every stage, so a long ripple counter can only run slowly. And anything that reads its outputs without waiting may see 6, 4 or 0 on the way from 7 to 8.

Quick check

Why does a ripple counter pass through wrong values?

Show the answer

Answer: A. Each flip-flop is clocked by the stage before, so it can only change after that stage has changed. The bits change one at a time, and the outputs spell out wrong numbers in between.

8.5 Synchronous counters

In a synchronous counter every flip-flop shares one clock, and gates decide which ones toggle. All the bits change together, at one clock-to-Q delay after the edge.

Look at binary counting again: a bit toggles when every bit below it is 1. So with T flip-flops on a common clock, each T input is the AND of the bits below:


T0 = 1, T1 = Q0, T2 = Q0·Q1, T3 = Q0·Q1·Q2

The simulator built this one from the same gate-level flip-flops, with a gate-level AND chain, and ran the same 18 cycles:


values the outputs pass through: 0 1 3 2 3 7 4 5 7 6 7 15 8 9 11 10 11 15 12 13 15 14 15 0 1 3 2
after the edge from 7 to 8: 7 -> 15 -> 8
after that edge Q0, Q1, Q2, Q3 settle 3, 3, 3, 2 steps later

Every bit now settles within one step of the others, instead of spreading over five steps. But look closely: the outputs still show one wrong value between counts, such as 15 on the way from 7 to 8. It lasts a single step, and it comes from the flip-flop itself: in Volume 07's measurement, Q rises 2 steps after the edge but falls 3 steps after it. So the rising bit arrives one step before the falling ones.

Remember

"Synchronous" means every bit changes on the same edge, not at exactly the same instant. The outputs are right once the clock-to-Q time has passed, so read them with another flip-flop on the next edge. Circuits that decode a counter's outputs directly, with no clock, must allow for the brief wrong values in any counter.

Quick check

In a 4-bit synchronous up-counter, when does bit 3 toggle?

Show the answer

Answer: D. A bit toggles when every bit below it is 1, so T3 = Q0·Q1·Q2. That is the change from 0111 to 1000, or from 1111 to 0000.

8.6 A counter for any sequence

A counter can follow any sequence you choose. Write down each state and its next state, look up the flip-flop inputs in the excitation table, then simplify each input with a K-map. Finally, check where the unused states go.

Say a counter must run 000, 001, 011, 111, 110, 100 and back to 000, using JK flip-flops.

Step 1: the state table with its excitations

For each state, write the next state, then use the JK excitation table from Volume 07 for each bit:

Q2 Q1 Q0 Next J2 K2 J1 K1 J0 K0
000 001 0 X 0 X 1 X
001 011 0 X 1 X X 0
011 111 1 X X 0 X 0
111 110 X 0 X 0 X 1
110 100 X 0 X 1 0 X
100 000 X 1 0 X 0 X

Step 2: simplify each input

The two states the sequence never uses, 010 and 101, are don't cares. With them, each input's K-map simplifies to a single letter:


J2 = Q1
K2 = Q1'
J1 = Q0
K1 = Q0'
J0 = Q2'
K0 = Q2

from 000: 000 -> 001 -> 011 -> 111 -> 110 -> 100 -> 000

Step 3: check the unused states

This step is the one that is easy to forget. Treating 010 and 101 as don't cares let the K-maps choose their next states freely - and they chose badly:


from unused 010: 010 -> 101 -> 010 -> 101 -> 010
from unused 101: 101 -> 010 -> 101 -> 010 -> 101
the unused states only lead to each other (lock-up): True
The counter's six-state loop, and a separate loop between the two unused states 000 001 011 111 110 100 010 101 reset
Figure 8.4 - The sequence runs round the six-state loop. But 010 and 101 lead only to each other, so a counter that powers up in either of them is locked out of the sequence for ever.

Step 4: design the lock-up out

The fix is to stop treating the unused states as don't cares, and give each one a next state that joins the sequence. Sending both to 000 costs a few more gates:


J2 = Q1·Q0
K2 = Q1'
J1 = Q2'·Q0
K1 = Q0'
J0 = Q2'·Q1'
K0 = Q2
from unused 010: 010 -> 000 -> 001
from unused 101: 101 -> 000 -> 001
every unused state now joins the sequence on the next clock: True
The fixed counter: both unused states now lead into the sequence 000 001 011 111 110 100 010 101 reset
Figure 8.5 - 010 and 101 now go to 000 on the next clock edge, so wherever the counter starts, it joins the six-state loop within one cycle.
Common mistake

Stopping once the counter follows its sequence. A design that counts correctly from its reset state can still lock up if it ever wakes in an unused state. Always follow every unused state to see where it goes, and fix any that do not reach the sequence.

Quick check

Why can treating unused states as don't cares cause trouble in a counter?

Show the answer

Answer: B. The K-maps pick whatever next state gives the smallest circuit, and that can be a loop among unused states. A counter that starts there - after power-up or noise - is locked up.

What you learned

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

Shift a word in

A 4-bit shift register starts at 0000. The bits 1, 0, 1, 1 enter one per clock edge, into Q3. What does it hold after each edge?

Show the solution

Each edge moves every bit one place right and puts the new bit in Q3:

Clock edge Serial in Q3 Q2 Q1 Q0
0 - 0000
1 1 1000
2 0 0100
3 1 1010
4 1 1101
Practice 2

How many flip-flops?

You need a counter that cycles through 8 states. How many flip-flops does it take as a binary counter, a Johnson counter and a ring counter?

Show the solution

A binary counter needs 3 (23 = 8). A Johnson counter has 2n states, so it needs 4. A ring counter has n states, so it needs 8. More flip-flops buy simpler decoding: a ring counter needs no decoder at all.

Practice 3

Toggle inputs for a 3-bit counter

Write the T inputs of a 3-bit synchronous up-counter built from T flip-flops.

Show the solution

Each bit toggles when every bit below it is 1, so T0 = 1, T1 = Q0 and T2 = Q0·Q1.

Interview corner

Interview question 1

Ripple or synchronous?

"Compare a ripple counter with a synchronous counter."

Show the solution

"A ripple counter clocks each flip-flop from the previous one's output. It needs no extra gates, but the change ripples through the stages. So the delay grows with the width, and the outputs pass through wrong values: in the course's gate-level simulation, 7 went to 6, 4 and 0 before reaching 8. A synchronous counter clocks every flip-flop together and uses AND gates to decide which toggle, so the bits all change on the same edge. It is faster and far cleaner, which is why almost every real design uses it."

Interview question 2

What is lock-up?

"What is lock-up in a counter, and how do you prevent it?"

Show the solution

"Lock-up is when a counter lands in an unused state and never returns to its proper sequence - for example, the unused states lead only to each other. It can happen after power-up or noise. You prevent it by checking where every unused state goes during design, and giving any that do not join the sequence an explicit next state, or by resetting the counter."

Volume 09 moves out to real chips: memories, converters between analogue and digital, and the logic families that gates are built from.