Datapath & Control Unit
How a single-cycle and multi-cycle processor wires registers, ALU and memory together, and how the control unit (hardwired or microprogrammed) drives them.
The datapath is the hardware that moves and transforms data: the PC, instruction memory, register file, ALU, data memory and the multiplexers between them. The control unit looks at each instruction and sets the control signals that tell the datapath what to do. Together they implement the ISA.
A single-cycle datapath
Every instruction completes in one (long) clock cycle. Here's the path a RISC-V lw or add takes:
| Stage | add x1, x2, x3 | lw x1, 8(x2) | sw x1, 8(x2) | beq x1, x2, L |
|---|---|---|---|---|
| Fetch | ✓ | ✓ | ✓ | ✓ |
| Read registers | x2, x3 | x2 | x1, x2 | x1, x2 |
| ALU | x2 + x3 | x2 + 8 (address) | x2 + 8 | x1 − x2 (zero?) |
| Memory | — | read | write | — |
| Write back | x1 | x1 | — | — |
| Next PC | PC + 4 | PC + 4 | PC + 4 | PC + 4 or target |
Control signals
The control unit decodes the opcode into signals such as:
| Signal | Chooses |
|---|---|
RegWrite | Whether to write the register file |
ALUSrc | Second ALU input: register or immediate |
ALUOp | Which operation the ALU performs |
MemRead / MemWrite | Whether to read or write data memory |
MemToReg | Write back the ALU result or the loaded value |
Branch | Whether the next PC may be the branch target |
Each is a wire into a MUX or an enable input: the "if statements" of hardware.
Why not single-cycle?
The clock period must fit the slowest instruction (a load: fetch + register read + ALU + memory + write back), so fast instructions waste most of their cycle.
Break execution into steps of about one memory access or ALU operation each; an instruction takes 3–5 short cycles depending on its type. Hardware can be reused across steps (one ALU, one memory). CPI is above 1 but the clock is much faster.
Use the same steps, but start a new instruction every cycle, with each stage working on a different instruction. Ideal CPI = 1 with a fast clock. This is what real CPUs do — see Pipelining.
Hardwired vs microprogrammed control
Control signals come from combinational logic (an FSM). Fast, but hard to change. Used by RISC cores and for the common, simple instructions in all modern CPUs.
Each machine instruction runs a small microprogram stored in a control ROM; each microinstruction sets the control signals for one step. Flexible and easy to extend; slower. Classic for CISC. x86 still uses microcode for complex instructions and can patch it with microcode updates (used for Spectre fixes).
Exceptions and interrupts in the datapath
When something unusual happens — an illegal instruction, a page fault, a divide by zero, or an external interrupt — the control unit must:
- Stop the faulting instruction (and everything after it) from changing state.
- Save the PC of the faulting instruction (RISC-V
mepc) and the cause (mcause). - Switch to privileged mode and jump to the OS trap handler.
- After the handler finishes, return to the saved PC (re-executing the instruction after, say, a page fault).
Doing this precisely in a pipelined, out-of-order CPU is why the reorder buffer exists. See I/O & interrupts.
- Datapath = PC, instruction memory, register file, ALU, data memory and the MUXes between them.
- The control unit decodes the opcode into control signals (RegWrite, ALUSrc, MemRead…).
- Single-cycle is simple but the clock must fit the slowest instruction.
- Multi-cycle reuses hardware; pipelining overlaps instructions for CPI ≈ 1.
- Hardwired control is fast; microprogrammed control is flexible and patchable.