Home

Awesome

Chip-8 Emulator

A Chip-8 Emulator written in C with OpenGL. Chip-8 is interpreted programming language from the 1970s originally developed for the COSMAC VIP and Telmac 1800 8-bit computers.

Virtual Machine Description

Chip-8 VM is composed of 16 8 bit general purpose data registers V0-VF. Register VF is used as a flag for some instructions. Two other special registers exist, the 16 bit address register, I, and the 16 bit Program Counter. The VM contains 4096 1 byte memory locations. Of those, the first 512 bytes are reserved for the interpreter with the program memory starting at address 0x200. Output is via a 64x32 monochromatic display, with input from a 16 key hex keyboard. Additionally, there are two timers, the delay time and the sound timer, both of which count down at 60 Hz until they reach zero. The sound times produces a tone when it is not zero

Opcodes

Each instruction is 16bit length and contains some combination of registers IDs, and immediate values. The opcodes are stored big-endian.

CodeDescriptionStatus
0NNNCall RCA 1802 program at address NNNNot implemented few ROMs depend on it
00E0Clear displayImplemented and Tested
00EEReturn from subroutineImplemented and Tested
1NNNJump to address NNNImplemented and Tested
2NNNCall subroutine at NNNImplemented and Tested
3XNNSkip next instruction if Vx = NNImplemented and Tested
4XNNSkip next instruction if Vx != NNImplemented and Tested
5XY0Skip next instruction if Vx == VyImplemented and Tested
6XNNSets Vx to NNImplemented and Tested
7XNNSets Vx to Vx plus NNImplemented and Tested
8XY0Sets Vx to VyImplemented and Tested
8XY1Sets Vx to bitwise Vx or VyImplemented and Tested
8XY2Sets Vx to bitwise Vx and VyImplemented and Tested
8XY3Sets Vx to bitwise Vx xor VyImplemented and Tested
8XY4Sets Vx to Vx plus VyImplemented and Tested
8XY5Sets Vx to Vx minus VyImplemented and Tested
8XY6Sets Vx and Vy to Vy shifted one bit to the right. Vf is set to the least significant bit of Vy from before the shiftImplemented and Tested
8XY7Sets Vx to Vy minus Vx + Implemented and TestedImplement and Tested
8XYESets Vx and Vy to Vy shifted one bit to the left. Vf is set to the most significant bit of Vy from before the shiftImplemented and Tested
9XY0Skips the next instruction if Vx != VyImplemented and Tested
ANNNSets I to NNNImplemented and Tested
BNNNJumps to Vx + NNNImplemented and Tested
CXNNSets Vx to random [0,255] anded with NNImplemented and Tested
DXYNDraws a sprite at (Vx,Vy) with a width of 8 pixels and a height of N pixels from the memory location pointed to by I. Sprites are xor'd with the current state of the screen. Vf is set to 1 if any pixels go from set to unsetImplemented and Tested
EX9ESkips the next instruction if the key code in Vx is pressedImplemented and Tested
EXA1Skips the next instruction if the key code in Vx is not pressedImplemented and Tested
FX07Sets Vx to the current value of the delay timerImplemented and Tested
FX0ABlocks until the next key press, and stores the key code in VxImplemented and Tested
FX15Sets the delay timer to VxImplemented and Tested
FX18Sets the sound timer to VxImplemented and Tested
FX1ESets I to I + VxImplemented and Tested
FX29Sets I to the location of the font character in Vx. For example if Vx 0xA, I will be set to the location of 'A'Implemented and Tested
FX33Causes the BCD representation of Vx to be stored starting at I. I + 0 is set to the hundreds place of Vx, I + 1 is set to the tens place of Vx, and I + 2 is set to the ones place of VxImplemented and Tested
FX55Dump registers [V0, VX] to memory starting at I. I is incremented for each value writtenImplemented and Tested
FX65Load registers [V0, VX] from memory starting at I. I is incremented for each value readImplemented and Tested

Project Structure

Building

Executable

  1. Build binary make

Unit Tests

  1. Build unit Tests

make test 2. Execute Unit Tests

./tests

Unit Tests

The unit tests for the Chip-8 core implementation are located in the test/chip8_test.c file along with additional units tests for other components. These tests are written using the Check C unit testing frame work. The goals of Chip-* core unit tests is to test the core functionality of the Chip-8 VM, by verify each instruction behaves as expected along verifying higher level functionality such as timers, drawing to the display, and program executions works as expected.

Current Status

The implementation of the emulator core is complete. The display output, and keyboarad input are functioning correctly.

Todo

  1. Key maps 2: Instruction rate limiting
  1. Sound timer