all repos — mgba @ 9eeb0512103a7108543587dba369bc88c3b815f4

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