A course in 20 parts
Rust by building a NES emulator
This course teaches two things at once: the Rust programming language, and how a 1980s games console works on the inside. It is for you if you have written small programs in any language (variables, loops, functions) but have never used Rust and have never seen how an emulator is made. You do not need to know what hexadecimal is, what a processor does, or anything about the NES.
We build one program, a little at a time. Each part adds one ability to the emulator and introduces the few pieces of Rust that ability needs, at the moment it needs them.
Where we are going
By the end, your own program will open a NES cartridge file, run the machine code inside it on a processor you wrote, draw its picture sixty times a second in a window, and listen to your keyboard as a controller. You will play this on it:

paddle.nes, a small game written for this course, running on the finished emulator.Nothing in your Rust code will know about balls or bats. The game is a cartridge: a file of bytes in the language of the NES's processor. Your emulator only does what the cartridge tells it, which is what makes it an emulator. Halfway through, in Part 8, you reach a first summit: the emulator runs a tiny cartridge and prints its picture as text.
To be honest about the size of the journey: the finished emulator is about 1,200 lines of Rust that you fully understand. Its processor is complete and checked against a reference, instruction by instruction. It runs the simplest kind of NES cartridge ("mapper 0"), without sound. It has been tested on a standard processor test and on the cartridges written for this course, not on commercial games; the last part says plainly what is still missing and how to add it.
How we get there
We start with the smallest version that works at all and add one capability per part.
- A CPU that can hold a numberComing Mon 30 NovAfter this part: your program runs a three-byte NES program. New ideas: bytes and hexadecimal, opcode, register, program counter. Rust:
cargo,let mut,u8,loop,match. - A CPU that can countComing Mon 30 NovAfter this part: it has a second register and can add one, even past 255. New ideas: overflow and wrapping. Rust:
struct,impl, methods. - A CPU with a memoryComing Mon 30 NovAfter this part: the program lives in 64K of memory and can store values there. New ideas: address, little-endian. Rust: arrays,
u16,as, slices, your first compiler error. - A CPU that can repeat itselfComing Mon 30 NovAfter this part: programs can loop, and the emulator prints a trace of every step. New ideas: flag, branch, two's complement. Rust:
bool,if,i8. - A CPU that can walk through a listComing Mon 30 NovAfter this part: a loop copies a list of bytes from one place to another. New ideas: indexing, addressing mode. Rust:
forover a range,char. - Load a real cartridge fileComing Mon 30 NovAfter this part: the program comes from a
.nesfile and starts where the cartridge says. New ideas: iNES header, PRG ROM, reset vector. Rust:Vec,Result, ownership and borrowing. - See the tiles hidden in the cartridgeComing Mon 30 NovAfter this part: the emulator decodes the cartridge's pictures and prints them. New ideas: tile, CHR ROM, bit plane. Rust: shift and mask, nested loops,
const. - Put the picture on the screenComing Mon 30 NovAfter this part: the cartridge's program draws its own picture through a second chip. New ideas: PPU, memory-mapped register, name table. Rust: a struct that owns another struct.
- Open a real windowComing Mon 30 NovAfter this part: the picture is painted, pixel by pixel, in a window. New ideas: crate, frame buffer. Rust: dependencies,
Vec<u32>,while. - Tidy the code into modulesComing Mon 30 NovAfter this part: one file per chip, and the cartridge is named on the command line. New ideas: module,
pub,Option. - Check the CPU against a known-good oneComing Thu 3 DecAfter this part: a truthful memory map, and a checker that compares your processor with a reference log line by line. New ideas: mirroring,
String. Rust: range patterns,if let. - Teach the CPU every addressing modeComing Mon 7 DecAfter this part: all 151 opcodes are recognised and every way of finding a value works. New ideas: enum, tuple, zero page.
- Subroutines and the rest of the flagsComing Thu 10 DecAfter this part: the processor can call and return, and has all its flags. New ideas: subroutine, stack, status register.
- Finish the instruction setComing Mon 14 DecAfter this part: the processor adds, subtracts and shifts, and passes the test: 5,003 lines. New ideas: carry, bitwise operations, rotate.
- Keep time: sixty pictures a secondComing Thu 17 DecAfter this part: the emulator runs frame by frame and something moves on screen. New ideas: cycle, scanline, vblank, interrupt.
- Show the picture in colourComing Mon 21 DecAfter this part: the cartridge's own colours appear. New ideas: palette, attribute table.
- Things that move: spritesComing Thu 24 DecAfter this part: objects glide over and behind the background. New ideas: sprite, OAM, DMA.
- Plug in a controller and playComing Mon 28 DecAfter this part: your keyboard is a NES controller and you play a game. New ideas: shift register, strobe. Rust: release builds.
- Scroll a world wider than the screenComing Thu 31 DecAfter this part: the view slides over two screens while a status bar stays put. New ideas: scrolling, sprite 0 hit.
- Get ready for other people's cartridgesComing Mon 4 Jan 2027After this part: the common gaps are filled, unsupported cartridges are refused politely, and you know where to go next. New ideas: mapper.
How to use this course
Type the code yourself, run it, and do the exercises before moving on; each part opens by assuming you tried them. Many parts ask you to break the program on purpose. Do that too: the error messages are part of the lesson.
The source for every part is in code/, one folder per part, so you can always compare your version with a known-good one. Each folder is a complete Rust project; to run a snapshot, go into its folder and type cargo run (from Part 10 on, add the cartridge: cargo run -- ../roms/hello.nes). The finished emulator is code/part-20.
The cartridges are in code/roms, each with its program as readable text beside it (.s files), and code/nesasm is the small assembler that turns that text into a cartridge. code/mkrom generates the first cartridge, hello.nes.
You need a computer with a terminal and about an hour per part; from Part 9 on, also a screen and an internet connection for one download. Part 1 begins with installing Rust.