The Multi-Cycle CPU
In chapter 31 we saw that the single-cycle CPU is slow because the clock period is set by the slowest instruction (here: lw, 800 ps). In this chapter we will split every instruction into short steps of one clock cycle each. Every instruction uses only as many steps as it needs, so the clock can be short, and fast instructions finish earlier. Our FPGA CPU in chapter 27 already needed two clock cycles per instruction because of the block RAM, so we are already a bit familiar with the idea.
Tip | All examples of this chapter are in the folder |
From one long cycle to several short ones
As the picture above shows, the single-cycle CPU executes every instruction in one long clock cycle (with the critical path taking 800 ps). The multi-cycle CPU splits every instruction into several short steps of one clock cycle each (here 200 ps, since this is the slowest step).
One step per clock cycle means that the clock period is set by the slowest step, not by the slowest instruction - this is a difference: the slowest instruction (lw) takes 5 steps, but the clock period is set by the slowest step (200 ps).
Note:
steps of 100 ps are padded to 200 ps - this is where some of the gain gets lost (see performance section).
Which instruction needs which steps?
Instruction | IF | ID | EX | MEM | WB | Cycles |
R-type, addi | x | x | x | x | 4 | |
lw | x | x | x | x | x | 5 |
sw | x | x | x | x | 4 | |
beq, bne | x | x | x | 3 |
Explanation: sw writes no register, a branch is decided in EX (compare + new PC), R-type needs no memory.
These are the step counts that chapter 31 already used for its preview.
The data path in steps
Problem : Values must survive from one clock cycle to the next. In the single-cycle CPU everything happened between two edges; now the instruction, the operands and the results are needed in later cycles.
Solution : Registers between the steps (RTL picture from chapter 24): - IR - instruction register: holds the instruction during all steps (the PC already points to the next one) - OldPC - address of the current instruction, written together with IR: the branch offset is relative to the branch itself, but after FETCH the PC already points to the next instruction - A, B - the two operands read from the register file - ALUOut - result of the ALU (address for lw/sw, result for R-type, branch target) - MDR - memory data register: the value read by lw Sharing hardware (a second advantage of multi-cycle): - one ALU instead of ALU + two adders: PC + 4 in IF, branch target in ID, the actual operation in EX - optionally one memory for instructions and data (classic multi-cycle design) - they are used in different steps Design decision (as implemented in hdl-training/32_multicycle): two memories as in chapter 26, one shared ALU (PC + 4 in FETCH, branch target in DECODE, operation in EXEC/ADDR/BRANCH). Mention that the classic textbook version also shares ONE memory for instructions and data.
The control unit becomes a finite state machine
Single-cycle: the control unit was pure combinational logic - opcode in, control signals out (chapter 17).
Multi-cycle: the control signals depend on the opcode AND on the current step ⇒ the control unit needs a state. It becomes a Moore machine from chapter 14.
States (first version):
FETCH => DECODE => one of: EXEC_R | ADDR (lw/sw) | BRANCH EXEC_R => WB_ALU => FETCH ADDR => MEM_READ => WB_MEM => FETCH (lw) ADDR => MEM_WRITE => FETCH (sw) BRANCH => FETCH
addi has its own state EXEC_I (A + imm), then the same WB_ALU as R-type. (implemented names: FETCH, DECODE, EXEC_R(egister), EXEC_I(mmediate), ADDR, MEM_READ, MEM_WRITE, WB_ALU, WB_MEM, BRANCH)
| State | What happens | IRWrite | PCWrite | PCWriteCond | PCSource | ALUSrcA | ALUSrcB | ALUOp | MemRead | MemWrite | RegWrite | MemtoReg |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
FETCH | IR ← Mem[PC], OldPC ← PC, PC ← PC + 4 | 1 | 1 | 0 | ALU | PC | 4 | add | 0 | 0 | 0 | – |
DECODE | A ← Reg[rs1], B ← Reg[rs2], ALUOut ← OldPC + imm | 0 | 0 | 0 | – | OldPC | imm | add | 0 | 0 | 0 | – |
EXEC_R | ALUOut ← A op B | 0 | 0 | 0 | – | A | B | funct | 0 | 0 | 0 | – |
EXEC_I | ALUOut ← A + imm | 0 | 0 | 0 | – | A | imm | add | 0 | 0 | 0 | – |
ADDR | ALUOut ← A + imm (address) | 0 | 0 | 0 | – | A | imm | add | 0 | 0 | 0 | – |
MEM_READ | MDR ← Mem[ALUOut] | 0 | 0 | 0 | – | – | – | – | 1 | 0 | 0 | – |
MEM_WRITE | Mem[ALUOut] ← B | 0 | 0 | 0 | – | – | – | – | 0 | 1 | 0 | – |
WB_ALU | Reg[rd] ← ALUOut | 0 | 0 | 0 | – | – | – | – | 0 | 0 | 1 | ALUOut |
WB_MEM | Reg[rd] ← MDR | 0 | 0 | 0 | – | – | – | – | 0 | 0 | 1 | MDR |
BRANCH | A − B; if taken: PC ← ALUOut | 0 | 0 | 1 | ALUOut | A | B | sub | 0 | 0 | 0 | – |
Encoding in the code: ALUSrcA 00 PC, 01 OldPC, 10 A; ALUSrcB 00 B, 01 4, 10 imm;
ALUOp 00 add, 01 sub, 10 funct (operation from funct3/funct7); PCSource 0 ALU result, 1 ALUOut; MemtoReg 0 ALUOut, 1 MDR.
A, B and ALUOut have no enable: they are written in every cycle.
Note | microprogramming - instead of an FSM in logic, the control signals per step are stored in a small ROM (historically important, e.g. in CISC processors). |
The control unit in Verilog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Control unit of the multi-cycle CPU (chapter 32): a Moore machine (chapter 14).
//
// Every instruction runs through several states, one state per clock cycle:
//
// FETCH -> DECODE -> EXEC_R -> WB_ALU R-type (add, sub, and, or) 4 cycles
// -> EXEC_I -> WB_ALU addi 4 cycles
// -> ADDR -> MEM_READ -> WB_MEM lw 5 cycles
// -> ADDR -> MEM_WRITE sw 4 cycles
// -> BRANCH beq, bne 3 cycles
//
// The outputs depend only on the state (Moore). Only the next state depends on the opcode.
module control_fsm (
input wire clk,
input wire rst,
input wire [6:0] opcode, // opcode of the instruction in the instruction register IR
output reg [3:0] state, // current state (for testbench and waveform)
// control signals
output reg pc_write, // PC <= next PC (unconditional)
output reg pc_write_cond, // PC <= branch target, if the branch is taken
output reg pc_source, // 0: ALU result (PC + 4), 1: ALUOut (branch target)
output reg ir_write, // IR <= instruction, OldPC <= PC
output reg [1:0] alu_src_a, // 00: PC, 01: OldPC, 10: register A
output reg [1:0] alu_src_b, // 00: register B, 01: constant 4, 10: immediate
output reg [1:0] alu_op, // 00: add, 01: subtract (compare), 10: from funct3/funct7
output reg mem_read, // data memory: read (result goes into MDR)
output reg mem_write, // data memory: write register B to address ALUOut
output reg reg_write, // register file: write rd
output reg mem_to_reg // 0: write ALUOut, 1: write MDR into rd
);
// ---- states --------------------------------------------------------------------------
localparam FETCH = 4'd0,
DECODE = 4'd1,
EXEC_R = 4'd2,
EXEC_I = 4'd3,
ADDR = 4'd4,
MEM_READ = 4'd5,
MEM_WRITE = 4'd6,
WB_ALU = 4'd7,
WB_MEM = 4'd8,
BRANCH = 4'd9;
// ---- opcodes ---------------------------------------------------------------------------
localparam OP_R = 7'b0110011,
OP_I = 7'b0010011,
OP_LW = 7'b0000011,
OP_SW = 7'b0100011,
OP_BRANCH = 7'b1100011;
// ---- state register ------------------------------------------------------------------
reg [3:0] next_state;
always @(posedge clk)
if (rst) state <= FETCH;
else state <= next_state;
// ---- next-state logic ------------------------------------------------------------------
always @(*) begin
case (state)
FETCH: next_state = DECODE;
DECODE: case (opcode)
OP_R: next_state = EXEC_R;
OP_I: next_state = EXEC_I;
OP_LW, OP_SW: next_state = ADDR;
OP_BRANCH: next_state = BRANCH;
default: next_state = FETCH; // unknown instruction: skip it
endcase
EXEC_R: next_state = WB_ALU;
EXEC_I: next_state = WB_ALU;
ADDR: next_state = (opcode == OP_LW) ? MEM_READ : MEM_WRITE;
MEM_READ: next_state = WB_MEM;
MEM_WRITE: next_state = FETCH;
WB_ALU: next_state = FETCH;
WB_MEM: next_state = FETCH;
BRANCH: next_state = FETCH;
default: next_state = FETCH;
endcase
end
// ---- output logic: control signals per state -----------------------------------------
always @(*) begin
// default: nothing happens
pc_write = 0; pc_write_cond = 0; pc_source = 0; ir_write = 0;
alu_src_a = 2'b00; alu_src_b = 2'b00; alu_op = 2'b00;
mem_read = 0; mem_write = 0; reg_write = 0; mem_to_reg = 0;
case (state)
FETCH: begin // IR <= instruction, PC <= PC + 4
ir_write = 1;
alu_src_a = 2'b00; alu_src_b = 2'b01; alu_op = 2'b00;
pc_write = 1; pc_source = 0;
end
DECODE: begin // A, B <= registers; ALUOut <= OldPC + imm (branch target)
alu_src_a = 2'b01; alu_src_b = 2'b10; alu_op = 2'b00;
end
EXEC_R: begin // ALUOut <= A op B
alu_src_a = 2'b10; alu_src_b = 2'b00; alu_op = 2'b10;
end
EXEC_I: begin // ALUOut <= A + imm
alu_src_a = 2'b10; alu_src_b = 2'b10; alu_op = 2'b00;
end
ADDR: begin // ALUOut <= A + imm (memory address)
alu_src_a = 2'b10; alu_src_b = 2'b10; alu_op = 2'b00;
end
MEM_READ: begin // MDR <= memory[ALUOut]
mem_read = 1;
end
MEM_WRITE: begin // memory[ALUOut] <= B
mem_write = 1;
end
WB_ALU: begin // rd <= ALUOut
reg_write = 1; mem_to_reg = 0;
end
WB_MEM: begin // rd <= MDR
reg_write = 1; mem_to_reg = 1;
end
BRANCH: begin // compare A - B; if taken: PC <= ALUOut (target)
alu_src_a = 2'b10; alu_src_b = 2'b00; alu_op = 2'b01;
pc_write_cond = 1; pc_source = 1;
end
endcase
end
endmodule
state register: always @(posedge clk), the only part with a clock; synchronous reset to FETCH; 10 states ⇒ 4 bits.
next-state logic: always @(*), pure combinational logic; only DECODE and ADDR look at the opcode, every other state has exactly one successor; unknown opcode ⇒ back to FETCH.
output logic: always @(*), pure combinational logic; the control signals depend only on the state (Moore machine); the table above is implemented as a case statement.
note: EXEC_I and ADDR are identical, but they are separate states because they have different successors (WB_ALU vs. MEM_READ/MEM_WRITE) ==== The testbench
The testbench feeds one opcode of each type and prints the states. The output shows the state sequence and the number of cycles for each instruction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
`timescale 1ns / 1ps
// Testbench for the control FSM: for every instruction type, the sequence of states is checked.
module control_fsm_tb;
reg clk = 0, rst = 1;
reg [6:0] opcode = 7'b0;
wire [3:0] state;
wire pc_write, pc_write_cond, pc_source, ir_write, mem_read, mem_write, reg_write, mem_to_reg;
wire [1:0] alu_src_a, alu_src_b, alu_op;
always #5 clk = ~clk;
control_fsm dut (
.clk(clk), .rst(rst), .opcode(opcode), .state(state),
.pc_write(pc_write), .pc_write_cond(pc_write_cond), .pc_source(pc_source), .ir_write(ir_write),
.alu_src_a(alu_src_a), .alu_src_b(alu_src_b), .alu_op(alu_op),
.mem_read(mem_read), .mem_write(mem_write), .reg_write(reg_write), .mem_to_reg(mem_to_reg)
);
// state names for the output
function [8*9-1:0] name(input [3:0] s);
case (s)
0: name = "FETCH"; 1: name = "DECODE"; 2: name = "EXEC_R";
3: name = "EXEC_I"; 4: name = "ADDR"; 5: name = "MEM_READ";
6: name = "MEM_WRITE";7: name = "WB_ALU"; 8: name = "WB_MEM";
9: name = "BRANCH"; default: name = "?";
endcase
endfunction
integer errors = 0;
// runs one instruction from FETCH until the next FETCH, prints the states, checks the number of cycles
task run(input [6:0] op, input [8*6-1:0] label, input integer expected_cycles);
integer cycles;
begin
opcode = op;
cycles = 0;
$write("%-6s:", label);
$write(" %0s", name(state)); // first state: FETCH
@(negedge clk); cycles = 1;
while (state != 0) begin // until we are back in FETCH
$write(" -> %0s", name(state));
@(negedge clk); cycles = cycles + 1;
end
$display(" (%0d cycles)", cycles);
if (cycles != expected_cycles) begin
$display(" ERROR: expected %0d cycles", expected_cycles);
errors = errors + 1;
end
end
endtask
initial begin
@(negedge clk); rst = 0; // now in FETCH
run(7'b0110011, "R-type", 4);
run(7'b0010011, "addi", 4);
run(7'b0000011, "lw", 5);
run(7'b0100011, "sw", 4);
run(7'b1100011, "branch", 3);
if (errors == 0) $display("PASSED");
else $display("FAILED: %0d errors", errors);
$finish;
end
endmodule
Output:
R-type: FETCH => DECODE => EXEC_R => WB_ALU (4 cycles) addi : FETCH => DECODE => EXEC_I => WB_ALU (4 cycles) lw : FETCH => DECODE => ADDR => MEM_READZ => WB_MEM (5 cycles) sw : FETCH => DECODE => ADDR => MEM_WRITE (4 cycles) branch: FETCH => DECODE => BRANCH (3 cycles) PASSED
These are exactly the paths in the state diagram and the cycle counts from the table above.
Performance: was it worth it?
With the performance equation from chapter 31, measured with the testbench (instruction mix + cycles): single-cycle: 33 × 1.0 × 800 ps = 26.4 ns multi-cycle: 33 × 3.7 × 200 ps = 24.4 ns ⇒ only 1.08× faster
Why so little?
steps are not equally long: ID (100 ps) and WB (100 ps) are padded to 200 ps,
every step adds the overhead of a register (clock-to-Q + setup, critical path box in chapter 31) - ignored so far,
the counter program has no lw, which would profit most (800 ps ⇒ 5 × 200 ps = 1000 ps: even slower!) ⇒ careful: per instruction, multi-cycle is only faster for branches (600 ps vs. 800 ps).
Where multi-cycle really helps: less hardware (one ALU, optionally one memory) - historically the main reason.
The real problem remains: in every cycle, most parts are idle. During EX of one instruction, the instruction memory could already fetch the next one … (we will see this in chapter 33).
Outlook: the assembly line
What if every step works on a DIFFERENT instruction at the same time? Same short clock (200 ps), but one instruction finishes in every cycle ⇒ CPI close to 1.
Preview from chapter 31: 37 cycles, 7.4 ns, 3.6× faster than single-cycle.
Try it yourself
Draw the state sequence for lw, sw and bne by hand, then compare with the testbench output.
The lw program from chapter 31: how many cycles does it need on the multi-cycle CPU? Measure it.
for which instruction mix is the multi-cycle CPU slower than the single-cycle CPU? (hint: lw)
Add a state for a new instruction (e.g. jal, or slt from chapter 26) - which states can be reused?
With 20 ps register overhead per step (clock-to-Q + setup): T = 220 ps - how does the result change?
Summary
Multi-cycle: one instruction is split into steps of one clock cycle each; the clock is set by the slowest step.
Registers between the steps keep the values (IR, A, B, ALUOut, MDR); hardware can be shared.
The control unit becomes a finite state machine (Moore machine from chapter 14).
CPI rises from 1 to 3.7, T falls from 800 to 200 ps - for our program only 8 % faster.
The parts are still idle most of the time ⇒ pipelining in chapter 33.
(Outlined with Claude AI, written by Sven Wehrend)