---
title: The multi-cycle processor
heading: One memory, one ALU, and an instruction spread over several clocks
eyebrow: C8 · Lesson 37 · The multi-cycle processor
xp: 40
---

::: hook
The single-cycle processor gives every instruction one long clock, as long as a load needs, and it needs an instruction memory, a data memory, an ALU and two more adders, because everything happens at once. Split each instruction into short steps instead, one per clock, and one memory and one ALU can be reused from step to step. Short instructions take fewer steps. Whether that's faster depends on how evenly the steps divide the work, and the arithmetic is the same one that decides how deep to pipeline.
:::

::: play
- The flat line is the single-cycle processor: 750 ps per instruction, whatever it is.
- The rising line is the multi-cycle processor: loads take 5 steps, branches 3 and everything else 4, so its average time depends on the share of loads (along the bottom) and branches (slider).
- Slide the step time. At 250 ps the multi-cycle design is slower for any mix; it would need steps of about 180 ps to break even, and real steps can't shrink that far, because the memory alone takes 200 ps.
:::

```sim plot
view: graph
x: l = 0..0.5
y: t = 0..1500 ps
slider: Tc = 150..350 step 10 = 250
slider: b = 0..0.3 step 0.01 = 0.15
plot: 750 | label: single-cycle
plot: (4 + l - b)*Tc | label: multi-cycle
readout: CPI at 25% loads = 4.25 - b | digits: 3
readout: time at 25% loads, ps = (4.25 - b)*Tc | digits: 4
readout: step time to break even, ps = 750/(4.25 - b) | digits: 3
```

::: predict
## The multi-cycle processor's clock is 250 ps, and `beq` takes 3 steps. How long does a branch take, against the single-cycle processor's 750 ps?

- [ ] 250 ps
- [x] 750 ps
- [ ] 1000 ps
- [ ] 1250 ps

::: reveal
750 ps: three steps of 250. Branches break even, loads (5 steps, 1250 ps) lose, and the others (4 steps, 1000 ps) lose too. The design saves hardware; it only saves time when the steps are short and well balanced.
:::
:::

::: nameit
## Steps and the registers between them

Each step does one major operation (a memory access, a register-file access or an ALU operation) and saves its result in a register for the next step. Those **nonarchitectural registers** are invisible to programs: IR (the instruction), OldPC, A and B (the two source registers), ALUOut and Data (a word read from memory). IR is needed because the one memory is used again for data, so the instruction must be kept somewhere.

| Step | Does |
|---|---|
| Fetch | IR ← memory[PC]; the ALU computes PC + 4 |
| Decode | read rs1 and rs2 into A and B; the ALU computes OldPC + immediate, the branch target |
| MemAdr | ALUOut ← A + immediate (loads and stores) |
| MemRead / MemWrite | Data ← memory[ALUOut], or memory[ALUOut] ← B |
| ExecuteR / ExecuteI | ALUOut ← A op B, or A op immediate |
| Write back | rd ← Data or ALUOut |
| Branch | compare A and B; if equal, PC ← target |

So `lw` takes 5 cycles (Fetch, Decode, MemAdr, MemRead, write back), `sw` 4, R-type and I-type 4, `beq` 3 and `jal` 4.

## Control is a state machine

The control unit becomes a finite state machine, one state per step, that branches after Decode according to the opcode and returns to Fetch when the instruction is done. About a dozen states cover these instructions. Each state's outputs are the control signals for that step.

## Timing and performance

The clock must fit the slowest step, typically

$$T_c = t_{pcq} + t_\text{mux} + \max(t_\text{ALU} + t_\text{mux},\ t_\text{mem}) + t_\text{setup}$$

and an instruction's time is its cycle count times $T_c$. Averaged over a program,

$$\text{CPI} = \sum_i f_i\,c_i, \qquad t = \text{CPI} \times T_c$$

## Why it matters

The multi-cycle processor uses less hardware: one memory, one ALU, a few registers. It's rarely faster than the single-cycle design, because the steps are unequal and each one pays the register overhead. But it introduced the idea that makes processors fast: if an instruction is a sequence of short steps, different instructions can be in different steps at the same moment. That's pipelining, the subject of lessons 39 and 40.
:::

::: example
## A multi-cycle processor has $t_{pcq}$ = 40 ps, multiplexers of 30 ps, an ALU of 120 ps, a memory of 200 ps and a register setup of 50 ps. A program is 25% loads, 10% stores, 11% branches, 2% jumps and 52% R-type. How long does an instruction take on average?

::: solution
1. **Find the slowest step.** The ALU path is 120 + 30 = 150 ps, shorter than the memory's 200, so
   $$\begin{aligned} T_c &= 40 + 30 + 200 + 50 \\ &= 320\ \text{ps} \end{aligned}$$
2. **CPI.**
   $$\begin{aligned} \text{CPI} &= 0.25(5) + 0.10(4) \\ &\quad + 0.11(3) + 0.02(4) \\ &\quad + 0.52(4) \\ &= 4.14 \end{aligned}$$
3. **Time per instruction.**
   $$4.14 \times 320 \approx 1325\ \text{ps}$$
   which is 1.77 times the single-cycle 750 ps.
4. **Break-even.** To break even the steps would need to be 750/4.14 = 181 ps, less than the memory takes.

**Answer:** 1325 ps per instruction on average, 1.77 times the single-cycle 750 ps.
:::
:::

::: example
## A loop body of `lw`, `add`, `addi` and `bne` runs 1000 times. How many cycles does the multi-cycle processor take at 320 ps, and how does it compare with the single-cycle one at 750 ps?

::: solution
1. **Cycles per pass.** `lw`, `add`, `addi` and `bne` take
   $$5 + 4 + 4 + 3 = 16\ \text{cycles}$$
2. **The whole loop.** 16,000 cycles at 320 ps:
   $$16{,}000 \times 320\ \text{ps} = 5.12\ \mu\text{s}$$
3. **Single-cycle.** It runs 4000 instructions at 750 ps: 3.0 µs.
4. **Compare.** The multi-cycle design is 1.7 times slower on this loop, which is 25% loads and 25% branches.

**Answer:** 16,000 cycles, 5.12 µs, against 3.0 µs for the single-cycle processor.
:::
:::

::: example
## What hardware does the multi-cycle design save, and what does it add?

::: solution
1. **It saves a memory.** Instructions and data share one, with a multiplexer choosing PC or ALUOut as the address.
2. **It saves both extra adders.** The ALU computes PC + 4 during Fetch and the branch target during Decode, steps when it's otherwise idle.
3. **It adds registers.** Six 32-bit registers, IR, OldPC, A, B, ALUOut and Data: 192 flip-flops.
4. **It adds control.** A few wider multiplexers, and a control unit that is a state machine instead of a truth table.

**Answer:** it saves a memory and two adders, and adds six 32-bit registers (192 flip-flops), a few wider multiplexers and a state-machine control unit.
:::
:::

::: check
kind: parts
given: l = 20 | 25 | 30; s = 8 | 12; b = 10 | 15 | 20; T = 250 | 320

## A program is {l}% loads, {s}% stores and {b}% branches; the rest are R-type. The multi-cycle clock is {T} ps.

::: part
answer: 5*l/100 + 4*s/100 + 3*b/100 + 4*(1 - l/100 - s/100 - b/100)
tolerance: 0.5%

### What's the CPI?

::: misses
- [any] Loads 5, stores and R-type 4, branches 3.
:::
:::

::: part
answer: (5*l/100 + 4*s/100 + 3*b/100 + 4*(1 - l/100 - s/100 - b/100))*T
unit: ps
tolerance: 0.5%

### What's the average time per instruction?

::: misses
- [any] CPI × clock period.
:::
:::
:::

::: check
kind: parts
given: P = 30 | 50; X = 25 | 40; A = 190 | 250; M = 150 | 200; S = 40 | 60

## A multi-cycle datapath has $t_{pcq}$ = {P} ps, multiplexers of {X} ps, an ALU of {A} ps, a memory of {M} ps and a setup time of {S} ps.

::: part
answer: P + X + (A + X + M + abs(A + X - M))/2 + S
unit: ps
tolerance: 0

### What's the clock period?

::: misses
- [P + X + M + S] Compare the memory with the ALU plus its multiplexer.
- [any] $t_{pcq} + t_\text{mux} + \max(t_\text{ALU} + t_\text{mux}, t_\text{mem}) + t_\text{setup}$.
:::
:::

::: part
answer: 5*(P + X + (A + X + M + abs(A + X - M))/2 + S)
unit: ps
tolerance: 0

### How long does a load take?

::: misses
- [any] Five cycles.
:::
:::
:::

::: check
kind: parts
given: N = 100 | 1000; k = 1 | 2; r = 2 | 3 | 5

## A loop body has {k} loads, {r} R-type instructions and one branch, and runs {N} times.

::: part
answer: N*(5*k + 4*r + 3)
tolerance: 0

### How many cycles does it take on the multi-cycle processor?

::: misses
- [any] Add each instruction's cycles, then multiply by the passes.
:::
:::

::: part
answer: N*(k + r + 1)
tolerance: 0

### And on the single-cycle one?

::: misses
- [any] One cycle per instruction.
:::
:::
:::

::: check
kind: order

## Put the multi-cycle steps of `lw t1, 8(t0)` in order.

1. IR ← memory[PC], and the ALU computes PC + 4
2. Read t0 into A
3. ALUOut ← A + 8
4. Data ← memory[ALUOut]
5. t1 ← Data

::: right
Right. Five steps, each through one major unit, with a register catching each result.
:::
:::

::: check
kind: choice

## Why does the multi-cycle processor need the IR register, when the single-cycle one doesn't?

- [ ] To hold the PC + 4 value
- [x] Because the same memory is used for data in later steps, so the instruction has to be saved
- [ ] Because the register file can't be read twice
- [ ] It doesn't: the instruction stays on the memory's output

::: misses
- [1] PC + 4 goes back into the PC itself.
- [3] The register file has two read ports, as before.
- [4] The memory's output changes when a load or store uses it for data.
:::
:::

::: check
kind: parts
given: s = 11 | 13 | 18

## Adding instructions grows the control state machine to {s} states.

::: part
answer: ceil(log(s)/log(2))
tolerance: 0

### How many flip-flops does a binary state encoding need?

::: misses
- [any] $\lceil\log_2 s\rceil$.
:::
:::

::: part
answer: s
tolerance: 0

### And a one-hot encoding?

::: misses
- [any] One per state.
:::
:::
:::

::: check
kind: derivation
vars: T = 300..2000, c = 3..5

## A single-cycle processor runs at period T; a multi-cycle one averages c cycles per instruction.

::: step
answer: T/c

### Below what step time is the multi-cycle design faster?
:::

::: step
answer: T/5

### What step time would make even loads (5 cycles) no slower than in the single-cycle design?
:::

::: right
Right. The steps would have to split the work almost perfectly, with nothing lost to register overhead.
:::
:::

::: explain
## The multi-cycle processor is usually slower than the single-cycle one. Why was it worth inventing?

::: model
It uses far less hardware: one memory instead of two and one ALU doing the work of the ALU and two adders, because each is used in a different step of the instruction. Before memory was cheap, that saving mattered more than speed. It also showed that an instruction can be broken into a fixed sequence of short steps with registers between them. Once that's done, nothing stops a new instruction from starting its first step while the previous one is in its second. Overlapping the steps is pipelining, which keeps the short clock and brings the CPI back near 1.
:::
:::

::: practice
::: check
given: l = 20 | 30; b = 10 | 20
answer: 4 + l/100 - b/100
tolerance: 0.5%

## {l}% loads (5 cycles), {b}% branches (3) and the rest 4 cycles. CPI?

::: misses
- [any] $4 + l - b$.
:::
:::

::: check
given: c = 3 | 4 | 5; T = 250 | 320
answer: c*T
unit: ps
tolerance: 0

## An instruction takes {c} cycles of {T} ps. How long is that?

::: misses
- [any] Cycles × period.
:::
:::

::: check
given: N = 50 | 500; k = 1 | 2; r = 3 | 4
answer: N*(5*k + 4*r + 3)
tolerance: 0

## {N} passes of {k} loads, {r} R-type instructions and a branch. Multi-cycle cycles?

::: misses
- [any] $N(5k + 4r + 3)$.
:::
:::
:::
