all repos — mgba @ a506f6cd9d6fd025410f62e3b663b0aaea3f0b82

mGBA Game Boy Advance Emulator

src/gba/timer.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "timer.h"
  7
  8#include "gba/gba.h"
  9#include "gba/io.h"
 10
 11static void GBATimerUpdate(struct mTiming* timing, struct GBA* gba, int timerId, uint32_t cyclesLate) {
 12	struct GBATimer* timer = &gba->timers[timerId];
 13	gba->memory.io[(REG_TM0CNT_LO >> 1) + (timerId << 1)] = timer->reload;
 14	timer->oldReload = timer->reload;
 15	timer->lastEvent = timing->masterCycles - cyclesLate;
 16
 17	if (GBATimerFlagsIsDoIrq(timer->flags)) {
 18		GBARaiseIRQ(gba, IRQ_TIMER0 + timerId);
 19	}
 20
 21	if (gba->audio.enable && timerId < 2) {
 22		if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == timerId) {
 23			GBAAudioSampleFIFO(&gba->audio, 0, cyclesLate);
 24		}
 25
 26		if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == timerId) {
 27			GBAAudioSampleFIFO(&gba->audio, 1, cyclesLate);
 28		}
 29	}
 30
 31	if (timerId < 4) {
 32		struct GBATimer* nextTimer = &gba->timers[timerId + 1];
 33		if (GBATimerFlagsIsCountUp(nextTimer->flags)) { // TODO: Does this increment while disabled?
 34			++gba->memory.io[(REG_TM1CNT_LO >> 1) + (timerId << 1)];
 35			if (!gba->memory.io[(REG_TM1CNT_LO >> 1) + (timerId << 1)] && GBATimerFlagsIsEnable(nextTimer->flags)) {
 36				mTimingSchedule(timing, &nextTimer->event, -cyclesLate);
 37			}
 38		}
 39	}
 40
 41	if (!GBATimerFlagsIsCountUp(timer->flags)) {
 42		uint32_t nextEvent = timer->overflowInterval - cyclesLate;
 43		mTimingSchedule(timing, &timer->event, nextEvent);
 44	}
 45}
 46
 47static void GBATimerUpdate0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 48	GBATimerUpdate(timing, context, 0, cyclesLate);
 49}
 50
 51static void GBATimerUpdate1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 52	GBATimerUpdate(timing, context, 1, cyclesLate);
 53}
 54
 55static void GBATimerUpdate2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 56	GBATimerUpdate(timing, context, 2, cyclesLate);
 57}
 58
 59static void GBATimerUpdate3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 60	GBATimerUpdate(timing, context, 3, cyclesLate);
 61}
 62
 63void GBATimerInit(struct GBA* gba) {
 64	memset(gba->timers, 0, sizeof(gba->timers));
 65	gba->timers[0].event.name = "GBA Timer 0";
 66	gba->timers[0].event.callback = GBATimerUpdate0;
 67	gba->timers[0].event.context = gba;
 68	gba->timers[1].event.name = "GBA Timer 1";
 69	gba->timers[1].event.callback = GBATimerUpdate1;
 70	gba->timers[1].event.context = gba;
 71	gba->timers[2].event.name = "GBA Timer 2";
 72	gba->timers[2].event.callback = GBATimerUpdate2;
 73	gba->timers[2].event.context = gba;
 74	gba->timers[3].event.name = "GBA Timer 3";
 75	gba->timers[3].event.callback = GBATimerUpdate3;
 76	gba->timers[3].event.context = gba;
 77}
 78
 79void GBATimerUpdateRegister(struct GBA* gba, int timer) {
 80	struct GBATimer* currentTimer = &gba->timers[timer];
 81	if (GBATimerFlagsIsEnable(currentTimer->flags) && !GBATimerFlagsIsCountUp(currentTimer->flags)) {
 82		int32_t prefetchSkew = 0;
 83		if (gba->memory.lastPrefetchedPc >= (uint32_t) gba->cpu->gprs[ARM_PC]) {
 84			prefetchSkew = (gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) * (gba->cpu->memory.activeSeqCycles16 + 1) / WORD_SIZE_THUMB;
 85		}
 86		// Reading this takes two cycles (1N+1I), so let's remove them preemptively
 87		int32_t diff = gba->cpu->cycles - (currentTimer->lastEvent - gba->timing.masterCycles);
 88		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((diff - 2 + prefetchSkew) >> GBATimerFlagsGetPrescaleBits(currentTimer->flags));
 89	}
 90}
 91
 92void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
 93	gba->timers[timer].reload = reload;
 94	gba->timers[timer].overflowInterval = (0x10000 - gba->timers[timer].reload) << GBATimerFlagsGetPrescaleBits(gba->timers[timer].flags);
 95}
 96
 97void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
 98	struct GBATimer* currentTimer = &gba->timers[timer];
 99	GBATimerUpdateRegister(gba, timer);
100
101	unsigned oldPrescale = GBATimerFlagsGetPrescaleBits(currentTimer->flags);
102	switch (control & 0x0003) {
103	case 0x0000:
104		currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 0);
105		break;
106	case 0x0001:
107		currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 6);
108		break;
109	case 0x0002:
110		currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 8);
111		break;
112	case 0x0003:
113		currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 10);
114		break;
115	}
116	currentTimer->flags = GBATimerFlagsTestFillCountUp(currentTimer->flags, timer > 0 && (control & 0x0004));
117	currentTimer->flags = GBATimerFlagsTestFillDoIrq(currentTimer->flags, control & 0x0040);
118	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << GBATimerFlagsGetPrescaleBits(currentTimer->flags);
119	bool wasEnabled = GBATimerFlagsIsEnable(currentTimer->flags);
120	currentTimer->flags = GBATimerFlagsTestFillEnable(currentTimer->flags, control & 0x0080);
121	if (!wasEnabled && GBATimerFlagsIsEnable(currentTimer->flags)) {
122		mTimingDeschedule(&gba->timing, &currentTimer->event);
123		if (!GBATimerFlagsIsCountUp(currentTimer->flags)) {
124			mTimingSchedule(&gba->timing, &currentTimer->event, currentTimer->overflowInterval);
125		}
126		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
127		currentTimer->oldReload = currentTimer->reload;
128		currentTimer->lastEvent = gba->timing.masterCycles + gba->cpu->cycles;
129	} else if (wasEnabled && !GBATimerFlagsIsEnable(currentTimer->flags)) {
130		if (!GBATimerFlagsIsCountUp(currentTimer->flags)) {
131			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
132		}
133	} else if (GBATimerFlagsIsEnable(currentTimer->flags) && GBATimerFlagsGetPrescaleBits(currentTimer->flags) != oldPrescale && !GBATimerFlagsIsCountUp(currentTimer->flags)) {
134		mTimingDeschedule(&gba->timing, &currentTimer->event);
135		mTimingSchedule(&gba->timing, &currentTimer->event, currentTimer->overflowInterval - currentTimer->lastEvent);
136	}
137}