CHIP-8 emulation with C# and Blazor - part 1
I have always been a fan of emulators and retrogaming in general. I even started writing my own 8080 emulator, but I haven’t managed to complete it (yet).
Emulating a complete system is no easy task, as you have to replicate all the inner complexities and micro-instructions.
Being the curious type I am, I decided to take a step back and find the easiest system possible to emulate. And after a bit of research (mostly on StackOverflow and Wikipedia), I stumbled on the CHIP-8!
Honestly, I had no idea it existed, but apparently, it had a quite active community in the late 70s. At its heart, it’s not really a physical machine, but rather an
“interpreted programming language, running on a virtual machine. It was made to allow video games to be more easily programmed” (Wikipedia)
The specs are quite easy:
- 4k total memory
- 16 8-bit data registers, called V (from 0 to F)
- a 16-bit address register, called I
- a stack (size might vary in each implementation, usually it’s 48 bytes)
- a keyboard with 16 keys
- a monochrome 64x32 display
That’s it! These can be quickly reproduced in a C# class like this:
In addition to those, we also need two more fields to track the Program Counter and the current Stack position:
This is already a good start. The next step is to implement each Opcode. Wikipedia still tells us that:
CHIP-8 has 35 opcodes, which are all two bytes long and stored big-endian.
There’s a nice table on that Wikipedia article with the entire list so I’m not going to replicate it here. In order to emulate these opcodes, I am using a struct to represent its data
… and a Dictionary<byte, Action<OpCode>> to link it to the function to execute.
So, for, example the 0xFX1E instruction, which simply adds the content of the memory at location VX to the I register, simply translates into this:
At this point, once we have read the ROM file into the _memory array, we can start processing the opcodes:
That’s it for today! I’ve pushed all the source on GitHub, so feel free to take a look.
The next time we’ll see how we can run the emulator in a Blazor WASM application. Ciao!