C8 · Lesson 37 · The multi-cycle processor
One memory, one ALU, and an instruction spread over several clocks
The single-cycle processor gives every instruction one long clock, as long as a load needs, and it needs an instruction memory, a data memory, an ALU and two more adders, because everything happens at once. Split each instruction into short steps instead, one per clock, and one memory and one ALU can be reused from step to step. Short instructions take fewer steps. Whether that's faster depends on how evenly the steps divide the work, and the arithmetic is the same one that decides how deep to pipeline.
- The flat line is the single-cycle processor: 750 ps per instruction, whatever it is.
- The rising line is the multi-cycle processor: loads take 5 steps, branches 3 and everything else 4, so its average time depends on the share of loads (along the bottom) and branches (slider).
- Slide the step time. At 250 ps the multi-cycle design is slower for any mix; it would need steps of about 180 ps to break even, and real steps can't shrink that far, because the memory alone takes 200 ps.
Predict
The multi-cycle processor's clock is 250 ps, and beq takes 3 steps. How long does a branch take, against the single-cycle processor's 750 ps?
750 ps: three steps of 250. Branches break even, loads (5 steps, 1250 ps) lose, and the others (4 steps, 1000 ps) lose too. The design saves hardware; it only saves time when the steps are short and well balanced.
Name it
Steps and the registers between them
Each step does one major operation (a memory access, a register-file access or an ALU operation) and saves its result in a register for the next step. Those nonarchitectural registers are invisible to programs: IR (the instruction), OldPC, A and B (the two source registers), ALUOut and Data (a word read from memory). IR is needed because the one memory is used again for data, so the instruction must be kept somewhere.
| Step | Does |
|---|---|
| Fetch | IR ← memory[PC]; the ALU computes PC + 4 |
| Decode | read rs1 and rs2 into A and B; the ALU computes OldPC + immediate, the branch target |
| MemAdr | ALUOut ← A + immediate (loads and stores) |
| MemRead / MemWrite | Data ← memory[ALUOut], or memory[ALUOut] ← B |
| ExecuteR / ExecuteI | ALUOut ← A op B, or A op immediate |
| Write back | rd ← Data or ALUOut |
| Branch | compare A and B; if equal, PC ← target |
So lw takes 5 cycles (Fetch, Decode, MemAdr, MemRead, write back), sw 4, R-type and I-type 4, beq 3 and jal 4.
Control is a state machine
The control unit becomes a finite state machine, one state per step, that branches after Decode according to the opcode and returns to Fetch when the instruction is done. About a dozen states cover these instructions. Each state's outputs are the control signals for that step.
Timing and performance
The clock must fit the slowest step, typically
and an instruction's time is its cycle count times . Averaged over a program,
Why it matters
The multi-cycle processor uses less hardware: one memory, one ALU, a few registers. It's rarely faster than the single-cycle design, because the steps are unequal and each one pays the register overhead. But it introduced the idea that makes processors fast: if an instruction is a sequence of short steps, different instructions can be in different steps at the same moment. That's pipelining, the subject of lessons 39 and 40.
Worked example
A multi-cycle processor has = 40 ps, multiplexers of 30 ps, an ALU of 120 ps, a memory of 200 ps and a register setup of 50 ps. A program is 25% loads, 10% stores, 11% branches, 2% jumps and 52% R-type. How long does an instruction take on average?
Show the solution
- Find the slowest step. The ALU path is 120 + 30 = 150 ps, shorter than the memory's 200, so
- CPI.
- Time per instruction. which is 1.77 times the single-cycle 750 ps.
- Break-even. To break even the steps would need to be 750/4.14 = 181 ps, less than the memory takes.
Answer 1325 ps per instruction on average, 1.77 times the single-cycle 750 ps.
Worked example
A loop body of lw, add, addi and bne runs 1000 times. How many cycles does the multi-cycle processor take at 320 ps, and how does it compare with the single-cycle one at 750 ps?
Show the solution
- Cycles per pass.
lw,add,addiandbnetake - The whole loop. 16,000 cycles at 320 ps:
- Single-cycle. It runs 4000 instructions at 750 ps: 3.0 µs.
- Compare. The multi-cycle design is 1.7 times slower on this loop, which is 25% loads and 25% branches.
Answer 16,000 cycles, 5.12 µs, against 3.0 µs for the single-cycle processor.
Worked example
What hardware does the multi-cycle design save, and what does it add?
Show the solution
- It saves a memory. Instructions and data share one, with a multiplexer choosing PC or ALUOut as the address.
- It saves both extra adders. The ALU computes PC + 4 during Fetch and the branch target during Decode, steps when it's otherwise idle.
- It adds registers. Six 32-bit registers, IR, OldPC, A, B, ALUOut and Data: 192 flip-flops.
- It adds control. A few wider multiplexers, and a control unit that is a state machine instead of a truth table.
Answer it saves a memory and two adders, and adds six 32-bit registers (192 flip-flops), a few wider multiplexers and a state-machine control unit.
Check
A program is 25% loads, 8% stores and 20% branches; the rest are R-type. The multi-cycle clock is 320 ps.
(a)
What's the CPI?
(b)
What's the average time per instruction?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A multi-cycle datapath has = 30 ps, multiplexers of 25 ps, an ALU of 250 ps, a memory of 200 ps and a setup time of 40 ps.
(a)
What's the clock period?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
(b)
How long does a load take?
Check
A loop body has 1 loads, 5 R-type instructions and one branch, and runs 100 times.
(a)
How many cycles does it take on the multi-cycle processor?
(b)
And on the single-cycle one?
Check
Put the multi-cycle steps of lw t1, 8(t0) in order.
- Data ← memory[ALUOut]
- IR ← memory[PC], and the ALU computes PC + 4
- ALUOut ← A + 8
- t1 ← Data
- Read t0 into A
Check
Why does the multi-cycle processor need the IR register, when the single-cycle one doesn't?
Check
Adding instructions grows the control state machine to 13 states.
(a)
How many flip-flops does a binary state encoding need?
(b)
And a one-hot encoding?
Check
A single-cycle processor runs at period T; a multi-cycle one averages c cycles per instruction.
Step 1
Below what step time is the multi-cycle design faster?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What step time would make even loads (5 cycles) no slower than in the single-cycle design?
Explain why
The multi-cycle processor is usually slower than the single-cycle one. Why was it worth inventing?
It uses far less hardware: one memory instead of two and one ALU doing the work of the ALU and two adders, because each is used in a different step of the instruction. Before memory was cheap, that saving mattered more than speed. It also showed that an instruction can be broken into a fixed sequence of short steps with registers between them. Once that's done, nothing stops a new instruction from starting its first step while the previous one is in its second. Overlapping the steps is pipelining, which keeps the short clock and brings the CPI back near 1.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.