---
title: The single-cycle processor
heading: A datapath and its control, one instruction per clock
eyebrow: C8 · Lesson 35 · The single-cycle processor
xp: 40
---

::: hook
Everything so far treated the processor as something that runs instructions. Now look inside. A processor is a datapath (the program counter, the instruction memory, the register file, an ALU, the data memory, and multiplexers between them) plus control logic that reads each instruction's opcode and sets the multiplexers and write enables. In the simplest design every instruction finishes in one clock cycle, which is easy to understand and makes the cost plain: the clock has to be slow enough for the slowest instruction, and every other instruction waits for it.
:::

::: play
- The program runs one of each kind of instruction: a load, two ALU operations, a store and a branch. Step it one instruction at a time with the datapath showing.
- For `lw`, the lit wires go through every unit: instruction memory, register file, ALU (adding the offset), data memory, and back to the register file.
- For `add` the data memory sits idle, for `sw` nothing is written back, and for `beq` the ALU compares and the PC takes the branch target.
:::
:::

```sim cpu
.data
x: .word 25, 0
.text
    la t0, x
    lw t1, 0(t0)        # a load: every unit is used
    addi t2, t1, 17     # the ALU with an immediate
    add t3, t1, t2      # the ALU with two registers
    sw t3, 4(t0)        # a store: nothing is written back
    beq t3, t3, done    # a branch: compare, then pick the next PC
    addi t3, t3, 1      # skipped
done:
    ebreak
view: datapath
show: t1, t2, t3
hex: t0
memory: 0x1000..0x1008
speed: 1
```

::: predict
## In a single-cycle processor, which instruction sets the length of the clock period?

- [ ] `add`, because it's the most common
- [x] `lw`, because its path goes through both memories, the register file and the ALU
- [ ] `beq`, because it changes the PC
- [ ] `sw`, because it writes memory

::: reveal
`lw`. Its path is the longest: read the instruction, read the base register, add the offset, read data memory, and write the result back to the register file before the next clock edge. Every instruction gets that much time, even the ones that need much less.
:::
:::

::: nameit
## The datapath

| Unit | Does |
|---|---|
| PC | a 32-bit register holding the current instruction's address |
| instruction memory | reads the instruction at the PC |
| register file | reads two registers (rs1, rs2) and, at the clock edge, writes one (rd) |
| immediate extender | builds the 32-bit immediate from the instruction's fields (I, S, B or J layout) |
| ALU | adds, subtracts, compares, does logic |
| data memory | read by loads, written by stores |
| adders | PC + 4, and PC + offset for branches and jumps |

## Control

The control unit decodes the opcode (and funct3 and funct7) into signals:

| Instruction | RegWrite | ImmSrc | ALUSrc | MemWrite | Result | Next PC |
|---|---|---|---|---|---|---|
| `lw` | 1 | I | immediate | 0 | memory | PC + 4 |
| `sw` | 0 | S | immediate | 1 | none | PC + 4 |
| R-type | 1 | none | register | 0 | ALU | PC + 4 |
| `addi` | 1 | I | immediate | 0 | ALU | PC + 4 |
| `beq` | 0 | B | register | 0 | none | PC + offset if equal |
| `jal` | 1 | J | none | 0 | PC + 4 | PC + offset |

## Timing

Everything between the PC's output and the next clock edge is combinational, so the clock period must cover the longest path, which is the load's:

$$T_c = t_{pcq} + t_\text{imem} + t_\text{RFread} + t_\text{ALU} + t_\text{dmem} + t_\text{mux} + t_\text{RFsetup}$$

The single-cycle CPI is exactly 1, but $T_c$ is long, and every instruction pays for the slowest one.
:::

::: example
## A single-cycle processor has $t_{pcq}$ = 40 ps, memories of 200 ps, a register-file read of 100 ps, an ALU of 120 ps, a multiplexer of 30 ps and a register-file setup of 60 ps. What is its clock period and frequency?

::: solution
1. **Add up the longest path.**
   $$\begin{aligned} T_c &= 40 + 200 + 100 + 120 \\ &\quad + 200 + 30 + 60 \\ &= 750\ \text{ps} \end{aligned}$$
2. **Frequency.**
   $$\begin{aligned} f &= \frac{1}{750\ \text{ps}} \\ &= 1.33\ \text{GHz} \end{aligned}$$
3. **An `add`.** It doesn't use the data memory, so it's done after 550 ps and idles for the other 200, 27% of the cycle.

**Answer:** a clock period of 750 ps, 1.33 GHz.
:::
:::

::: example
## A 300 ps multiplier is added beside the 120 ps ALU, with results chosen by the same path. What happens to the clock?

::: solution
1. **The new longest path.** It grows by 300 − 120 = 180 ps:
   $$750 + 180 = 930\ \text{ps}$$
2. **The new clock.** It falls from 1.33 GHz to 1.08 GHz.
3. **Who pays.** Every instruction slows down by 24%, including the 98% of them that aren't multiplies.
4. **What real designs do.** They give a slow unit several cycles instead, which is the idea behind multicycle and pipelined processors.

**Answer:** the clock falls from 1.33 GHz to 1.08 GHz, and every instruction slows down by 24%.
:::
:::

::: example
## A multi-cycle version of the same processor clocks at the memory's 200 ps plus 50 ps of register overhead. Loads take 5 cycles, stores and R-type 4, branches 3. With 25% loads, 10% stores, 50% R-type and 15% branches, how does it compare with the single-cycle 750 ps?

::: solution
1. **CPI.**
   $$\begin{aligned} \text{CPI} &= 0.25(5) + 0.10(4) \\ &\quad + 0.50(4) + 0.15(3) \\ &= 4.1 \end{aligned}$$
2. **Time per instruction.** Each cycle is 200 + 50 = 250 ps:
   $$4.1 \times 250 = 1025\ \text{ps}$$
   which is 37% slower than the single-cycle design.
3. **Why.** Splitting the work saves the idle time of short instructions, but each step pays the register overhead and the steps aren't balanced.
4. **Where it wins.** The multi-cycle design wins on hardware (one memory, one adder) and loses on speed; pipelining is what makes the short steps pay.

**Answer:** 1025 ps per instruction, 37% slower than the single-cycle 750 ps.
:::
:::

::: check
kind: parts
given: cq = 30 | 50; mem = 150 | 250; rf = 80 | 120; alu = 100 | 180; mux = 20 | 40; su = 40 | 70

## A single-cycle processor has $t_{pcq}$ = {cq}, memory reads of {mem}, a register-file read of {rf}, an ALU of {alu}, a mux of {mux} and register-file setup of {su} (all ps).

::: part
answer: cq + 2*mem + rf + alu + mux + su
unit: ps
tolerance: 0.5%

### What is the minimum clock period?

::: misses
- [cq + mem + rf + alu + mux + su] The load reads two memories: the instruction and then the data.
- [any] Not yet. The load's whole path.
:::
:::

::: part
answer: 1e6/(cq + 2*mem + rf + alu + mux + su)
unit: MHz
tolerance: 0.5%

### What is the highest clock frequency?

::: misses
- [any] One over the period.
:::
:::
:::

::: check
kind: parts
given: Tc = 600 | 850 | 1000; mem = 150 | 200; N = 1 | 50

## A single-cycle processor's clock period is {Tc} ps, set by `lw`, whose path includes a {mem} ps data-memory read.

::: part
answer: N*Tc/1000
unit: ms
tolerance: 0.5%

### How long do {N} million instructions take?

::: misses
- [any] CPI is 1: N × $T_c$.
:::
:::

::: part
answer: mem/Tc*100
unit: %
tolerance: 0.5%

### What percentage of each cycle does an `add`, which skips the data memory, spend idle?

::: misses
- [any] The data memory's delay over the period.
:::
:::
:::

::: check
kind: parts
given: Tc = 700 | 900; d = 50 | 120; old = 200 | 250

## A designer replaces the {old} ps data memory of a {Tc} ps single-cycle processor with one {d} ps faster.

::: part
answer: Tc - d
unit: ps
tolerance: 0.5%

### What is the new clock period, if `lw` is still the slowest?

::: misses
- [any] The load's path shrinks by {d} ps.
:::
:::

::: part
answer: Tc/(Tc - d)
tolerance: 0.5%

### How many times faster does every program run?

::: misses
- [any] Old period over new.
:::
:::
:::

::: check
kind: choice

## Which control signal differs between `add t0, t1, t2` and `addi t0, t1, 5`?

- [ ] RegWrite
- [x] ALUSrc: the ALU's second input is a register for add and the immediate for addi
- [ ] MemWrite
- [ ] The result source

::: misses
- [1] Both write t0.
- [3] Neither writes memory.
- [4] Both write back the ALU's result.
:::
:::

::: check
kind: choice

## What are RegWrite and MemWrite for `sw t0, 8(sp)`?

- [ ] RegWrite = 1, MemWrite = 1
- [x] RegWrite = 0, MemWrite = 1
- [ ] RegWrite = 1, MemWrite = 0
- [ ] RegWrite = 0, MemWrite = 0

::: misses
- [1] A store reads t0 but writes no register.
- [3] That's a load.
- [4] A store writes memory.
:::
:::

::: check
kind: order

## Put the steps of `beq t0, t1, loop` in a single-cycle processor in order.

1. Read the instruction at the PC
2. Read t0 and t1 from the register file, and build the branch offset from the immediate fields
3. Subtract in the ALU and check for zero
4. Load PC + offset into the PC at the clock edge if they were equal, PC + 4 otherwise

::: right
Right. No memory access and no register write: a branch only decides the next PC.
:::
:::

::: check
kind: parts
given: M = 200 | 250; o = 60 | 100; l = 20 | 25; s = 10 | 15; b = 10 | 20

## A multi-cycle processor clocks at the memory's {M} ps plus {o} ps of overhead. Loads ({l}%) take 5 cycles, stores ({s}%) 4, branches ({b}%) 3 and everything else 4.

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

### What's its CPI?

::: misses
- [any] Weight each class's cycles by its share.
:::
:::

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

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

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

::: check
kind: parts
given: P = 30 | 50; M = 150 | 250; R = 80 | 120; A = 100 | 180; X = 25; S = 40 | 70

## A single-cycle processor has $t_{pcq}$ = {P} ps, memories of {M} ps, a register-file read of {R} ps, an ALU of {A} ps, a multiplexer of {X} ps and a register-file setup of {S} ps.

::: part
answer: P + 2*M + R + A + X + S
unit: ps
tolerance: 0

### What's the clock period, set by a load?

::: misses
- [P + M + R + A + X + S] A load reads memory twice: the instruction and the data.
- [any] Every block on the load's path.
:::
:::

::: part
answer: M/(P + 2*M + R + A + X + S)*100
unit: %
tolerance: 0.5%

### What share of the cycle does an add leave idle?

::: misses
- [any] An add skips the data memory.
:::
:::
:::

::: explain
## Why does a single-cycle processor waste time on most instructions?

::: model
The clock period is fixed, so it has to be long enough for the slowest instruction to finish, and that's the load, whose path runs through the instruction memory, the register file, the ALU, the data memory and back to the register file. Most instructions are adds, branches and other operations that skip the data memory or the write-back, so they finish early and then sit idle until the next clock edge. Adding any slow unit, like a multiplier, makes it worse, because it stretches the period for every instruction, not just the ones that use it.
:::
:::

::: practice

::: check
given: a = 30 | 40; m = 180 | 220; r = 90 | 110; l = 110 | 150; x = 25 | 35; s = 50 | 65
answer: a + 2*m + r + l + x + s
unit: ps
tolerance: 0.5%

## $t_{pcq}$ = {a}, memories {m}, register read {r}, ALU {l}, mux {x}, register setup {s} (ps): what is the single-cycle clock period?

::: misses
- [any] The load's path, through both memories.
:::
:::

::: check
given: T = 500 | 800 | 1250
answer: 1e6/T
unit: MHz
tolerance: 0.5%

## What clock frequency does a {T} ps period give?

::: misses
- [any] 1/T.
:::
:::

::: check
given: T = 600 | 900; N = 2 | 30
answer: N*T/1000
unit: ms
tolerance: 0.5%

## How long do {N} million instructions take on a single-cycle processor with a {T} ps clock?

::: misses
- [any] N × T.
:::
:::
:::
