all repos — mgba @ d6a45781f0ea2c12e6e785f5b01a22c37e55c355

mGBA Game Boy Advance Emulator

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