604cef7ccc51bd4d1f21e472f30e62ab0a4fd716
[fceu.git] / drivers / cli / throttle.c
1 #include <sys/time.h>
2 #include "main.h"
3 #include "throttle.h"
4
5 static uint64 tfreq;
6 static uint64 desiredfps;
7
8 void RefreshThrottleFPS(void)
9 {
10  desiredfps=FCEUI_GetDesiredFPS()>>8;
11  tfreq=1000000;
12  tfreq<<=16;    /* Adjustment for fps returned from FCEUI_GetDesiredFPS(). */
13 }
14
15 static uint64 GetCurTime(void)
16 {
17  uint64 ret;
18  struct timeval tv;
19
20  gettimeofday(&tv,0);
21  ret=(uint64)tv.tv_sec*1000000;
22  ret+=tv.tv_usec;
23  return(ret);
24 }
25
26 void SpeedThrottle(void)
27 {
28  static uint64 ttime,ltime;
29
30  waiter:
31
32  ttime=GetCurTime();
33
34  if( (ttime-ltime) < (tfreq/desiredfps) )
35   goto waiter;
36  if( (ttime-ltime) >= (tfreq*4/desiredfps))
37   ltime=ttime;
38  else
39   ltime+=tfreq/desiredfps;
40 }
41