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