Skip to main content
Be Bitwise

Building a Simple Processor

Digital Logic Design · Hardware Foundations track

4 min read

In this module

All the Pieces Come Together

You've now got adders, flip-flops, registers, multiplexers, decoders, and an ALU. Time to wire them up into a working processor — a simplified one, sure, but built on the same principles a real CPU uses.

The Datapath

The datapath is the set of components data physically flows through while an instruction executes. Our simple processor's datapath has:

  • Program Counter (PC) — a register holding the address of the next instruction.
  • Instruction Memory — stores the program. The PC's value indexes in to fetch the current instruction.
  • Instruction Register (IR) — latches the fetched instruction.
  • Register File — a bank of general-purpose registers (R0 through R7 in our design). It supports two simultaneous reads and one write per cycle.
  • ALU — performs whatever computation the instruction calls for.
  • Data Memory — main memory for load and store operations.
  • Multiplexers — sprinkled through the datapath, MUXes pick between alternative data sources based on control signals.
Key Concept

The datapath is the hardware pathway through which data flows during instruction execution. It connects the PC, instruction memory, register file, ALU, data memory, and multiplexers. The control unit orchestrates the datapath by generating the right control signals for each instruction.

The Control Unit

The control unit reads the opcode out of the IR and emits control signals that configure every component. The crucial ones are RegWrite, ALUSrc, ALUOp, MemRead, MemWrite, MemToReg, and Branch. The control unit itself is just a combinational decoder, mapping each opcode to its own unique cocktail of those signals.

Tracing Instructions Through the Datapath

Trace ADD R0, R1, R2 (add R1 and R2, drop the result into R0):

  1. Fetch — PC ships its address to instruction memory. The instruction lands in the IR. PC increments.
  2. Decode — Control unit sets RegWrite=1, ALUSrc=0 (register), ALUOp=ADD, MemRead=0, MemToReg=0 (ALU result).
  3. Register Read — R1 and R2 values appear on the register file outputs.
  4. Execute — The ALU adds both values. Flags update.
  5. Writeback — The MUX picks the ALU result and it gets written into R0.

Now trace LDR R3, [R4] (load from the memory address held in R4):

  1. Fetch/Decode — Control signals assert MemRead=1, MemToReg=1 and ALUSrc=1 (immediate offset).
  2. Execute — The ALU adds R4 and the instruction's immediate offset (zero here) to form the address, so ALUSrc=1 selects the immediate.
  3. Memory — Data memory reads the value sitting at that address.
  4. Writeback — The MUX picks the memory data (not the ALU result), which gets written into R3.

Decoding Opcodes into Control Signals

The control unit's logic maps each opcode to a signal pattern:

c
Opcode | RegWrite | ALUSrc | ALUOp | MemRead | MemWrite | MemToReg | Branch
-------|----------|--------|-------|---------|----------|----------|-------
ADD | 1 | 0 | ADD | 0 | 0 | 0 | 0
SUB | 1 | 0 | SUB | 0 | 0 | 0 | 0
LDR | 1 | 1 | ADD | 1 | 0 | 1 | 0
STR | 0 | 1 | ADD | 0 | 1 | X | 0
BEQ | 0 | 0 | SUB | 0 | 0 | X | 1

Notice LDR and STR use ALUSrc=1 and ALUOp=ADD to compute base register plus offset as the memory address, while BEQ uses SUB to compare registers. If the zero flag comes back set and Branch is asserted, the PC jumps to the target.

Key Concept

The control unit is a combinational decoder that translates each opcode into a unique pattern of control signals. These signals configure the register file, ALU, memory, and multiplexers for each instruction type.

Click between ADD, LDR and BEQ below and watch RegWrite, ALUSrc and MemToReg flip as different wires light up. Then switch to step mode and walk LDR through all five stages, or tick "R1 equals R2" on BEQ to see which input the PC mux grabs.

Which wires does each instruction use?

Pick an instruction to see its control signals and the paths it drives. Greyed-out paths carry values nobody uses.

ADD R0, R1, R2 ; R0 = R1 + R2

Unused this time: immediate extender, data memory, branch target adder, Branch AND Zero gate.

PCInstructionmemoryIRControlRegisterfileRead 1Read 2WriteRegWrite=1Imm extend01ALUSrc=0ALUALUOp=ADDDatamemoryMemRead=0MemWrite=010MemToReg=0PC + 4TargetANDBranch=0Zero01
UsedNot usedDon't care
Control signals for ADD
SignalValueEffect
RegWrite1Register file writes a result
ALUSrc0ALU input B is a register
ALUOpADDALU adds
MemRead0No memory read
MemWrite0No memory write
MemToReg0Write-back takes the ALU result
Branch0Not a branch

Values match the lesson's control table. The PC + 4 adder assumes 4-byte instructions, as in 32-bit ARM and RISC-V.

How This Connects to Real CPUs

Real CPUs run these same principles at wildly bigger scale: hundreds of instructions, pipelined stages, out-of-order execution, multiple ALUs and floating-point units, layered cache hierarchies. Peel back all those optimisations, though, and the same core sits underneath. A register file, an ALU, a control unit decoding opcodes into signals, and a clock driving fetch-decode-execute around billions of times a second.

Explore the Processor

Step through the simulator below to watch data flow through the datapath instruction by instruction. The simulator shows each instruction's opcode and the registers it changes, not the control lines themselves, so at each step use the table above to name the signals the control unit would assert.

The simulator uses the same two-operand instruction set as the previous module (ADD R2, R1 means R2 = R2 + R1), so the three-operand ADD R0, R1, R2 traced above becomes a copy followed by an add.

For the best simulation experience, use a desktop browser.

Assembly

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded