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