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