1. Commodore 64
The best-selling home computer of all time
The Commodore 64 sold somewhere between 12 and 17 million units between 1982 and 1994, making it the best-selling single computer model of all time. It shipped with 64 KB of RAM, a 1 MHz processor, and three custom chips that gave it graphics and sound capabilities far beyond its price point.
What made the C64 special wasn't raw power — it was accessibility. Turn it on
and you're in BASIC. Type POKE 53281,0 and the screen turns black. You're
already talking to the hardware. The distance between "hello world" and "I
understand how this machine works" was shorter on the C64 than on almost any
computer before or since.
This article covers the C64 from a programmer's perspective: the CPU, the memory map, the graphics chip, the sound chip, and how to drive it all from assembly language.
The System at a Glance
Commodore 64 Specifications
| CPU | MOS 6510 @ 0.985 MHz (PAL) / 1.023 MHz (NTSC) |
| RAM | 64 KB |
| ROM | 20 KB (BASIC, KERNAL, character generator) |
| Graphics | VIC-II — 320x200 hi-res, 160x200 multicolor, 8 hardware sprites |
| Sound | SID 6581/8580 — 3 oscillators, 4 waveforms, programmable filter |
| I/O | 2 CIA 6526 chips — joystick, keyboard, serial, timers |
| Video output | PAL or NTSC composite + RF |
The 6510 CPU
The heart of the C64 is the MOS 6510, a variant of the legendary MOS 6502. The only difference from a standard 6502 is an integrated 6-bit I/O port at addresses $00-$01 used for memory bank switching. In every other respect — the instruction set, the addressing modes, the cycle timing — it's a 6502.
Registers
The 6510 has very few registers compared to modern CPUs. You do almost everything through memory.
CPU Registers
| A | Accumulator (8-bit) | Main working register. Math, logic, loads, and stores all go through A. |
| X | Index X (8-bit) | Used for indexing, counting, and as a secondary working register. |
| Y | Index Y (8-bit) | Same as X but with different addressing modes available. |
| SP | Stack Pointer (8-bit) | Points into the hardware stack at $0100-$01FF. Grows downward. |
| PC | Program Counter (16-bit) | Address of the next instruction to execute. |
| P | Status Register (8-bit) | Flags: N (negative), V (overflow), B (break), D (decimal), I (interrupt disable), Z (zero), C (carry). |
That's it. Three 8-bit general-purpose registers (A, X, Y), an 8-bit stack pointer, a 16-bit program counter, and a flags register. No 16-bit arithmetic. No multiply or divide. No floating point. Every 16-bit operation requires multiple instructions.
Addressing Modes
The 6502 family compensates for its tiny register file with a rich set of addressing modes — different ways an instruction can specify its operand. This is where the architecture gets interesting.
Key Addressing Modes
| Immediate | LDA #$42 | Load the literal value $42 into A |
| Zero Page | LDA $FB | Load from address $00FB (1-byte address, fast) |
| Zero Page,X | LDA $FB,X | Load from address $00FB + X |
| Absolute | LDA $D020 | Load from 16-bit address $D020 |
| Absolute,X | LDA $D000,X | Load from $D000 + X |
| Absolute,Y | LDA $D000,Y | Load from $D000 + Y |
| (Indirect,X) | LDA ($FB,X) | Read pointer from zero page $FB+X, load from that address |
| (Indirect),Y | LDA ($FB),Y | Read pointer from zero page $FB, add Y, load from that address |
Zero Page is Your Register File
The first 256 bytes of memory ($0000-$00FF) are called the zero page. Accessing zero page takes one fewer byte and one fewer cycle than a full 16-bit address. With only three registers, experienced 6502 programmers treat the zero page as an extended register file — storing pointers, counters, and temporary values there for fast access.
The (Indirect),Y mode is particularly important. It lets you set up a 16-bit
pointer in the zero page, then index off it with Y. This is how you traverse
arrays, copy memory blocks, and access data structures on a CPU that has no
16-bit registers.
The Instruction Set
The 6502 has 56 official instructions. Here are the ones you'll use constantly:
Essential Instructions
| Load / Store | |||
LDA | Load A | STA | Store A |
LDX | Load X | STX | Store X |
LDY | Load Y | STY | Store Y |
| Arithmetic | |||
ADC | Add with carry (always CLC first for plain addition) | ||
SBC | Subtract with borrow (always SEC first for plain subtraction) | ||
INC/DEC | Increment / decrement memory | ||
INX/DEX | Increment / decrement X | ||
INY/DEY | Increment / decrement Y | ||
| Logic | |||
AND | Bitwise AND with A | ||
ORA | Bitwise OR with A | ||
EOR | Bitwise XOR with A | ||
ASL/LSR | Shift left / right (multiply / divide by 2) | ||
ROL/ROR | Rotate left / right through carry | ||
| Compare & Branch | |||
CMP | Compare A (sets flags, doesn't change A) | ||
CPX/CPY | Compare X / Y | ||
BEQ/BNE | Branch if equal / not equal (Z flag) | ||
BCC/BCS | Branch if carry clear / set | ||
BMI/BPL | Branch if minus / plus (N flag) | ||
JMP | Unconditional jump | ||
JSR/RTS | Jump to subroutine / return from subroutine | ||
| Stack | |||
PHA/PLA | Push / pull A to/from stack | ||
| Flags | |||
CLC/SEC | Clear / set carry flag | ||
CLI/SEI | Clear / set interrupt disable | ||
16-Bit Addition
Since the 6510 is an 8-bit CPU, adding two 16-bit numbers requires two steps, just like adding multi-digit numbers by hand — you add the low bytes first, then carry into the high bytes.
; Add 16-bit value at $FB/$FC to $FD/$FE
; Result in $FD/$FE
CLC ; clear carry before addition
LDA $FB ; load low byte of first number
ADC $FD ; add low byte of second number
STA $FD ; store result low byte
LDA $FC ; load high byte of first number
ADC $FE ; add high byte + carry from low byte
STA $FE ; store result high byte
This pattern — CLC, add low bytes, add high bytes with carry — is something
you'll write hundreds of times on the 6502.
Memory Map
The 6510 has a 16-bit address bus, giving it a 64 KB address space. But the C64 actually has 64 KB of RAM plus 20 KB of ROM plus I/O registers — more than 64 KB total. They share the same addresses through bank switching, controlled by the 6510's built-in I/O port at address $01.
C64 Memory Map
$FFFF ┌────────────────────┐
│ KERNAL ROM (8K) │ Operating system routines
$E000 ├────────────────────┤
│ I/O or CHAR ROM │ $D000-$D3FF: VIC-II
│ (4K window) │ $D400-$D7FF: SID
│ │ $D800-$DBFF: Color RAM
$D000 ├────────────────────┤ $DC00-$DCFF: CIA 1
│ │ $DD00-$DDFF: CIA 2
│ Free RAM (4K) │
$C000 ├────────────────────┤
│ BASIC ROM (8K) │ BASIC interpreter
$A000 ├────────────────────┤
│ │
│ Free RAM (≈38K) │ Your programs live here
│ │
$0800 ├────────────────────┤
│ Screen RAM (1K) │ 40x25 character codes
$0400 ├────────────────────┤
│ System (1K) │ BASIC pointers, vectors
$0200 ├────────────────────┤
│ Stack (256 B) │ Hardware stack ($0100-$01FF)
$0100 ├────────────────────┤
│ Zero Page (256B) │ Fast access, pointers
$0000 └────────────────────┘
Bank Switching
Address $01 controls which chip responds in the overlapping regions. By
writing different values to $01, you can:
- Switch out BASIC ROM (
$A000-$BFFF) — reveals 8K of RAM underneath. Essential for games and demos that don't need BASIC. - Switch out KERNAL ROM (
$E000-$FFFF) — reveals 8K of RAM. Be careful: you lose interrupt handling unless you provide your own vectors. - Switch between I/O and Character ROM (
$D000-$DFFF) — I/O is mapped by default; switching to character ROM lets you read the built-in font data. Switching to pure RAM gives you another 4K.
; Switch out BASIC ROM to reclaim 8K of RAM
LDA $01
AND #%11111110 ; clear bit 0 (LORAM)
STA $01 ; BASIC ROM disappears, RAM visible at $A000-$BFFF
; Switch out everything — all 64K is RAM (except I/O at $D000)
LDA #$35
STA $01
RAM Under ROM
A crucial trick: the CPU can always write to RAM under the ROMs, even when reads go to ROM. If you store data at $A000-$BFFF while BASIC ROM is mapped, it goes into RAM. When you later switch out the ROM, the data is there. This means you effectively have all 64 KB of RAM available for data storage even with the ROMs mapped in.
VIC-II — The Graphics Chip
The
VIC-II
is the C64's graphics processor. It generates the video signal and handles
characters, bitmaps, sprites, scrolling, and raster interrupts — all in
hardware. It occupies I/O registers from $D000 to $D03F.
Display Modes
The VIC-II has several display modes, selectable by setting bits in registers
$D011 and $D016.
VIC-II Display Modes
| Mode | Resolution | Colors per cell | Cells |
|---|---|---|---|
| Standard Character | 320 x 200 | 2 (foreground + background) | 40 x 25 |
| Multicolor Character | 160 x 200 | 4 (bg + 3 shared/per-cell) | 40 x 25 |
| Standard Bitmap | 320 x 200 | 2 per 8x8 cell | — |
| Multicolor Bitmap | 160 x 200 | 4 per 4x8 cell | — |
| Extended Background Color | 320 x 200 | 1 of 4 backgrounds per char | 40 x 25 |
Standard Character Mode (Default)
This is what you see when you turn on the C64. The screen is a 40x25 grid of characters, each 8x8 pixels.
Three pieces of memory define what you see:
- Screen RAM (default at
$0400, 1000 bytes) — one byte per cell, selects which character to display (0-255). - Color RAM (
$D800, always fixed, 1000 nybbles) — one nybble per cell, sets the foreground color (0-15). - Character ROM/RAM (default at
$D000, 2K per charset) — the 8x8 pixel patterns for each of the 256 characters. Each character is 8 bytes: one byte per row, each bit is a pixel.
; Put character 'A' (PETSCII 1) at the top-left corner in green
LDA #1 ; character code for 'A'
STA $0400 ; screen RAM, position (0,0)
LDA #5 ; color 5 = green
STA $D800 ; color RAM, position (0,0)
Custom Characters
The C64's default character set lives in ROM, but you can point the VIC-II at RAM and define your own characters. Each character is just 8 bytes:
; Define a custom smiley face at character position 0
; Character data — 8 bytes, one per row, MSB on left
;
; .##..##. = %01100110 = $66
; .##..##. = %01100110 = $66
; ........ = %00000000 = $00
; ........ = %00000000 = $00
; #......# = %10000001 = $81
; .#....#. = %01000010 = $42
; ..####.. = %00111100 = $3C
; ........ = %00000000 = $00
LDX #0
loop:
LDA chardata,X
STA $3000,X ; store character data in RAM
INX
CPX #8
BNE loop
; Point VIC-II to character set at $3000
LDA $D018
AND #%11110001
ORA #%00001100 ; bits 1-3 = %110 → offset $3000 in current VIC bank
STA $D018
chardata:
.byte $66,$66,$00,$00,$81,$42,$3C,$00
Multicolor Character Mode
In multicolor mode, pixels are 2 bits wide (double-width), giving you 4 colors per character cell but halving the horizontal resolution to 160 pixels.
The 4 colors come from:
| Bit pair | Color source |
|---|---|
00 |
Background color ($D021) |
01 |
Color from upper nybble of screen RAM |
10 |
Color from lower nybble of screen RAM |
11 |
Color from color RAM ($D800) |
This mode is enabled per-character: if bit 3 of the character's color RAM entry is set, that character renders in multicolor. Otherwise it uses standard mode. You can mix both on the same screen.
The Color Palette
The VIC-II has a fixed palette of 16 colors. You cannot change them.
VIC-II Color Palette
| $0 Black | $4 Purple | $8 Orange | $C Medium grey |
| $1 White | $5 Green | $9 Brown | $D Light green |
| $2 Red | $6 Blue | $A Light red | $E Light blue |
| $3 Cyan | $7 Dark grey | $B Dark grey | $F Light grey |
Sprites
The VIC-II supports 8 hardware sprites, each 24x21 pixels (or 12x21 in multicolor mode with 3 colors). The hardware handles drawing them on screen — you just set their position and data pointer.
Each sprite's pixel data is 63 bytes (3 bytes per row x 21 rows), stored on a 64-byte boundary. Sprite pointers — the byte that tells the VIC-II where each sprite's data lives — are at the last 8 bytes of screen RAM (default $07F8-$07FF).
; Enable sprite 0 and position it at (100, 150)
LDA #%00000001
STA $D015 ; sprite enable register — bit 0 = sprite 0
LDA #100
STA $D000 ; sprite 0 X position (low 8 bits)
LDA #150
STA $D001 ; sprite 0 Y position
LDA #$01 ; sprite 0 color = white
STA $D027
; Point sprite 0 to data at $0340 (block 13: $0340/64 = 13)
LDA #13
STA $07F8 ; sprite 0 data pointer
Sprites support:
- Expansion — double width and/or height (
$D01D,$D017) - Priority — behind or in front of background graphics (
$D01B) - Collision detection — sprite-sprite and sprite-background collisions are detected in hardware (
$D01E,$D01F) - Multicolor — pairs of bits select from 3 colors + transparent (
$D01C) - X position MSB — since 8 bits only covers 0-255 and the screen is wider, bit 8 of each sprite's X position is packed into register
$D010
Raster Interrupts
The VIC-II generates the display one scanline at a time, top to bottom, 50 (PAL) or 60 (NTSC) times per second. You can program it to fire an interrupt when the raster beam reaches a specific scanline. This is the foundation of almost every advanced C64 technique.
; Set up a raster interrupt at scanline 100
SEI ; disable interrupts while we set up
LDA #$7F
STA $DC0D ; disable CIA 1 interrupts
STA $DD0D ; disable CIA 2 interrupts
LDA #$01
STA $D01A ; enable VIC-II raster interrupt
LDA #100
STA $D012 ; trigger at scanline 100
LDA $D011
AND #%01111111 ; clear bit 7 of $D011 (raster line bit 8)
STA $D011
LDA #<irq_handler
STA $0314 ; IRQ vector low byte
LDA #>irq_handler
STA $0315 ; IRQ vector high byte
CLI ; re-enable interrupts
RTS
irq_handler:
LDA $D019
STA $D019 ; acknowledge the interrupt (write to clear)
; --- do your work here ---
; Change border color, swap character set, etc.
JMP $EA31 ; jump to KERNAL's standard IRQ handler (returns via RTI)
Raster Tricks
By chaining multiple raster interrupts — each one reprogramming the next scanline trigger — you can change graphics mode, colors, scroll position, or character set mid-screen. This is how C64 demos display more than 16 colors, mix text and bitmap areas, and create split-screen effects. The entire "demo scene" is built on raster timing.
VIC-II Banks
The VIC-II can only "see" 16 KB of memory at a time. The 64 KB address space is
divided into four banks, selected via CIA 2 at $DD00:
Bits 0-1 of $DD00 |
VIC Bank | Address range |
|---|---|---|
%11 |
Bank 0 | $0000-$3FFF (default) |
%10 |
Bank 1 | $4000-$7FFF |
%01 |
Bank 2 | $8000-$BFFF |
%00 |
Bank 3 | $C000-$FFFF |
Within the selected bank, register $D018 sets the offsets for screen RAM and
character/bitmap data. Moving the VIC bank is common when you need character
data in RAM (banks 0 and 2 have character ROM "shadows" that override RAM at
certain offsets).
SID — The Sound Chip
The
SID
is what made the C64 a music machine. While other home computers of the era
could produce basic beeps and square waves, the SID was a proper synthesizer on
a chip. It occupies registers $D400-$D41C.
Architecture
The SID has 3 independent oscillators (voices), each with:
- Frequency (16-bit) — sets the pitch
- Pulse width (12-bit) — for the pulse waveform only, controls the duty cycle
- Waveform select — triangle, sawtooth, pulse, or noise (one at a time, or combined for ring modulation effects)
- Envelope generator (ADSR) — attack, decay, sustain, release, each 4 bits
- Ring modulation — modulate one voice's triangle wave with another voice's frequency
- Sync — hard-sync one voice's oscillator to another
Plus a global filter shared across all voices:
- 12-bit cutoff frequency
- Resonance control (4 bits)
- Low-pass, band-pass, and high-pass modes (can be combined)
- Each voice can be individually routed through the filter
Voice Registers
Each voice occupies 7 bytes. Voice 1 starts at $D400, voice 2 at $D407, voice
3 at $D40E.
SID Voice Registers (Voice 1)
| $D400-$D401 | Frequency (16-bit, low/high) |
| $D402-$D403 | Pulse width (12-bit, low/high bits 0-3) |
| $D404 | Control: gate, sync, ring mod, waveform select |
| $D405 | Attack (high nybble) / Decay (low nybble) |
| $D406 | Sustain (high nybble) / Release (low nybble) |
Playing a Note
; Play a middle-C on voice 1 with a sawtooth wave
; Set frequency for middle-C (approximately $1168 for PAL)
LDA #$68
STA $D400 ; frequency low byte
LDA #$11
STA $D401 ; frequency high byte
; Set envelope: fast attack, medium decay, medium sustain, medium release
LDA #$09 ; attack=0 (fastest), decay=9
STA $D405
LDA #$A5 ; sustain=$A, release=5
STA $D406
; Set volume to maximum
LDA #$0F
STA $D418 ; global volume (low nybble)
; Select sawtooth waveform and open the gate (start the note)
LDA #$21 ; bit 0 = gate, bit 5 = sawtooth
STA $D404
; ... later, to release the note:
LDA #$20 ; clear gate bit, keep sawtooth selected
STA $D404 ; envelope enters release phase
The ADSR Envelope
The envelope generator shapes each note's volume over time:
ADSR Envelope
Volume
^
| /\
| / \___________
| / \
| / \
|/ \
+--+---+----------+---+--→ Time
A D S R
A = Attack: time to rise from 0 to peak volume
D = Decay: time to fall from peak to sustain level
S = Sustain: volume level held while gate is open
R = Release: time to fall from sustain to 0 after gate closes
Attack and decay times range from 2ms to 8 seconds. Sustain is a volume level (0-15). Release ranges from 6ms to 24 seconds.
Filter
The SID's analog filter is what gives it its distinctive warm sound. Many SID tunes would sound flat and lifeless without it.
; Route voice 1 through the filter, set to low-pass at medium cutoff
LDA #$01
STA $D417 ; filter voice 1 (bit 0)
LDA #$30
STA $D415 ; cutoff frequency low 3 bits (bits 0-2 used)
LDA #$40
STA $D416 ; cutoff frequency high 8 bits
LDA #$1F ; resonance = $1 (low nybble), volume = $F (max)
ORA #$10 ; bit 4 = low-pass filter on
STA $D418
CIA Chips — I/O and Timers
Two CIA 6526 chips handle keyboard, joystick, serial communication, and timing.
CIA 1 ($DC00-$DC0F):
- Keyboard matrix scanning (port A = columns, port B = rows)
- Joystick port 2 (directly readable from port A) and joystick port 1 (port B, shared with keyboard)
- Two 16-bit timers — used by the KERNAL for the system clock (
TI$)
CIA 2 ($DD00-$DD0F):
- VIC-II bank select (bits 0-1 of port A)
- Serial bus (IEC) for disk drive communication
- RS-232 (accent accent accent accent accent accent accent)
- Two more 16-bit timers
Reading a Joystick
Joystick port 2 is the easiest — just read CIA 1 port A:
; Read joystick port 2
LDA $DC00 ; CIA 1 port A
; Bit 0 = up (0 = pressed)
; Bit 1 = down (0 = pressed)
; Bit 2 = left (0 = pressed)
; Bit 3 = right (0 = pressed)
; Bit 4 = fire (0 = pressed)
LSR ; shift bit 0 (up) into carry
BCC player_up ; carry clear = up pressed
Note the active-low logic: bits are 0 when pressed, 1 when released.
Putting It All Together — A Minimal Program
Here's a complete assembly program that clears the screen, sets custom colors, displays a message, and plays a note:
; A minimal C64 program
; Assemble to load at $0801 with a BASIC SYS stub
*=$0801
; BASIC stub: 10 SYS 2064
.byte $0C,$08,$0A,$00,$9E,$32,$30,$36,$34,$00,$00,$00
*=$0810
start:
; Black background and border
LDA #0
STA $D020 ; border color
STA $D021 ; background color
; Clear screen
LDX #0
clear:
LDA #$20 ; space character
STA $0400,X
STA $0500,X
STA $0600,X
STA $06E8,X
LDA #1 ; white
STA $D800,X
STA $D900,X
STA $DA00,X
STA $DAE8,X
INX
BNE clear
; Print "HELLO C64" at row 12, column 15
LDX #0
print:
LDA message,X
BEQ done_print ; zero-terminated
STA $0400 + 12*40 + 15,X ; screen position
LDA #5 ; green
STA $D800 + 12*40 + 15,X ; color
INX
BNE print
done_print:
; Play a note on SID voice 1
LDA #$68
STA $D400 ; freq low
LDA #$11
STA $D401 ; freq high
LDA #$09
STA $D405 ; attack/decay
LDA #$A5
STA $D406 ; sustain/release
LDA #$0F
STA $D418 ; volume max
LDA #$21 ; sawtooth + gate
STA $D404
; Loop forever
JMP *
message:
; PETSCII: "HELLO C64" (screen codes, not ASCII)
.byte 8,5,12,12,15,$20,3,$36,$34,0
Programming Tips
Key Takeaways
- The 6510 is an 8-bit CPU with just three general-purpose registers (A, X, Y). Use zero page as your extended register file.
- The 64 KB address space is shared between RAM, ROM, and I/O. Bank switching at address $01 controls what's visible where.
- The VIC-II handles all graphics: character and bitmap modes, 8 hardware sprites, hardware scrolling, and raster interrupts.
- Raster interrupts are the key to advanced effects — changing colors, modes, and scroll positions mid-screen.
- The SID is a 3-voice synthesizer with ADSR envelopes, ring modulation, and an analog filter. It's a legitimate musical instrument.
- Everything is memory-mapped. Reading a joystick, changing a color, playing a note — it's all loads and stores to specific addresses.
- Timing matters. The VIC-II and CPU share the memory bus. On "bad lines" (every 8th scanline when character pointers are fetched), the VIC-II steals ~40 cycles from the CPU.
Tools and Emulators
If you want to start programming the C64 today, here's what you need:
| Tool | Purpose |
|---|---|
| VICE | The gold-standard C64 emulator. Includes a built-in monitor (debugger). |
| Kick Assembler | Modern cross-assembler with macros, scripting, and VICE integration. |
| ACME | Simple, fast cross-assembler. Good for getting started. |
| cc65 | C compiler and assembler suite targeting the 6502 family. |
| CBM prg Studio | Windows IDE with integrated assembler and sprite/character editors. |
| SpritePad / CharPad | Dedicated editors for designing sprites and character sets. |
| Codebase64 | Community wiki with tutorials, code snippets, and reference material. |
The C64 runs a variant of the 6502 — the same CPU family found in the NES. Next up, we'll look at how Nintendo took that same processor and built an entirely different kind of machine around it, with a PPU designed from the ground up for scrolling tile-based games.