Lecture 02
The 6502 CPU
The MOS Technology 6502 is the heart of the Apple II — and of the Commodore 64, the Atari 2600, the BBC Micro, and the NES. It has just six programmer-visible registers and about 90 instructions, yet it ran a whole generation of computing. This lecture teaches you its mental model; Lecture 06 will make you program it.
1. Why the 6502?
In 1975 a capable microprocessor like the Intel 8080 or Motorola 6800 cost around $175. Chuck Peddle's team at MOS Technology launched the 6502 at $25. Wozniak, building a computer with his own money, chose it for exactly that reason. The chip was not only cheap: its simple, regular design and clever memory tricks made it surprisingly fast for its clock speed. In the Apple II it runs at about 1.023 MHz — roughly one million clock cycles per second, with typical instructions taking 2–7 cycles.
2. The registers — all six of them
A register is a tiny storage cell inside the CPU itself, accessible in a single cycle. The 6502 gives the programmer six:
- A (accumulator) — the workhorse. Addition, subtraction, AND/OR/EOR, comparisons: they all happen "through" A. Think of it as the CPU's single pair of hands.
- X and Y (index registers) — used to count loops and to index into tables: the instruction
LDA $0400,Xloads from address $0400 plus whatever is in X. - SP (stack pointer) — points into the fixed stack area ($0100–$01FF). Subroutine calls (
JSR) push a return address here;RTSpops it. - PC (program counter) — the only 16-bit register: it holds the address of the next instruction. Jumps and branches work by rewriting the PC.
- P (status flags) — updated automatically by most instructions. The Zero and Carry flags drive nearly all decision-making: "branch if the result was zero" (
BEQ), "branch if carry set" (BCS), and so on.
3. The heartbeat: fetch → decode → execute
Every CPU ever made runs the same eternal loop: fetch the next instruction from memory (at the address in PC), decode what it means, execute it, repeat — about a million times a second on the Apple II.
The animation below runs a real two-instruction program exactly the way the 6502 does. The program is:
0300: A9 42 LDA #$42 ; load the value $42 into A
0302: 8D 00 04 STA $0400 ; store A into address $0400
Address $0400 happens to be the first character of the top row of the text screen —
so when this program finishes, a flashing B appears in the top-left corner of the
display (byte $42 in screen memory means "flashing B"). Two instructions, five bytes, and you've
done graphics programming.
4. Instructions and addressing modes
A 6502 instruction is one opcode byte, optionally followed by one or two operand bytes. The same operation (say, "load A") exists in several flavors depending on where the data comes from — these flavors are called addressing modes:
| Mode | Example | Meaning |
|---|---|---|
| Immediate | LDA #$42 | A ← the literal value $42 |
| Zero page | LDA $06 | A ← contents of address $0006 (fast: 1-byte address) |
| Absolute | LDA $0400 | A ← contents of address $0400 |
| Absolute,X | LDA $0400,X | A ← contents of ($0400 + X) — table indexing |
| Indirect indexed | LDA ($06),Y | Read a 16-bit pointer from $06/$07, add Y, load from there |
The essential instruction families — you will meet all of these in Lecture 06:
- Moves:
LDA/LDX/LDY(load),STA/STX/STY(store),TAX/TXA/TAY/TYA(transfer) - Arithmetic:
ADC(add with carry),SBC(subtract),INC/DEC/INX/INY/DEX/DEY - Logic & shifts:
AND, ORA, EOR, ASL, LSR, ROL, ROR - Compare & branch:
CMP/CPX/CPYthenBEQ, BNE, BCC, BCS, BMI, BPL - Flow:
JMP(goto),JSR/RTS(call/return),BRK(break to Monitor)
5. Cycles: why programmers counted them
Each instruction takes a fixed, documented number of clock cycles: LDA #$42 takes 2,
STA $0400 takes 4, a taken branch takes 3. At 1.023 MHz you get about
one million cycles per second — and a hi-res screen has 7,680 bytes. Redrawing the whole
screen even once costs tens of thousands of cycles, so fast Apple II games were won and lost on cycle
counting. You'll feel this personally in the exercises when your BASIC ball animation flickers and your
assembly version doesn't.
Check your understanding
Q1. Which register would you use to walk through a 20-byte table in memory?
Q2. LDA #$05 then CMP #$05 executes. Which flag makes BEQ branch?
Q3. How many bytes is the instruction STA $0400?