[hatari-devel] More precise VBL delays

Eero Tamminen oak at helsinkinet.fi
Sun Feb 6 21:43:30 CET 2011


Hi,

On sunnuntai 06 helmikuu 2011, Nicolas Pomarède wrote:
> 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 :)

This fixed the US TOS issue for ST TOS, but I just noticed a regression
for STE TOS.  There isn't anymore keyclick sound on STE desktop,
just a short sound on first key press and then silence...


> 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).

As only the interval is interesting (not timezone etc corrected
wall clock time) why to use gettimeofday() instead of 
clock_gettime(CLOCK_MONOTONIC,...)?


> 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)


	- Eero



More information about the hatari-devel mailing list