all repos — mgba @ d86939b5b6d36f3c3b6e58e4bc256af09e422689

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
 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->stream = NULL;
 65
 66	gb->eiPending = false;
 67	gb->doubleSpeed = 0;
 68}
 69
 70bool GBLoadROM(struct GB* gb, struct VFile* vf) {
 71	GBUnloadROM(gb);
 72	gb->romVf = vf;
 73	gb->pristineRomSize = vf->size(vf);
 74	vf->seek(vf, 0, SEEK_SET);
 75#ifdef _3DS
 76	gb->pristineRom = 0;
 77	if (gb->pristineRomSize <= romBufferSize) {
 78		gb->pristineRom = romBuffer;
 79		vf->read(vf, romBuffer, gb->pristineRomSize);
 80	}
 81#else
 82	gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
 83#endif
 84	if (!gb->pristineRom) {
 85		return false;
 86	}
 87	gb->yankedRomSize = 0;
 88	gb->memory.rom = gb->pristineRom;
 89	gb->memory.romSize = gb->pristineRomSize;
 90	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
 91
 92	// TODO: error check
 93	return true;
 94}
 95
 96bool GBLoadSave(struct GB* gb, struct VFile* vf) {
 97	gb->sramVf = vf;
 98	if (vf) {
 99		// TODO: Do this in bank-switching code
100		if (vf->size(vf) < 0x20000) {
101			vf->truncate(vf, 0x20000);
102		}
103		gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE);
104	} else {
105		gb->memory.sram = anonymousMemoryMap(0x20000);
106	}
107	return gb->memory.sram;
108}
109
110void GBUnloadROM(struct GB* gb) {
111	// TODO: Share with GBAUnloadROM
112	if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
113		if (gb->yankedRomSize) {
114			gb->yankedRomSize = 0;
115		}
116		mappedMemoryFree(gb->memory.rom, 0x400000);
117	}
118	gb->memory.rom = 0;
119
120	if (gb->romVf) {
121#ifndef _3DS
122		gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
123#endif
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 > 0x400000) {
143		patchedSize = 0x400000;
144	}
145	gb->memory.rom = anonymousMemoryMap(0x400000);
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, 4);
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->oldLicensee == 0x33) {
373		memcpy(out, cart->maker, 11);
374	}
375}