---
title: The control unit
heading: Decoding an instruction into the signals that steer the datapath
eyebrow: C8 · Lesson 36 · The control unit
xp: 40
---

::: hook
The datapath of lesson 35 can add, load, store and branch, but by itself it does nothing: every multiplexer needs to be told which input to pass, the register file whether to write, the memory whether to store. Those choices come from the control unit, which reads the instruction's opcode and a few function bits. For the core RISC-V instructions it fits in a few dozen gates, and you can design it from a table.
:::

::: play
- These are the main decoder's equations for six instruction types: `lw`, `sw`, R-type, I-type arithmetic, `beq` and `jal`. Their opcodes differ in bits 6, 5, 4 and 2 (bits 1:0 are always 11, and bit 3 isn't needed to tell these six apart).
- Set the switches to an opcode and read the signals: lw is 0000, sw is 0100, R-type 0110, I-type 0010, beq 1100 and jal 1101 (op6, op5, op4, op2).
- The other ten rows aren't instructions this processor runs. The logic is free to do anything there, which is what keeps it this small.
:::

```sim logic
input op6, op5, op4, op2
output RegWrite, ALUSrc, MemWrite, ResultMem, Branch, Jump
assign RegWrite = op4 | ~op5 | op2
assign ALUSrc = ~op6 & (~op5 | ~op4)
assign MemWrite = ~op6 & op5 & ~op4
assign ResultMem = ~op5 & ~op4
assign Branch = op6 & ~op2
assign Jump = op6 & op2
table: on
```

::: predict
## `add` and `sub` have the same opcode. Which control signals differ between them?

- [ ] RegWrite and ALUSrc
- [ ] All of them
- [x] Only the ALU's operation, chosen from funct7
- [ ] None: the ALU works out which one from its operands

::: reveal
Only the ALU's operation. Both write a register, both take their second operand from a register, neither touches memory. The difference is bit 30 of the instruction (bit 5 of funct7), which the ALU decoder turns into "subtract".
:::
:::

::: nameit
## Two decoders

Control is split in two:

- The **main decoder** reads the 7-bit opcode and produces the signals that depend only on the instruction's type: RegWrite, ImmSrc (which immediate layout), ALUSrc (register or immediate), MemWrite, ResultSrc (ALU, memory or PC + 4), Branch and Jump, plus a 2-bit **ALUOp**.
- The **ALU decoder** combines ALUOp with funct3 and funct7 to choose the ALU's operation: ALUOp 00 means add (for addresses in `lw` and `sw`), 01 means subtract (for comparing in `beq`), and 10 means "look at funct3".

| Opcode | Type | RegWrite | ImmSrc | ALUSrc | MemWrite | Result | Branch | Jump | ALUOp |
|---|---|---|---|---|---|---|---|---|---|
| 0000011 | `lw` | 1 | I | imm | 0 | memory | 0 | 0 | 00 |
| 0100011 | `sw` | 0 | S | imm | 1 | none | 0 | 0 | 00 |
| 0110011 | R-type | 1 | none | reg | 0 | ALU | 0 | 0 | 10 |
| 0010011 | I-type | 1 | I | imm | 0 | ALU | 0 | 0 | 10 |
| 1100011 | `beq` | 0 | B | reg | 0 | none | 1 | 0 | 01 |
| 1101111 | `jal` | 1 | J | none | 0 | PC + 4 | 0 | 1 | none |

## The ALU decoder

funct3 picks the operation family: 000 add or subtract, 001 shift left, 010 `slt`, 011 `sltu`, 100 XOR, 101 shift right (logical or arithmetic), 110 OR, 111 AND. Bit 30 then picks subtract over add, or arithmetic over logical right shift. For `sub` it matters only in R-type instructions: in `addi` those bits belong to the immediate, so the decoder subtracts only when bit 30 is set **and** opcode bit 5 is 1.

## Choosing the next PC

The PC takes PC + offset when

$$\text{PCSrc} = \text{Branch} \cdot \text{Zero} + \text{Jump}$$

where Zero is the ALU's "result was zero" output, so `beq` jumps when its operands were equal. `bne`, `blt` and the others use funct3 to choose which ALU flag to test.

## Hardwired or microprogrammed

This control is **hardwired**: logic gates computed from a truth table. Processors with large, irregular instruction sets often use **microcode** instead: each instruction starts a short sequence of control words read from a small ROM, the control store. Hardwired control is faster; microcode is easier to change and to extend with complicated instructions.

## Adding an instruction

A new instruction means a new row in the table, and sometimes new hardware. `jalr` (opcode 1100111) writes PC + 4 to rd like `jal`, but its target is rs1 + immediate, computed by the ALU, so the PC multiplexer needs a third input: the ALU result.
:::

The same six types on the datapath. Step through and watch which wires each one uses.

```sim cpu
    addi t0, zero, 10    # I-type
    addi sp, sp, -16
    sw t0, 12(sp)        # S-type
    lw t1, 12(sp)        # load
    add t2, t0, t1       # R-type
    beq t1, t0, skip     # B-type: taken
    addi t2, t2, 1
skip:
    jal ra, done         # J-type
done:
    ebreak
show: t0, t1, t2, ra
hex: sp
view: datapath
speed: 1
```

::: example
## Decode 0x00C58533 into control signals.

::: solution
1. **Opcode.** The low 7 bits are 0110011: R-type.
2. **Main decoder.** RegWrite = 1, ALUSrc = register, MemWrite = 0, the result comes from the ALU, Branch = Jump = 0, and ALUOp = 10.
3. **ALU decoder.** funct3 (bits 14:12) is 000 and bit 30 is 0, so the ALU adds.
4. **Registers.** rd = 10, rs1 = 11, rs2 = 12: it's `add a0, a1, a2`, the instruction encoded in lesson 4.

**Answer:** it's `add a0, a1, a2`: RegWrite = 1, ALUSrc = register, MemWrite = 0, result from the ALU, Branch = Jump = 0, ALUOp = 10, and the ALU adds.
:::
:::

::: example
## `addi t0, t0, -1024` encodes as 0xC0028293, with bit 30 set. Why doesn't the ALU subtract?

::: solution
1. **What bit 30 is here.** For `addi`, bits 31:20 are the immediate: −1024 is 1100 0000 0000 in 12 bits, so bit 30 is immediate bit 10 rather than a function bit.
2. **When the ALU subtracts.** The ALU decoder subtracts only when bit 30 is 1 and opcode bit 5 is 1, which marks an R-type instruction.
3. **Apply it to `addi`.** `addi`'s opcode is 0010011, bit 5 is 0, so it adds.
4. **Without that extra term.** `addi` would subtract whenever immediate bit 10 was 1, as it is for every constant from −1024 to −1 and from 1024 to 2047: a bug that depends on the constant.

**Answer:** the decoder subtracts only when opcode bit 5 is also 1, and `addi`'s opcode has bit 5 = 0, so the ALU adds.
:::
:::

::: example
## What does adding `jalr` (opcode 1100111) to this processor take?

::: solution
1. **A new decoder row.** RegWrite = 1, ImmSrc = I, ALUSrc = immediate, MemWrite = 0, Result = PC + 4, and a new kind of jump.
2. **A new PC input.** The ALU computes rs1 + immediate, which is the target, so the PC multiplexer needs a third input, the ALU result, and a second select bit.
3. **What's already there.** Everything else (the register file write of PC + 4, the I-type immediate) already exists for `jal` and `addi`.

**Answer:** a new row in the main decoder, and a third PC multiplexer input (the ALU result) with a second select bit.
:::
:::

::: check
kind: boolean
vars: op6, op5, op4, op2
answer: ~op6 & op5 & ~op4
ops: and, or, not
dontcare: 1, 3, 5, 7, 8, 9, 10, 11, 14, 15

## Write MemWrite for the six instruction types (lw 0000, I-type 0010, sw 0100, R-type 0110, beq 1100, jal 1101 in op6, op5, op4, op2). The other rows don't matter.

::: right
Right. Only `sw` stores: $\overline{op_6}\,op_5\,\overline{op_4}$.
:::

::: misses
- [any] Not yet. Only the row for `sw` should be 1, and `beq` (1100) must stay 0.
:::
:::

::: check
kind: boolean
vars: op6, op5, op4, op2
answer: op4 | ~op5 | op2
ops: and, or, not
dontcare: 1, 3, 5, 7, 8, 9, 10, 11, 14, 15

## Write RegWrite for the same six types. Every one writes a register except `sw` and `beq`.

::: right
Right. One simple form is $op_4 + \overline{op_5} + op_2$.
:::

::: misses
- [any] Not yet. It must be 0 only for 0100 (`sw`) and 1100 (`beq`).
:::
:::

::: check
kind: boolean
vars: Branch, Zero, Jump
answer: Branch & Zero | Jump
ops: and, or, not

## Write PCSrc, which selects PC + offset as the next PC.

::: right
Right. A taken branch or any jump.
:::

::: misses
- [any] Not yet. A branch is taken only when Zero says the operands were equal.
:::
:::

::: check
kind: parts
given: w = 8463619 | 10560547 | 12944691 | 10486419 | 6457443 | 8388847

## An instruction word is {w:hex8}.

::: part
answer: w - 128*floor(w/128)
tolerance: 0
base: 16
width: 2

### What's its opcode (bits 6:0)?

::: misses
- [any] The low 7 bits.
:::
:::

::: part
answer: floor(w/128) - 32*floor(w/4096)
tolerance: 0

### What do bits 11:7 hold, as a number?

::: misses
- [any] Shift right by 7 and keep 5 bits.
:::
:::
:::

::: check
kind: match

## Match each funct3 value to its R-type operation.

- 000 :: add or subtract
- 001 :: shift left logical
- 100 :: exclusive OR
- 110 :: OR
- 111 :: AND

::: right
Right. Bit 30 then picks subtract or arithmetic shift where there's a choice.
:::
:::

::: check
kind: choice

## Why does the ALU decoder require opcode bit 5 to be 1 before it lets bit 30 select subtraction?

- [ ] Because only loads use bit 5
- [x] Because in I-type instructions bit 30 is part of the immediate, not a function bit
- [ ] Because subtraction is slower and needs a second cycle
- [ ] It doesn't; bit 30 alone decides

::: misses
- [1] Loads have bit 5 clear, but that's not why it's checked.
- [3] Subtraction takes the same time as addition: it's an add with the second operand inverted and a carry in.
- [4] Then `addi` with some negative immediates would subtract.
:::
:::

::: check
kind: parts
given: S = 64 | 256 | 1024; W = 24 | 48 | 80

## A microprogrammed control store holds {S} micro-instructions of {W} bits each.

::: part
answer: S*W/8
unit: bytes
tolerance: 0

### How big is it?

::: misses
- [S*W] That's in bits; divide by 8.
- [any] Words × bits per word ÷ 8.
:::
:::

::: part
answer: round(log(S)/log(2))
tolerance: 0

### How many address bits does its micro-PC need?

::: misses
- [any] $\log_2$ of the number of words.
:::
:::
:::

::: check
kind: choice

## Which instruction can't run on the single-cycle datapath without adding a new input to the PC multiplexer?

- [ ] `addi`
- [ ] `bne`
- [x] `jalr`
- [ ] `lui`, if the immediate can reach the result multiplexer

::: misses
- [1] It uses the same paths as `add`, with the immediate.
- [2] It uses the branch adder like `beq`, with the opposite test.
- [4] `lui` needs a new result source, not a new PC source.
:::
:::

::: explain
## Why does the control unit split into a main decoder and an ALU decoder?

::: model
Most control signals depend only on the instruction's type: whether it writes a register, uses an immediate, stores, branches or jumps. The opcode decides those, so the main decoder needs just the 7 opcode bits. Only the ALU's operation depends on funct3 and funct7, and only for some instructions; for loads and stores the ALU always adds, for branches it subtracts. Passing a 2-bit ALUOp from the main decoder to a small ALU decoder keeps each truth table small: the main decoder never sees funct bits, and the ALU decoder doesn't need to know about memory or branches. The two small tables are easier to get right and to extend than one big one.
:::
:::

::: practice
::: check
given: w = 8463619 | 12944691 | 6457443 | 3221389971
answer: w - 128*floor(w/128)
tolerance: 0
base: 16
width: 2

## What's the opcode of {w:hex8}?

::: misses
- [any] The low 7 bits.
:::
:::

::: check
given: w = 1086686515 | 12944691 | 3221389971
answer: floor(w/2^30) - 2*floor(w/2^31)
tolerance: 0

## What's bit 30 of {w:hex8}?

::: misses
- [any] Shift right by 30, keep 1 bit.
:::
:::

::: check
given: S = 128 | 512; W = 32 | 64
answer: S*W/8
unit: bytes
tolerance: 0

## How big is a control store of {S} words of {W} bits?

::: misses
- [any] $SW/8$.
:::
:::
:::
