GB: Fix timer resetting
Jeffrey Pfau jeffrey@endrift.com
Mon, 30 May 2016 12:52:03 -0700
1 files changed,
12 insertions(+),
6 deletions(-)
jump to
M
src/gb/timer.c
→
src/gb/timer.c
@@ -30,13 +30,19 @@
if (timer->nextTima != INT_MAX) { timer->nextTima -= timer->eventDiff; if (timer->nextTima <= 0) { - ++timer->p->memory.io[REG_TIMA]; if (!timer->p->memory.io[REG_TIMA]) { timer->p->memory.io[REG_TIMA] = timer->p->memory.io[REG_TMA]; timer->p->memory.io[REG_IF] |= (1 << GB_IRQ_TIMER); GBUpdateIRQs(timer->p); + timer->nextTima = timer->timaPeriod - 4; + } else { + ++timer->p->memory.io[REG_TIMA]; + if (!timer->p->memory.io[REG_TIMA]) { + timer->nextTima = 4; + } else { + timer->nextTima = timer->timaPeriod; + } } - timer->nextTima = timer->timaPeriod; } if (timer->nextTima < timer->nextEvent) { timer->nextEvent = timer->nextTima;@@ -51,8 +57,8 @@
void GBTimerDivReset(struct GBTimer* timer) { timer->p->memory.io[REG_DIV] = 0; timer->nextDiv = timer->eventDiff + timer->p->cpu->cycles + GB_DMG_DIV_PERIOD; - if (timer->eventDiff + GB_DMG_DIV_PERIOD < timer->nextEvent) { - timer->nextEvent = timer->eventDiff + GB_DMG_DIV_PERIOD; + if (timer->nextDiv - timer->eventDiff < timer->nextEvent) { + timer->nextEvent = timer->nextDiv - timer->eventDiff; if (timer->nextEvent < timer->p->cpu->nextEvent) { timer->p->cpu->nextEvent = timer->nextEvent; }@@ -84,8 +90,8 @@ }
void GBTimerUpdateTIMA(struct GBTimer* timer) { timer->nextTima = timer->eventDiff + timer->p->cpu->cycles + timer->timaPeriod; - if (timer->eventDiff + timer->timaPeriod < timer->nextEvent) { - timer->nextEvent = timer->eventDiff + timer->timaPeriod; + if (timer->nextTima - timer->eventDiff < timer->nextEvent) { + timer->nextEvent = timer->nextTima - timer->eventDiff; if (timer->nextEvent < timer->p->cpu->nextEvent) { timer->p->cpu->nextEvent = timer->nextEvent; }