cli added for debug
[fceu.git] / drivers / cli / throttle.c
diff --git a/drivers/cli/throttle.c b/drivers/cli/throttle.c
new file mode 100644 (file)
index 0000000..33941bb
--- /dev/null
@@ -0,0 +1,45 @@
+#include <sys/time.h>
+#include <unistd.h>
+#include "main.h"
+#include "throttle.h"
+
+static uint64 tfreq;
+static uint64 desiredfps;
+
+void RefreshThrottleFPS(void)
+{
+ desiredfps=FCEUI_GetDesiredFPS()>>8;
+ tfreq=1000000;
+ tfreq<<=16;    /* Adjustment for fps returned from FCEUI_GetDesiredFPS(). */
+}
+
+static uint64 GetCurTime(void)
+{
+ uint64 ret;
+ struct timeval tv;
+
+ gettimeofday(&tv,0);
+ ret=(uint64)tv.tv_sec*1000000;
+ ret+=tv.tv_usec;
+ return(ret);
+}
+
+void SpeedThrottle(void)
+{
+ static uint64 ttime,ltime;
+
+ waiter:
+
+ ttime=GetCurTime();
+
+ if( (ttime-ltime) < (tfreq/desiredfps) )
+ {
+  usleep(1000);
+  goto waiter;
+ }
+ if( (ttime-ltime) >= (tfreq*4/desiredfps))
+  ltime=ttime;
+ else
+  ltime+=tfreq/desiredfps;
+}
+