Skip to content

[PCL] Add Chip-8 emulator test program - #131

Merged
kostis merged 2 commits into
kostis:masterfrom
wheremyfoodat:chip8
Sep 29, 2026
Merged

kostis merged 2 commits into
kostis:masterfrom
wheremyfoodat:chip8

Conversation

@wheremyfoodat

@wheremyfoodat wheremyfoodat commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Implements a small emulator for the Chip-8, a bytecode VM from the 1970s, originally designed for the Cosmac VIP personal computer, and further popularized by later implementations targetting HP-48 graphing calculators. The code uses this page as a reference for the Chip-8 instruction set.

Thanks to its simplicity, the Chip-8 is one of the most common systems to make an emulator for, with over 14 thousand implementations on Github.

The emulator comes with 3 builtin "games", which are all public domain and can thus be freely distributed. These games are hardcoded into the source code in a fairly awkward way, since the PCL specification does not include file IO or array initializers. In addition, the games all render to the terminal, and do not accept keyboard input (as PCL stdlib can't read from the keyboard in a non-blocking manner).

PCL also has several other intricacies that make it awkward to write an emulator with:

  • There's no bitwise operators (and/or/xor/shifts), so implementing them is very complex (and an emulator needs to do a lot of them)
    • ANDing by a constant can be done using modulo, if the constant is a power-of-two-minus-1
    • Left/right shifts by a constant can be done using division and multiplication
    • Operations that don't have a constant operand are much more complicated. AND/OR/XOR by a non-constant requires using big LUTs with precomputed values (which is what the code does, it calculates the result 4 bits at a time using 256-entry LUTs), shifts by a non-constant require multiplication/division in a loop
  • Integers are always signed, and mod calculates a signed remainder, so operations like (a - b) & 0xFF in C can't be done easily, because if (a-b) goes into the negatives, the calculated mod will be negative too (and not an 8-bit integer like we want it to be)
  • Can't declare variables inside a block's statement list, so writing large programs is a pain
  • No hexadecimal literals
  • ...

There's hundreds of other chip-8 games that could be added, many of which can be found here: https://johnearnest.github.io/chip8Archive/. However, some games might require adjustments to the emulator

Below are screenshots of the 3 games built into the source code. Some "artifacts" (things disappearing in certain frames) aren't bugs, and are actually just a side effect of how rendering works on the chip-8. You can toggle how long the emulator runs for, and how many instructions to actually execute per frame, by changing some constants inside the source code.

Space Invaders:
image

Pong:
image

IBM logo:
image

@kostis
kostis merged commit f7bc071 into kostis:master Sep 29, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants