GB: Fix div timer

This commit is contained in:
Jeffrey Pfau 2016-01-20 22:35:09 -08:00
parent 7685bc8b00
commit f8469822d7
1 changed files with 7 additions and 6 deletions

View File

@ -11,16 +11,13 @@
void GBTimerReset(struct GBTimer* timer) { void GBTimerReset(struct GBTimer* timer) {
timer->nextDiv = GB_DMG_DIV_PERIOD; // TODO: GBC differences timer->nextDiv = GB_DMG_DIV_PERIOD; // TODO: GBC differences
timer->nextTima = INT_MAX; timer->nextTima = INT_MAX;
timer->nextEvent = INT_MAX; timer->nextEvent = GB_DMG_DIV_PERIOD;
} }
int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) { int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) {
if (timer->nextEvent == INT_MAX) {
return INT_MAX;
}
timer->eventDiff += cycles; timer->eventDiff += cycles;
timer->nextEvent -= cycles; timer->nextEvent -= cycles;
if (timer->nextEvent < 0) { if (timer->nextEvent <= 0) {
timer->nextDiv -= timer->eventDiff; timer->nextDiv -= timer->eventDiff;
if (timer->nextDiv <= 0) { if (timer->nextDiv <= 0) {
++timer->p->memory.io[REG_DIV]; ++timer->p->memory.io[REG_DIV];
@ -51,7 +48,11 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) {
void GBTimerDivReset(struct GBTimer* timer) { void GBTimerDivReset(struct GBTimer* timer) {
timer->p->memory.io[REG_DIV] = 0; timer->p->memory.io[REG_DIV] = 0;
// TODO: Do we need to reset the event? timer->nextDiv = timer->eventDiff + GB_DMG_DIV_PERIOD;
timer->nextEvent = timer->nextTima;
if (timer->nextDiv < timer->nextEvent) {
timer->nextEvent = timer->nextDiv;
}
} }
uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) { uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) {