all repos — mgba @ 7bb5e29a984c2d509b072eafddc78ec712426c5a

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