: 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.