[hatari-devel] More precise VBL delays

David Savinkoff dsavnkff at telus.net
Sun Feb 6 22:37:24 CET 2011



On Feb 6, 2011, Nicolas Pomarède <npomarede at corp.free.fr> wrote: 

Hello

I finally committed some improvements to Main_WaitOnVbl to better handle 
sound synchronisation at 60 Hz.

First of all, sorry to David for not including his patch directly, but 
although I agree with the way this patch works, it has one major 
drawback : it doesn't work if sound is turned off in Hatari :)

******
(DS)
My patch 'does' work when sound is turned off. I tested it also.
Hatari runs very slightly slower when the sound is turned off.
20*(VBL frequency)/213000 * 100% = percent slower
This insignificant (mis)feature can be easily changed.

Note that my patch can still be adapted to the current code
to complete the improvements.
******

I know most people are using sound, but still, I prefered to include a 
patch that just rely on OS functions to get time/ sleep, with no 
relation to other parts of Hatari (this works similarly in other 
emulator, for example Vice for C64).

So, 2 changes were made :

1) all times are now computed in micro seconds instead of milliseconds. 
By just doing this, we would now sleep for 17 ms instead of 16 ms before 
when running at 60 Hz. This would give a 2% error instead of the 
previous 4% (as the real sleep time should be 16.6666... ms)

2) When nanosleep() and gettimeofday() are available at compilation 
time, we use them instead of SDL_Delay and SDL_GetTicks. This means we 
get microsec precision when reading time and nanosec precision 
(theorically) when sleeping.

nanosleep() and gettimeofday() are POSIX, so all conforming unixes 
should have them (Linux and OSX for example).

For Windows we're still using SDL functions, so we use milli sec, but 
precision will still be improved by point 1) and there should be no 
sound delay either when running at 60 Hz under Windows now.

With the 60 Hz example, previous version was rounding each VBL at 16 ms, 
so you got 960 ms sleep instead 1000 ms during one sec (4% error).

With microsec computation and SDL_Delay / SDL_GetTicks, Hatari would now 
repeat the following sequence of sleep : 17 17 16 17 17 16 ..


If nanosleep is available, we can really sleep for the equivalent of 
16.667 ms (instead of alternating sleeps of 16 and 17 ms)

In the end, Hatari VBL precision should now be improved by more than 
x1000 (we could do more by using nano sec for computation, but I'm not 
sure this would be hearable)


Nicolas



More information about the hatari-devel mailing list