Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 67 ยท CPU Design

Building a Single-Cycle CPU

Combine the datapath and the control unit into a complete single-cycle CPU, run real programs on a Python simulator, and see why one cycle per instruction is slow.

You now have all the pieces: a datapath that can move and combine values, and a control unit that decides which wires are active. This lesson joins them into a working processor, the simplest complete design there is: the single-cycle CPU. Every instruction is fetched, executed and finished within exactly one clock cycle, so the cycles-per-instruction (CPI) is always 1.

You will write the whole CPU as a small Python class, run two real programs on it (a multiplication loop and a Fibonacci table), watch the registers change cycle by cycle, and then measure the design's big weakness: a clock that must wait for the slowest instruction.

How the pieces fit

In a single-cycle machine, nothing is remembered between the steps of an instruction. Everything happens along one long combinational path from the PC to the register write, and at the end of the cycle three things are updated together: the register file (if reg_write is on), data memory (if mem_write is on) and the PC.

flowchart LR
  subgraph STATE["State, updated at the clock edge"]
    PC["PC"]
    RF["Registers"]
    DM["Data memory"]
  end
  PC --> IM["Instruction memory"]
  IM --> CU["Control unit"]
  IM --> DP["Datapath logic<br/>reg read, mux, ALU, mux"]
  CU -->|"control signals"| DP
  DP --> RF
  DP --> DM
  DP --> PC

Everything between two clock edges is combinational logic; only the PC, registers and data memory hold state.

Two consequences follow from this structure:

  1. Separate instruction and data memories. One memory cannot be read twice (instruction and data) in one cycle, so the CPU has two, like a Harvard machine.
  2. Duplicated hardware. The ALU is busy computing the result or address, so the PC increment and the branch target need their own adders. Nothing can be shared between steps, because all steps happen in the same cycle.

The complete simulator

The class below is the entire CPU. Its cycle method follows the datapath order: fetch, read the registers, choose the ALU input, run the ALU, access memory, choose the write-back value, write the register, compute the next PC. The control signals come from the same CONTROL table you met in the last two lessons (a hardwired unit would produce them with a decoder and OR gates). An assembler turns text into 16-bit words using the formats from the datapath lesson, so the programs below are real machine code in instruction memory.

The first program multiplies 3 by 2 by repeated addition, because the machine has no multiply instruction. With trace=True you see the cycle number, the instruction and the registers r1 to r3 after each one.

Python A single-cycle CPU: multiply by repeated addition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Follow the trace and you can watch the loop work: r3 goes 0, 3, 6, r2 counts 2, 1, 0, and when BEQ finds r2 == r0 the PC jumps over J loop to the store. The last line confirms CPI = 1.0, because the machine does exactly one instruction per cycle.

What the CPU can and cannot do

The instruction set is small on purpose. There is no multiply, no shift, no immediate form of AND, and the immediate is only 6 bits (-32 to 31), so bigger constants need several instructions. A branch can reach only 31 instructions forward or 32 back from the next instruction; J reaches anywhere in the 4,096-word space. Real instruction sets deal with the same limits using longer encodings or special "load upper immediate" instructions (see Instruction Formats and Encoding).

If you want to add an instruction, you touch the three places it lives: the opcode table and assembler, one row of CONTROL (which signals and ALU operation), and, if it needs something new from the ALU, the alu function. That is the same set of edits a hardware designer makes, in a different form.

A second program: arrays in memory

Loops and memory are the real test of a CPU. This program fills ten memory words with the first ten Fibonacci numbers using SW, then reads them back with LW and adds them up, storing the total at address 20. It also keeps a count of how many instructions of each kind ran, which we need for the timing analysis that follows.

Python Fibonacci table in memory, then a checksum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The array program fills the table with [0, 1, 1, 2, 3, 5, 8, 13, 21, 34], and the stored total is 88, which agrees with Python's own sum. The program is a good example of how the datapath handles memory: SW and LW both compute register + offset in the ALU, then use that as the data memory address.

Why one cycle per instruction is not enough

The timing numbers at the end of the TryIt (made up but realistic delays, the same as in the datapath lesson) expose three problems.

  1. The clock follows the slowest instruction. Every instruction, even a 170 ps jump, takes a full 660 ps cycle because the clock must suit LW. The 125 instructions of the Fibonacci program take 82,500 ps; if each instruction needed only its own path time, they would take 54,860 ps. A third of the time (33.5%) is waiting.
  2. Speeding up a fast instruction does not help. Only the longest path matters. Making jumps or branches faster changes nothing while LW still needs 660 ps; to shorten the clock you must shorten the load path itself.
  3. Hardware sits idle. While the data memory works on a load, the ALU and instruction memory are waiting. Each unit is busy a small fraction of the cycle.

The multicycle numbers show that splitting the work into steps is not a magic fix. With a 190 ps clock the multicycle design needs 89,680 ps for the same program, about 9% more than the single-cycle machine, because the steps are not perfectly balanced and every instruction pays for at least three whole cycles. Its real advantages are that it shares hardware (one ALU) and that it can treat complex instructions as longer sequences.

flowchart LR
  A["Single-cycle<br/>CPI 1, long clock"] -->|"split into steps"| B["Multicycle<br/>CPI 3-5, short clock"]
  B -->|"overlap the steps"| C["Pipelined<br/>CPI near 1, short clock"]

Pipelining combines the short clock of multicycle designs with the throughput of single-cycle designs.

The way out is not to make the cycle longer or the work shorter but to overlap the work of several instructions: while one instruction is writing its result, another is already using the ALU and a third is being fetched. That idea, pipelining, is the subject of Instruction Pipelining. It uses the very same datapath, cut into stages, and it is the reason real CPUs run at several gigahertz.

Why single-cycle designs still matter

They are not obsolete as teaching tools or as simple hardware:

  • Tiny microcontrollers and educational CPUs often use them because they are easy to verify.
  • Every other design is explained as a change to this one, so you can reason about what the change buys.
  • Their correctness argument is simple: each instruction is a pure function of the machine state.

When you build your own processor in the mini CPU project, you will recognize this exact structure.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the CPI of an ideal single-cycle CPU?

    Every instruction completes in exactly one clock cycle, so CPI is 1 by definition.
  2. 2. Why does a single-cycle CPU need separate instruction and data memories?

    In one cycle the CPU fetches an instruction and may also read or write data, which needs two separate access paths.
  3. 3. What determines the clock period of a single-cycle design?

    All instructions share one cycle length, so it must cover the slowest one.
  4. 4. Which pair of operations gets the data memory address from the ALU in our CPU?

    Loads and stores add the base register and offset in the ALU and use the sum as the memory address.
  5. 5. What does pipelining add to the single-cycle design?

    Pipelining cuts the same datapath into stages so several instructions are in progress at once.

Summary

  • A single-cycle CPU does fetch, decode, execute, memory access and write back for one instruction in one clock cycle; CPI is 1.
  • It needs separate instruction and data memories and extra adders for the PC, because nothing can be reused within a cycle.
  • State (PC, registers, data memory) is updated at the end of the cycle; everything before that is combinational logic driven by the control signals.
  • The whole machine fits in a short Python class, and programs such as loops and array fills run on it unchanged.
  • The clock is as slow as the slowest instruction (a load), so faster instructions waste time and hardware stands idle.
  • Pipelining keeps the short stage time and overlaps instructions to approach one completed instruction per cycle.

Next: Instruction Pipelining.