The source code of these emulators are even available for the curious that wants to know how they work. I myself had a peek at the source code of these projects. Looking at the complexity of the code for these projects just triggered another curiosity in the back of my head: How do you create such an emulator from scratch?
There are a couple of blog posts out there giving an overview and some pointers on how to write them, but no one actually goes into much detail on the steps they followed, problems they encountered and the steps they followed to solve them.
So, last year I wrote an Commodore 64 emulator from scratch in Java. This was lots of fun, but also on some days generated an equal amount of frustration!
This kept me busy for a couple of months. In the end I manage to write an emulator with support to load games from TAPE images (.tap files). I tried it on two .tap game images and gave sort of acceptable results. At that point duties called at work and I had to put this project on the back-burner and effectively forgot about until now...
Planned ApproachIn this series of block posts I am planning to follow an incremental approach. Starting out with a very simple emulator implementing only two machine language instructions of the 6510/6502 CPU (the CPU under the hood of the C64) and implementing the other instructions bit by bit. During this phase, I will also be writing very simple 6502 assembly language programs to test if I am implementing these instructions correctly.
Once I have implemented the full 6502 instruction set for the emulator, I will be running some 6502 test harnesses to see if our emulator behaves more or less as expected as a whole. The is a couple of 6502 test harnesses on the web that we can use for this purpose.
After running the test harnesses, we will attempt to boot the C64 with its ROMS (e.g. KERNEL ROM and BASIC ROM). At this phase, we will not have any C64 screen display logic implemented yet, so the only way to check if the boot process is successfully, would be to inspect the contents of the screen memory (which is the 1000 byte memory block starting at memory location 1024) and see if it is populated with BASIC "welcome message".
After been convinced screen memory is populated with a welcome message, it would be time to start implementing emulation for the hardware. First a very basic implementation for a C64 screen in text mode (Black and white) and then the keyboard.
Well, TAPE loading actually always fascinated me on the Commodore 64. Firstly, because a Tape is analogue in nature, how did the C64 manage to extract digital information from it? Another thing that fascinated me about C64 game tapes, was that most of theme managed to high-jack the C64 and complete the loading process and fire up the game without the user typing RUN. As part of writing the emulator I wanted to found out how this "High-jacking" worked.
So, I will be focussing on TAPE instead of disk. I will choose a c64 game image on the WEB available in TAPE format and try to get it to run on our emulator, Implementing any missing functionality as we go along.
This whole approach will mean at times refining our code and doing some re-factoring.
About my Java emulatorFor those that want to have a look at the of my Java emulator I wrote last year you can go to https://github.com/ovalcode/c64jjs
To run the project, add it as a project to eclipse. To kick it off use the main method in the class
Currently there is 4 important binary files missing from the project:
- The three C64 ROMS: Kernel, Basic and Characer ROM. These ROMS are readily available on the WEB. Download them to your local disk and adjust the paths in the Machine class constructor
- Any game image in .tap format. Remember to adjust the path accordingly in contructor on the Tape class
With the emulator started, to the load the game, type LOAD at the ready prompt an press enter. When prompted "Press Play On Tape" hit the backslah(\) key and loading will start.
In this series of posts we will be developing a C64 emulator from scratch and will try try to document my experience in as much detail as possible.
Thats it! I hope you will enjoy my coming series of posts. Feel free to give comments and suggestions.