all repos — mgba @ 47313497013c8802145cbe83d3c824f28e6fa5ab

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

  1/* Copyright (c) 2013-2014 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 "gba.h"
  7
  8#include "gba-bios.h"
  9#include "gba-io.h"
 10#include "gba-rr.h"
 11#include "gba-sio.h"
 12#include "gba-thread.h"
 13
 14#include "util/crc32.h"
 15#include "util/memory.h"
 16#include "util/patch.h"
 17#include "util/vfs.h"
 18
 19const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
 20const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 21
 22static const size_t GBA_ROM_MAGIC_OFFSET = 2;
 23static const uint8_t GBA_ROM_MAGIC[] = { 0x00, 0xEA };
 24
 25enum {
 26	SP_BASE_SYSTEM = 0x03FFFF00,
 27	SP_BASE_IRQ = 0x03FFFFA0,
 28	SP_BASE_SUPERVISOR = 0x03FFFFE0
 29};
 30
 31struct GBACartridgeOverride {
 32	const char id[4];
 33	enum SavedataType type;
 34	int gpio;
 35	uint32_t busyLoop;
 36};
 37
 38static const struct GBACartridgeOverride _overrides[] = {
 39	// Boktai: The Sun is in Your Hand
 40	{ "U3IE", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
 41	{ "U3IP", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
 42
 43	// Boktai 2: Solar Boy Django
 44	{ "U32E", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
 45	{ "U32P", SAVEDATA_EEPROM, GPIO_RTC | GPIO_LIGHT_SENSOR, -1 },
 46
 47	// Drill Dozer
 48	{ "V49J", SAVEDATA_SRAM, GPIO_RUMBLE, -1 },
 49	{ "V49E", SAVEDATA_SRAM, GPIO_RUMBLE, -1 },
 50
 51	// Final Fantasy Tactics Advance
 52	{ "AFXE", SAVEDATA_FLASH512, GPIO_NONE, 0x8000418 },
 53
 54	// Mega Man Battle Network
 55	{ "AREE", SAVEDATA_SRAM, GPIO_NONE, 0x800032E },
 56
 57	// Pokemon Ruby
 58	{ "AXVJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 59	{ "AXVE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 60	{ "AXVP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 61	{ "AXVI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 62	{ "AXVS", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 63	{ "AXVD", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 64	{ "AXVF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 65
 66	// Pokemon Sapphire
 67	{ "AXPJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 68	{ "AXPE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 69	{ "AXPP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 70	{ "AXPI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 71	{ "AXPS", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 72	{ "AXPD", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 73	{ "AXPF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 74
 75	// Pokemon Emerald
 76	{ "BPEJ", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 77	{ "BPEE", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 78	{ "BPEP", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 79	{ "BPEI", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 80	{ "BPES", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 81	{ "BPED", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 82	{ "BPEF", SAVEDATA_FLASH1M, GPIO_RTC, -1 },
 83
 84	// Pokemon Mystery Dungeon
 85	{ "B24J", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 86	{ "B24E", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 87	{ "B24P", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 88	{ "B24U", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 89
 90	// Pokemon FireRed
 91	{ "BPRJ", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 92	{ "BPRE", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 93	{ "BPRP", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 94
 95	// Pokemon LeafGreen
 96	{ "BPGJ", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 97	{ "BPGE", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 98	{ "BPGP", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
 99
100	// RockMan EXE 4.5 - Real Operation
101	{ "BR4J", SAVEDATA_FLASH512, GPIO_RTC, -1 },
102
103	// Super Mario Advance 4
104	{ "AX4J", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
105	{ "AX4E", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
106	{ "AX4P", SAVEDATA_FLASH1M, GPIO_NONE, -1 },
107
108	// Wario Ware Twisted
109	{ "RZWJ", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
110	{ "RZWE", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
111	{ "RZWP", SAVEDATA_SRAM, GPIO_RUMBLE | GPIO_GYRO, -1 },
112
113	{ { 0, 0, 0, 0 }, 0, 0, -1 }
114};
115
116static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
117static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
118static void GBAProcessEvents(struct ARMCore* cpu);
119static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
120static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
121static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
122
123static void _checkOverrides(struct GBA* gba, uint32_t code);
124
125void GBACreate(struct GBA* gba) {
126	gba->d.id = GBA_COMPONENT_MAGIC;
127	gba->d.init = GBAInit;
128	gba->d.deinit = 0;
129}
130
131static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
132	struct GBA* gba = (struct GBA*) component;
133	gba->cpu = cpu;
134	gba->debugger = 0;
135
136	GBAInterruptHandlerInit(&cpu->irqh);
137	GBAMemoryInit(gba);
138	GBASavedataInit(&gba->memory.savedata, 0);
139
140	gba->video.p = gba;
141	GBAVideoInit(&gba->video);
142
143	gba->audio.p = gba;
144	GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
145
146	GBAIOInit(gba);
147
148	gba->sio.p = gba;
149	GBASIOInit(&gba->sio);
150
151	gba->timersEnabled = 0;
152	memset(gba->timers, 0, sizeof(gba->timers));
153
154	gba->springIRQ = 0;
155	gba->keySource = 0;
156	gba->rotationSource = 0;
157	gba->rumble = 0;
158	gba->rr = 0;
159
160	gba->romVf = 0;
161	gba->biosVf = 0;
162
163	gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
164
165	gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
166
167	gba->busyLoop = -1;
168}
169
170void GBADestroy(struct GBA* gba) {
171	if (gba->pristineRom == gba->memory.rom) {
172		gba->memory.rom = 0;
173	}
174
175	if (gba->romVf) {
176		gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
177	}
178
179	if (gba->biosVf) {
180		gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
181	}
182
183	GBAMemoryDeinit(gba);
184	GBAVideoDeinit(&gba->video);
185	GBAAudioDeinit(&gba->audio);
186	GBARRContextDestroy(gba);
187}
188
189void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
190	irqh->reset = GBAReset;
191	irqh->processEvents = GBAProcessEvents;
192	irqh->swi16 = GBASwi16;
193	irqh->swi32 = GBASwi32;
194	irqh->hitIllegal = GBAIllegal;
195	irqh->readCPSR = GBATestIRQ;
196	irqh->hitStub = GBAHitStub;
197}
198
199void GBAReset(struct ARMCore* cpu) {
200	ARMSetPrivilegeMode(cpu, MODE_IRQ);
201	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
202	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
203	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
204	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
205	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
206
207	struct GBA* gba = (struct GBA*) cpu->master;
208	if (!GBARRIsPlaying(gba->rr) && !GBARRIsRecording(gba->rr)) {
209		GBASavedataUnmask(&gba->memory.savedata);
210	}
211	GBAMemoryReset(gba);
212	GBAVideoReset(&gba->video);
213	GBAAudioReset(&gba->audio);
214	GBAIOInit(gba);
215
216	GBASIODeinit(&gba->sio);
217	GBASIOInit(&gba->sio);
218
219	gba->timersEnabled = 0;
220	memset(gba->timers, 0, sizeof(gba->timers));
221}
222
223static void GBAProcessEvents(struct ARMCore* cpu) {
224	do {
225		struct GBA* gba = (struct GBA*) cpu->master;
226		int32_t cycles = cpu->cycles;
227		int32_t nextEvent = INT_MAX;
228		int32_t testEvent;
229
230		gba->bus = cpu->prefetch;
231		if (cpu->executionMode == MODE_THUMB) {
232			gba->bus |= cpu->prefetch << 16;
233		}
234
235		if (gba->springIRQ) {
236			ARMRaiseIRQ(cpu);
237			gba->springIRQ = 0;
238		}
239
240		testEvent = GBAVideoProcessEvents(&gba->video, cycles);
241		if (testEvent < nextEvent) {
242			nextEvent = testEvent;
243		}
244
245		testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
246		if (testEvent < nextEvent) {
247			nextEvent = testEvent;
248		}
249
250		testEvent = GBATimersProcessEvents(gba, cycles);
251		if (testEvent < nextEvent) {
252			nextEvent = testEvent;
253		}
254
255		testEvent = GBAMemoryRunDMAs(gba, cycles);
256		if (testEvent < nextEvent) {
257			nextEvent = testEvent;
258		}
259
260		testEvent = GBASIOProcessEvents(&gba->sio, 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
274static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
275	int32_t nextEvent = INT_MAX;
276	if (gba->timersEnabled) {
277		struct GBATimer* timer;
278		struct GBATimer* nextTimer;
279
280		timer = &gba->timers[0];
281		if (timer->enable) {
282			timer->nextEvent -= cycles;
283			timer->lastEvent -= cycles;
284			if (timer->nextEvent <= 0) {
285				timer->lastEvent = timer->nextEvent;
286				timer->nextEvent += timer->overflowInterval;
287				gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
288				timer->oldReload = timer->reload;
289
290				if (timer->doIrq) {
291					GBARaiseIRQ(gba, IRQ_TIMER0);
292				}
293
294				if (gba->audio.enable) {
295					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
296						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
297					}
298
299					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
300						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
301					}
302				}
303
304				nextTimer = &gba->timers[1];
305				if (nextTimer->countUp) {
306					++gba->memory.io[REG_TM1CNT_LO >> 1];
307					if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
308						nextTimer->nextEvent = 0;
309					}
310				}
311			}
312			nextEvent = timer->nextEvent;
313		}
314
315		timer = &gba->timers[1];
316		if (timer->enable) {
317			timer->nextEvent -= cycles;
318			timer->lastEvent -= cycles;
319			if (timer->nextEvent <= 0) {
320				timer->lastEvent = timer->nextEvent;
321				timer->nextEvent += timer->overflowInterval;
322				gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
323				timer->oldReload = timer->reload;
324
325				if (timer->doIrq) {
326					GBARaiseIRQ(gba, IRQ_TIMER1);
327				}
328
329				if (gba->audio.enable) {
330					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
331						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
332					}
333
334					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
335						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
336					}
337				}
338
339				if (timer->countUp) {
340					timer->nextEvent = INT_MAX;
341				}
342
343				nextTimer = &gba->timers[2];
344				if (nextTimer->countUp) {
345					++gba->memory.io[REG_TM2CNT_LO >> 1];
346					if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
347						nextTimer->nextEvent = 0;
348					}
349				}
350			}
351			if (timer->nextEvent < nextEvent) {
352				nextEvent = timer->nextEvent;
353			}
354		}
355
356		timer = &gba->timers[2];
357		if (timer->enable) {
358			timer->nextEvent -= cycles;
359			timer->lastEvent -= cycles;
360			nextEvent = timer->nextEvent;
361			if (timer->nextEvent <= 0) {
362				timer->lastEvent = timer->nextEvent;
363				timer->nextEvent += timer->overflowInterval;
364				gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
365				timer->oldReload = timer->reload;
366
367				if (timer->doIrq) {
368					GBARaiseIRQ(gba, IRQ_TIMER2);
369				}
370
371				if (timer->countUp) {
372					timer->nextEvent = INT_MAX;
373				}
374
375				nextTimer = &gba->timers[3];
376				if (nextTimer->countUp) {
377					++gba->memory.io[REG_TM3CNT_LO >> 1];
378					if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
379						nextTimer->nextEvent = 0;
380					}
381				}
382			}
383			if (timer->nextEvent < nextEvent) {
384				nextEvent = timer->nextEvent;
385			}
386		}
387
388		timer = &gba->timers[3];
389		if (timer->enable) {
390			timer->nextEvent -= cycles;
391			timer->lastEvent -= cycles;
392			nextEvent = timer->nextEvent;
393			if (timer->nextEvent <= 0) {
394				timer->lastEvent = timer->nextEvent;
395				timer->nextEvent += timer->overflowInterval;
396				gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
397				timer->oldReload = timer->reload;
398
399				if (timer->doIrq) {
400					GBARaiseIRQ(gba, IRQ_TIMER3);
401				}
402
403				if (timer->countUp) {
404					timer->nextEvent = INT_MAX;
405				}
406			}
407			if (timer->nextEvent < nextEvent) {
408				nextEvent = timer->nextEvent;
409			}
410		}
411	}
412	return nextEvent;
413}
414
415void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
416	gba->debugger = debugger;
417}
418
419void GBADetachDebugger(struct GBA* gba) {
420	gba->debugger = 0;
421}
422
423void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
424	gba->romVf = vf;
425	gba->pristineRomSize = vf->seek(vf, 0, SEEK_END);
426	vf->seek(vf, 0, SEEK_SET);
427	gba->pristineRom = vf->map(vf, SIZE_CART0, MAP_READ);
428	if (!gba->pristineRom) {
429		GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
430		return;
431	}
432	gba->memory.rom = gba->pristineRom;
433	gba->activeFile = fname;
434	gba->memory.romSize = gba->pristineRomSize;
435	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
436	GBASavedataInit(&gba->memory.savedata, sav);
437	GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
438	_checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id);
439	// TODO: error check
440}
441
442void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
443	gba->biosVf = vf;
444	uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
445	if (!bios) {
446		GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
447		return;
448	}
449	gba->memory.bios = bios;
450	gba->memory.fullBios = 1;
451	uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
452	GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
453	if (checksum == GBA_BIOS_CHECKSUM) {
454		GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
455	} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
456		GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
457	} else {
458		GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
459	}
460	gba->biosChecksum = checksum;
461	if (gba->memory.activeRegion == REGION_BIOS) {
462		gba->cpu->memory.activeRegion = gba->memory.bios;
463	}
464	// TODO: error check
465}
466
467void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
468	size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
469	if (!patchedSize) {
470		return;
471	}
472	gba->memory.rom = anonymousMemoryMap(patchedSize);
473	memcpy(gba->memory.rom, gba->pristineRom, gba->memory.romSize > patchedSize ? patchedSize : gba->memory.romSize);
474	if (!patch->applyPatch(patch, gba->memory.rom, patchedSize)) {
475		mappedMemoryFree(gba->memory.rom, patchedSize);
476		gba->memory.rom = gba->pristineRom;
477		return;
478	}
479	gba->memory.romSize = patchedSize;
480	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
481}
482
483void GBATimerUpdateRegister(struct GBA* gba, int timer) {
484	struct GBATimer* currentTimer = &gba->timers[timer];
485	if (currentTimer->enable && !currentTimer->countUp) {
486		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
487	}
488}
489
490void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
491	gba->timers[timer].reload = reload;
492}
493
494void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
495	struct GBATimer* currentTimer = &gba->timers[timer];
496	GBATimerUpdateRegister(gba, timer);
497
498	int oldPrescale = currentTimer->prescaleBits;
499	switch (control & 0x0003) {
500	case 0x0000:
501		currentTimer->prescaleBits = 0;
502		break;
503	case 0x0001:
504		currentTimer->prescaleBits = 6;
505		break;
506	case 0x0002:
507		currentTimer->prescaleBits = 8;
508		break;
509	case 0x0003:
510		currentTimer->prescaleBits = 10;
511		break;
512	}
513	currentTimer->countUp = !!(control & 0x0004);
514	currentTimer->doIrq = !!(control & 0x0040);
515	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
516	int wasEnabled = currentTimer->enable;
517	currentTimer->enable = !!(control & 0x0080);
518	if (!wasEnabled && currentTimer->enable) {
519		if (!currentTimer->countUp) {
520			currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
521		} else {
522			currentTimer->nextEvent = INT_MAX;
523		}
524		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
525		currentTimer->oldReload = currentTimer->reload;
526		currentTimer->lastEvent = 0;
527		gba->timersEnabled |= 1 << timer;
528	} else if (wasEnabled && !currentTimer->enable) {
529		if (!currentTimer->countUp) {
530			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
531		}
532		gba->timersEnabled &= ~(1 << timer);
533	} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
534		// FIXME: this might be before present
535		currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
536	}
537
538	if (currentTimer->nextEvent < gba->cpu->nextEvent) {
539		gba->cpu->nextEvent = currentTimer->nextEvent;
540	}
541};
542
543void GBAWriteIE(struct GBA* gba, uint16_t value) {
544	if (value & (1 << IRQ_KEYPAD)) {
545		GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
546	}
547
548	if (value & (1 << IRQ_GAMEPAK)) {
549		GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
550	}
551
552	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
553		ARMRaiseIRQ(gba->cpu);
554	}
555}
556
557void GBAWriteIME(struct GBA* gba, uint16_t value) {
558	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
559		ARMRaiseIRQ(gba->cpu);
560	}
561}
562
563void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
564	gba->memory.io[REG_IF >> 1] |= 1 << irq;
565	gba->cpu->halted = 0;
566
567	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
568		ARMRaiseIRQ(gba->cpu);
569	}
570}
571
572void GBATestIRQ(struct ARMCore* cpu) {
573	struct GBA* gba = (struct GBA*) cpu->master;
574	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
575		gba->springIRQ = 1;
576		gba->cpu->nextEvent = 0;
577	}
578}
579
580void GBAHalt(struct GBA* gba) {
581	gba->cpu->nextEvent = 0;
582	gba->cpu->halted = 1;
583}
584
585static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
586	struct GBAThread* threadContext = GBAThreadGetContext();
587	if (threadContext) {
588		if (!gba) {
589			gba = threadContext->gba;
590		}
591	}
592
593	if (gba && !(level & gba->logLevel) && level != GBA_LOG_FATAL) {
594		return;
595	}
596
597	if (threadContext && threadContext->logHandler) {
598		threadContext->logHandler(threadContext, level, format, args);
599		return;
600	}
601
602	vprintf(format, args);
603	printf("\n");
604
605	if (level == GBA_LOG_FATAL) {
606		abort();
607	}
608}
609
610void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
611	va_list args;
612	va_start(args, format);
613	_GBAVLog(gba, level, format, args);
614	va_end(args);
615}
616
617void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
618	struct GBA* gba = 0;
619	if (debugger->cpu) {
620		gba = (struct GBA*) debugger->cpu->master;
621	}
622
623	enum GBALogLevel gbaLevel;
624	switch (level) {
625	default: // Avoids compiler warning
626	case DEBUGGER_LOG_DEBUG:
627		gbaLevel = GBA_LOG_DEBUG;
628		break;
629	case DEBUGGER_LOG_INFO:
630		gbaLevel = GBA_LOG_INFO;
631		break;
632	case DEBUGGER_LOG_WARN:
633		gbaLevel = GBA_LOG_WARN;
634		break;
635	case DEBUGGER_LOG_ERROR:
636		gbaLevel = GBA_LOG_ERROR;
637		break;
638	}
639	va_list args;
640	va_start(args, format);
641	_GBAVLog(gba, gbaLevel, format, args);
642	va_end(args);
643}
644
645bool GBAIsROM(struct VFile* vf) {
646	if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
647		return false;
648	}
649	uint8_t signature[sizeof(GBA_ROM_MAGIC)];
650	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
651		return false;
652	}
653	return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
654}
655
656void GBAGetGameCode(struct GBA* gba, char* out) {
657	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
658}
659
660void GBAGetGameTitle(struct GBA* gba, char* out) {
661	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
662}
663
664void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
665	struct GBA* gba = (struct GBA*) cpu->master;
666	enum GBALogLevel level = GBA_LOG_FATAL;
667	if (gba->debugger) {
668		level = GBA_LOG_STUB;
669		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
670	}
671	GBALog(gba, level, "Stub opcode: %08x", opcode);
672}
673
674void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
675	struct GBA* gba = (struct GBA*) cpu->master;
676	GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
677	if (gba->debugger) {
678		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
679	}
680}
681
682void _checkOverrides(struct GBA* gba, uint32_t id) {
683	int i;
684	gba->busyLoop = -1;
685	if ((id & 0xFF) == 'F') {
686		GBALog(gba, GBA_LOG_DEBUG, "Found Classic NES Series game, using EEPROM saves");
687		GBASavedataInitEEPROM(&gba->memory.savedata);
688		return;
689	}
690	for (i = 0; _overrides[i].id[0]; ++i) {
691		const uint32_t* overrideId = (const uint32_t*) _overrides[i].id;
692		if (*overrideId == id) {
693			GBALog(gba, GBA_LOG_DEBUG, "Found override for game %s!", _overrides[i].id);
694			switch (_overrides[i].type) {
695				case SAVEDATA_FLASH512:
696				case SAVEDATA_FLASH1M:
697					gba->memory.savedata.type = _overrides[i].type;
698					GBASavedataInitFlash(&gba->memory.savedata);
699					break;
700				case SAVEDATA_EEPROM:
701					GBASavedataInitEEPROM(&gba->memory.savedata);
702					break;
703				case SAVEDATA_SRAM:
704					GBASavedataInitSRAM(&gba->memory.savedata);
705					break;
706				case SAVEDATA_NONE:
707					break;
708			}
709
710			if (_overrides[i].gpio & GPIO_RTC) {
711				GBAGPIOInitRTC(&gba->memory.gpio);
712			}
713
714			if (_overrides[i].gpio & GPIO_GYRO) {
715				GBAGPIOInitGyro(&gba->memory.gpio);
716			}
717
718			if (_overrides[i].gpio & GPIO_RUMBLE) {
719				GBAGPIOInitRumble(&gba->memory.gpio);
720			}
721
722			gba->busyLoop = _overrides[i].busyLoop;
723			return;
724		}
725	}
726}