GB Timer: Batch timer updates as needed
Jeffrey Pfau jeffrey@endrift.com
Sun, 28 Aug 2016 10:32:47 -0700
1 files changed,
13 insertions(+),
2 deletions(-)
jump to
M
src/gb/timer.c
→
src/gb/timer.c
@@ -29,12 +29,11 @@ GBUpdateIRQs(timer->p);
timer->irqPending = false; timer->nextEvent += timer->nextDiv; } - if (timer->nextDiv <= 0) { + while (timer->nextDiv <= 0) { if ((timer->internalDiv & 15) == 15) { ++timer->p->memory.io[REG_DIV]; } timer->nextDiv += GB_DMG_DIV_PERIOD; - timer->nextEvent += GB_DMG_DIV_PERIOD; // Make sure to trigger when the correct bit is a falling edge if (timer->timaPeriod > 0 && (timer->internalDiv & (timer->timaPeriod - 1)) == timer->timaPeriod - 1) {@@ -45,6 +44,18 @@ timer->nextEvent += 4;
} } ++timer->internalDiv; + } + if (timer->nextEvent <= 0) { + // Batch div increments + int divsToGo = 16 - (timer->internalDiv & 15); + int timaToGo = INT_MAX; + if (timer->timaPeriod) { + timaToGo = timer->timaPeriod - (timer->internalDiv & (timer->timaPeriod - 1)); + } + if (timaToGo < divsToGo) { + divsToGo = timaToGo; + } + timer->nextEvent += GB_DMG_DIV_PERIOD * divsToGo; } timer->eventDiff = 0; }