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