C836 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, 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.

Predict

add and sub have the same opcode. Which control signals differ between them?

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 lw and sw), 01 means subtract (for comparing in beq), and 10 means "look at funct3".
OpcodeTypeRegWriteImmSrcALUSrcMemWriteResultBranchJumpALUOp
0000011lw1Iimm0memory0000
0100011sw0Simm1none0000
0110011R-type1nonereg0ALU0010
0010011I-type1Iimm0ALU0010
1100011beq0Breg0none1001
1101111jal1Jnone0PC + 401none

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

PCSrc=Branch⋅Zero+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.

Worked example

Decode 0x00C58533 into control signals.

Show the 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.

Worked example

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

Show the 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.

Worked example

What does adding jalr (opcode 1100111) to this processor take?

Show the 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

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.

  1. Aadd or subtract
  2. Bexclusive OR
  3. COR
  4. DAND
  5. 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?

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.