diff --git a/src/gba/gba.c b/src/gba/gba.c index f639e6063..35d5d88fd 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -452,8 +452,8 @@ void GBATimerUpdateRegister(struct GBA* gba, int timer) { struct GBATimer* currentTimer = &gba->timers[timer]; if (currentTimer->enable && !currentTimer->countUp) { int32_t prefetchSkew = 0; - if ((gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) < gba->memory.lastPrefetchedLoads * WORD_SIZE_THUMB) { - prefetchSkew = (gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) / gba->cpu->memory.activeSeqCycles16; + if (gba->memory.lastPrefetchedPc - gba->memory.lastPrefetchedLoads * WORD_SIZE_THUMB >= (uint32_t) gba->cpu->gprs[ARM_PC]) { + prefetchSkew = (gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) * (gba->cpu->memory.activeSeqCycles16 + 1) / WORD_SIZE_THUMB; } // Reading this takes two cycles (1N+1I), so let's remove them preemptively gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent - 2 + prefetchSkew) >> currentTimer->prescaleBits); diff --git a/src/gba/memory.c b/src/gba/memory.c index 6f5fa9fbb..704f217c0 100644 --- a/src/gba/memory.c +++ b/src/gba/memory.c @@ -235,6 +235,8 @@ static void GBASetActiveRegion(struct ARMCore* cpu, uint32_t address) { } gba->lastJump = address; + memory->lastPrefetchedPc = 0; + memory->lastPrefetchedLoads = 0; if (newRegion == memory->activeRegion && (newRegion < REGION_CART0 || (address & (SIZE_CART0 - 1)) < memory->romSize)) { return; } @@ -1541,10 +1543,10 @@ int32_t GBAMemoryStall(struct ARMCore* cpu, int32_t wait) { int32_t previousLoads = 0; // Don't prefetch too much if we're overlapping with a previous prefetch - if ((memory->lastPrefetchedPc - cpu->gprs[ARM_PC]) < memory->lastPrefetchedLoads * WORD_SIZE_THUMB) { + if (UNLIKELY((memory->lastPrefetchedPc - cpu->gprs[ARM_PC]) < memory->lastPrefetchedLoads * WORD_SIZE_THUMB)) { previousLoads = (memory->lastPrefetchedPc - cpu->gprs[ARM_PC]) >> 1; } - while (stall < wait && loads + previousLoads < 8) { + while (stall < wait && LIKELY(loads + previousLoads < 8)) { stall += s; ++loads; }