So you’ve just written yourself a fancy new emulator with multiple cpu’s support and things are looking up. The only problem is, it’s slow. Not only is it slow, it’s just too darn slow. So you go back and optimize, optimize, optimize. And still, it’s just not very fast. So what do you do?
Well, there’s been all sorts of radical tricks people have used in the past. From dynamic and static recompilation(which I may get into another time, though I’m sure it’s been talked to death already) to high-level emulation. There’s one technique though you don’t hear too much about, but can yield some truly awesome results on cpu’s that end up waiting around a lot: idle detection.
The concept is that programs tend to wait on events to happen before they continue on with what they were doing. Like v-sync, dma transfers, a second cpu, etc. And most times the code looks something like this:
while (SomeVariable == SomeValue)
{
// Do nothing
}
So essentially the cpu is just spinning its tires while doing nothing productive. So how can we cut down on the waste? Well, first of all you’ll need to add a mechanism in your cpu emulation code that checks to see if we’re in such a state. You can either have your code check for certain instruction patterns, or use tables to speed things up. Once you’ve done that, then stop executing cpu instructions until you hit your next instruction executing phase.
At the next phase, see if the variable or register has changed, if it hasn’t once again stop executing cpu instructions. Keep repeating until there’s a change, then resume normal operation. It’s a pretty simple process.

No comments yet.
RSS Feed for comments on this post. TrackBack URI.
You must be logged in to post a comment.