src/gb/gb.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 "gb.h"
7
8#include "gb/io.h"
9
10#include "core/core.h"
11#include "util/crc32.h"
12#include "util/memory.h"
13#include "util/math.h"
14#include "util/patch.h"
15#include "util/vfs.h"
16
17const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
18const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
19const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
20
21const uint32_t GB_COMPONENT_MAGIC = 0x400000;
22
23mLOG_DEFINE_CATEGORY(GB, "GB");
24
25static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component);
26static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
27static void GBProcessEvents(struct LR35902Core* cpu);
28static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
29static void GBIllegal(struct LR35902Core* cpu);
30static void GBHitStub(struct LR35902Core* cpu);
31
32void GBCreate(struct GB* gb) {
33 gb->d.id = GB_COMPONENT_MAGIC;
34 gb->d.init = GBInit;
35 gb->d.deinit = 0;
36}
37
38static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component) {
39 struct GB* gb = (struct GB*) component;
40 gb->cpu = cpu;
41
42 GBInterruptHandlerInit(&cpu->irqh);
43 GBMemoryInit(gb);
44
45 gb->video.p = gb;
46 GBVideoInit(&gb->video);
47
48 gb->audio.p = gb;
49 GBAudioInit(&gb->audio, 2048); // TODO: Remove magic constant
50
51 gb->timer.p = gb;
52
53 gb->romVf = 0;
54 gb->sramVf = 0;
55
56 gb->pristineRom = 0;
57 gb->pristineRomSize = 0;
58 gb->yankedRomSize = 0;
59
60 gb->diPending = false;
61}
62
63bool GBLoadROM(struct GB* gb, struct VFile* vf) {
64 GBUnloadROM(gb);
65 gb->romVf = vf;
66 gb->pristineRomSize = vf->size(vf);
67 vf->seek(vf, 0, SEEK_SET);
68#ifdef _3DS
69 gb->pristineRom = 0;
70 if (gb->pristineRomSize <= romBufferSize) {
71 gb->pristineRom = romBuffer;
72 vf->read(vf, romBuffer, gb->pristineRomSize);
73 }
74#else
75 gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
76#endif
77 if (!gb->pristineRom) {
78 return false;
79 }
80 gb->yankedRomSize = 0;
81 gb->memory.rom = gb->pristineRom;
82 gb->memory.romSize = gb->pristineRomSize;
83 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
84
85 // TODO: error check
86 return true;
87}
88
89bool GBLoadSave(struct GB* gb, struct VFile* vf) {
90 gb->sramVf = vf;
91 if (vf) {
92 // TODO: Do this in bank-switching code
93 if (vf->size(vf) < 0x20000) {
94 vf->truncate(vf, 0x20000);
95 }
96 gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE);
97 } else {
98 gb->memory.sram = anonymousMemoryMap(0x20000);
99 }
100 return gb->memory.sram;
101}
102
103void GBUnloadROM(struct GB* gb) {
104 // TODO: Share with GBAUnloadROM
105 if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
106 if (gb->yankedRomSize) {
107 gb->yankedRomSize = 0;
108 }
109 mappedMemoryFree(gb->memory.rom, 0x400000);
110 }
111 gb->memory.rom = 0;
112
113 if (gb->romVf) {
114#ifndef _3DS
115 gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
116#endif
117 gb->pristineRom = 0;
118 gb->romVf = 0;
119 }
120
121 if (gb->sramVf) {
122 gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
123 } else if (gb->memory.sram) {
124 mappedMemoryFree(gb->memory.sram, 0x8000);
125 }
126 gb->memory.sram = 0;
127}
128
129void GBDestroy(struct GB* gb) {
130 GBUnloadROM(gb);
131
132 GBMemoryDeinit(gb);
133}
134
135void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
136 irqh->reset = GBReset;
137 irqh->processEvents = GBProcessEvents;
138 irqh->setInterrupts = GBSetInterrupts;
139 irqh->hitIllegal = GBIllegal;
140 irqh->hitStub = GBHitStub;
141 irqh->halt = GBHalt;
142}
143
144void GBReset(struct LR35902Core* cpu) {
145 cpu->a = 1;
146 cpu->f.packed = 0xB0;
147 cpu->b = 0;
148 cpu->c = 0x13;
149 cpu->d = 0;
150 cpu->e = 0xD8;
151 cpu->h = 1;
152 cpu->l = 0x4D;
153 cpu->sp = 0xFFFE;
154 cpu->pc = 0x100;
155
156 struct GB* gb = (struct GB*) cpu->master;
157
158 if (gb->yankedRomSize) {
159 gb->memory.romSize = gb->yankedRomSize;
160 gb->yankedRomSize = 0;
161 }
162 GBMemoryReset(gb);
163 GBVideoReset(&gb->video);
164 GBTimerReset(&gb->timer);
165 GBIOReset(gb);
166 GBAudioReset(&gb->audio);
167}
168
169void GBUpdateIRQs(struct GB* gb) {
170 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
171 if (!irqs) {
172 return;
173 }
174 gb->cpu->halted = false;
175
176 if (!gb->memory.ime) {
177 return;
178 }
179
180 if (irqs & (1 << GB_IRQ_VBLANK)) {
181 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
182 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
183 return;
184 }
185 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
186 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
187 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
188 return;
189 }
190 if (irqs & (1 << GB_IRQ_TIMER)) {
191 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
192 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
193 return;
194 }
195 if (irqs & (1 << GB_IRQ_SIO)) {
196 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
197 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
198 return;
199 }
200 if (irqs & (1 << GB_IRQ_KEYPAD)) {
201 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
202 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
203 }
204}
205
206void GBProcessEvents(struct LR35902Core* cpu) {
207 struct GB* gb = (struct GB*) cpu->master;
208 do {
209 int32_t cycles = cpu->nextEvent;
210 int32_t nextEvent = INT_MAX;
211 int32_t testEvent;
212
213 if (gb->diPending) {
214 gb->memory.ime = false;
215 gb->diPending = false;
216 }
217
218 testEvent = GBVideoProcessEvents(&gb->video, cycles);
219 if (testEvent < nextEvent) {
220 nextEvent = testEvent;
221 }
222
223 testEvent = GBAudioProcessEvents(&gb->audio, cycles);
224 if (testEvent < nextEvent) {
225 nextEvent = testEvent;
226 }
227
228 testEvent = GBTimerProcessEvents(&gb->timer, cycles);
229 if (testEvent < nextEvent) {
230 nextEvent = testEvent;
231 }
232
233 testEvent = GBMemoryProcessEvents(gb, cycles);
234 if (testEvent < nextEvent) {
235 nextEvent = testEvent;
236 }
237
238 cpu->cycles -= cycles;
239 cpu->nextEvent = nextEvent;
240
241 if (cpu->halted) {
242 cpu->cycles = cpu->nextEvent;
243 }
244 } while (cpu->cycles >= cpu->nextEvent);
245}
246
247void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
248 struct GB* gb = (struct GB*) cpu->master;
249 if (enable) {
250 gb->memory.ime = enable;
251 GBUpdateIRQs(gb);
252 } else {
253 if (cpu->nextEvent > 4) {
254 cpu->nextEvent = 4;
255 }
256 gb->diPending = true;
257 }
258}
259
260void GBHalt(struct LR35902Core* cpu) {
261 cpu->cycles = cpu->nextEvent;
262 cpu->halted = true;
263}
264
265void GBIllegal(struct LR35902Core* cpu) {
266 // TODO
267 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
268}
269
270void GBHitStub(struct LR35902Core* cpu) {
271 // TODO
272 mLOG(GB, STUB, "Hit stub at address %04X:%02X\n", cpu->pc, cpu->bus);
273}
274
275bool GBIsROM(struct VFile* vf) {
276 vf->seek(vf, 0x104, SEEK_SET);
277 uint8_t header[4];
278 static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
279
280 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
281 return false;
282 }
283 if (memcmp(header, knownHeader, sizeof(header))) {
284 return false;
285 }
286 return true;
287}