Skip to content →

throttling the emulation

When writing code, and executing it, the CPU normally will try to execute it as fast as it can, if there are no precautions taken against that. The original Gameboy CPU ran around roughly 4 MHz, while modern PCs run on several GHz per core. Therefore, our emulation usually is way to fast, to properly simulate our emulated system.

So, how can we prevent our code from being executed too fast? Well, we know how many cycles the Gameboy CPU is clocking per second / per frame. We can just count the cycles that have passed, and once we reach our threshold of, let’s say a frame’s length, we will force our emulator to pause, until a whole frame has passed.

int cyc = 0;
while(1) { 
    t_start = std::chrono::high_resolution_clock::now();
    cyc += stepCPU(registers, flags, pc, sp, interrupts_enabled);
    if(cyc >= 17476) {
        cyc = 0;
        while(std::chrono::high_resolution_clock::now() - t_start < 16.67) 
           // do nothing 
Note: this is only untested pseudocode!

This will make sure, our frames take as much time, as they would on a Gameboy. This isn’t very precise, because the Gameboy doesn’t run at 60 Hz, but rather on 59.7 Hz, but this will do for now. Now with the throttle implemented, we should be able to see the Nintendo logo, actually scrolling.

This implementation of a throttle will be removed, once we develop the APU / SPU, as the emulator will by synced-by-audio.


Leave a Reply