liveBook · Manning

CHIP-8

As explained in Systems Programming with Zig

The CHIP-8 instruction set is the collection of opcodes and execution semantics defined for the CHIP-8 virtual machine.

CHIP-8 is a tiny virtual machine whose bytecode can drive a simple game. Its instruction set has 31 two-byte instructions: each combines an opcode pattern with operands. One instruction calls COSMAC VIP machine code and is left out of the interpreter described here. section 6.0 section 6.2.1

The machine has 16 8-bit variables, a 12-bit pointer register called I, and 4,096 bytes of memory. Its devices are a 16-key hexadecimal keypad and a 64-by-32 black-and-white display. An interpreter can represent these as separate keypad, display, and memory structures, with a 12-bit program counter tracking execution. section 6.2.1 section 6.3.2

Listing 6.5 Everything we can remember
pub const Memory = struct {
    const Self = @This();

    pub const total_memory_bytes = 0x1000; #1

    pc: u12, #2
    i: u12, #2
    vars: [16]u8,
    data: [total_memory_bytes]u8,
}

The memory structure makes the address widths and 4 KiB memory limit explicit. section 6.3.2

A few instructions show how bytecode interacts with the machine:

  • FX0A waits for a key and puts its hexadecimal value in register X. If no key is pressed, the interpreter can subtract two from the program counter to repeat the instruction. section 6.5.7
  • 00E0 clears the display. section 6.5.7
  • DXYN reads N bytes starting at I and XORs each byte's bits with a display row, starting at coordinates in registers X and Y. Each successive byte advances one row, producing an 8-by-N sprite. section 6.5.7

Drawing needs some bit work: extract each bit from a loaded byte before updating a pixel. This interpreter ignores pixels outside the display; the original manual does not directly specify that behavior. section 6.5.7

const byte: u8 = // ...
for (0..8) |i| {
    const bit: u1 = @truncate(byte >> (7 - @as(u3, @intCast(i)));
    // do stuff with the bit
}

This Zig loop extracts the bits of a sprite byte one at a time, from most significant to least significant. section 6.5.7

To run a program on a desktop, load its bytecode into memory and repeatedly step the interpreter. The runtime uses Raylib for window and device handling, reads the keypad before stepping, and draws afterward. Tests help with instruction implementation; original COSMAC VIP programs offer a fun way to watch progress, even before every instruction works. section 6.4 section 6.4.4

How Systems Programming with Zig covers it

How deep it goes
Develops virtual-machine state, runtime integration, and instruction behavior; leaves the COSMAC VIP machine-code call unimplemented and some input/output work to the reader.
Examples
Uses original COSMAC VIP bytecode programs and an XOR-based sprite-drawing instruction to exercise the interpreter.
What you can do after
Build an interpreter from scratch in Zig and run original CHIP-8 programs through a desktop runtime.
How it connects
Connects bytecode interpretation with virtual machines, instruction sets, memory, graphics, keyboard input, and bit manipulation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.