src/gb/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 "gb/gb.h"
9#include "gb/io.h"
10#include "gb/serialize.h"
11
12void _GBTimerIRQ(struct mTiming* timing, void* context, uint32_t cyclesLate) {
13 UNUSED(timing);
14 UNUSED(cyclesLate);
15 struct GBTimer* timer = context;
16 timer->p->memory.io[REG_TIMA] = timer->p->memory.io[REG_TMA];
17 timer->p->memory.io[REG_IF] |= (1 << GB_IRQ_TIMER);
18 GBUpdateIRQs(timer->p);
19}
20
21void _GBTimerIncrement(struct mTiming* timing, void* context, uint32_t cyclesLate) {
22 struct GBTimer* timer = context;
23 timer->nextDiv += cyclesLate;
24 while (timer->nextDiv > 0) {
25 timer->nextDiv -= GB_DMG_DIV_PERIOD;
26
27 // Make sure to trigger when the correct bit is a falling edge
28 if (timer->timaPeriod > 0 && (timer->internalDiv & (timer->timaPeriod - 1)) == timer->timaPeriod - 1) {
29 ++timer->p->memory.io[REG_TIMA];
30 if (!timer->p->memory.io[REG_TIMA]) {
31 mTimingSchedule(timing, &timer->irq, 4 - cyclesLate);
32 }
33 }
34 ++timer->internalDiv;
35 timer->p->memory.io[REG_DIV] = timer->internalDiv >> 4;
36 }
37 // Batch div increments
38 int divsToGo = 16 - (timer->internalDiv & 15);
39 int timaToGo = INT_MAX;
40 if (timer->timaPeriod) {
41 timaToGo = timer->timaPeriod - (timer->internalDiv & (timer->timaPeriod - 1));
42 }
43 if (timaToGo < divsToGo) {
44 divsToGo = timaToGo;
45 }
46 timer->nextDiv = GB_DMG_DIV_PERIOD * divsToGo;
47 mTimingSchedule(timing, &timer->event, timer->nextDiv - cyclesLate);
48}
49
50void GBTimerReset(struct GBTimer* timer) {
51 timer->event.context = timer;
52 timer->event.name = "GB Timer";
53 timer->event.callback = _GBTimerIncrement;
54 timer->irq.context = timer;
55 timer->irq.name = "GB Timer IRQ";
56 timer->irq.callback = _GBTimerIRQ;
57
58 timer->nextDiv = GB_DMG_DIV_PERIOD; // TODO: GBC differences
59 timer->timaPeriod = 1024 >> 4;
60 timer->internalDiv = 0;
61}
62
63void GBTimerDivReset(struct GBTimer* timer) {
64 timer->p->memory.io[REG_DIV] = 0;
65 timer->internalDiv = 0;
66 timer->nextDiv = GB_DMG_DIV_PERIOD;
67 mTimingSchedule(&timer->p->timing, &timer->event, timer->nextDiv);
68}
69
70uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) {
71 if (GBRegisterTACIsRun(tac)) {
72 switch (GBRegisterTACGetClock(tac)) {
73 case 0:
74 timer->timaPeriod = 1024 >> 4;
75 break;
76 case 1:
77 timer->timaPeriod = 16 >> 4;
78 break;
79 case 2:
80 timer->timaPeriod = 64 >> 4;
81 break;
82 case 3:
83 timer->timaPeriod = 256 >> 4;
84 break;
85 }
86 } else {
87 timer->timaPeriod = 0;
88 }
89 return tac;
90}
91
92void GBTimerSerialize(const struct GBTimer* timer, struct GBSerializedState* state) {
93 STORE_32LE(timer->nextDiv, 0, &state->timer.nextDiv);
94 STORE_32LE(timer->internalDiv, 0, &state->timer.internalDiv);
95 STORE_32LE(timer->timaPeriod, 0, &state->timer.timaPeriod);
96}
97
98void GBTimerDeserialize(struct GBTimer* timer, const struct GBSerializedState* state) {
99 LOAD_32LE(timer->nextDiv, 0, &state->timer.nextDiv);
100 LOAD_32LE(timer->internalDiv, 0, &state->timer.internalDiv);
101 LOAD_32LE(timer->timaPeriod, 0, &state->timer.timaPeriod);
102}