Emulation Document - Part 1
Author: zenogais Difficulty Level: Newbie

What is Emulation?

    To begin explaining how to write an emulator, you must know what the heck I'm talking about. An emulator is, according to Cxbx Xbox Emulator Progress Report by Aaron Robinson, which I have adapted here: "Any program that is designed to translate and execute machine instructions of a specific architecture into their equivalent machine instructions on another architecture." What this means is basically that an emulator is a program designed to execute binary code written for a specific hardware device on the hardware of another machine. For instance ePSXe is a Playstation emulator designed to run Playstation games on x86 (Intel/AMD) CPUs.

What Makes Emulators Exciting?

    Do you like to play video games, or did you like to play video games on say Nintendo, Amiga, Altair, Sega Saturn, Sega Genesis, Super Nintendo, Nintendo 64, or Playstation. Do you get nostalgic thinking about how much you loved your old Amiga, and to make it worse you remember that you threw it out or sold it. Well because of emulators you can play those old games on all those old systems and relive the past all over again. There are many emulators out there, for many different computers, and the best part about these emulators is that they are almost all free.
    The emulation scene is also a very exciting place. It moves extremely fast, as new emulators are released almost constantly. There is also often an emulation project begun for every new video game console that comes out. Well all, and if not all, brand new console emulators are far from complete, and many don't even play games. It is exciting to watch as the developers write new improvements for their emulators. Also emu authors are very well respected because of the time and effort they put into their emulators, and also the fact that the emulators come at the best possible price...free.

Emulator Writing Preparation

    Before you can begin an emulator, there is much preparation to be done. The most important aspect of this preparation is making sure that you have a good understanding of the programming language that you wish to design the emulator in. Also it is extremely helpful, and strongly urged, that you have at least a basic understanding of assembly language and that you are willing to expand that understanding if necessary. An emulator is the worst possible project to just be learning a language on, unless you think you can handle the EXTREMELY large learning curve that comes about from inadequate understanding of the programming language. Also I won't lie, emulation is hard to learn, but once you've learned it once you can write almost any emulator you want because you got over the learning curve. Now that we have that step out of the way, we can start the actual emulator programming preparation.
    The most important thing to do when beginning a new emulator is to, of course, decide what you want to emulate. Whether you want to emulate the latest console, or the oldest PC known to man, it’s all up to you. Once you have that step settled you have to research the hardware. Most of the time for any hardware more than 4-10 years old there is always a fairly large amount of documentation available, however if you chose the latest greatest console then chances are you will have to reverse engineer the information about the hardware yourself. Unfortunately reverse engineering is a whole other document in itself, so your own your own for that one. The last step isn't mandatory, though it can help immensely later on when you’re working on the emulator and you want to know how to go about programming it. This step is planning. Planning for an emulator is difficult, but you can draw class diagrams if you are using an Object-Oriented language, or else you can write up a plan from scratch. For those who don't know what a class diagram is I'd recommend that they Google the word "UML".

Beginning Work on Your Emulator

    When programming an emulator, the first step is sometimes the hardest step. Where should you start? Well this is where the design phase kicks in. If you wrote a design (just do it already, it will take you like 15 minutes to 1 hour depending on how in depth you get) then you should already know where to start. If not then here’s my technique, start with the part of the emulator that relies the least on any other part. For NeoPSX it was the GUI (Graphical User Interface), because it relied on nothing else at all.
     So what do you do next after you've completed that, move on to the portion of your emulator that relies on the second least amount of objects from your emulator? Keep this pattern continuously going until you have a good portion of your emulator done. Another good technique is the release early, release often philosophy. This basically means don't wait a long time between releases. Make releases before they are ready, and show progress as often as possible. Also, remember that programming an emulator is hard work, and sometimes you may feel like you just can't take anymore (i.e. when you find a really frustrating bug that you just can't shake out), but keep with it because success is often a few feet from failure.

I'm Finished, Now What?

    Optimize, Optimize, Optimize, and then Optimize some more. Your emulator is never fully complete. I'm sure that there will always be a certain tweak that you can perform in order to make it faster in various different areas. ZSNES is a great example of this.

Wrap Up

    Hopefully now that you are done with this document you understand what an emulator is, how to go about writing one fairly simply. Hope you enjoy this, and here on some resources to help you out until I release the next document:

Dynamic Recompilation(dynarec) resources:

General Emulation Writing: For Those Daring Individuals: