| Mnemonic | Opcode | Bytes | Description |
|---|---|---|---|
| NOP | 0x00 | 1 | No operation |
| LDA addr | 0x01 | 2 | A ← RAM[addr] |
| ADD addr | 0x02 | 2 | A ← A + RAM[addr] |
| SUB addr | 0x03 | 2 | A ← A − RAM[addr] |
| STA addr | 0x04 | 2 | RAM[addr] ← A |
| LDI imm | 0x05 | 2 | A ← imm (0–255) |
| JMP addr | 0x06 | 2 | PC ← addr |
| JC addr | 0x07 | 2 | If CF: PC ← addr |
| JZ addr | 0x08 | 2 | If ZF: PC ← addr |
| AND addr | 0x09 | 2 | A ← A & RAM[addr] |
| OR addr | 0x0A | 2 | A ← A | RAM[addr] |
| XOR addr | 0x0B | 2 | A ← A ^ RAM[addr] |
| SHL | 0x0C | 1 | A ← A << 1, CF=msb |
| SHR | 0x0D | 1 | A ← A >> 1, CF=lsb |
| OUT | 0x0E | 1 | OUTPUT ← A |
| HLT | 0x0F | 1 | Halt clock |
| PUSH | 0x10 | 1 | RAM[SP]←A; SP-- |
| POP | 0x11 | 1 | SP++; A←RAM[SP] |
| CALL addr | 0x12 | 2 | Push PC; PC←addr |
| RET | 0x13 | 1 | Pop PC |
| IN | 0x14 | 1 | A ← INPUT reg |
| RTI | 0x15 | 1 | Return from interrupt |
| JNZ addr | 0x16 | 2 | If !ZF: PC←addr |
| JNC addr | 0x17 | 2 | If !CF: PC←addr |
| LDB imm | 0x18 | 2 | B ← imm |
| CMP addr | 0x19 | 2 | Flags only: A−RAM[addr] |
Why Pipelining?
Our SAP-1 is a non-pipelined (sequential) design: each instruction must fully complete before the next one starts. A pipelined CPU overlaps stages, so while Instruction N is executing, Instruction N+1 is being decoded and N+2 is being fetched.
The 3-Stage Classic Pipeline
Hazards
- Data Hazard: Instruction N+1 needs a result that N hasn't finished computing yet. Solved with forwarding or stall bubbles.
- Control Hazard: A branch (JMP/JC) changes PC — instructions already in the pipeline must be flushed. Branch prediction helps here.
- Structural Hazard: Two stages need the same hardware simultaneously (e.g., both IF and EX want RAM). Solved with separate instruction/data caches.
How This CPU Works
This is an extended SAP-1 (Simple As Possible) design inspired by Ben Eater's 8-bit breadboard computer, expanded with a 256-byte RAM, stack, more ALU operations, hardware interrupts, and a full 26-instruction set.
The Fetch-Decode-Execute Cycle
Every instruction runs through the same two fetch micro-steps first:
- T0 — Fetch (Addr): CO+MI — PC drives address bus, MAR latches it
- T1 — Fetch (Data): RO+II+CE — RAM[MAR] → IR, PC++
- T2+ — Execute: Instruction-specific micro-steps
2-byte instructions need an extra operand fetch at T2: CO+MI, then RO+?I+CE to read the operand byte.
The Stack
The stack grows downward from address 0xFF. SP starts at 0xFF. PUSH decrements SP after writing; POP increments SP before reading. CALL automatically pushes the return address; RET pops it.
PUSH: RAM[SP] ← A; SP-- POP: SP++; A ← RAM[SP] CALL: push PC (return addr); PC ← target RET: pop PC
Interrupts
Press the INT button to trigger a hardware interrupt. The CPU saves PC and flags to the stack, then jumps to the interrupt vector at 0xF0. Write your ISR (Interrupt Service Routine) starting at 0xF0 and end it with RTI.
Control Signals
| Signal | Meaning |
|---|---|
| HLT | Halt the clock |
| MI | MAR In — load address from bus |
| RI | RAM In — write bus to RAM[MAR] |
| RO | RAM Out — RAM[MAR] to data bus |
| II | IR In — load data bus into IR |
| AI | A In — load data bus into A |
| AO | A Out — A to data bus |
| BI | B In — load data bus into B |
| BO | B Out — B to data bus |
| EO | ALU Out — ALU result to data bus |
| SU | Subtract mode (ALU) |
| ANDI | AND mode (ALU) |
| ORI | OR mode (ALU) |
| XORI | XOR mode (ALU) |
| SHLI | Shift Left mode (ALU) |
| SHRI | Shift Right mode (ALU) |
| OI | Output In — load data bus into OUT |
| CE | Counter Enable — PC++ |
| CO | Counter Out — PC to bus |
| J | Jump — load PC from bus |
| FI | Flags In — update CF/ZF |
| SPO | SP Out — SP to address bus |
| SPI | SP In — load bus to SP |
| SPD | SP Decrement — SP-- |
| SPUP | SP Increment — SP++ |
| INO | Input Out — INPUT register to data bus |
Architecture Key Facts
- 8-bit data bus — all data in 8-bit chunks
- 8-bit address bus — 256 addressable RAM locations
- Stack: 8-bit SP, grows down from 0xFF
- Interrupt vector: 0xF0 (hardwired)
- Instruction format: opcode byte (1-byte instrs) or opcode + operand (2-byte)
- ALU: ADD, SUB, AND, OR, XOR, SHL, SHR — all 8-bit
Learning Tips
- Use Step T+1 to watch one micro-step at a time
- Use the Microcode tab to see — and edit — the control ROM
- Try the Challenges tab for guided exercises
- Click a memory address to set a breakpoint (red dot)
- Check the History tab to replay any past CPU state
- The INT button fires a hardware interrupt — write an ISR at 0xF0