| 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 | uint64 f=FCEUI_GetDesiredFPS(); |
| 11 | // great, a bit faster than before |
| 12 | //f = (f*65) >> 6; |
| 13 | desiredfps=f>>8; |
| 14 | tfreq=1000000; |
| 15 | tfreq<<=16; /* Adjustment for fps returned from FCEUI_GetDesiredFPS(). */ |
| 16 | } |
| 17 | |
| 18 | static uint64 GetCurTime(void) |
| 19 | { |
| 20 | uint64 ret; |
| 21 | struct timeval tv; |
| 22 | |
| 23 | gettimeofday(&tv,0); |
| 24 | ret=(uint64)tv.tv_sec*1000000; |
| 25 | ret+=tv.tv_usec; |
| 26 | return(ret); |
| 27 | } |
| 28 | |
| 29 | INLINE void SpeedThrottle(void) |
| 30 | { |
| 31 | static uint64 ttime,ltime; |
| 32 | |
| 33 | waiter: |
| 34 | |
| 35 | ttime=GetCurTime(); |
| 36 | |
| 37 | if( (ttime-ltime) < (tfreq/desiredfps) ) |
| 38 | { |
| 39 | goto waiter; |
| 40 | } |
| 41 | if( (ttime-ltime) >= (tfreq*4/desiredfps)) |
| 42 | ltime=ttime; |
| 43 | else |
| 44 | ltime+=tfreq/desiredfps; |
| 45 | } |
| 46 | |