From 1a51fa791356b52a88e8ae0e6941034b23cd354f Mon Sep 17 00:00:00 2001 From: gameblabla Date: Sun, 20 Oct 2019 03:20:08 +0200 Subject: Improve frameskipping code. (to be tested) --- shell/emu/core.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'shell/emu') diff --git a/shell/emu/core.c b/shell/emu/core.c index be99e72..e20e43d 100644 --- a/shell/emu/core.c +++ b/shell/emu/core.c @@ -255,12 +255,40 @@ void SaveState(char* path, uint_fast8_t state) if (buffer) free(buffer); } + +#ifdef FRAMESKIP +static uint32_t Timer_Read(void) +{ + /* Timing. */ + struct timeval tval; + gettimeofday(&tval, 0); + return (((tval.tv_sec*1000000) + (tval.tv_usec))); +} +static long lastTick = 0, newTick; +static uint32_t SkipCnt = 0, video_frames = 0, FPS = 60, FrameSkip; +static const uint32_t TblSkip[4][4] = { + {0, 0, 0, 0}, + {0, 0, 0, 1}, + {0, 0, 1, 1}, + {0, 1, 1, 1} +}; +#endif + void Emulation_Run (void) { #ifndef USE_BLARGG_APU static int16_t audio_buf[2048]; #endif + +#ifdef FRAMESKIP + SkipCnt++; + if (SkipCnt > 3) SkipCnt = 0; + if (TblSkip[FrameSkip][SkipCnt]) IPPU.RenderThisFrame = false; + else IPPU.RenderThisFrame = true; +#else IPPU.RenderThisFrame = true; +#endif + #ifdef USE_BLARGG_APU S9xSetSoundMute(false); #endif @@ -295,6 +323,26 @@ void Emulation_Run (void) IPPU.RenderThisFrame = true; } #endif + +#ifdef FRAMESKIP + video_frames++; + newTick = Timer_Read(); + if ( (newTick) - (lastTick) > 1000000) + { + FPS = video_frames; + video_frames = 0; + lastTick = newTick; + if (FPS >= 60) + { + FrameSkip = 0; + } + else + { + FrameSkip = 60 / FPS; + if (FrameSkip > 3) FrameSkip = 3; + } + } +#endif } -- cgit v1.2.3