---
title: Registers, counters and clocks
heading: Flip-flops in rows, stepping together
eyebrow: C7 · Lesson 28 · Registers, counters and clocks
xp: 50
---

::: hook
Put n flip-flops side by side on one clock and you have an n-bit register: a word of memory that updates all at once. Feed a register's output through an adder back to its input and it counts. Feed each flip-flop into the next and it shifts. Registers, counters and shift registers are how digital circuits keep time, divide frequencies, convert between serial and parallel data, and hold every value a processor works with.

::: play
- A 3-bit counter: each clock edge adds 1 to the number held in Q2 Q1 Q0. Hold R at 1 to reset it.
- Read the count off the timing diagram. Compare the frequencies of CLK, Q0, Q1 and Q2.
- After 7, what comes next?
:::
:::

```sim logic
input R
output Q0, Q1, Q2
clock CLK 8
always @(posedge CLK) begin
  if (R) Q0 <= 0; else Q0 <= ~Q0;
  if (R) Q1 <= 0; else Q1 <= Q1 ^ Q0;
  if (R) Q2 <= 0; else Q2 <= Q2 ^ (Q1 & Q0);
end
wave: CLK, Q0, Q1, Q2
time: 136
```

::: predict
## A 3-bit counter is clocked at 8 MHz. What frequency appears on its top bit, Q2?

- [ ] 8 MHz
- [ ] 4 MHz
- [x] 1 MHz
- [ ] 2.67 MHz

::: reveal
1 MHz. Each bit toggles half as often as the one below it: Q0 at half the clock, Q1 at a quarter, Q2 at an eighth. A counter is a frequency divider by $2^n$. That's how a 32.768 kHz watch crystal becomes one tick per second: 15 stages of division, since $2^{15} = 32768$.
:::
:::

::: nameit
## Sequential building blocks

**Register.** n D flip-flops sharing a clock (and often an enable and a reset). On each edge all n bits load at once.

**Binary counter.** A register plus an incrementer: Q ← Q + 1. Bit k toggles when all lower bits are 1:

$$Q_0 \leftarrow \bar Q_0 \qquad Q_1 \leftarrow Q_1 \oplus Q_0 \qquad Q_2 \leftarrow Q_2 \oplus Q_1Q_0$$

An n-bit counter counts 0 to $2^n - 1$, then wraps to 0. Bit k has frequency $f_\text{clk}/2^{k+1}$.

**Modulo-N counter.** Reset to 0 when the count reaches N − 1: a divide-by-N, for clock dividers that aren't powers of 2.

**Shift register.** Each flip-flop's input is the previous one's output, so on every edge the bits move one place along. Used for serial links (one bit per clock in, a whole word out), delay lines and pseudo-random sequences.

**Clocks.** Every flip-flop in a synchronous design shares one clock, generated by a crystal oscillator and often multiplied up by a phase-locked loop. The clock must reach all flip-flops at nearly the same moment; the difference, **clock skew**, eats into the timing budget of lesson 19. The counter above shows that budget at work: its top bit's next value passes through three gates (an AND, an XOR and the reset multiplexer), so with a flip-flop's own delay it needs four ticks. Its clock period is 8 ticks; at 4, the top bit would be sampled before its logic settled and the count would go wrong.
:::


::: example
## A 4-bit binary counter runs from a 16 MHz clock. At what frequency does its top bit toggle?

::: solution
1. **Clocks per cycle of the top bit.** Bit 3 completes one cycle every $2^4$ = 16 clocks.
2. **Divide.**
   $$\frac{16\ \text{MHz}}{16} = 1\ \text{MHz}$$
   Each bit divides the frequency by two.

**Answer:** 1 MHz.
:::
:::

::: example
## Design a decimal (modulo-10) counter. How many flip-flops, and when does it reset?

::: solution
1. **Count the flip-flops.** Four, since $2^4 \ge 10$.
2. **When it resets.** It counts 0 to 9 and returns to 0 on the clock after 9 (1001).
3. **Its output.** Its output toggles at a tenth of the clock rate.

**Answer:** four flip-flops, returning to 0 on the clock after 9 (1001).
:::
:::

::: example
## A 4-bit Johnson counter (a shift register whose inverted last bit feeds its input) starts at 0000. List its states. How many are there, and what's it good for?

::: solution
1. **List the states.** Each clock shifts right and feeds back the complement of the last bit: 0000, 1000, 1100, 1110, 1111, 0111, 0011, 0001, then 0000 again.
2. **Count them.** Eight states from four flip-flops, 2n in general, against 16 for a binary counter.
3. **Decoding.** In return, only one bit changes per step, so each state can be decoded by a two-input gate without glitches.
4. **Dividing.** The counter divides its clock by 8 with a square wave on every bit.

**Answer:** eight states; it gives glitch-free decoding with two-input gates and divides its clock by 8 with a square wave on every bit.
:::
:::

::: check
given: n = 2 | 3 | 4 | 8
answer: 2^n - 1
tolerance: 0

## What's the highest value an {n}-bit binary counter reaches before wrapping to 0?

::: right
Right: {answer}.
:::

::: misses
- [2^n] It starts at 0, so it tops out one below $2^n$.
- [any] Not yet. $2^n - 1$.
:::
:::

::: check
given: f = 1 | 8 | 50; k = 0 | 2 | 3
answer: f*1000/2^(k + 1)
unit: kHz
tolerance: 0.5%

## A binary counter runs from a {f} MHz clock. What's the frequency of bit Q{k}?

::: right
Right. $f/2^{k+1}$ = {answer}.
:::

::: misses
- [f*1000/2^k] Bit 0 is already half the clock: divide by $2^{k+1}$.
- [any] Not yet. Each bit halves the frequency.
:::
:::

::: check
given: f = 32768 | 65536 | 1048576 | 2097152 | 4194304 | 16777216
answer: log(f)/log(2)
tolerance: 0.5%

## How many divide-by-2 stages turn a {f} Hz crystal into 1 Hz?

::: right
Right: {answer}.
:::

::: misses
- [any] Not yet. Find n with $2^n$ = {f}.
:::
:::

::: check
given: v = 1..15 int; k = 1 | 2
answer: v*2^k - 16*floor(v*2^k/16)
base: 2
width: 4

## A 4-bit shift register holds {v:bin4} and shifts left {k} time(s), with 0s shifted in at the right. What does it hold?

::: right
Right: {answer}. Bits that shift off the left end are lost.
:::

::: misses
- [floor(v/2^k)] That's shifting right.
- [any] Not yet. Move every bit {k} place(s) left, dropping what falls off.
:::
:::

::: check
given: f = 10 | 48; N = 3 | 5 | 10
answer: f*1000/N
unit: kHz
tolerance: 0.5%

## A modulo-{N} counter runs from {f} MHz. How often does it wrap around?

::: right
Right. Once every N clocks: {answer}.
:::

::: misses
- [f*1000/2^N] It's modulo N, not $2^N$.
- [any] Not yet. $f/N$.
:::
:::

::: check
kind: parts
given: n = 3 | 4 | 6; f = 10 | 50

## An n-flip-flop register with n = {n} runs from a {f} MHz clock.

::: part
answer: 2*n
tolerance: 0

### How many states does a Johnson counter made from it have?

::: misses
- [2^n] That's a binary counter.
- [n] That's a ring counter.
- [any] $2n$.
:::
:::

::: part
answer: f*1000/2^n
unit: kHz
tolerance: 0.5%

### At what frequency does a binary counter's top bit toggle?

::: misses
- [any] $\frac{f}{2^n}$.
:::
:::
:::

::: check
kind: derivation
vars: N = 2..1000000, f = 1000..1000000000

## A modulo-N counter runs from a clock of frequency f.

::: step
answer: ceil(log(N)/log(2))

### How many flip-flops does it need?
:::

::: step
answer: f/N

### At what frequency does it wrap around?
:::

::: right
Right. $\lceil\log_2N\rceil$ flip-flops, dividing by N.
:::
:::

::: explain
## Why does each bit of a binary counter run at half the frequency of the bit below it?

::: model
In binary counting, the lowest bit flips on every count. Each higher bit flips only when every bit below it is 1 and the count carries into it, which happens once for every two flips of the bit just below. So each bit completes one full cycle, low then high, for every two cycles of the bit beneath it, halving the frequency at each stage.
:::
:::

::: practice

::: check
given: n = 4 | 10 | 16
answer: 2^n
tolerance: 0

## How many distinct values does an {n}-bit counter step through?

::: misses
- [2^n - 1] Count the 0 too.
- [any] $2^n$.
:::
:::

::: check
given: f = 16 | 100; n = 2 | 4
answer: f/2^n
unit: MHz
tolerance: 0.5%

## A {f} MHz clock through {n} divide-by-2 stages. Output?

::: misses
- [any] $f/2^n$.
:::
:::

::: check
given: v = 1..7 int
answer: floor(v/2)
base: 2
width: 3

## A 3-bit shift register holds {v:bin3} and shifts right once (0 in at the left). Result?

::: misses
- [any] Move each bit one place right.
:::
:::
:::
