Completed 6502 Emulator

I've managed to tick off another ultra nerdy item from my programming bucket list - implement a CPU emulator. I'm pleased to say that I've just finished implementing a cycle-accurate emulator for the MOS Technology 6502 8-bit microprocessor written entirely in Xojo.

You can read about the technical implementation of the project here: 6502 Project. The code is on GitHub.

The 6502 was an incredibly popular chip in its heyday and was the heart of loads of popular systems in the 1980s including the Nintendo Entertainment System, the Commodore 64 and the BBC Micro.

I've long been fascinated with emulation and I've often fantasised about writing a complete emulator for a system from my childhood - my favourite being the Amstrad CPC 646. This of course uses the Zilog Z80 chip, not the 6502. Over the years I've started and given up writing a Z80 emulator but for various reasons (boredom, lack of understanding, whatever) I always quit about halfway through.

This time was different. I've recently decided to make a real effort to follow through on projects that I start, no matter how challenging they become. I chose to emulate the 6502 rather than the Z80 for a few reasons:

  1. There is so much documentation out there on this chip and there are many open source emulators whose source code can be inspected.
  2. This incredible set of tests that I was able to use to verify the chip is working as expected.
  3. It has less opcodes to implement than the Z80.

The end result is a Xojo module that can be dropped into a project to form the core of an emulated system. I've painstakingly documented every method and property and littered the code with comments to help others learn how a CPU works on the inside.

The project contains an extensive test suite where each opcode (all 151 documented ones) is tested in 10,000 different register and memory states. That's right, there are over 1.5 million tests - all of which pass. This does make the GitHub repository large (since each opcode test is a separate JSON file).

I'm not sure if I will ever get round to implementing an entire system around this CPU (even a "simple" system such as the Apple II would be a big effort to build) but if I ever do (or enough people express an interest in learning about it) then at least the hardest part is done.