all repos — mgba @ 0de46a78679adf820eba08c3f72e64202540b692

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-serialize.h"
 12#include "gba-sio.h"
 13#include "gba-thread.h"
 14
 15#include "isa-inlines.h"
 16
 17#include "util/crc32.h"
 18#include "util/memory.h"
 19#include "util/patch.h"
 20#include "util/vfs.h"
 21
 22const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
 23const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 24
 25static const size_t GBA_ROM_MAGIC_OFFSET = 3;
 26static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
 27
 28static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
 29static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
 30static void GBAProcessEvents(struct ARMCore* cpu);
 31static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
 32static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
 33static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
 34
 35void GBACreate(struct GBA* gba) {
 36	gba->d.id = GBA_COMPONENT_MAGIC;
 37	gba->d.init = GBAInit;
 38	gba->d.deinit = 0;
 39}
 40
 41static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
 42	struct GBA* gba = (struct GBA*) component;
 43	gba->cpu = cpu;
 44	gba->debugger = 0;
 45
 46	GBAInterruptHandlerInit(&cpu->irqh);
 47	GBAMemoryInit(gba);
 48	GBASavedataInit(&gba->memory.savedata, 0);
 49
 50	gba->video.p = gba;
 51	GBAVideoInit(&gba->video);
 52
 53	gba->audio.p = gba;
 54	GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
 55
 56	GBAIOInit(gba);
 57
 58	gba->sio.p = gba;
 59	GBASIOInit(&gba->sio);
 60
 61	gba->timersEnabled = 0;
 62	memset(gba->timers, 0, sizeof(gba->timers));
 63
 64	gba->springIRQ = 0;
 65	gba->keySource = 0;
 66	gba->rotationSource = 0;
 67	gba->luminanceSource = 0;
 68	gba->rtcSource = 0;
 69	gba->rumble = 0;
 70	gba->rr = 0;
 71
 72	gba->romVf = 0;
 73	gba->biosVf = 0;
 74
 75	gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
 76
 77	gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
 78
 79	gba->idleOptimization = IDLE_LOOP_REMOVE;
 80	gba->idleLoop = IDLE_LOOP_NONE;
 81	gba->lastJump = 0;
 82	gba->idleDetectionStep = 0;
 83	gba->idleDetectionFailures = 0;
 84	gba->performingDMA = false;
 85}
 86
 87void GBADestroy(struct GBA* gba) {
 88	if (gba->pristineRom == gba->memory.rom) {
 89		gba->memory.rom = 0;
 90	}
 91
 92	if (gba->romVf) {
 93		gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
 94	}
 95
 96	if (gba->biosVf) {
 97		gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
 98	}
 99
100	GBAMemoryDeinit(gba);
101	GBAVideoDeinit(&gba->video);
102	GBAAudioDeinit(&gba->audio);
103	GBARRContextDestroy(gba);
104}
105
106void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
107	irqh->reset = GBAReset;
108	irqh->processEvents = GBAProcessEvents;
109	irqh->swi16 = GBASwi16;
110	irqh->swi32 = GBASwi32;
111	irqh->hitIllegal = GBAIllegal;
112	irqh->readCPSR = GBATestIRQ;
113	irqh->hitStub = GBAHitStub;
114}
115
116void GBAReset(struct ARMCore* cpu) {
117	ARMSetPrivilegeMode(cpu, MODE_IRQ);
118	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
119	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
120	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
121	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
122	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
123
124	struct GBA* gba = (struct GBA*) cpu->master;
125	if (!GBARRIsPlaying(gba->rr) && !GBARRIsRecording(gba->rr)) {
126		GBASavedataUnmask(&gba->memory.savedata);
127	}
128	GBAMemoryReset(gba);
129	GBAVideoReset(&gba->video);
130	GBAAudioReset(&gba->audio);
131	GBAIOInit(gba);
132
133	GBASIODeinit(&gba->sio);
134	GBASIOInit(&gba->sio);
135
136	gba->timersEnabled = 0;
137	memset(gba->timers, 0, sizeof(gba->timers));
138}
139
140void GBASkipBIOS(struct ARMCore* cpu) {
141	if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
142		cpu->gprs[ARM_PC] = BASE_CART0;
143		int currentCycles = 0;
144		ARM_WRITE_PC;
145	}
146}
147
148static void GBAProcessEvents(struct ARMCore* cpu) {
149	do {
150		struct GBA* gba = (struct GBA*) cpu->master;
151		int32_t cycles = cpu->nextEvent;
152		int32_t nextEvent = INT_MAX;
153		int32_t testEvent;
154
155		gba->bus = cpu->prefetch[1];
156		if (cpu->executionMode == MODE_THUMB) {
157			gba->bus |= cpu->prefetch[1] << 16;
158		}
159
160		if (gba->springIRQ) {
161			ARMRaiseIRQ(cpu);
162			gba->springIRQ = 0;
163		}
164
165		testEvent = GBAVideoProcessEvents(&gba->video, cycles);
166		if (testEvent < nextEvent) {
167			nextEvent = testEvent;
168		}
169
170		testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
171		if (testEvent < nextEvent) {
172			nextEvent = testEvent;
173		}
174
175		testEvent = GBATimersProcessEvents(gba, cycles);
176		if (testEvent < nextEvent) {
177			nextEvent = testEvent;
178		}
179
180		testEvent = GBAMemoryRunDMAs(gba, cycles);
181		if (testEvent < nextEvent) {
182			nextEvent = testEvent;
183		}
184
185		testEvent = GBASIOProcessEvents(&gba->sio, cycles);
186		if (testEvent < nextEvent) {
187			nextEvent = testEvent;
188		}
189
190		cpu->cycles -= cycles;
191		cpu->nextEvent = nextEvent;
192
193		if (cpu->halted) {
194			cpu->cycles = cpu->nextEvent;
195		}
196	} while (cpu->cycles >= cpu->nextEvent);
197}
198
199static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
200	int32_t nextEvent = INT_MAX;
201	if (gba->timersEnabled) {
202		struct GBATimer* timer;
203		struct GBATimer* nextTimer;
204
205		timer = &gba->timers[0];
206		if (timer->enable) {
207			timer->nextEvent -= cycles;
208			timer->lastEvent -= cycles;
209			if (timer->nextEvent <= 0) {
210				timer->lastEvent = timer->nextEvent;
211				timer->nextEvent += timer->overflowInterval;
212				gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
213				timer->oldReload = timer->reload;
214
215				if (timer->doIrq) {
216					GBARaiseIRQ(gba, IRQ_TIMER0);
217				}
218
219				if (gba->audio.enable) {
220					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
221						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
222					}
223
224					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
225						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
226					}
227				}
228
229				nextTimer = &gba->timers[1];
230				if (nextTimer->countUp) {
231					++gba->memory.io[REG_TM1CNT_LO >> 1];
232					if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
233						nextTimer->nextEvent = 0;
234					}
235				}
236			}
237			nextEvent = timer->nextEvent;
238		}
239
240		timer = &gba->timers[1];
241		if (timer->enable) {
242			timer->nextEvent -= cycles;
243			timer->lastEvent -= cycles;
244			if (timer->nextEvent <= 0) {
245				timer->lastEvent = timer->nextEvent;
246				timer->nextEvent += timer->overflowInterval;
247				gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
248				timer->oldReload = timer->reload;
249
250				if (timer->doIrq) {
251					GBARaiseIRQ(gba, IRQ_TIMER1);
252				}
253
254				if (gba->audio.enable) {
255					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
256						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
257					}
258
259					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
260						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
261					}
262				}
263
264				if (timer->countUp) {
265					timer->nextEvent = INT_MAX;
266				}
267
268				nextTimer = &gba->timers[2];
269				if (nextTimer->countUp) {
270					++gba->memory.io[REG_TM2CNT_LO >> 1];
271					if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
272						nextTimer->nextEvent = 0;
273					}
274				}
275			}
276			if (timer->nextEvent < nextEvent) {
277				nextEvent = timer->nextEvent;
278			}
279		}
280
281		timer = &gba->timers[2];
282		if (timer->enable) {
283			timer->nextEvent -= cycles;
284			timer->lastEvent -= cycles;
285			if (timer->nextEvent <= 0) {
286				timer->lastEvent = timer->nextEvent;
287				timer->nextEvent += timer->overflowInterval;
288				gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
289				timer->oldReload = timer->reload;
290
291				if (timer->doIrq) {
292					GBARaiseIRQ(gba, IRQ_TIMER2);
293				}
294
295				if (timer->countUp) {
296					timer->nextEvent = INT_MAX;
297				}
298
299				nextTimer = &gba->timers[3];
300				if (nextTimer->countUp) {
301					++gba->memory.io[REG_TM3CNT_LO >> 1];
302					if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
303						nextTimer->nextEvent = 0;
304					}
305				}
306			}
307			if (timer->nextEvent < nextEvent) {
308				nextEvent = timer->nextEvent;
309			}
310		}
311
312		timer = &gba->timers[3];
313		if (timer->enable) {
314			timer->nextEvent -= cycles;
315			timer->lastEvent -= cycles;
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		struct DebuggerEntryInfo info = {
631			.address = cpu->gprs[ARM_PC],
632			.opcode = opcode
633		};
634		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
635	}
636	GBALog(gba, level, "Stub opcode: %08x", opcode);
637}
638
639void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
640	struct GBA* gba = (struct GBA*) cpu->master;
641	GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
642	if (gba->debugger) {
643		struct DebuggerEntryInfo info = {
644			.address = cpu->gprs[ARM_PC],
645			.opcode = opcode
646		};
647		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
648	}
649}
650
651void GBAFrameStarted(struct GBA* gba) {
652	UNUSED(gba);
653
654	struct GBAThread* thread = GBAThreadGetContext();
655	if (!thread) {
656		return;
657	}
658
659	if (thread->rewindBuffer) {
660		--thread->rewindBufferNext;
661		if (thread->rewindBufferNext <= 0) {
662			thread->rewindBufferNext = thread->rewindBufferInterval;
663			GBARecordFrame(thread);
664		}
665	}
666}
667
668void GBAFrameEnded(struct GBA* gba) {
669	if (gba->rr) {
670		GBARRNextFrame(gba->rr);
671	}
672
673	struct GBAThread* thread = GBAThreadGetContext();
674	if (!thread) {
675		return;
676	}
677
678	if (thread->stream) {
679		thread->stream->postVideoFrame(thread->stream, thread->renderer);
680	}
681
682	if (thread->frameCallback) {
683		thread->frameCallback(thread);
684	}
685}