all repos — mgba @ a450b0470ac44fd0714872bc7602b1339e31b175

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