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 mTimingDeschedule(&timer->p->timing, &timer->event);
68 mTimingSchedule(&timer->p->timing, &timer->event, timer->nextDiv);
69}
70
71uint8_t GBTimerUpdateTAC(struct GBTimer* timer, GBRegisterTAC tac) {
72 if (GBRegisterTACIsRun(tac)) {
73 switch (GBRegisterTACGetClock(tac)) {
74 case 0:
75 timer->timaPeriod = 1024 >> 4;
76 break;
77 case 1:
78 timer->timaPeriod = 16 >> 4;
79 break;
80 case 2:
81 timer->timaPeriod = 64 >> 4;
82 break;
83 case 3:
84 timer->timaPeriod = 256 >> 4;
85 break;
86 }
87 } else {
88 timer->timaPeriod = 0;
89 }
90 return tac;
91}
92
93void GBTimerSerialize(const struct GBTimer* timer, struct GBSerializedState* state) {
94 STORE_32LE(timer->nextDiv, 0, &state->timer.nextDiv);
95 STORE_32LE(timer->internalDiv, 0, &state->timer.internalDiv);
96 STORE_32LE(timer->timaPeriod, 0, &state->timer.timaPeriod);
97}
98
99void GBTimerDeserialize(struct GBTimer* timer, const struct GBSerializedState* state) {
100 LOAD_32LE(timer->nextDiv, 0, &state->timer.nextDiv);
101 LOAD_32LE(timer->internalDiv, 0, &state->timer.internalDiv);
102 LOAD_32LE(timer->timaPeriod, 0, &state->timer.timaPeriod);
103}