From 5ff3e339fe3ce5613b1e3e7439395cba7f52733d Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 8 Oct 2016 09:57:01 -0700 Subject: [PATCH] GB Timer: Improve DIV reset behavior --- CHANGES | 1 + src/gb/timer.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 39ad5517c..bfc303bd7 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,7 @@ Features: - GBA: Support printing debug strings from inside a game Bugfixes: - LR35902: Fix core never exiting with certain event patterns + - GB Timer: Improve DIV reset behavior Misc: - SDL: Remove scancode key input - GBA Video: Clean up unused timers diff --git a/src/gb/timer.c b/src/gb/timer.c index 4b4502961..a1acdb574 100644 --- a/src/gb/timer.c +++ b/src/gb/timer.c @@ -30,9 +30,6 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) { 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 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) { } } ++timer->internalDiv; + timer->p->memory.io[REG_DIV] = timer->internalDiv >> 4; } if (timer->nextEvent <= 0) { // Batch div increments @@ -64,6 +62,15 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) { 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) {