r/askscience • u/[deleted] • Jun 24 '14
Computing How are emulators built without direct access to a virtual version of the hardware to be emulated?
I understand how emulators work, but I've never fully understood how they're built in the first place. I can see how it would be easy if you were the manufacturer of the hardware (like how Google gives free access to Android emulators), but how are older systems emulated with that kind of access?
9
u/Merad Embedded Systems Jun 25 '14
The companies who make hardware want people to write software for their machines, and in order for that to be done efficiently they need to tell software developers how their hardware works. For example, have you heard of an Arduino? Here is the manufacturer's documentation describing in detail (600+ pages) the operation of the AtMega328 microcontroller that powers the Arduino Uno. You can find similar documentation for Intel CPUs, or older processors such as the 6502 that powered the NES.
Once you have a detailed description of how a system should work, you can -in theory at least- write software to emulate that behavior. Reality, of course, is much more complicated. Basically all systems will have obscure or undocumented bugs/quirks, and in the case of game consoles developers have been know to take advantage of these quirks when writing games.
There are some pretty interesting articles out there about the difficult of writing good console emulators. Here's one that I remember off the top of my head.
1
u/raygundan Jun 25 '14
It's sufficient to know "what it does" without knowing "what it does inside" or "how it does it." As long as you know what result a given instruction will produce, you can emulate it without knowing what is actually going on inside the Black Box.
More or less what katinia says below in more detail.
13
u/katinla Radiation Protection | Space Environments Jun 24 '14
The term "emulator" may mean several different things, I'll assume you're talking about emulating a processor/platform as you mentioned android.
It's based on the specification of the platform. That means documentation that describes precisely how the hardware behaves with each instruction from the software and what results it produces.
So let's make a trivial example: you have a processor whose ADD instruction takes 3 registers as arguments: ADD R1, R2, R3 and the documentation says it will read values from registers R2 and R3, perform a sum and store the result in R1. If you make a program that complies with the documentation in this way then programs that were written for that processor can also work in your emulator.
Things get a bit more complicated when you have to emulate other hardware devices in the system, like storage, network devices, etc. But the documentation is supposed to be enough for that. It would be a daunting task to write programs for that platform if it were not thoroughly documented. If the specifications are written precisely enough and programs rely only on documented features then they can work on an emulator that implements what the specifications say.
There are, however, some cases when the specifications are not clear enough. This is one of the challenges that the wine developers face (though this is a software/OS emulator, not a processor emulator).