Tuesday 25 October 2016

Chip 8 Emulator

Chip 8 Emulator


Hello, world! Sorry its been so long since my last post. I havent been feeling very well lately, and school is constantly taking up all my free time. Ive got something awesome to show you all though! Im taking a microprocessor applications class this semester and the final project was to spend the last three weeks making whatever I wanted. I ended up making a Chip-8 emulator for the PIC32MX270F256B microcontroller. Heres the final circuit :)
This whole semester has been entirely focused on using the Arduino Mega 2560 R3, and, in my opinion, Arduinos take all the fun out of hardware and software design. Fortunately my teacher said theres no limitations on the final project, so I jumped on the chance to use a PIC microcontroller since Ive had a few years of experience with them.

I bought a Nokia 6100 LCD a few years ago, but never did anything with it. Seemed like a perfect chance to put it to good use! It took about a week of optimizing the software before I was able to get it running at a reasonable speed. The fastest I could get it to refresh was 69 frames per second, but I limited it to 30 frames per second for this emulator project since the additional frames didnt cause any noticeable difference. Also I overclocked the PIC microcontroller to 60 MHz even though its only rated to 50 MHz. At 64MHz there was serious problems, but 60MHz seems to be stable enough.

I ended up using ChaNs FatFS library to allow the microcontroller to communicate with the SD card using a FAT32 file system. I still had to implement all the low level SD I/O functions to use this library though, so it wasnt nearly as simple as I make it out to be. In fact getting the SD card to work was the most difficult part of this project...

The last big hurtle was the actual Chip-8 emulator, which I based a lot of it off of Cowgods Chip-8 Technical Reference. I started writing it for the PC since I can debug computer software much faster than I can debug a microcontroller program. The Chip-8 is a very simple interpreter, so it only took about a day to get it working and port it over from the PC to the PIC. Then I spent another day optimizing it by reducing the complexity of the opcode functions and using several function pointer tables to indirectly call the opcode functions. The Chip-8 uses sixteen buttons, but my final project only has eight buttons. To accommodate for this difference, each Chip-8 rom has a configurations file that details how the keys should be mapped and what speed the emulator should be run. I also went ahead and added the ten SChip opcodes to my project since I had some extra free time.

As a quick addition, I added an opening image and jingle to my project when it starts up. To get it to display an image, I reverse engineered the 24bit BMP format and developed a program to convert those image files into the 8bit color format that my screens code can use. The extra padding that BMPs add if their width is not a multiple of four almost tricked me, but I finally figured it out after a while...

Heres the final schematic of the circuit, and heres the source code if anyone is curious. It contains all the code for the micrcontroller and all the code for the programs I created to assist in its development. Enjoy! And sorry if the function lookup tables obfuscated the Chip-8 code too much ;)
And since its kind of hard to describe how awesome this project is, heres a quick video to demonstrate it :) Ive never really liked how my voice sounds on camera...sorry if everything seems awkward. I bought an iPod Touch a few months ago because I wanted to start recording videos to post here, so if this video goes well then Ill try to make it a regular thing.



Also I passed the Fundamentals of Engineering (FE) Exam! So Im now officially donovan6000 EIT. Lol adding a suffix to my alias seems weird... XD

Available link for download