C8 · Lesson 36 · The control unit
Decoding an instruction into the signals that steer the datapath
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.
- These are the main decoder's equations for six instruction types:
lw,sw, R-type, I-type arithmetic,beqandjal. 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.
Predict
add and sub have the same opcode. Which control signals differ between them?
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".
Name it
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
lwandsw), 01 means subtract (for comparing inbeq), 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
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.
Worked example
Decode 0x00C58533 into control signals.
Show the solution
- Opcode. The low 7 bits are 0110011: R-type.
- Main decoder. RegWrite = 1, ALUSrc = register, MemWrite = 0, the result comes from the ALU, Branch = Jump = 0, and ALUOp = 10.
- ALU decoder. funct3 (bits 14:12) is 000 and bit 30 is 0, so the ALU adds.
- 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.
Worked example
addi t0, t0, -1024 encodes as 0xC0028293, with bit 30 set. Why doesn't the ALU subtract?
Show the solution
- 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. - 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.
- Apply it to
addi.addi's opcode is 0010011, bit 5 is 0, so it adds. - Without that extra term.
addiwould 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.
Worked example
What does adding jalr (opcode 1100111) to this processor take?
Show the solution
- A new decoder row. RegWrite = 1, ImmSrc = I, ALUSrc = immediate, MemWrite = 0, Result = PC + 4, and a new kind of jump.
- 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.
- What's already there. Everything else (the register file write of PC + 4, the I-type immediate) already exists for
jalandaddi.
Answer a new row in the main decoder, and a third PC multiplexer input (the ALU result) with a second select bit.
Check
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.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write RegWrite for the same six types. Every one writes a register except sw and beq.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write PCSrc, which selects PC + offset as the next PC.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
An instruction word is 00C58533.
(a)
What's its opcode (bits 6:0)?
(b)
What do bits 11:7 hold, as a number?
Check
Match each funct3 value to its R-type operation.
- Aadd or subtract
- Bexclusive OR
- COR
- DAND
- Eshift left logical
Check
Why does the ALU decoder require opcode bit 5 to be 1 before it lets bit 30 select subtraction?
Check
A microprogrammed control store holds 256 micro-instructions of 24 bits each.
(a)
How big is it?
(b)
How many address bits does its micro-PC need?
Check
Which instruction can't run on the single-cycle datapath without adding a new input to the PC multiplexer?
Explain why
Why does the control unit split into a main decoder and an ALU decoder?
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
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.