GB: Fix timer resetting

This commit is contained in:
Jeffrey Pfau 2016-05-30 12:52:03 -07:00
parent f40b9ec82e
commit cf8868d5cb
1 changed files with 12 additions and 6 deletions

View File

@ -30,13 +30,19 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) {
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 @@ int32_t GBTimerProcessEvents(struct GBTimer* timer, int32_t cycles) {
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 @@ uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC 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->nextTima - timer->eventDiff < timer->nextEvent) {
timer->nextEvent = timer->nextTima - timer->eventDiff;
if (timer->nextEvent < timer->p->cpu->nextEvent) {
timer->p->cpu->nextEvent = timer->nextEvent;
}