Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 67 ยท CPU Design

The CPU Datapath

Follow data through a processor: program counter, instruction memory, register file, ALU, data memory and multiplexers, with a Python datapath model.

Inside a processor there are only a handful of building blocks: a place that holds the next instruction's address, a memory that supplies instructions, a small bank of registers, an arithmetic unit, a data memory, and some switches that choose between alternatives. Together they form the datapath, the wiring along which numbers travel while an instruction runs. Understanding it means you can answer a question that sounds simple: what exactly happens, wire by wire, when the CPU executes add r3, r1, r2?

This lesson walks through each component, shows how one datapath serves several kinds of instructions by switching a few multiplexers, and gives you a Python model to run. Who flips those switches is the job of the control unit; here we simply hand the datapath its control signals. You should know the fetch-decode-execute cycle and the ALU.

A small RISC machine

From now on the CPU Design lessons use one small RISC processor in the style of MIPS and RISC-V. Its rules are chosen to keep the hardware simple:

  • 16-bit words, 8 registers r0 to r7, and r0 always reads as 0.
  • Every instruction is one 16-bit word. Instruction and data memory are separate (a Harvard-style view, as discussed in the Von Neumann lesson).
  • Only LW and SW touch data memory. Everything else works on registers.
Instruction Meaning
ADD rd, rs, rt (also SUB, AND, OR) rd = rs op rt
ADDI rt, rs, n rt = rs + n (n is a signed 6-bit number)
LW rt, n(rs) rt = memory[rs + n]
SW rt, n(rs) memory[rs + n] = rt
BEQ rs, rt, label if rs == rt, jump n instructions from the next one
J label jump to an absolute instruction number
HALT stop

The 16 bits are split into fields. In the R-format (register) the fields are op(4) rs(3) rt(3) rd(3) unused(3). In the I-format (immediate) they are op(4) rs(3) rt(3) imm(6). The J-format is op(4) address(12). More about encoding choices is in Instruction Formats and Encoding.

The components

flowchart LR
  PC["PC"] --> IM["Instruction<br/>memory"]
  IM --> RF["Register file<br/>2 read ports, 1 write port"]
  RF -->|"A"| ALU["ALU"]
  RF -->|"B"| M1(["mux: ALUSrc"])
  IM -->|"imm (sign-extended)"| M1
  M1 --> ALU
  ALU --> DM["Data<br/>memory"]
  ALU --> M2(["mux: MemToReg"])
  DM --> M2
  M2 -->|"write data"| RF
  PC --> ADD1["PC + 1"]
  ADD1 --> M3(["mux: next PC"])
  IM -->|"branch / jump target"| M3
  M3 --> PC

A simplified datapath: rounded boxes are multiplexers that the control signals set.

Walk along it from the left.

  • Program counter (PC). A register holding the address of the current instruction. It is updated at the end of every instruction.
  • Instruction memory. Given the PC, it outputs the 16-bit instruction. It only reads.
  • Register file. A tiny memory with two read ports and one write port. The rs and rt fields select two registers whose values appear at once on outputs A and B; writing happens only if a "register write" signal is on.
  • Sign extender. The 6-bit immediate is widened to 16 bits by copying its top bit, so negative offsets stay negative (see two's complement).
  • ALU. Takes two inputs and an operation code, produces a result and a zero flag, which is how BEQ checks equality. See Building an ALU.
  • Data memory. Reads a word at the ALU's result (the address) or writes register B's value there.
  • Multiplexers (muxes). A mux picks one of its inputs based on a control signal. They are what let one datapath do many jobs.
  • PC adder. Computes PC + 1 (our instructions are one word each).

Why the muxes matter

Each mux answers one question about the instruction in flight.

Mux (control signal) Choose Because
ALUSrc register B or the immediate ADD needs a register, ADDI and LW need a constant
RegDst rd field or rt field as destination R-format puts the destination in rd, I-format in rt
MemToReg ALU result or data memory output ADD writes the ALU result, LW writes what it loaded
Next PC PC + 1, branch target or jump target sequential code, taken branch, or J

Without muxes you would need a separate datapath for each instruction type. With them, one set of wires handles all.

One instruction through the wires

The first TryIt is the model. State is a dictionary with the PC, registers and memories. step follows the diagram in order: fetch, read registers, choose the ALU input, run the ALU, access memory, choose the write-back value, write the register, choose the next PC. The control signals come from the CONTROL table, which we explain properly in the next lesson.

It then runs a single ADD r3, r1, r2 with r1 = 7 and r2 = 5 and prints what is on each wire.

Python A datapath model: one ADD

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.

You can read the whole story in the output: the instruction word, its fields, the signals that are on (reg_dst and reg_write), what the ALU did, and where the result went. Notice that mem_read and mem_write are off: the data memory is present in the datapath, but this instruction leaves it idle.

Five instruction types, one datapath

The strength of the design is that the same step function runs every instruction. Only the control signals change. The next TryIt adds a small assembler (it packs the fields into 16-bit words using the formats above) and runs a program that uses ADDI, ADD, SW, LW and BEQ, printing the signals, the ALU result and the next PC of each instruction.

Python Running a program through the datapath

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.

Study the signal column. ADDI turns on alu_src and reg_write. SW turns on alu_src and mem_write but not reg_write, because a store changes memory only. LW is the only instruction with both mem_read and mem_to_reg. The BEQ row shows an ALU result of 0 (12 - 12), a zero flag of True and a next PC of 7: the branch skipped instruction 6, so r5 stays 0.

Writing 0 into dest is ignored in the model, which is how a hardwired zero register behaves in real RISC chips: the write may happen in hardware, but the register is wired to read as 0.

How long does one instruction take?

In a single-cycle design every instruction finishes in one clock tick, so the clock must be slow enough for the longest path through the datapath. Each component takes some time. The next TryIt uses made-up but plausible delays and adds them up for each instruction class, following the path that instruction uses through the diagram.

Python Critical path of each instruction

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 clock period is set by LW at 660 ps (about 1,515 MHz), yet a branch needs only 390 ps and a jump 170 ps, so they simply wait. With the instruction mix in the code, the average instruction needs 520 ps, so about 21% of every cycle is wasted. This waste is the reason single-cycle designs are not used for fast processors, and it points toward the pipelining you will study in Instruction Pipelining.

Datapath and the instruction cycle

The five phases you met in the first lessons now have physical meaning:

Phase Datapath parts used
Fetch PC, instruction memory, PC adder
Decode / register read register file, sign extender
Execute ALU
Memory data memory (only LW and SW)
Write back MemToReg mux, register file write port

That table is also the plan for the five pipeline stages later in the track. The datapath does not change; it only gets cut into slices.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the register file have two read ports?

    An R-format instruction needs rs and rt together. Two read ports deliver both values in the same step.
  2. 2. What does the ALUSrc multiplexer choose between?

    ADD takes its second operand from a register, while ADDI, LW and SW take a constant (the immediate or an offset).
  3. 3. Which signal pair identifies a load (LW)?

    A load reads data memory (mem_read) and writes that value, not the ALU result, to a register (mem_to_reg).
  4. 4. In a single-cycle design, what sets the clock period?

    Every instruction gets one cycle, so the cycle must be long enough for the longest datapath path.

Summary

  • The datapath is the set of components and wires that carry values during an instruction: PC, instruction memory, register file, sign extender, ALU, data memory, adders and multiplexers.
  • Multiplexers let one datapath serve many instruction types; control signals set them.
  • R-format instructions use two registers and write rd; I-format instructions use an immediate and write rt; loads and stores use the ALU to compute an address.
  • The ALU's zero flag drives BEQ; a taken branch changes the next PC.
  • In a single-cycle machine the slowest path, normally LW, decides the clock period and the faster instructions waste time.

Continue with The Control Unit: Hardwired and Microprogrammed.