đī¸ Control Unit
The Control Unit is the brain's conductor â it generates timing and control signals that orchestrate every operation in the CPU. Learn how instructions are decoded and how control flows through the processor.
What is a Control Unit?
Definition
Instruction Decode
Interprets opcode and funct fields to determine which operation to perform
Control Signal Generation
Produces RegWrite, MemRead, ALUSrc, ALUOp, and other timing signals
Sequencing
Determines the next micro-instruction or state in the instruction cycle
Hardwired vs Microprogrammed Control
Tradeoff: Speed vs Flexibility
Microprogrammed Control
Advantages:
â Flexible & easy to modify
â Systematic design
â Handles complex ISAs
Disadvantages:
â Slower (memory access)
â Larger area
â More power
Best for: CISC processors (x86)
Finite State Machine for Instruction Execution
FSM-based Control
S0: IFetch
MAR â PC, MDR â MEM[MAR], IR â MDR, PC â PC + 4
S1: Decode
Control unit decodes opcode, reads register file, sign-extends immediate
S2: Execute
ALU performs operation on operands, computes branch targets
S3: Memory
Load/store: MEM[MAR] â MDR or MDR â MEM[MAR]
S4: WriteBack
ALU result or memory data written to register file
How the Control Unit Decodes Instructions
Instruction Fetch
1 cycleGet instruction from memory at PC address
Instruction Decode
1 cycleExtract opcode, registers, immediate; generate control signals
Operand Fetch
1 cycleRead register file or sign-extend immediate
Execute
1-3 cyclesALU operation, address calculation, or branch evaluation
Memory Access
1 cycleLoad/store to data memory (skipped for ALU instructions)
Write Back
1 cycleWrite result to destination register
R-type vs I-type Decoding
Control Signal Generation Example
verilog
// Simplified control unit in Verilog
module control_unit (
input logic [5:0] opcode,
input logic [5:0] funct,
output logic reg_write, mem_read, mem_write,
output logic alu_src, mem_to_reg, reg_dst,
output logic [1:0] alu_op,
output logic branch
);
logic is_rtype = (opcode == 6'b000000);
always_comb begin
// Default values
{reg_write, mem_read, mem_write} = 3'b000;
{alu_src, mem_to_reg, reg_dst} = 3'b000;
alu_op = 2'b00;
branch = 1'b0;
case (opcode)
6'b000000: begin // R-type (add, sub, and, or, slt, etc.)
reg_write = 1'b1; reg_dst = 1'b1; alu_op = 2'b10;
// funct field selects specific ALU operation
end
6'b100011: begin // lw (load word)
reg_write = 1'b1; mem_read = 1'b1;
alu_src = 1'b1; mem_to_reg = 1'b1;
alu_op = 2'b00; // ADD for address calc
end
6'b101011: begin // sw (store word)
mem_write = 1'b1; alu_src = 1'b1;
alu_op = 2'b00; // ADD for address calc
end
6'b000100: begin // beq (branch if equal)
branch = 1'b1; alu_op = 2'b01; // SUB for comparison
end
endcase
end
endmoduleMicro-operations (Micro-ops)
Micro-ops Decomposition
| Instruction | Micro-op Sequence | Active Components |
|---|---|---|
| ADD R1, R2, R3 | Fetch â Decode â Read RF â ALU â Write RF | PC, MEM, IR, RegFile, ALU |
| LW R1, 0(R2) | Fetch â Decode â Read RF â Addr Calc â MEM â Write RF | PC, MEM, IR, RegFile, ALU |
| BEQ R1, R2, L | Fetch â Decode â Read RF â SUB â Branch Check | PC, MEM, IR, RegFile, ALU |
| JAL R1, TARGET | Fetch â Decode â PC+4 â Write RF â PC â Target | PC, MEM, IR, RegFile, ALU |
Interview Questions
Explain the difference between hardwired and microprogrammed control units.
Hardwired control uses combinational logic gates (PLA, ROM, or random logic) to generate control signals directly from the instruction opcode. It is fast but inflexible. Microprogrammed control stores micro-instructions in a control store ROM. Each machine instruction is implemented as a sequence of micro-instructions. It is slower (ROM access time) but easier to design, modify, and extend â ideal for CISC ISAs with complex instructions.
How does a control unit decode an R-type vs an I-type instruction?
For R-type instructions (opcode = 0 in MIPS), the control unit uses the funct field (bits 5:0) to determine the specific ALU operation. RegDst=1 selects rd as the destination register. For I-type instructions, the opcode directly encodes the operation. ALUSrc=1 selects the sign-extended immediate as the second ALU operand. MemRead/MemWrite are asserted for load/store instructions. Branch is asserted for branch instructions to modify PC.
What is the role of the FSM in control unit design?
The control unit FSM manages the instruction cycle through discrete states (Fetch, Decode, Execute, Memory, WriteBack). Each state maps to a clock cycle. The current state and the instruction opcode determine the next state and the control signals output. The FSM ensures proper sequencing: for example, a LOAD instruction goes through Fetch â Decode â AddrCalc â Memory â WriteBack, while an ADD skips the Memory state.
How does a control unit handle branching and how does it affect the pipeline?
The control unit detects branch instructions in the Decode stage. It asserts the Branch signal, which causes the ALU to compare source registers. The zero output from the ALU, combined with the Branch signal, selects the next PC (branch target vs PC+4). In pipelined processors, branches cause control hazards. Modern CUs use branch prediction, branch target buffers (BTB), and delayed branch slots to mitigate these penalties.