At its most basic, a computer program (including a game on a computer or gaming console) is a list of very rudimentary instructions. If you have any familiarity with high-level programming, you might be familiar with stuff like this:
10 I = 0
20 WHILE I < 10
30 PRINT I
40 I = I + 1
50 WEND
When this is compiled to a program binary, it might end up with a binary representation which looks essentially like:
1. Create a variable called [I] and set it to the value of 0.
2. Jump to line 6 when the variable [I] is equal to or greater than 10.
3. Output the value of variable [I] to StdOut (the screen)
4. Increment the variable [I] up by 1
5. Jump to line 2
6. [End Program]
Now, these instructions above (in binary form) would normally be sent directly to the CPU, no further processing. Each line takes one to three clock cycles and is super fast.
The problem arises when you find out that the binary representation of these instructions on an Intel x64 processor is completely different than the binary representation of the same instructions on a MOS Technology 6502 (used on the NES). Also, (going back to hypothetical since CPU architecture is out of my wheelhouse) the instructions on line 2 and line 4 have to be rewritten for the Intel x64 due to it having a different instruction set:
* For line 2, the instruction might be something like “Define a loop from lines 2 to 4. Loop while [I] is less than 10.” and line 5 is skipped altogether.
* Line 4, the Increment statement cannot increment by 1 but must specify a second variable to increment by.
(These are just examples)
What an emulator does is it steps through each instruction and, as best as it can, attempts to execute the equivalent instruction. The emulator knows how to handle situations such as line 2.
So why is it slow?
In the most optimized emulator, it first has to read the instruction from the code being emulated which is like 3 cpu instructions. Then figure out what the equivalent cpu instruction should be which may be as few as one instruction and as many as 30, especially is there’s a dynamic rewrite like the example with the loop. Then it has to run the equivalent instruction(s) which very likely requires reading or writing variables from ram, which is also being emulated, so more instructions.
And this is just for the game logic. This doesn’t cover anything about graphics or sound.
It’d be easy if it were simply translating the instructions from one cpu to another, but on a game console, the game assumes it’s the only thing running and that it has complete control over the hardware which is NOT the case on modern computers.
Latest Answers