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.
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):
- Fetch — PC ships its address to instruction memory. The instruction lands in the IR. PC increments.
- Decode — Control unit sets RegWrite=1, ALUSrc=0 (register), ALUOp=ADD, MemRead=0, MemToReg=0 (ALU result).
- Register Read — R1 and R2 values appear on the register file outputs.
- Execute — The ALU adds both values. Flags update.
- 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):
- Fetch/Decode — Control signals assert MemRead=1, MemToReg=1 and ALUSrc=1 (immediate offset).
- Execute — The ALU adds R4 and the instruction's immediate offset (zero here) to form the address, so ALUSrc=1 selects the immediate.
- Memory — Data memory reads the value sitting at that address.
- 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:
Opcode | RegWrite | ALUSrc | ALUOp | MemRead | MemWrite | MemToReg | Branch-------|----------|--------|-------|---------|----------|----------|-------ADD | 1 | 0 | ADD | 0 | 0 | 0 | 0SUB | 1 | 0 | SUB | 0 | 0 | 0 | 0LDR | 1 | 1 | ADD | 1 | 0 | 1 | 0STR | 0 | 1 | ADD | 0 | 1 | X | 0BEQ | 0 | 0 | SUB | 0 | 0 | X | 1Notice 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.
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.
| Signal | Value | Effect |
|---|---|---|
| RegWrite | 1 | Register file writes a result |
| ALUSrc | 0 | ALU input B is a register |
| ALUOp | ADD | ALU adds |
| MemRead | 0 | No memory read |
| MemWrite | 0 | No memory write |
| MemToReg | 0 | Write-back takes the ALU result |
| Branch | 0 | Not 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
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |