all repos — mgba @ c9b69bba44337992d0b6de63a57ea162ae9757f9

mGBA Game Boy Advance Emulator

GB Timer: Batch timer updates as needed
Jeffrey Pfau jeffrey@endrift.com
Sun, 28 Aug 2016 10:32:47 -0700
commit

c9b69bba44337992d0b6de63a57ea162ae9757f9

parent

4c5ba8d8c14ec3110c732eddf2aac91379fd4e14

1 files changed, 13 insertions(+), 2 deletions(-)

jump to
M src/gb/timer.csrc/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; }