Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 18 min to read

Play it as a journeyTutor comes with a plan

A CHIP-8 emulator

The CPU
The CPU · Open board

: the CPU board shows a chip that fetches an instruction, works out what it means, and does it. This lesson builds that loop in about 110 lines of Python, for a tiny chip that never existed in metal.

In 1977 RCA sold a small computer called the COSMAC VIP. You ordered it by mail and built it from a kit. The price was 275 US dollars. It came with about 20 games. Joseph Weisbecker designed the machine. He also made a tiny language for it, called CHIP-8, because games were slow to write in raw machine code and BASIC used too much of the small memory. His daughter Joyce Weisbecker wrote some of those games.

Today nobody owns a VIP. But those games still run, on phones and laptops and in web pages. The reason is an emulator: a program that pretends to be another computer, so that programs made for that computer run unchanged. This is the problem. A game file is only a list of numbers. Something must read each number and act on it.

By the end of this lesson you will be able to write a working CHIP-8 emulator, load a real program file into it, watch it draw on a screen, wait on a timer, and react to a key.

Before you start. The CPU (fetch-decode-execute, the ISA, assembly) showed that a processor repeats one job forever: get an instruction, do it, move on. Everything Is a Number (bits & bytes) said that text, pictures and instructions are all stored as numbers, and that hex (base 16) is a short way to write them. An interpreter of your own built a program that reads a program and acts on it, and this lesson does the same for numbers instead of text. A text editor of your own read single key presses from a terminal, which is the input side we need. You need Python 3 to run the code.

Lesson 114 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The machine we pretend to be
  • Registers: sixteen small boxes for numbers
  • Opcodes: every instruction is two bytes
  • The fetch, decode, execute loop
  • Sprites: drawing by flipping pixels
  • Timers: a counter that falls 60 times a second
  • The keyboard: asking whether a key is down
  • Running a real program
  • The code
  • Decisions that are easy to get wrong
  • Checking the arithmetic opcodes
  • Quirks: where CHIP-8 programs disagree
  • Trade-offs and real systems

Terms it explains: emulator, register, program counter, opcode, fetch-decode-execute, sprite, XOR, timer, ALU, assembly, branch prediction, CISC vs RISC, clock, core, flags, immediate, IPC, ISA, operand, out-of-order, PC (program counter), pipelining, superscalar.

Related paper

Review From Scratch