Write a 6502 emulator. Learn Rust. Learn 6502.
Understand computers well enough to build one.
This book has you write a 6502 emulator in Rust, one working program at a time. Thirty builds take it from Hello, world to an emulator that passes all three standard conformance suites, and finish with a complete chess program running on Windows, Linux, macOS or a Raspberry Pi.
You will write the processor that powered the BBC Micro, the Commodore 64, the NES and the Apple II.
Who it is for
Written for developers with a year or two of professional experience in a mainstream language — C#, Java, Python, TypeScript or C++ — who are comfortable reading code they did not write and following a compiler error to its cause. You do not need assembly, and you do not need emulator experience; both are introduced here as they are needed.
On Rust: you do not need to know it. Install the toolchain and read the official tutorial as far as its chapter on enums and pattern matching; Chapter 4 teaches the subset this project uses, and any later build that brings in new Rust opens with a short Rust Primer box saying what is new and where to read about it.
What it asks of you: each build is a specification — a goal, the behavior that proves it, and tests you can run. Five of the thirty are markedly bigger than the rest, and the book names them in advance and says how to approach each.
What you learn
The patterns the old machines forced on everyone — multiplication by shift-and-add because the chip cannot multiply, jump-table dispatch, linear-feedback shift registers, binary-coded decimal and the flags it quietly breaks, interrupt handling, and why crossing a page boundary costs a clock cycle.
And modern Rust, taught through a problem that genuinely needs it: ownership on a real data structure rather than a toy, 151 opcodes decoded with enums and pattern matching, deliberate wrapping arithmetic that models 8-bit overflow without undefined behaviour, and a debugger with breakpoints and live disassembly.
Then the part almost nobody teaches: how you know it is right. Anyone can write an emulator that boots. Proving one behaves like the machine it imitates is a different discipline.
What you get
- DRM-free PDF and EPUB
- The reference code — the complete working emulator, build by build
- Updates for life — every future version of this edition, free
Every listing in the book is spliced in from a working repository at build time, so the code on the page is the code that compiles.
Read it before you buy
Three chapters, the first build and a live episode are free and in full — the episode runs a real program in your browser one instruction at a time, showing the registers, flags and memory as they change:
- Chapter 1 - Introduction
- Chapter 4 - Rust and System Programming
- Chapter 5 - The Method and the Map
- Build 1 - Hello, world
- Episode: Random numbers - a sequence that only looks random
- The full contents
What is in it
30 builds, 7 chapters and 7 appendices. Every build is a real 6502 program — it prints, counts, multiplies, parses — and the emulator work is whatever that program forces. It is why the order looks nothing like an opcode table.
Introduction- Chapter 0 — Preface
- Chapter 1 — Introduction — free to read
- Chapter 2 — The 6502 Processor for Developers
- Chapter 3 — The 6502 Itself
- Chapter 4 — Rust and System Programming — free to read
- Chapter 5 — The Method and the Map — free to read
- Chapter 6 — The Method with an AI Assistant — free to read
- Build 1 — Hello, world — free to read
- Build 2 — Count 0 to 9 with subroutines
- Build 3 — Interlude: The Disassembler
- Build 4 — Print via zero-page indirect pointer
- Build 5 — Hex byte output
- Build 6 — Memory dump
- Build 7 — Interlude: A Filing System for the Match
- Build 8 — Print 99 down to 0 in decimal
- Build 9 — Pseudo-random number generator (LFSR)
- Build 10 — Multiplication via shift-and-add
- Build 11 — Jump-table dispatch (with the famous JMP indirect bug)
- Build 12 — 16-bit decimal output (0–65535)
- Build 13 — Multi-byte arithmetic demo (16-bit add and subtract)
- Build 14 — Division via shift-and-subtract
- Build 15 — Bit-field / status-byte manipulation
- Build 16 — String operations (strlen, strcmp)
- Build 17 — Memory copy (memcpy)
- Build 18 — Lookup tables (advanced indexed addressing)
- Build 19 — Run-length decoder
- Build 20 — State machine / parser
- Build 21 — Tiny interactive monitor
- Build 22 — Interlude: Completing the Instruction Set
- Build 23 — Interlude: Interrupts (BRK, IRQ, NMI)
- Build 24 — Interrupts (BRK round-trip)
- Build 25 — BCD mode (decimal-mode ADC/SBC)
- Build 26 — Conformance: finding and fixing compatibility bugs
- Build 27 — Cycle counting
- Build 28 — Error Handling: Making the Core's Failure Model Explicit
- Build 29 — The Emulator Application
- Build 30 — Case Study: MicroChess
- Afterword
- Appendix A — The Scorecard
- Appendix B — Notation and the Machine
- Appendix C — 6502 Instruction Reference
- Appendix D — Addressing Modes
- Appendix E — The Opcode Matrix
- Appendix F — The egui Primer
- Appendix G — Credits and References
Status
Book 1 is complete — 437 pages, thirty builds, all thirty written. Book 2, which builds a machine compatible with the BBC Model B on top of this emulator, is in progress and sold separately.