all repos — mgba @ ad38ae63ec3615da4a9e485affe38b7371bf47ea

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