Single-Cycle, Multicycle or Pipelined: CPI and One Program on Three Cores

The single-cycle core makes every instruction as slow as a load. There are two ways out. Both cut an instruction into steps. This page runs one program on all three cores, and compares the time.

You Will Be Able To

  • Describe a multicycle core as a state machine, and count the cycles of an instruction on it.
  • Work out the CPI of a program, and its time from the instruction count, the CPI and the clock period.
  • Compare a single-cycle, a multicycle and a pipelined core for one program.
Lesson
S2.23
Time
28 min
Runs on
Your browser
Check
6 questions

An add was ready after 96 levels and waited until 124. The cure seems plain: cut the long cycle into short steps, and let an add take fewer of them. A core built that way has a clock almost twice as fast. And it loses the race.

Before You Start

You need the critical path of S2.6: a load needs 123 levels, so the single-cycle clock period is 124.

You need a simple state machine: states, and a rule for the next state.

Five questions this page answers
Question
1How does a multicycle core run an instruction?
2What does CPI stand for, and what is it for a single-cycle core?
3Which three numbers give the time of a program?
4What sets the clock period of a multicycle core?
5Why can a core with a faster clock be the slower core?

The Idea: Three Numbers Make the Time

A multicycle core cuts each instruction into steps: fetch, decode, execute, memory, write back. Each step takes one short clock cycle. An instruction uses only the steps it needs. A branch is done after three, a load after five.

So instructions now take different numbers of cycles. The average over a program is its CPI: cycles per instruction. For the single-cycle core the CPI is exactly 1.

The time of a program is a product of three numbers. It is the number of instructions, times the CPI, times the clock period.

A pipelined core cuts the instruction into the same steps. But it does not wait for one instruction to finish. Each cycle, a new instruction starts, and the others move one step on. Five instructions are in the core at once.

So the pipelined core has the short clock of the multicycle core, and a CPI near 1. That is why almost every real core is pipelined. The next lessons build one.

New Words

New words on this page
WordMeaning
MulticycleA processor that takes several short clock cycles for each instruction: one for each step.
Step of an instructionOne part of the work of an instruction: fetch, decode, execute, memory or write back.
CPICycles per instruction: the clock cycles of a program, divided by the instructions it executes.
Instruction countThe number of instructions that a program executes. An instruction in a loop counts each time.
Execution timeThe time a program takes: instruction count times CPI times clock period.
PipelinedA processor that starts a new instruction each cycle, while older ones are still in later steps.
Pipeline stageOne step of a pipelined processor, with the hardware that does it.

One Picture: The Steps of Each Kind of Instruction

The Multicycle Core as a State MachineStep through the picture. Each state is one step of an instruction and takes one short clock cycle. An instruction visits only the steps it needs.
  • Control / FSM
The Multicycle Core as a State MachineStep through the picture. Each state is one step of an instruction and takes one short clock cycle. An instruction visits only the steps it needs. component Fetch read the word at thePC component Decode decode, and read tworegisters component Execute the ALU, the compare,the target component Memory read or write thedata memory component Write back write the register rd alwaysalwaysload, storearithmetic, jalbranch: doneloadstore: donedone
  1. component Fetchread the word at the PC
    • always → Decode
  2. component Decodedecode, and read two registers
    • always → Execute
  3. component Executethe ALU, the compare, the target
    • load, store → Memory
    • arithmetic, jal → Write back
    • branch: done → Fetch
  4. component Write backwrite the register rd
    • done → Fetch
  5. component Memoryread or write the data memory
    • load → Write back
    • store: done → Fetch
  1. An add: four steps

    Fetch, decode, execute, write back. An add has nothing to do in the memory step, so it skips it.

  2. A load: five steps

    A load is the only kind that needs every step. After the ALU has the address, the memory is read, and then the register is written.

  3. A store: four steps

    A store ends in the memory step. It writes no register.

  4. A branch: three steps

    A branch is decided in the execute step. Then the next instruction is fetched, from the target or from the next address.

Read the diagram as text
  • Fetch: read the word at the PC
  • Decode: decode, and read two registers
  • Execute: the ALU, the compare, the target
  • Memory: read or write the data memory
  • Write back: write the register rd
  • Fetch → Decode: always
  • Decode → Execute: always
  • Execute → Memory: load, store
  • Execute → Write back: arithmetic, jal
  • Execute → Fetch: branch: done
  • Memory → Write back: load
  • Memory → Fetch: store: done
  • Write back → Fetch: done

The steps for each kind of instruction are a choice of this page. A real multicycle core may cut them differently.

The steps, and what each costs
StepWhat happensLevelsUsed by
Fetchthe instruction memory is read20every instruction
Decodethe decoder works, and two registers are read11every instruction
Executethe ALU, the branch compare and the target adder work65every instruction
Memorythe data memory is read or written20load, store
Write backthe load unit, and the write of rd7arithmetic, load, jal

The levels are those of the lab of S2.6. The longest step is execute: the clock period of the multicycle core is 66.

The Smallest Example, Worked by Hand

Take five instructions: addi, slli, lw, addi, add. Count their cycles on each core.

Five instructions on three cores
CoreCyclesClock periodTime, in levels
Single-cycle5: one each124620
Multicycle21: four each, and five for the lw661386
Pipelined9: five, and 4 to fill66594

Time is cycles times period. In the pipelined core, the first instruction needs 5 cycles, and each later one ends a cycle after it.

The multicycle core has the shorter clock, and takes more than twice the time. It needs 21 cycles where the single-cycle core needs 5.

The Rules

  1. Rule 1: Time is instruction count, times CPI, times clock period.

    A core is faster only if this product is smaller.

  2. Rule 2: A single-cycle core has a CPI of 1.

    Its clock period fits the longest instruction.

  3. Rule 3: A multicycle core takes one cycle for each step of an instruction.

    Its clock period fits the longest step.

  4. Rule 4: The CPI of a program depends on its mix of instructions.

    On a multicycle core, many loads raise it and many branches lower it.

  5. Rule 5: A pipelined core completes one instruction in each cycle, once it is full.

    It loses cycles when it must throw work away, as after a taken branch.

  6. Rule 6: Never compare two cores by clock period alone, or by CPI alone.

    Only the product counts.

Sources for These Rules

Two things on this page are not measured on a real core. The first is the multicycle core. The core ap_rv32 has no multicycle build, so the lab holds a small state machine. Its number of steps for each kind of instruction is a choice of this page, close to what textbooks commonly show. The second is the clock periods. They follow from the levels of the lab of S2.6, where two delays are a choice. The plan of this track points to Patterson and Hennessy, sections 4.5 and 4.14. It also points to Harris and Harris, sections 7.2 and 7.4. Those places are not checked. The pipelined core is counted in the lab, and its costs are measured. The five-stage build of ap_rv32 was measured on its pipeline view, with memories that answer at once. A taken branch or a jump loses 2 cycles. A load with its reader right behind loses 1. A program with no such event takes its instructions plus 4 cycles. The measurement is in docs/soc/ap_rv32-blueprint.md, section 14.2, and S2.7 to S2.10 show it.

Now Run It

The lab at the end of this page takes the program of S2.21. It executes 15 instructions: nine arithmetic ones, one load, one store, three branches and one jal. The real core gave that list.

The lab first shows the first five instructions on each core. Then it counts the whole program.

Printed by the lab on Icarus. F, D, E, M and W are the five steps. A dot is a cycle before an instruction starts.
single-cycle | addi | one cycle of 124 levels
single-cycle | slli | one cycle of 124 levels
single-cycle | lw   | one cycle of 124 levels
single-cycle | addi | one cycle of 124 levels
single-cycle | add  | one cycle of 124 levels
multicycle | addi | F D E W
multicycle | slli | F D E W
multicycle | lw   | F D E M W
multicycle | addi | F D E W
multicycle | add  | F D E W
pipelined | addi | F D E M W
pipelined | slli | . F D E M W
pipelined | lw   | . . F D E M W
pipelined | addi | . . . F D E M W
pipelined | add  | . . . . F D E M W
single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 124 | time 1860
multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 66 | time 3828
pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 66 | time 1650

Rule 3 at work: the five rows of the multicycle core. The lw has five letters, the others four.

Rule 5 at work: the five rows of the pipelined core. Each instruction starts one cycle after the one before. In the fifth cycle, all five are in the core, each in another step.

Now the three totals. The multicycle core needs 58 cycles: 9 times 4, then 5, 4, 3 times 3, and 4. Its CPI is 58 divided by 15.

Rule 6 at work: the multicycle core has the short clock, and the worst time: 3828 against 1860.

The pipelined core needs 25 cycles. Fifteen are for the instructions, and 4 to fill. Two branches are taken and one jal jumps: each costs 2 cycles.

Its time is 1650. That is the best of the three, though not by much. The program is short, so the 4 cycles to fill weigh a lot.

Why It Is Built This Way

CoreWhat it buysWhat it costs
Single-cycleThe simplest control: one table, no state.A clock period as long as the longest instruction. Every instruction pays it.
MulticycleA short clock. One ALU and one memory port can be used in several steps.A state machine, and registers between the steps. Several cycles for every instruction.
PipelinedA short clock and a CPI near 1.Registers between the stages, and logic for instructions that depend on each other: the next four lessons.

The multicycle core is rare in new designs. It is still worth knowing. The steps of a pipeline are its steps, and a core that waits for a slow memory behaves like one.

The Trap: The Faster Clock That Lost

Two cores are on the table. One runs at a period of 124, the other at 66. Many people would call the second one nearly twice as fast. It is the multicycle core, and it needs twice the time.

Why is it so bad? Look at one load. Its five steps need 20, 11, 65, 20 and 7 levels. That is 123 in all. The multicycle core gives each step a full period of 66. Five periods are 330.

The steps are far from equal. The period fits the ALU, and the other four steps waste most of their cycle. What if the ALU had only 20 levels?

The same lab with an ALU of 20 levels. Nothing else is changed.
single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 79 | time 1185
multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 21 | time 1218
pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 21 | time 525

Now the steps are nearly equal, and the period drops to 21. The multicycle core comes close: 1218 against 1185. It still does not win. Every step is rounded up to a full period.

The pipelined core gains the most. It is now more than twice as fast as the single-cycle core. A short clock pays only when the CPI stays low.

So the question about a clock is always: how much work does one cycle do?

At Scale

This page15 instructions, 3 cores
A real workloadbillions of instructions, measured on the chip

This section looks ahead. Every processor is compared by this product of three numbers.

  1. Long programs

    The 4 cycles to fill a pipeline vanish in a long run. What remains are the cycles lost at branches: S2.10 to S2.12.

  2. CPI below 1

    A core that starts two or more instructions in each cycle can have a CPI under 1: S2.15.

  3. Memory in the CPI

    A load that waits for a slow memory adds cycles. In real systems that part of the CPI is often the largest: S3.

  4. Counting on the chip

    A core counts its own cycles and instructions in two registers. Software divides them to get the CPI: S8.6.

Try It

Run the lab. Check the 58 cycles of the multicycle core by hand, from the mix of the program.

Give a branch only two steps in the state machine. How does the CPI of the multicycle core change?

Set T_MEMORY to 80. Which numbers of the three totals change, and which core is hit hardest?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: single-cycle | addi | one cycle of 124 levels single-cycle | slli | one cycle of 124 levels single-cycle | lw | one cycle of 124 levels single-cycle | addi | one cycle of 124 levels single-cycle | add | one cycle of 124 levels multicycle | addi | F D E W multicycle | slli | F D E W multicycle | lw | F D E M W multicycle | addi | F D E W multicycle | add | F D E W pipelined | addi | F D E M W pipelined | slli | . F D E M W pipelined | lw | . . F D E M W pipelined | addi | . . . F D E M W pipelined | add | . . . . F D E M W single-cycle | 15 instructions | 15 cycles | CPI 1.00 | period 124 | time 1860 multicycle | 15 instructions | 58 cycles | CPI 3.87 | period 66 | time 3828 pipelined | 15 instructions | 25 cycles | CPI 1.67 | period 66 | time 1650

Check Yourself

Predict the outputIn this lab the data memory has 80 levels in place of 20. The program and the cycle counts are as before. What does the last line say?
// The data memory is slower here: 80 levels. What does the last line say?
// The 15 instructions are those that the program of S2.21 executes, in order.
// The single-cycle core is the real one. The multicycle core is the state machine below.
// The pipelined core is counted here, with the costs measured on the five-stage core of S2.7 to S2.10.
module tb;
  // ---- The delay of each step, in levels of gates (the blocks of S2.6).
  localparam int T_FETCH   = 20 ;   // the instruction memory
  localparam int T_DECODE  = 11 ;   // the decoder, beside the register read
  localparam int T_EXECUTE = 65 ;   // the ALU
  localparam int T_MEMORY  = 80 ;   // the data memory
  localparam int T_WRITE   = 7  ;   // the load unit, on the way to the register file

  // ---- The clock period of each core: one level longer than its longest path.
  function automatic int largest(input int a, input int b);
    return (a > b) ? a : b;
  endfunction
  localparam int PERIOD_SINGLE = T_FETCH + T_DECODE + T_EXECUTE + T_MEMORY + T_WRITE + 1;   // a load, in one cycle
  localparam int PERIOD_STEP   = largest(largest(largest(T_FETCH, T_DECODE), largest(T_EXECUTE, T_MEMORY)), T_WRITE) + 1;   // the longest single step

  // ---- The program: the kind of each instruction that runs, in order.
  typedef enum { ARITHMETIC, LOAD, STORE, BRANCH_TAKEN, BRANCH_NOT_TAKEN, JUMP } kind_t;
  localparam int COUNT = 15;
  kind_t kind [0:COUNT-1];
  string name [0:COUNT-1];
  initial begin
    kind[0] = ARITHMETIC;        name[0] = "addi";
    kind[1] = ARITHMETIC;        name[1] = "slli";
    kind[2] = LOAD;              name[2] = "lw";
    kind[3] = ARITHMETIC;        name[3] = "addi";
    kind[4] = ARITHMETIC;        name[4] = "add";
    kind[5] = ARITHMETIC;        name[5] = "addi";
    kind[6] = BRANCH_TAKEN;      name[6] = "bne";
    kind[7] = ARITHMETIC;        name[7] = "add";
    kind[8] = ARITHMETIC;        name[8] = "addi";
    kind[9] = BRANCH_TAKEN;      name[9] = "bne";
    kind[10] = ARITHMETIC;        name[10] = "add";
    kind[11] = ARITHMETIC;        name[11] = "addi";
    kind[12] = BRANCH_NOT_TAKEN;  name[12] = "bne";
    kind[13] = STORE;             name[13] = "sw";
    kind[14] = JUMP;              name[14] = "jal";
  end

  // ---- The multicycle core as a state machine. One state for each step; an instruction visits only the steps it needs.
  typedef enum { FETCH, DECODE, EXECUTE, MEMORY, WRITE_BACK } state_t;
  logic   clk = 0;
  state_t state;
  int     at, cycles_multi;
  string  line;
  bit     done = 0;
  function automatic string letter(input state_t s);
    case (s)
      FETCH: return "F";  DECODE: return "D";  EXECUTE: return "E";  MEMORY: return "M";  default: return "W";
    endcase
  endfunction
  always @(posedge clk) if (!done) begin
    cycles_multi++;
    line = {line, " ", letter(state)};
    case (state)
      FETCH:   state <= DECODE;
      DECODE:  state <= EXECUTE;
      EXECUTE: if (kind[at] == LOAD || kind[at] == STORE) state <= MEMORY;
               else if (kind[at] == BRANCH_TAKEN || kind[at] == BRANCH_NOT_TAKEN) state <= FETCH;   // a branch is done after three steps
               else state <= WRITE_BACK;
      MEMORY:  if (kind[at] == LOAD) state <= WRITE_BACK;
               else state <= FETCH;                                                                // a store is done after four
      default: state <= FETCH;
    endcase
    // The last step of an instruction: print its steps and go to the next instruction.
    if (state == WRITE_BACK || (state == MEMORY && kind[at] == STORE) || (state == EXECUTE && (kind[at] == BRANCH_TAKEN || kind[at] == BRANCH_NOT_TAKEN))) begin
      line = "";
      if (at == COUNT - 1) done = 1;
      else at++;
    end
  end

  int redirects, cycles_single, cycles_pipe;
  string pad;
  initial begin
    state = FETCH;  at = 0;  cycles_multi = 0;  line = "";
    while (!done) begin #5 clk = 1;  #5 clk = 0; end
    // ---- The three totals.
    cycles_single = COUNT;                                       // one cycle for each instruction
    redirects = 0;
    for (int n = 0; n < COUNT; n++) if (kind[n] == BRANCH_TAKEN || kind[n] == JUMP) redirects++;
    cycles_pipe = COUNT + 4 + 2 * redirects;                     // 4 cycles to fill, 2 lost at each taken branch or jump
    $display("single-cycle | %0d instructions | %0d cycles | CPI %.2f | period %0d | time %0d", COUNT, cycles_single, real'(cycles_single) / COUNT, PERIOD_SINGLE, cycles_single * PERIOD_SINGLE);
    $display("multicycle | %0d instructions | %0d cycles | CPI %.2f | period %0d | time %0d", COUNT, cycles_multi, real'(cycles_multi) / COUNT, PERIOD_STEP, cycles_multi * PERIOD_STEP);
    $display("pipelined | %0d instructions | %0d cycles | CPI %.2f | period %0d | time %0d", COUNT, cycles_pipe, real'(cycles_pipe) / COUNT, PERIOD_STEP, cycles_pipe * PERIOD_STEP);
    $finish;
  end
endmodule
ConceptHow does a multicycle core run an instruction?
ConceptWhat does CPI stand for, and what is it for a single-cycle core?
ConceptWhich three numbers give the time of a program?
ConceptWhat sets the clock period of a multicycle core?
ConceptThe multicycle core of the lab has a period of 66 against 124, and loses. Why?

Common Mistakes

Wrong Call the core with the shortest clock period the fastest.

A short period with many cycles for each instruction can take longer in all.

Fix Multiply: instruction count, CPI and clock period.

Wrong Give the multicycle core the average of the step delays as its period.

One clock serves every step. It must fit the longest one.

Fix Take the longest step. The other steps waste the rest of their cycle.

Wrong Count a pipelined core as one cycle for each instruction, and nothing more.

It needs cycles to fill, and it loses cycles after a taken branch or jump.

Fix Add the cycles to fill and the cycles lost.

Remember

  • Time is instruction count times CPI times clock period. Only the product compares two cores.
  • A single-cycle core has a CPI of 1 and a long period. A multicycle core has a short period and a CPI of 3 to 5.
  • A multicycle period fits the longest step, so unequal steps waste time.
  • A pipelined core has the short period and a CPI near 1. It pays with cycles to fill and cycles lost at taken branches.

Practice

Report a problem with this page