all repos — mgba @ dba275c5705f54501ae6819fb34b9f93a29185c6

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gba.h"
  7
  8#include "gba-bios.h"
  9#include "gba-io.h"
 10#include "gba-rr.h"
 11#include "gba-sio.h"
 12#include "gba-thread.h"
 13
 14#include "isa-inlines.h"
 15
 16#include "util/crc32.h"
 17#include "util/memory.h"
 18#include "util/patch.h"
 19#include "util/vfs.h"
 20
 21const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
 22const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 23
 24static const size_t GBA_ROM_MAGIC_OFFSET = 3;
 25static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
 26
 27static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
 28static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
 29static void GBAProcessEvents(struct ARMCore* cpu);
 30static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
 31static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
 32static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
 33
 34void GBACreate(struct GBA* gba) {
 35	gba->d.id = GBA_COMPONENT_MAGIC;
 36	gba->d.init = GBAInit;
 37	gba->d.deinit = 0;
 38}
 39
 40static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
 41	struct GBA* gba = (struct GBA*) component;
 42	gba->cpu = cpu;
 43	gba->debugger = 0;
 44
 45	GBAInterruptHandlerInit(&cpu->irqh);
 46	GBAMemoryInit(gba);
 47	GBASavedataInit(&gba->memory.savedata, 0);
 48
 49	gba->video.p = gba;
 50	GBAVideoInit(&gba->video);
 51
 52	gba->audio.p = gba;
 53	GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
 54
 55	GBAIOInit(gba);
 56
 57	gba->sio.p = gba;
 58	GBASIOInit(&gba->sio);
 59
 60	gba->timersEnabled = 0;
 61	memset(gba->timers, 0, sizeof(gba->timers));
 62
 63	gba->springIRQ = 0;
 64	gba->keySource = 0;
 65	gba->rotationSource = 0;
 66	gba->luminanceSource = 0;
 67	gba->rtcSource = 0;
 68	gba->rumble = 0;
 69	gba->rr = 0;
 70
 71	gba->romVf = 0;
 72	gba->biosVf = 0;
 73
 74	gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
 75
 76	gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
 77
 78	gba->idleOptimization = IDLE_LOOP_REMOVE;
 79	gba->idleLoop = -1;
 80	gba->lastJump = 0;
 81	gba->idleDetectionStep = 0;
 82	gba->idleDetectionFailures = 0;
 83}
 84
 85void GBADestroy(struct GBA* gba) {
 86	if (gba->pristineRom == gba->memory.rom) {
 87		gba->memory.rom = 0;
 88	}
 89
 90	if (gba->romVf) {
 91		gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
 92	}
 93
 94	if (gba->biosVf) {
 95		gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
 96	}
 97
 98	GBAMemoryDeinit(gba);
 99	GBAVideoDeinit(&gba->video);
100	GBAAudioDeinit(&gba->audio);
101	GBARRContextDestroy(gba);
102}
103
104void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
105	irqh->reset = GBAReset;
106	irqh->processEvents = GBAProcessEvents;
107	irqh->swi16 = GBASwi16;
108	irqh->swi32 = GBASwi32;
109	irqh->hitIllegal = GBAIllegal;
110	irqh->readCPSR = GBATestIRQ;
111	irqh->hitStub = GBAHitStub;
112}
113
114void GBAReset(struct ARMCore* cpu) {
115	ARMSetPrivilegeMode(cpu, MODE_IRQ);
116	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
117	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
118	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
119	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
120	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
121
122	struct GBA* gba = (struct GBA*) cpu->master;
123	if (!GBARRIsPlaying(gba->rr) && !GBARRIsRecording(gba->rr)) {
124		GBASavedataUnmask(&gba->memory.savedata);
125	}
126	GBAMemoryReset(gba);
127	GBAVideoReset(&gba->video);
128	GBAAudioReset(&gba->audio);
129	GBAIOInit(gba);
130
131	GBASIODeinit(&gba->sio);
132	GBASIOInit(&gba->sio);
133
134	gba->timersEnabled = 0;
135	memset(gba->timers, 0, sizeof(gba->timers));
136}
137
138void GBASkipBIOS(struct ARMCore* cpu) {
139	if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
140		cpu->gprs[ARM_PC] = BASE_CART0;
141		int currentCycles = 0;
142		ARM_WRITE_PC;
143	}
144}
145
146static void GBAProcessEvents(struct ARMCore* cpu) {
147	do {
148		struct GBA* gba = (struct GBA*) cpu->master;
149		int32_t cycles = cpu->nextEvent;
150		int32_t nextEvent = INT_MAX;
151		int32_t testEvent;
152
153		gba->bus = cpu->prefetch[1];
154		if (cpu->executionMode == MODE_THUMB) {
155			gba->bus |= cpu->prefetch[1] << 16;
156		}
157
158		if (gba->springIRQ) {
159			ARMRaiseIRQ(cpu);
160			gba->springIRQ = 0;
161		}
162
163		testEvent = GBAVideoProcessEvents(&gba->video, cycles);
164		if (testEvent < nextEvent) {
165			nextEvent = testEvent;
166		}
167
168		testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
169		if (testEvent < nextEvent) {
170			nextEvent = testEvent;
171		}
172
173		testEvent = GBATimersProcessEvents(gba, cycles);
174		if (testEvent < nextEvent) {
175			nextEvent = testEvent;
176		}
177
178		testEvent = GBAMemoryRunDMAs(gba, cycles);
179		if (testEvent < nextEvent) {
180			nextEvent = testEvent;
181		}
182
183		testEvent = GBASIOProcessEvents(&gba->sio, cycles);
184		if (testEvent < nextEvent) {
185			nextEvent = testEvent;
186		}
187
188		cpu->cycles -= cycles;
189		cpu->nextEvent = nextEvent;
190
191		if (cpu->halted) {
192			cpu->cycles = cpu->nextEvent;
193		}
194	} while (cpu->cycles >= cpu->nextEvent);
195}
196
197static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
198	int32_t nextEvent = INT_MAX;
199	if (gba->timersEnabled) {
200		struct GBATimer* timer;
201		struct GBATimer* nextTimer;
202
203		timer = &gba->timers[0];
204		if (timer->enable) {
205			timer->nextEvent -= cycles;
206			timer->lastEvent -= cycles;
207			if (timer->nextEvent <= 0) {
208				timer->lastEvent = timer->nextEvent;
209				timer->nextEvent += timer->overflowInterval;
210				gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
211				timer->oldReload = timer->reload;
212
213				if (timer->doIrq) {
214					GBARaiseIRQ(gba, IRQ_TIMER0);
215				}
216
217				if (gba->audio.enable) {
218					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
219						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
220					}
221
222					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
223						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
224					}
225				}
226
227				nextTimer = &gba->timers[1];
228				if (nextTimer->countUp) {
229					++gba->memory.io[REG_TM1CNT_LO >> 1];
230					if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
231						nextTimer->nextEvent = 0;
232					}
233				}
234			}
235			nextEvent = timer->nextEvent;
236		}
237
238		timer = &gba->timers[1];
239		if (timer->enable) {
240			timer->nextEvent -= cycles;
241			timer->lastEvent -= cycles;
242			if (timer->nextEvent <= 0) {
243				timer->lastEvent = timer->nextEvent;
244				timer->nextEvent += timer->overflowInterval;
245				gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
246				timer->oldReload = timer->reload;
247
248				if (timer->doIrq) {
249					GBARaiseIRQ(gba, IRQ_TIMER1);
250				}
251
252				if (gba->audio.enable) {
253					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
254						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
255					}
256
257					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
258						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
259					}
260				}
261
262				if (timer->countUp) {
263					timer->nextEvent = INT_MAX;
264				}
265
266				nextTimer = &gba->timers[2];
267				if (nextTimer->countUp) {
268					++gba->memory.io[REG_TM2CNT_LO >> 1];
269					if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
270						nextTimer->nextEvent = 0;
271					}
272				}
273			}
274			if (timer->nextEvent < nextEvent) {
275				nextEvent = timer->nextEvent;
276			}
277		}
278
279		timer = &gba->timers[2];
280		if (timer->enable) {
281			timer->nextEvent -= cycles;
282			timer->lastEvent -= cycles;
283			nextEvent = timer->nextEvent;
284			if (timer->nextEvent <= 0) {
285				timer->lastEvent = timer->nextEvent;
286				timer->nextEvent += timer->overflowInterval;
287				gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
288				timer->oldReload = timer->reload;
289
290				if (timer->doIrq) {
291					GBARaiseIRQ(gba, IRQ_TIMER2);
292				}
293
294				if (timer->countUp) {
295					timer->nextEvent = INT_MAX;
296				}
297
298				nextTimer = &gba->timers[3];
299				if (nextTimer->countUp) {
300					++gba->memory.io[REG_TM3CNT_LO >> 1];
301					if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
302						nextTimer->nextEvent = 0;
303					}
304				}
305			}
306			if (timer->nextEvent < nextEvent) {
307				nextEvent = timer->nextEvent;
308			}
309		}
310
311		timer = &gba->timers[3];
312		if (timer->enable) {
313			timer->nextEvent -= cycles;
314			timer->lastEvent -= cycles;
315			nextEvent = timer->nextEvent;
316			if (timer->nextEvent <= 0) {
317				timer->lastEvent = timer->nextEvent;
318				timer->nextEvent += timer->overflowInterval;
319				gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
320				timer->oldReload = timer->reload;
321
322				if (timer->doIrq) {
323					GBARaiseIRQ(gba, IRQ_TIMER3);
324				}
325
326				if (timer->countUp) {
327					timer->nextEvent = INT_MAX;
328				}
329			}
330			if (timer->nextEvent < nextEvent) {
331				nextEvent = timer->nextEvent;
332			}
333		}
334	}
335	return nextEvent;
336}
337
338void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
339	gba->debugger = debugger;
340	gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
341	ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
342}
343
344void GBADetachDebugger(struct GBA* gba) {
345	gba->debugger = 0;
346	ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
347	gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
348}
349
350void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
351	gba->romVf = vf;
352	gba->pristineRomSize = vf->size(vf);
353	vf->seek(vf, 0, SEEK_SET);
354	if (gba->pristineRomSize > SIZE_CART0) {
355		gba->pristineRomSize = SIZE_CART0;
356	}
357	gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
358	if (!gba->pristineRom) {
359		GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
360		return;
361	}
362	gba->memory.rom = gba->pristineRom;
363	gba->activeFile = fname;
364	gba->memory.romSize = gba->pristineRomSize;
365	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
366	GBASavedataInit(&gba->memory.savedata, sav);
367	GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
368	// TODO: error check
369}
370
371void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
372	gba->biosVf = vf;
373	uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
374	if (!bios) {
375		GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
376		return;
377	}
378	gba->memory.bios = bios;
379	gba->memory.fullBios = 1;
380	uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
381	GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
382	if (checksum == GBA_BIOS_CHECKSUM) {
383		GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
384	} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
385		GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
386	} else {
387		GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
388	}
389	gba->biosChecksum = checksum;
390	if (gba->memory.activeRegion == REGION_BIOS) {
391		gba->cpu->memory.activeRegion = gba->memory.bios;
392	}
393	// TODO: error check
394}
395
396void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
397	size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
398	if (!patchedSize) {
399		return;
400	}
401	gba->memory.rom = anonymousMemoryMap(patchedSize);
402	if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
403		mappedMemoryFree(gba->memory.rom, patchedSize);
404		gba->memory.rom = gba->pristineRom;
405		return;
406	}
407	gba->memory.romSize = patchedSize;
408	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
409}
410
411void GBATimerUpdateRegister(struct GBA* gba, int timer) {
412	struct GBATimer* currentTimer = &gba->timers[timer];
413	if (currentTimer->enable && !currentTimer->countUp) {
414		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
415	}
416}
417
418void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
419	gba->timers[timer].reload = reload;
420}
421
422void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
423	struct GBATimer* currentTimer = &gba->timers[timer];
424	GBATimerUpdateRegister(gba, timer);
425
426	int oldPrescale = currentTimer->prescaleBits;
427	switch (control & 0x0003) {
428	case 0x0000:
429		currentTimer->prescaleBits = 0;
430		break;
431	case 0x0001:
432		currentTimer->prescaleBits = 6;
433		break;
434	case 0x0002:
435		currentTimer->prescaleBits = 8;
436		break;
437	case 0x0003:
438		currentTimer->prescaleBits = 10;
439		break;
440	}
441	currentTimer->countUp = !!(control & 0x0004);
442	currentTimer->doIrq = !!(control & 0x0040);
443	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
444	int wasEnabled = currentTimer->enable;
445	currentTimer->enable = !!(control & 0x0080);
446	if (!wasEnabled && currentTimer->enable) {
447		if (!currentTimer->countUp) {
448			currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
449		} else {
450			currentTimer->nextEvent = INT_MAX;
451		}
452		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
453		currentTimer->oldReload = currentTimer->reload;
454		currentTimer->lastEvent = 0;
455		gba->timersEnabled |= 1 << timer;
456	} else if (wasEnabled && !currentTimer->enable) {
457		if (!currentTimer->countUp) {
458			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
459		}
460		gba->timersEnabled &= ~(1 << timer);
461	} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
462		// FIXME: this might be before present
463		currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
464	}
465
466	if (currentTimer->nextEvent < gba->cpu->nextEvent) {
467		gba->cpu->nextEvent = currentTimer->nextEvent;
468	}
469};
470
471void GBAWriteIE(struct GBA* gba, uint16_t value) {
472	if (value & (1 << IRQ_KEYPAD)) {
473		GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
474	}
475
476	if (value & (1 << IRQ_GAMEPAK)) {
477		GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
478	}
479
480	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
481		ARMRaiseIRQ(gba->cpu);
482	}
483}
484
485void GBAWriteIME(struct GBA* gba, uint16_t value) {
486	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
487		ARMRaiseIRQ(gba->cpu);
488	}
489}
490
491void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
492	gba->memory.io[REG_IF >> 1] |= 1 << irq;
493	gba->cpu->halted = 0;
494
495	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
496		ARMRaiseIRQ(gba->cpu);
497	}
498}
499
500void GBATestIRQ(struct ARMCore* cpu) {
501	struct GBA* gba = (struct GBA*) cpu->master;
502	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
503		gba->springIRQ = 1;
504		gba->cpu->nextEvent = 0;
505	}
506}
507
508void GBAHalt(struct GBA* gba) {
509	gba->cpu->nextEvent = 0;
510	gba->cpu->halted = 1;
511}
512
513static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
514	struct GBAThread* threadContext = GBAThreadGetContext();
515	enum GBALogLevel logLevel = -1;
516
517	if (gba) {
518		logLevel = gba->logLevel;
519	}
520
521	if (threadContext) {
522		logLevel = threadContext->logLevel;
523		gba = threadContext->gba;
524	}
525
526	if (!(level & logLevel) && level != GBA_LOG_FATAL) {
527		return;
528	}
529
530	if (level == GBA_LOG_FATAL && gba) {
531		gba->cpu->nextEvent = 0;
532	}
533
534	if (threadContext) {
535		if (level == GBA_LOG_FATAL) {
536			MutexLock(&threadContext->stateMutex);
537			threadContext->state = THREAD_CRASHED;
538			MutexUnlock(&threadContext->stateMutex);
539		}
540		if (threadContext->logHandler) {
541			threadContext->logHandler(threadContext, level, format, args);
542			return;
543		}
544	}
545
546	vprintf(format, args);
547	printf("\n");
548
549	if (level == GBA_LOG_FATAL && !threadContext) {
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	default: // Avoids compiler warning
570	case DEBUGGER_LOG_DEBUG:
571		gbaLevel = GBA_LOG_DEBUG;
572		break;
573	case DEBUGGER_LOG_INFO:
574		gbaLevel = GBA_LOG_INFO;
575		break;
576	case DEBUGGER_LOG_WARN:
577		gbaLevel = GBA_LOG_WARN;
578		break;
579	case DEBUGGER_LOG_ERROR:
580		gbaLevel = GBA_LOG_ERROR;
581		break;
582	}
583	va_list args;
584	va_start(args, format);
585	_GBAVLog(gba, gbaLevel, format, args);
586	va_end(args);
587}
588
589bool GBAIsROM(struct VFile* vf) {
590	if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
591		return false;
592	}
593	uint8_t signature[sizeof(GBA_ROM_MAGIC)];
594	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
595		return false;
596	}
597	return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
598}
599
600bool GBAIsBIOS(struct VFile* vf) {
601	if (vf->seek(vf, 0, SEEK_SET) < 0) {
602		return false;
603	}
604	uint32_t interruptTable[7];
605	if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
606		return false;
607	}
608	int i;
609	for (i = 0; i < 7; ++i) {
610		if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
611			return false;
612		}
613	}
614	return true;
615}
616
617void GBAGetGameCode(struct GBA* gba, char* out) {
618	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
619}
620
621void GBAGetGameTitle(struct GBA* gba, char* out) {
622	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
623}
624
625void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
626	struct GBA* gba = (struct GBA*) cpu->master;
627	enum GBALogLevel level = GBA_LOG_FATAL;
628	if (gba->debugger) {
629		level = GBA_LOG_STUB;
630		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
631	}
632	GBALog(gba, level, "Stub opcode: %08x", opcode);
633}
634
635void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
636	struct GBA* gba = (struct GBA*) cpu->master;
637	GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
638	if (gba->debugger) {
639		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP);
640	}
641}