I'm trying to emulate a gameboy, but it plays a little too fast.
This how I'm doing my timing inside the main loop.
if (cpu.T >= CLOCKSPEED / 40) // if more than 1/40th of cycles passed
{
// Get milliseconds passed
QueryPerformanceCounter(&EndCounter);
unsigned long long counter = EndCounter.QuadPart - LastCounter.QuadPart;
MSperFrame = 1000.0 * ((double)counter / (double)PerfCountFrequency);
LastCounter = EndCounter;
// if 1/40th of a second hasn't passed, wait until it passes
if (MSperFrame < 25)
Sleep(25 - MSperFrame);
cpu.T -= CLOCKSPEED / 40;
}
CLOCKSPEED is the cycles per second of the emulated cpu (4194304)
cpu.T is cycles passed.
I'm using Visual Studio 2013. I even tried switching to C++ and using steady_clock but nothing changed. What could be the problem?