64K memories – 6502 part 1
2026-09-20
I had a Commodore 64 when I was a kid in the early 90s. It was passed to me from a family friend who had upgraded to an Amiga 500. C64 was already ancient technology at that time, an 8-bit computer in the 16-bit era. Luckily I didn’t know that was the case and so I didn’t have to care. To me it was just a super cool machine.
The machine came with a bunch of games on floppies and cassettes, but I remember most of the cassettes failing to load. This could have also been a user error since I couldn’t really read or write at the time yet, especially not English. I operated the machine with the help of a notebook that had a bunch of commands my dad had written down that I monkeyed to the prompt by matching shapes. I had no idea what the commands meant. Looking at them now, it wouldn’t be obvious to present day me either:
**** COMMODORE 64 BASIC V2 ****
64K RAM SYSTEM 38911 BASIC BYTES FREE
READY.
LOAD "$",8
SEARCHING FOR $
LOADING
READY.
LIST
0 "ADVENTURE_DISK " 8A 2A
12 "MYGAME" PRG
4 "TITLE" PRG
20 "LEVEL1" PRG
18 "LEVEL2" PRG
22 "LEVEL3" PRG
8 "SOUND" PRG
30 "GRAPHICS" PRG
2 "HIGHSCOR" SEQ
548 BLOCKS FREE.
READY.
LOAD "MYGAME",8,1
SEARCHING FOR MYGAME
LOADING
READY.
RUN
For example, what 8 and 1 signify here? (spoiler: 8 is the first disk drive device identifier, and 1 means the program should be loaded to where the image instructs.)
Somehow I still could load games and play them.
Sadly the C64 I played with in my childhood has been lost, most likely discarded as ancient junk that had no use in the modern world. But maybe I could write something that could do the same things?
6502
The CPU that C64 uses, MOS 6502, was used in a bunch of home computers starting from the 70s. Notably Apple I and II, and many Commodore micros used the CPU.
It’s a simple processor with only a few 8-bit registers: index registers X and Y, and one 8-bit accumulator A.
There are 56 instructions and 8 addressing modes that combine to 151 opcodes. The instructions are 1-3 bytes long.
Compared to Z80, another CPU of the same era, which has multiple 16-bit registers that can be split into two 8-bit registers each, 6502 is downright toy-like. It has so little state that a programmer can keep it all in their head.
That would make it a pretty good CPU to write an emulator for. And it is. There are ~8300 public repositories on GitHub that refer to 6502. So one more shouldn’t hurt.
phakebit
I had a week off of work to spend in the countryside during the part of autumn when it should be winter already but wasn’t. The weather was dark and wet, so might as well write some code.
In the end I still don’t have a C64 emulator but I did write phakebit, a 6502 emulator written in Rust.
Stay tuned for part 2 of the emulator saga where I describe the CPU emulator part and part 3 where an Apple I emulator is born.
Back to blog