GB Timer: Improve DIV reset behavior
Jeffrey Pfau jeffrey@endrift.com
Sat, 08 Oct 2016 09:57:01 -0700
2 files changed,
11 insertions(+),
3 deletions(-)
M
src/gb/timer.c
→
src/gb/timer.c
@@ -30,9 +30,6 @@ timer->irqPending = false;
timer->nextEvent += timer->nextDiv; } while (timer->nextDiv <= 0) { - if ((timer->internalDiv & 15) == 15) { - ++timer->p->memory.io[REG_DIV]; - } timer->nextDiv += GB_DMG_DIV_PERIOD; // Make sure to trigger when the correct bit is a falling edge@@ -44,6 +41,7 @@ timer->nextEvent += 4;
} } ++timer->internalDiv; + timer->p->memory.io[REG_DIV] = timer->internalDiv >> 4; } if (timer->nextEvent <= 0) { // Batch div increments@@ -64,6 +62,15 @@ }
void GBTimerDivReset(struct GBTimer* timer) { timer->p->memory.io[REG_DIV] = 0; + timer->internalDiv = 0; + timer->nextDiv = timer->p->cpu->cycles + GB_DMG_DIV_PERIOD; + if (timer->nextDiv < timer->nextEvent) { + timer->nextEvent = timer->nextDiv; + } + if (timer->nextDiv < timer->p->cpu->nextEvent) { + timer->p->cpu->nextEvent = timer->nextDiv; + } + timer->nextDiv += timer->eventDiff; } uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) {