6 A CHIP-8 Interpreter
This chapter covers
- Designing an API from scratch with data-oriented reasoning
- Advanced build crafting, with multiple modules, tests and third-party dependencies
- Using C library code without wrappers
- Manipulating bits with Zig’s unique set of operators
- Contemplating computation from the perspective of the virtual machine
There are many ways to get to know computers deeply, but one of my favorites is working with compilers, interpreters, and emulators. These programs force you to grapple with a massive slice of the layers of software and hardware that let computers do useful things. Implementing one requires lots of critical thinking about structuring complicated data and abstracting away messy constraints. These skills are hard to learn and impossible to fully master.
Let’s write a bytecode interpreter for a tiny and interesting virtual machine called CHIP-8. It was invented in 1977 by Joseph Weisbecker for the RCA COSMAC VIP microcomputer, which became one of the most popular single-board computers of the early 1980s. The COSMAC VIP came with a 512-byte program for interpreting CHIP-8 bytecode, and even a few CHIP-8 video games. Of course, none of that was preloaded into memory — you had to type in each byte yourself. (I must say, I am incredibly grateful to have been born after this "manually type out bytes in hex" era of computing.)