I’ve spent the last couple of weeks writing a CHIP-8 emulator in my free time. I have been talking about writing an emulator in my blog for a while now. I wanted to write a Z80 emulator after playing Metal Gear Solid Ground Zero but I settled for writing a CHIP-8 emulator.
What is an Emulator?
In computing, an emulator is hardware or software that enables one computer system (called the host) to behave like another computer system (called the guest).
The above quote has been copied from Wikipedia
Why an Emulator?
I’ve been fascinated with an emulator ever since I was a kid. I think my first emulator was CVGX, I spent countless hours playing TEKKEN 3 on it only to realize that it was emulating a PlayStaiton 1. Then I spent countless hours playing Smackdown Here Comes the pain after figuring out that it could play any PlayStation 1 game.
I eventually found VGBA an emulator of Gameboy and Gameboy. I started playing Pokemon Red on it. That was fun and I think I followed that with Pokemon Blue. I think I caught all the legendary Pokemons including Mewto. That’s a different story for a different time.
I eventually bought a Playstation 2, my computer was too slow to emulate it then. I think I had a computer running Intel Pentium 4 at 1.6 Ghz and I got terrible frame rates with the PCSX2 emulator. In University I tried PCSX2 again as my PS2 wasn’t working and I really wanted to play Downhill Domination.
What is CHIP-8?
CHIP-8 is an interpreted programming language that run on the CHIP-8 virtual machine. It’s a fairly simple system that can run games like PONG and Space Invaders.
Why CHIP-8?
CHIP-8 looked like the Hello World! in the world of emulation. It is a fairly simple system with monochrome graphics and only one sound. It has 16 8 bit registers, 16 bit program counter, 8 bit delay timer, 8 bit sound timer and 4k of memory. If you want to write an emulator for CHIP-8 I’d highly recommend the following manual and the Wikipedia article.
If you want further help then you can have a peep at this article.
How do I do it?
This is how the chip-8 roughly works.
func emulateCycle() {
// fetch opcode
// decode opcode
// execute opcode
}
func Run() {
for true {
emulateCycle()
renderGraphics()
handleKeyPress()
}
}
For the CPU to work you’ll have to implement about 35 instructions though only 34 them are needed in modern interpreters.
What did you write the emulator in?
I wrote the emulator in Go, primarily because I wanted to learn the language. I didn’t use any of the concurrency related features like channels or goroutines. Using Golang allowed me to be conscious about the size of every register that I created as Golang doesn’t support implicit typecasting. Another feature that I loved about the language was the out of the box support for testing.
I had a chat with a friend who pointed out some resources to improve your Golang abilities.
I haven’t found time to read any of this yet, I hope to soon.
Update: For graphics/keyboard/sound I am using this library called Ebiten. I just had a pull request from the author of the library helping improve the performance of the emulaotr.
Is the emulation perfect?
At the time of writing I figured out that there is some bug while running Invaders on the emulator as my implementation
of the instruction 0xFx0A
is buggy. I need to work on it.
Show Me The Code
You can find the code on my Github here