all repos — mgba @ e53135a7b9a67812f1c5518eeea3f3f476bd3905

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