---
title: Analyzing sequential circuits
heading: From a schematic of flip-flops and gates to a state table, a diagram, and what the circuit is for
eyebrow: C7 · Lesson 38 · Analyzing sequential circuits
xp: 40
---

::: hook
Lesson 37 went from a description to a state machine; lesson 39 will turn a design into flip-flops and gates. This lesson runs the other way, which is what you do when you inherit a schematic, debug someone else's board, or check that a synthesized netlist does what the description said. The method is mechanical: the flip-flops' outputs are the state, their inputs are the next state, and a table of every state, including the ones that should never happen, tells you what the circuit does. Those "impossible" states matter. A circuit that powers up in one of them may never leave, and finding that on paper is much cheaper than finding it in the field.
:::

::: play
- Two D flip-flops wired as $D_1 = Q_0$ and $D_0 = \bar Q_1$. Run the clock and read the state $Q_1Q_0$ off the waveforms.
- It steps 00 → 01 → 11 → 10 → 00: a four-state counter in which only one bit changes per step, a two-bit Johnson counter.
:::

```sim logic
output Q1, Q0
clock CLK 4
assign n1 = ~Q1
always @(posedge CLK) begin Q1 <= Q0; Q0 <= n1; end
wave: CLK, Q1, Q0
time: 40
```

::: predict
## A 3-bit ring counter has $D_2 = Q_1$, $D_1 = Q_0$, $D_0 = Q_2$ and is meant to circulate a single 1: 001 → 010 → 100. It powers up in state 000. What happens?

- [ ] It moves to 001 on the first clock and runs normally
- [x] It stays at 000 forever
- [ ] It moves to 111
- [ ] It counts in binary

::: reveal
It stays at 000: every flip-flop copies a 0. The ring counter has eight states, but only three are in its intended cycle. 000 and 111 each lead to themselves, and 011 → 110 → 101 → 011 is a second, unwanted cycle. A circuit that can get stuck outside its intended cycle has **lockout**. The fix is to design the unused states' next states on purpose, or to guarantee a reset.
:::
:::

::: nameit
## The procedure

1. **State variables:** each flip-flop's output. $n$ flip-flops give up to $2^n$ states.
2. **Next-state equations:** for D flip-flops, $Q^+ = D$. For T flip-flops, $Q^+ = T \oplus Q$. For JK flip-flops, $Q^+ = J\bar Q + \bar KQ$.
3. **Output equations:** if the outputs depend only on the state, it's a Moore machine; if they also depend directly on the inputs, Mealy.
4. **State table:** for every present state and input, the next state and output. Include every state, not just the expected ones.
5. **State diagram:** draw it, and describe the behavior: a counter, a detector, a controller.
6. **Unused states:** check where each one leads. If any can't reach the main cycle, the circuit can lock out.

## Timing

The analysis also gives the fastest clock: the longest path from a flip-flop's output through logic to a flip-flop's input must fit in one period (lesson 27):

$$f_{max} = \frac{1}{t_{cq} + t_{logic,max} + t_{su}}$$

## Self-correcting designs

A circuit is **self-correcting** when every unused state leads, in some number of clocks, into the intended cycle. The cheapest fix is often to change one next-state equation so the unused states fall through. For the ring counter, $D_0 = \bar Q_1\bar Q_0$ instead of $Q_2$ keeps the three-state cycle and sends every other state into it.
:::

::: example
## Analyze three T flip-flops with $T_0 = 1$, $T_1 = Q_0$, $T_2 = Q_1Q_0$. What does the circuit do, and how fast can it run with 80 ps clock-to-Q, 40 ps AND gates and 30 ps setup?

::: solution
1. **What each flip-flop does.** $Q_0$ toggles every clock, $Q_1$ toggles when $Q_0$ = 1, and $Q_2$ toggles when $Q_1 = Q_0 = 1$.
2. **Trace it.** From 000: 001, 010, 011, 100, 101, 110, 111, 000: a 3-bit binary up counter, using all eight states, so no lockout is possible.
3. **Find the longest path.** It runs from $Q_0$ or $Q_1$ through the AND gate to $T_2$:
   $$80 + 40 + 30 = 150\ \text{ps}$$
4. **Maximum frequency.**
   $$\begin{aligned} f_{max} &= \frac{1}{150\ \text{ps}} \\ &= 6.67\ \text{GHz} \end{aligned}$$
   Wider counters chain more ANDs, or use lookahead, as adders do.

**Answer:** a 3-bit binary up counter, which can run at up to 6.67 GHz.
:::
:::

::: example
## A circuit has one input $x$, two D flip-flops with $D_1 = xQ_0$ and $D_0 = x$, and output $z = Q_1$. Build its state table and describe it.

::: solution
1. **Read the equations.** $Q_0^+ = x$ remembers the last input; $Q_1^+ = xQ_0$ is 1 when the current and previous inputs were both 1.
2. **Build the state table.**

| $Q_1Q_0$ | $x$ = 0 | $x$ = 1 | $z$ |
|---|---|---|---|
| 00 | 00 | 01 | 0 |
| 01 | 00 | 11 | 0 |
| 10 | 00 | 01 | 1 |
| 11 | 00 | 11 | 1 |

3. **Describe it.** The output is 1 one clock after two consecutive 1s have arrived: a "11" detector.
4. **Classify it.** $z$ depends only on the state, so it's a Moore machine.
5. **Check for lockout.** All four states are reachable and every one goes to 00 on a 0, so there's no lockout.

**Answer:** a Moore machine that detects "11": $z$ is 1 one clock after two consecutive 1s.
:::
:::

::: example
## Check the ring counter of the predict question with $D_0$ changed to $\bar Q_1\bar Q_0$. Is it self-correcting?

::: solution
1. **Tabulate.** Each state goes $Q_2Q_1Q_0 \to Q_1, Q_0, \bar Q_1\bar Q_0$.
2. **The intended cycle.** 001 → 010 → 100 → 001.
3. **The unused states.** 000 → 001; 011 → 110 → 100; 101 → 010; 111 → 110 → 100.
4. **Check.** Every unused state reaches the cycle within two clocks: self-correcting, with one 2-input NOR gate in place of a wire.

**Answer:** yes: every unused state reaches the cycle within two clocks.
:::
:::

::: check
given: s = 0..3 int
answer: s + 1 - 4*floor((s + 1)/4)
base: 2
width: 2

## Two D flip-flops have $D_1 = Q_1 \oplus Q_0$ and $D_0 = \bar Q_0$. The present state $Q_1Q_0$ is {s:bin2}. What is the next state?

::: right
Right: {answer}. The circuit is a 2-bit binary up counter.
:::

::: misses
- [any] Not yet. Evaluate $D_1$ and $D_0$ from the present state.
:::
:::

::: check
given: s = 0..7 int
answer: 2*(s - 4*floor(s/4)) + floor(s/4)
base: 2
width: 3

## A ring counter has $D_2 = Q_1$, $D_1 = Q_0$, $D_0 = Q_2$. From state $Q_2Q_1Q_0$ = {s:bin3}, what's the next state?

::: right
Right: {answer}, the bits rotated left by one.
:::

::: misses
- [any] Not yet. Each flip-flop copies its neighbor: rotate left.
:::
:::

::: check
kind: boolean
vars: J, K, Q
answer: J & ~Q | ~K & Q
ops: and, or, not

## Write the next state $Q^+$ of a JK flip-flop.

::: right
Right. $Q^+ = J\bar Q + \bar KQ$: J sets, K resets, both toggle, neither holds.
:::

::: misses
- [J | ~K & Q] That makes J = K = 1 set instead of toggle.
- [any] Not yet. J = 1 sets from 0; K = 0 holds a 1.
:::
:::

::: check
kind: boolean
vars: Q2, Q1, Q0
answer: Q1 & Q0
ops: and, or, not

## A 3-bit binary up counter built from T flip-flops: write $T_2$.

::: right
Right. $Q_2$ toggles when every lower bit is 1, about to carry.
:::

::: misses
- [Q1] $Q_2$ toggles only when both lower bits are 1.
- [any] Not yet. A bit toggles when all bits below it are 1.
:::
:::

::: check
given: tcq = 50 | 80 | 120; tl = 100 | 250 | 400; tsu = 20 | 40
answer: 1/((tcq + tl + tsu)*1e-12)/1e9
unit: GHz
tolerance: 0.5%

## A sequential circuit's flip-flops have {tcq} ps clock-to-Q and {tsu} ps setup, and its longest logic path is {tl} ps. What's its maximum clock frequency?

::: right
Right. $1/(t_{cq} + t_{logic} + t_{su})$ = {answer}.
:::

::: misses
- [1/(tl*1e-12)/1e9] Include the flip-flops' clock-to-Q and setup times.
- [1/((tcq + tl)*1e-12)/1e9] Include the setup time.
- [any] Not yet. The period must cover $t_{cq} + t_{logic} + t_{su}$.
:::
:::

::: check
given: n = 3 | 4 | 5 | 8
answer: 2*n
tolerance: 0.5%

## An {n}-bit Johnson counter has $D_{n-1} = Q_{n-2}, \ldots, D_1 = Q_0$ and $D_0 = \bar Q_{n-1}$. How many states are in its main cycle?

::: right
Right: {answer}. It fills with 1s from one end, then with 0s, $2n$ steps in all.
:::

::: misses
- [2^n] Only $2n$ of the $2^n$ states are in the cycle; the rest are unused.
- [n] That's a ring counter. The inversion doubles the cycle.
- [any] Not yet. Count the shifts to fill with 1s and then with 0s.
:::
:::

::: check
kind: choice

## A circuit's output is $z = xQ_1$, where $x$ is an input and $Q_1$ a flip-flop. What kind of machine is it, and what does that mean for timing?

- [x] Mealy: z can change as soon as x does, without waiting for a clock
- [ ] Moore: z changes only on clock edges
- [ ] Neither: it's combinational
- [ ] Moore, because $Q_1$ is a state variable

::: right
Right. The output depends directly on an input, so a glitch or a late change on x passes straight to z. Registering the output turns it into a Moore output, one clock later.
:::

::: misses
- [2] z depends on x directly, so it doesn't wait for the clock.
- [3] $Q_1$ is a flip-flop, so the circuit has state.
- [4] It uses a state variable, but it also uses the input directly: that's Mealy.
:::
:::

::: check
kind: python
given: n = 3..8 int

## Write `analyze(next_state, n)` for a circuit with no inputs, where `next_state(s)` gives the next state of an `n`-bit state `s`. Return `(cycles, lockout)`: `cycles` is a sorted list of the cycles' lengths (each cycle counted once), and `lockout` is `True` if more than one cycle exists. Test it on a ring counter and a Johnson counter of {n} bits, setting `ring` and `johnson` to the results.

```python starter
def analyze(next_state, n):
    return [2 ** n], False
ring = analyze(lambda s: s, n)
johnson = analyze(lambda s: s, n)
```

```python solution
def analyze(next_state, n):
    seen_cycles = set()
    lengths = []
    for s0 in range(1 << n):
        path = {}
        s, i = s0, 0
        while s not in path:
            path[s] = i
            s = next_state(s)
            i += 1
        start = s
        cyc = [start]
        t = next_state(start)
        while t != start:
            cyc.append(t)
            t = next_state(t)
        key = min(cyc)
        if key not in seen_cycles:
            seen_cycles.add(key)
            lengths.append(len(cyc))
    lengths.sort()
    return lengths, len(lengths) > 1
M = (1 << n) - 1
ring = analyze(lambda s: ((s << 1) | (s >> (n - 1))) & M, n)
johnson = analyze(lambda s: ((s << 1) | (1 - (s >> (n - 1)))) & M, n)
```

```python tests
M = (1 << n) - 1
def ref(f):
    seen, out = set(), []
    for s0 in range(1 << n):
        p, s = set(), s0
        while s not in p:
            p.add(s); s = f(s)
        c, t = [s], f(s)
        while t != s:
            c.append(t); t = f(t)
        if min(c) not in seen:
            seen.add(min(c)); out.append(len(c))
    return sorted(out), len(out) > 1
assert analyze(lambda s: (s + 1) & M, n) == ([1 << n], False), "A binary counter has one cycle through every state."
rf = lambda s: ((s << 1) | (s >> (n - 1))) & M
jf = lambda s: ((s << 1) | (1 - (s >> (n - 1)))) & M
assert ring == ref(rf), f"The ring counter's cycles should be {ref(rf)}."
assert johnson == ref(jf), f"The Johnson counter's cycles should be {ref(jf)}."
assert analyze(lambda s: 0, n) == ([1], False), "A circuit that always goes to 0 has one cycle of length 1."
```

::: right
Right. The ring counter splits its states into many cycles, two of them of length 1 (all 0s and all 1s), and the Johnson counter has its $2n$ cycle plus others: both can lock out without a reset or a corrected next-state equation.
:::
:::

::: explain
## A board's 4-bit Johnson counter works perfectly after power-up on the bench but sometimes produces a wrong sequence after an electrostatic discharge. Explain how analyzing its unused states explains the failure, and how to fix it.

::: model
A 4-bit Johnson counter uses only 8 of its 16 states. The other 8 form at least one separate cycle, for example 0010 → 0101 → 1011 → 0110 → 1101 → 1010 → 0100 → 1001 → 0010, which never passes through the intended states. A clean power-up reset puts the counter in 0000, inside the main cycle, so the bench tests pass. A discharge can flip a flip-flop, landing the counter in an unused state, and from there it follows the wrong cycle indefinitely. Tabulating all 16 states finds this on paper. The fix is to make it self-correcting by changing one next-state equation so the unused states fall into the main cycle, or to add a detector that forces a reset when an illegal state appears.
:::
:::

::: practice

::: check
given: tcq = 40 | 90; tl = 150 | 300; tsu = 25 | 50
answer: 1/((tcq + tl + tsu)*1e-12)/1e9
unit: GHz
tolerance: 0.5%

## What's $f_{max}$ with {tcq} ps clock-to-Q, a {tl} ps logic path and {tsu} ps setup?

::: misses
- [1/(tl*1e-12)/1e9] Add the flip-flop times.
- [any] $1/(t_{cq} + t_{logic} + t_{su})$.
:::
:::

::: check
given: n = 3 | 6 | 10
answer: n
tolerance: 0.5%

## How many states are in an {n}-bit ring counter's intended cycle (a single 1 circulating)?

::: misses
- [2*n] That's a Johnson counter.
- [any] The single 1 visits each of the {n} positions.
:::
:::
:::
