CrabEmu Logo

What is Emulation?

 

Before I start, this isn't meant to explain everything about the topic of emulation in excruciating detail, I'll leave things such as that up to much better sources (wikipedia is your friend here). Rather, this is meant to explain the basics of what emulators (such as CrabEmu) actually are and what they do. With that said, I personally find emulation to be one of the more interesting topics in computer science, mostly because of the challenge that writing an emulator entails. Through writing CrabEmu, I've personally learned quite a bit about many things that I probably wouldn't have had much contact with otherwise.

At its root, an emulator is simply either a piece of software (most common) or hardware (more common in the past, but still used) that simulates the hardware of another device. The rest of this piece will focus upon software emulators rather than hardware ones, as they are the more interesting case, in my opinion. Since the emulator simulates the exact hardware of the original device (in theory, anyway) it should be able to execute the same code as that original device. The goal of software emulation is to obtain as close to perfect emulation of the original device so as to be able to run as much software as possible from that original device. Perfect emulation however is quite difficult and, due to many factors (like incomplete/incorrect/missing/non-existant documentation) is often times not easily possible.

The basic way that a software emulator works is by simulating each important piece of the original hardware in code somehow. This includes things like memory, sound chips, video chips, CPUs, as well as various input devices and many other things as well. Each individual component must be carefully coded so as to work like the original device as well as work together with other components. There are always various ways of emulating components, some lending to more speed than others (almost always at the cost of complexity). The major example of this in emulation is in CPUs.

There are basically two different ways (realistically) of doing CPU emulation: pure interpretation (aka fetch-decode-execute) and dynamic binary translation (aka dynamic recompilation, Just-In-Time compilation, dynarec and many other terms). Pure interpretation basically means that you take each instruction of the original machine, fetch it from memeory, decode what its saying to do, and execute it in the host system. This method of emulation of a CPU can be quite accurate but is often quite slow. This must be done for every instruction every time it is seen, which means that running the same piece of code over and over again incurs the same penalty in speed every time it is executed. CrabEmu uses pure interpretation as speed is not too terribly high of a concern in emulating older systems.

Dynamic binary translation on the other hand, takes the piece of code written for the target system, and figures out an equivalent piece of code for the host system. It then will often times cache this rewritten code so that when the code is executed again it will not incur the translation penalty again. In this way, a dynamic binary translator will generally be faster than a pure interpreter (if the two systems are similar enough), but will be vastly more complex and will be tied to the host system. Dynamic binary translation is most useful in emulating newer systems where the speed gap between the host and target isn't as great as it is with older systems.

That will pretty much do it for this article for now. I may add more later if anyone is really interested, but I somewhat doubt that'll be the case.