CHIP-8 Emulator (bonus)

Context

Created back in the 70s, CHIP-8 is a Virtual Instruction Set that was meant to allow developing games on COSMAC VIP and Telmac microcomputers without worrying about the underlying architecture. Back then, computers used to have only 8KB of RAM, split equally between the kernel and the user application. B, predecessor to the C language you are so fond of, was developed on such systems. The reason why the language is called B is because Ken Thompson and Dennis Ritchie only had one byte left for the name in the compiler's source code, so they shortened it to just one letter. When Ritchie added data structures to the language it was considered such a paradigm shift that it warranted giving it a new name (B++ –> C). Needless to say, systems were pretty resource constrained.

But back to our assignment. The CHIP-8 architecture was comprised of two elements: a 512 byte interpreter and a 3.5KB game. This may not seem like a lot, but it was enough to implement Pong and Space Invaders. Your task is to write a CHIP-8 interpreter in C/C++ (i.e. not Python).

Code Skeleton

This archive contains the skeleton you will be using. It already implements an SDL2-based module that will handle rendering the 64×32 screen for you. Compile and run the application to see how you're supposed to interact with it.

Install libsdl2-dev and any other libraries before compiling.

The structure of the skeleton source code is as follows:

Resources

The following links should contain all the information needed to implement the emulator

System Specifications

This section contains a short description of the components of a CHIP-8 system. This documentation is not exhaustive. Also consult the resources linked above.

CPU

The processing power of modern CPUs exceeds that of the CHIP-8 system by orders of magnitude. Consequently, you will have to limit the interpreter's CPU frequency to around 200-500Hz. Add a CLI argument for the CPU frequency in cli_args.c.

Memory

As mentioned before, the first 512 (0x00 - 0xFF) bytes of RAM are reserved for the interpreter, so no program will try to access it. Don't worry, your interpreter doesn't need to fit in this space. Note that people tend to store the built-in font sprites at the 0x50 offset. These font sprites are the binary representation of hex digits ranging from 0 to F (see the global array in main.c).

Registers

The CHIP-8 system has a number of registers:

In addition to all this, you also have two Timer Registers. The Timer Registers can be set by the program and are automatically decremented at 60Hz (i.e.: decremented by 1, 60 times per second). Note that their value should never underflow, so when it hits 0, stop the decrementing process.

Keyboard

The computers that originally used CHIP-8 had a 4×4 keypad, as shown in the figure below. You can map these keys to your keyboard anyway you want. Note that SDL2 also implements support for keyboard input detection. Check out SDL_GetKeyboardState(), or the event driven API.

Display

The original CHIP-8 used a 64×32 pixel monochrome display, (0, 0) being the top-left corner and (63, 31) being the bottom-right corner. The implementation of the display is already provided in display.c. Feel free to change the color scheme by modifying the DARK_{R,G,B} and LIGHT_{R,G,B} macro definitions.

Grading

The base assignment constitutes 2.5p out of your final grade (100p homework = 2.5p final grade). The 100p are split between the following tasks:

There are also two bonus tasks that amount to 20% of assignment base score (i.e.: 20p bonus = 0.5p final grade).

Write a README containing the description of your implementation, design choices, challenges you encountered, etc. Feel free to add your feedback here as well. All submissions that do not include a README will be ignored.


NOTE: Assistants are free do deduct points for bad / illegible code.

Test ROMs

When evaluating your implementation, we are going to run a few games and test ROMs. The test ROMs are CHIP-8 binaries that target certain instructions and tell you if your decoders behave as expected. Needless to say, you first need to correctly implement a subset of the CHIP-8 instructions that are needed to execute the tests.

FAQ

Q: Can I write the emulator in something other than C/C++?
A: No.

Q: What platform will this assignment be tested on?
A: Linux only.

Q: How do I read the instruction codes in the specification?
A: In the spec, the instructions are represented as 4 characters, each signifying a nibble (4 bits). Hex digits (0-9A-F) are invariants and you should try to match them exactly. E.g. 00EE (RET) is composed of 4 constant nibbles. Whenever you see X or Y, that part of the opcode is variable and it represents what general purpose register (V) is involved in that operation. E.g. FX65 (LD Vx, [I]) will load in register Vx the value located at the memory address stored in register I; basically, it's dereferencing a pointer. Finally, whenever you see a sequence of K or N characters (notation can differ), know that these are immediate values. These are basically arguments for the instructions that are embedded in the opcode itself. E.g. 6XKK (LD Vx, byte) will load the value KK into Vx. So for 614F, we will have V1 = 0x4F.

Q: There are so many CHIP-8 emulators out there. What if I'm tempted to “take inspiration” from some of them?
A: Once you come up with a solution of your own and implement it, it's ok to look at other approaches. After all, to write good code you first need to read good code. However, taking inspiration and “taking inspiration” are two different things. In addition to a manual review, we'll also perform an AST comparison of your submitted code, against each other and as many public CHIP-8 projects as we can find. Just keep that in mind :p

TODO: Collect questions from Teams / lab and add them here.