all repos — mgba @ eb2809829d52fa442611fd1594812c623dc4d4ca

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

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