Certain old computers — most frequently those using the RCA 1802 — were fond of using an early form of byte-code interpreter for programs, especially games. The interpreter, CHIP-8, was very simple to create but offered high-level features that were tedious to recreate in the native assembly language. Because there are a fair number of simple games written in CHIP-8, there are of course, emulators for it, and [River Gillis] decided to look inside the CHIP-8 byte code interpreter.
Part of the power of CHIP-8 was it only had 35 virtual instructions. That was important when you were trying to shoehorn a game and the interpreter into a very small memory. Remember, in those days 1K of memory wasn’t an unusual number, although the prototypical CHIP-8 host would have 4K.
The virtual machine has 16 8-bit data registers and a stack. Since the idea was to create games, there are features useful for that purpose. For example, drawing to the screen (by default a 64×32 pixel screen) using sprites, you get information about any collisions on the screen automatically. The instructions offered some virtualization of the display and input device — usually, a hex keypad — and also provides the ability to convert binary numbers to binary coded decimal.
So the interpreter isn’t difficult to create by design. However, [River] has to emulate a target machine, and that’s where part two will pick up. However, if you are interested in retrocomputing, retrogaming, or you just want a better understanding of how a byte-coded interpreter works, this is a fun read with lots of code examples.
Making a custom virtual computer is actually not a bad idea when you are trying to conserve memory space and make development easier. Java uses the same idea on steroids, although mostly for different reasons. P-Code was another similar system and Forth, at its core, is the same basic concept.
Photo credit: RCA Cosmac VIP by [Dave Ruske] CC BY 2.0