From eee24961b1f2b2bcdb50b132486c8690b8d7237c Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Tue, 26 Jan 2016 20:57:34 -0800 Subject: [PATCH] GB: Partially fix timers --- src/gb/io.c | 4 +--- src/gb/timer.c | 18 +++++++++++------- src/gb/timer.h | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/gb/io.c b/src/gb/io.c index 3a0bb95ea..c8a38d131 100644 --- a/src/gb/io.c +++ b/src/gb/io.c @@ -55,10 +55,8 @@ void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) { case REG_DIV: GBTimerDivReset(&gb->timer); return; - case REG_TIMA: - // ??? - return; case REG_JOYP: + case REG_TIMA: case REG_TMA: case REG_LYC: // Handled transparently by the registers diff --git a/src/gb/timer.c b/src/gb/timer.c index 45a07fa45..47b240563 100644 --- a/src/gb/timer.c +++ b/src/gb/timer.c @@ -71,15 +71,19 @@ uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) { timer->timaPeriod = 256; break; } - timer->nextTima = timer->eventDiff + timer->timaPeriod; - if (timer->nextTima < timer->nextEvent) { - timer->nextEvent = timer->nextTima; - if (timer->nextEvent < timer->p->cpu->nextEvent) { - timer->p->cpu->nextEvent = timer->nextEvent; - } - } + GBTimerUpdateTIMA(timer); } else { timer->nextTima = INT_MAX; } return tac; } + +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->nextEvent < timer->p->cpu->nextEvent) { + timer->p->cpu->nextEvent = timer->nextEvent; + } + } +} diff --git a/src/gb/timer.h b/src/gb/timer.h index af31ea81f..c650dad3a 100644 --- a/src/gb/timer.h +++ b/src/gb/timer.h @@ -32,5 +32,6 @@ void GBTimerReset(struct GBTimer*); int32_t GBTimerProcessEvents(struct GBTimer*, int32_t cycles); void GBTimerDivReset(struct GBTimer*); uint8_t GBTimerUpdateTAC(struct GBTimer*, GBRegisterTAC tac); +void GBTimerUpdateTIMA(struct GBTimer* timer); #endif