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