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 8 bits Accumulator — where arithmetic and logic happen X 8 bits Index register — loop counters, table offsets Y 8 bits Index register — a second one, for two-pointer work SP 8 bits Stack pointer — top of the stack in page $01 PC 16 bits Program counter — address of the next instruction P (status): each bit is a flag — N V B D I Z C
The complete 6502 programmer's model. Three 8-bit data registers (A, X, Y), an 8-bit stack pointer, a 16-bit program counter, and a status byte of flags: Negative, oVerflow, Break, Decimal, Interrupt-disable, Zero, Carry. That's the whole CPU state.
NOTE: No multiply. No divide. No floating point. If you want 16-bit arithmetic you build it yourself out of 8-bit adds and the carry flag. This constraint is what makes 6502 programming feel like a puzzle game — and why Applesoft BASIC (Lecture 05) exists: it packages all that arithmetic for you.

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.

FETCH DECODE EXECUTE 6502 CPU PC $0300 IR -- A $00 IR = instruction register (internal) $A9 $42 Memory $0300A9 $030142 $03028D $030300 $030404 $040000 $0400 = top-left of the text screen TEXT SCREEN, ROW 0: ........................................

Animated: a real 6502 program, step by step. Watch the program counter advance, each byte travel over the bus into the CPU, and the final store land in screen memory. Use STEP to move one bus transaction at a time.

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:

ModeExampleMeaning
ImmediateLDA #$42A ← the literal value $42
Zero pageLDA $06A ← contents of address $0006 (fast: 1-byte address)
AbsoluteLDA $0400A ← contents of address $0400
Absolute,XLDA $0400,XA ← contents of ($0400 + X) — table indexing
Indirect indexedLDA ($06),YRead 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:

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?

← Lecture 01: History & Overview Lecture 03: Memory Map →