all repos — mgba @ 3f9abf2b05396e7219a7e9a6034c95b74cb0c055

mGBA Game Boy Advance Emulator

src/gba/gba.c (view raw)

  1/* Copyright (c) 2013-2015 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/cheats.h"
 10#include "gba/io.h"
 11#include "gba/supervisor/rr.h"
 12#include "gba/supervisor/thread.h"
 13#include "gba/serialize.h"
 14#include "gba/sio.h"
 15
 16#include "isa-inlines.h"
 17
 18#include "util/crc32.h"
 19#include "util/memory.h"
 20#include "util/patch.h"
 21#include "util/vfs.h"
 22
 23const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
 24const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
 25
 26static const size_t GBA_ROM_MAGIC_OFFSET = 3;
 27static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
 28
 29static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
 30static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
 31static void GBAProcessEvents(struct ARMCore* cpu);
 32static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
 33static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
 34static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
 35static void GBABreakpoint(struct ARMCore* cpu, int immediate);
 36
 37static bool _setSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
 38static bool _clearSoftwareBreakpoint(struct ARMDebugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
 39
 40void GBACreate(struct GBA* gba) {
 41	gba->d.id = GBA_COMPONENT_MAGIC;
 42	gba->d.init = GBAInit;
 43	gba->d.deinit = 0;
 44}
 45
 46static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
 47	struct GBA* gba = (struct GBA*) component;
 48	gba->cpu = cpu;
 49	gba->debugger = 0;
 50
 51	GBAInterruptHandlerInit(&cpu->irqh);
 52	GBAMemoryInit(gba);
 53	GBASavedataInit(&gba->memory.savedata, 0);
 54
 55	gba->video.p = gba;
 56	GBAVideoInit(&gba->video);
 57
 58	gba->audio.p = gba;
 59	GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
 60
 61	GBAIOInit(gba);
 62
 63	gba->sio.p = gba;
 64	GBASIOInit(&gba->sio);
 65
 66	gba->timersEnabled = 0;
 67	memset(gba->timers, 0, sizeof(gba->timers));
 68
 69	gba->springIRQ = 0;
 70	gba->keySource = 0;
 71	gba->rotationSource = 0;
 72	gba->luminanceSource = 0;
 73	gba->rtcSource = 0;
 74	gba->rumble = 0;
 75	gba->rr = 0;
 76
 77	gba->romVf = 0;
 78	gba->biosVf = 0;
 79
 80	gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
 81
 82	gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
 83
 84	gba->idleOptimization = IDLE_LOOP_REMOVE;
 85	gba->idleLoop = IDLE_LOOP_NONE;
 86	gba->lastJump = 0;
 87	gba->idleDetectionStep = 0;
 88	gba->idleDetectionFailures = 0;
 89	gba->performingDMA = false;
 90}
 91
 92void GBADestroy(struct GBA* gba) {
 93	if (gba->pristineRom == gba->memory.rom) {
 94		gba->memory.rom = 0;
 95	}
 96
 97	if (gba->romVf) {
 98		gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
 99	}
100
101	if (gba->biosVf) {
102		gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
103	}
104
105	GBAMemoryDeinit(gba);
106	GBAVideoDeinit(&gba->video);
107	GBAAudioDeinit(&gba->audio);
108	GBASIODeinit(&gba->sio);
109	gba->rr = 0;
110}
111
112void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
113	irqh->reset = GBAReset;
114	irqh->processEvents = GBAProcessEvents;
115	irqh->swi16 = GBASwi16;
116	irqh->swi32 = GBASwi32;
117	irqh->hitIllegal = GBAIllegal;
118	irqh->readCPSR = GBATestIRQ;
119	irqh->hitStub = GBAHitStub;
120	irqh->bkpt16 = GBABreakpoint;
121	irqh->bkpt32 = GBABreakpoint;
122}
123
124void GBAReset(struct ARMCore* cpu) {
125	ARMSetPrivilegeMode(cpu, MODE_IRQ);
126	cpu->gprs[ARM_SP] = SP_BASE_IRQ;
127	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
128	cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
129	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
130	cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
131
132	struct GBA* gba = (struct GBA*) cpu->master;
133	if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
134		GBASavedataUnmask(&gba->memory.savedata);
135	}
136	GBAMemoryReset(gba);
137	GBAVideoReset(&gba->video);
138	GBAAudioReset(&gba->audio);
139	GBAIOInit(gba);
140
141	GBASIODeinit(&gba->sio);
142	GBASIOInit(&gba->sio);
143
144	gba->timersEnabled = 0;
145	memset(gba->timers, 0, sizeof(gba->timers));
146}
147
148void GBASkipBIOS(struct ARMCore* cpu) {
149	if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
150		cpu->gprs[ARM_PC] = BASE_CART0;
151		int currentCycles = 0;
152		ARM_WRITE_PC;
153	}
154}
155
156static void GBAProcessEvents(struct ARMCore* cpu) {
157	do {
158		struct GBA* gba = (struct GBA*) cpu->master;
159		int32_t cycles = cpu->nextEvent;
160		int32_t nextEvent = INT_MAX;
161		int32_t testEvent;
162
163		gba->bus = cpu->prefetch[1];
164		if (cpu->executionMode == MODE_THUMB) {
165			gba->bus |= cpu->prefetch[1] << 16;
166		}
167
168		if (gba->springIRQ) {
169			ARMRaiseIRQ(cpu);
170			gba->springIRQ = 0;
171		}
172
173		testEvent = GBAVideoProcessEvents(&gba->video, cycles);
174		if (testEvent < nextEvent) {
175			nextEvent = testEvent;
176		}
177
178		testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
179		if (testEvent < nextEvent) {
180			nextEvent = testEvent;
181		}
182
183		testEvent = GBATimersProcessEvents(gba, cycles);
184		if (testEvent < nextEvent) {
185			nextEvent = testEvent;
186		}
187
188		testEvent = GBAMemoryRunDMAs(gba, cycles);
189		if (testEvent < nextEvent) {
190			nextEvent = testEvent;
191		}
192
193		testEvent = GBASIOProcessEvents(&gba->sio, cycles);
194		if (testEvent < nextEvent) {
195			nextEvent = testEvent;
196		}
197
198		cpu->cycles -= cycles;
199		cpu->nextEvent = nextEvent;
200
201		if (cpu->halted) {
202			cpu->cycles = cpu->nextEvent;
203		}
204	} while (cpu->cycles >= cpu->nextEvent);
205}
206
207static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
208	int32_t nextEvent = INT_MAX;
209	if (gba->timersEnabled) {
210		struct GBATimer* timer;
211		struct GBATimer* nextTimer;
212
213		timer = &gba->timers[0];
214		if (timer->enable) {
215			timer->nextEvent -= cycles;
216			timer->lastEvent -= cycles;
217			if (timer->nextEvent <= 0) {
218				timer->lastEvent = timer->nextEvent;
219				timer->nextEvent += timer->overflowInterval;
220				gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
221				timer->oldReload = timer->reload;
222
223				if (timer->doIrq) {
224					GBARaiseIRQ(gba, IRQ_TIMER0);
225				}
226
227				if (gba->audio.enable) {
228					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
229						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
230					}
231
232					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
233						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
234					}
235				}
236
237				nextTimer = &gba->timers[1];
238				if (nextTimer->countUp) {
239					++gba->memory.io[REG_TM1CNT_LO >> 1];
240					if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
241						nextTimer->nextEvent = 0;
242					}
243				}
244			}
245			nextEvent = timer->nextEvent;
246		}
247
248		timer = &gba->timers[1];
249		if (timer->enable) {
250			timer->nextEvent -= cycles;
251			timer->lastEvent -= cycles;
252			if (timer->nextEvent <= 0) {
253				timer->lastEvent = timer->nextEvent;
254				timer->nextEvent += timer->overflowInterval;
255				gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
256				timer->oldReload = timer->reload;
257
258				if (timer->doIrq) {
259					GBARaiseIRQ(gba, IRQ_TIMER1);
260				}
261
262				if (gba->audio.enable) {
263					if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
264						GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
265					}
266
267					if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
268						GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
269					}
270				}
271
272				if (timer->countUp) {
273					timer->nextEvent = INT_MAX;
274				}
275
276				nextTimer = &gba->timers[2];
277				if (nextTimer->countUp) {
278					++gba->memory.io[REG_TM2CNT_LO >> 1];
279					if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
280						nextTimer->nextEvent = 0;
281					}
282				}
283			}
284			if (timer->nextEvent < nextEvent) {
285				nextEvent = timer->nextEvent;
286			}
287		}
288
289		timer = &gba->timers[2];
290		if (timer->enable) {
291			timer->nextEvent -= cycles;
292			timer->lastEvent -= cycles;
293			if (timer->nextEvent <= 0) {
294				timer->lastEvent = timer->nextEvent;
295				timer->nextEvent += timer->overflowInterval;
296				gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
297				timer->oldReload = timer->reload;
298
299				if (timer->doIrq) {
300					GBARaiseIRQ(gba, IRQ_TIMER2);
301				}
302
303				if (timer->countUp) {
304					timer->nextEvent = INT_MAX;
305				}
306
307				nextTimer = &gba->timers[3];
308				if (nextTimer->countUp) {
309					++gba->memory.io[REG_TM3CNT_LO >> 1];
310					if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
311						nextTimer->nextEvent = 0;
312					}
313				}
314			}
315			if (timer->nextEvent < nextEvent) {
316				nextEvent = timer->nextEvent;
317			}
318		}
319
320		timer = &gba->timers[3];
321		if (timer->enable) {
322			timer->nextEvent -= cycles;
323			timer->lastEvent -= cycles;
324			if (timer->nextEvent <= 0) {
325				timer->lastEvent = timer->nextEvent;
326				timer->nextEvent += timer->overflowInterval;
327				gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
328				timer->oldReload = timer->reload;
329
330				if (timer->doIrq) {
331					GBARaiseIRQ(gba, IRQ_TIMER3);
332				}
333
334				if (timer->countUp) {
335					timer->nextEvent = INT_MAX;
336				}
337			}
338			if (timer->nextEvent < nextEvent) {
339				nextEvent = timer->nextEvent;
340			}
341		}
342	}
343	return nextEvent;
344}
345
346void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
347	debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
348	debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
349	gba->debugger = debugger;
350	gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
351	ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
352}
353
354void GBADetachDebugger(struct GBA* gba) {
355	gba->debugger = 0;
356	ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
357	gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
358}
359
360void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
361	gba->romVf = vf;
362	gba->pristineRomSize = vf->size(vf);
363	vf->seek(vf, 0, SEEK_SET);
364	if (gba->pristineRomSize > SIZE_CART0) {
365		gba->pristineRomSize = SIZE_CART0;
366	}
367	gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
368	if (!gba->pristineRom) {
369		GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
370		return;
371	}
372	gba->memory.rom = gba->pristineRom;
373	gba->activeFile = fname;
374	gba->memory.romSize = gba->pristineRomSize;
375	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
376	GBASavedataInit(&gba->memory.savedata, sav);
377	GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
378	// TODO: error check
379}
380
381void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
382	gba->biosVf = vf;
383	uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
384	if (!bios) {
385		GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
386		return;
387	}
388	gba->memory.bios = bios;
389	gba->memory.fullBios = 1;
390	uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
391	GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
392	if (checksum == GBA_BIOS_CHECKSUM) {
393		GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
394	} else if (checksum == GBA_DS_BIOS_CHECKSUM) {
395		GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
396	} else {
397		GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
398	}
399	gba->biosChecksum = checksum;
400	if (gba->memory.activeRegion == REGION_BIOS) {
401		gba->cpu->memory.activeRegion = gba->memory.bios;
402	}
403	// TODO: error check
404}
405
406void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
407	size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
408	if (!patchedSize) {
409		return;
410	}
411	gba->memory.rom = anonymousMemoryMap(patchedSize);
412	if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
413		mappedMemoryFree(gba->memory.rom, patchedSize);
414		gba->memory.rom = gba->pristineRom;
415		return;
416	}
417	gba->memory.romSize = patchedSize;
418	gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
419}
420
421void GBATimerUpdateRegister(struct GBA* gba, int timer) {
422	struct GBATimer* currentTimer = &gba->timers[timer];
423	if (currentTimer->enable && !currentTimer->countUp) {
424		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
425	}
426}
427
428void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
429	gba->timers[timer].reload = reload;
430}
431
432void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
433	struct GBATimer* currentTimer = &gba->timers[timer];
434	GBATimerUpdateRegister(gba, timer);
435
436	int oldPrescale = currentTimer->prescaleBits;
437	switch (control & 0x0003) {
438	case 0x0000:
439		currentTimer->prescaleBits = 0;
440		break;
441	case 0x0001:
442		currentTimer->prescaleBits = 6;
443		break;
444	case 0x0002:
445		currentTimer->prescaleBits = 8;
446		break;
447	case 0x0003:
448		currentTimer->prescaleBits = 10;
449		break;
450	}
451	currentTimer->countUp = !!(control & 0x0004);
452	currentTimer->doIrq = !!(control & 0x0040);
453	currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
454	int wasEnabled = currentTimer->enable;
455	currentTimer->enable = !!(control & 0x0080);
456	if (!wasEnabled && currentTimer->enable) {
457		if (!currentTimer->countUp) {
458			currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
459		} else {
460			currentTimer->nextEvent = INT_MAX;
461		}
462		gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
463		currentTimer->oldReload = currentTimer->reload;
464		currentTimer->lastEvent = 0;
465		gba->timersEnabled |= 1 << timer;
466	} else if (wasEnabled && !currentTimer->enable) {
467		if (!currentTimer->countUp) {
468			gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
469		}
470		gba->timersEnabled &= ~(1 << timer);
471	} else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
472		// FIXME: this might be before present
473		currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
474	}
475
476	if (currentTimer->nextEvent < gba->cpu->nextEvent) {
477		gba->cpu->nextEvent = currentTimer->nextEvent;
478	}
479};
480
481void GBAWriteIE(struct GBA* gba, uint16_t value) {
482	if (value & (1 << IRQ_KEYPAD)) {
483		GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
484	}
485
486	if (value & (1 << IRQ_GAMEPAK)) {
487		GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
488	}
489
490	if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
491		ARMRaiseIRQ(gba->cpu);
492	}
493}
494
495void GBAWriteIME(struct GBA* gba, uint16_t value) {
496	if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
497		ARMRaiseIRQ(gba->cpu);
498	}
499}
500
501void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
502	gba->memory.io[REG_IF >> 1] |= 1 << irq;
503	gba->cpu->halted = 0;
504
505	if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
506		ARMRaiseIRQ(gba->cpu);
507	}
508}
509
510void GBATestIRQ(struct ARMCore* cpu) {
511	struct GBA* gba = (struct GBA*) cpu->master;
512	if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
513		gba->springIRQ = 1;
514		gba->cpu->nextEvent = 0;
515	}
516}
517
518void GBAHalt(struct GBA* gba) {
519	gba->cpu->nextEvent = 0;
520	gba->cpu->halted = 1;
521}
522
523static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
524	struct GBAThread* threadContext = GBAThreadGetContext();
525	enum GBALogLevel logLevel = -1;
526
527	if (gba) {
528		logLevel = gba->logLevel;
529	}
530
531	if (threadContext) {
532		logLevel = threadContext->logLevel;
533		gba = threadContext->gba;
534	}
535
536	if (!(level & logLevel) && level != GBA_LOG_FATAL) {
537		return;
538	}
539
540	if (level == GBA_LOG_FATAL && gba) {
541		gba->cpu->nextEvent = 0;
542	}
543
544	if (threadContext) {
545		if (level == GBA_LOG_FATAL) {
546			MutexLock(&threadContext->stateMutex);
547			threadContext->state = THREAD_CRASHED;
548			MutexUnlock(&threadContext->stateMutex);
549		}
550		if (threadContext->logHandler) {
551			threadContext->logHandler(threadContext, level, format, args);
552			return;
553		}
554	}
555
556	vprintf(format, args);
557	printf("\n");
558
559	if (level == GBA_LOG_FATAL && !threadContext) {
560		abort();
561	}
562}
563
564void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
565	va_list args;
566	va_start(args, format);
567	_GBAVLog(gba, level, format, args);
568	va_end(args);
569}
570
571void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
572	struct GBA* gba = 0;
573	if (debugger->cpu) {
574		gba = (struct GBA*) debugger->cpu->master;
575	}
576
577	enum GBALogLevel gbaLevel;
578	switch (level) {
579	default: // Avoids compiler warning
580	case DEBUGGER_LOG_DEBUG:
581		gbaLevel = GBA_LOG_DEBUG;
582		break;
583	case DEBUGGER_LOG_INFO:
584		gbaLevel = GBA_LOG_INFO;
585		break;
586	case DEBUGGER_LOG_WARN:
587		gbaLevel = GBA_LOG_WARN;
588		break;
589	case DEBUGGER_LOG_ERROR:
590		gbaLevel = GBA_LOG_ERROR;
591		break;
592	}
593	va_list args;
594	va_start(args, format);
595	_GBAVLog(gba, gbaLevel, format, args);
596	va_end(args);
597}
598
599bool GBAIsROM(struct VFile* vf) {
600	if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
601		return false;
602	}
603	uint8_t signature[sizeof(GBA_ROM_MAGIC)];
604	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
605		return false;
606	}
607	return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
608}
609
610bool GBAIsBIOS(struct VFile* vf) {
611	if (vf->seek(vf, 0, SEEK_SET) < 0) {
612		return false;
613	}
614	uint32_t interruptTable[7];
615	if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
616		return false;
617	}
618	int i;
619	for (i = 0; i < 7; ++i) {
620		if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
621			return false;
622		}
623	}
624	return true;
625}
626
627void GBAGetGameCode(struct GBA* gba, char* out) {
628	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
629}
630
631void GBAGetGameTitle(struct GBA* gba, char* out) {
632	memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
633}
634
635void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
636	struct GBA* gba = (struct GBA*) cpu->master;
637	enum GBALogLevel level = GBA_LOG_FATAL;
638	if (gba->debugger) {
639		level = GBA_LOG_STUB;
640		struct DebuggerEntryInfo info = {
641			.address = _ARMPCAddress(cpu),
642			.opcode = opcode
643		};
644		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
645	}
646	GBALog(gba, level, "Stub opcode: %08x", opcode);
647}
648
649void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
650	struct GBA* gba = (struct GBA*) cpu->master;
651	GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
652	if (gba->debugger) {
653		struct DebuggerEntryInfo info = {
654			.address = _ARMPCAddress(cpu),
655			.opcode = opcode
656		};
657		ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
658	}
659}
660
661void GBABreakpoint(struct ARMCore* cpu, int immediate) {
662	struct GBA* gba = (struct GBA*) cpu->master;
663	if (immediate >= GBA_COMPONENT_MAX) {
664		return;
665	}
666	switch (immediate) {
667	case GBA_COMPONENT_DEBUGGER:
668		if (gba->debugger) {
669			struct DebuggerEntryInfo info = {
670				.address = _ARMPCAddress(cpu)
671			};
672			ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_BREAKPOINT, &info);
673		}
674		break;
675	case GBA_COMPONENT_CHEAT_DEVICE:
676		if (gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
677			struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
678			struct GBACheatHook* hook = 0;
679			size_t i;
680			for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
681				struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
682				if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
683					GBACheatRefresh(device, cheats);
684					hook = cheats->hook;
685				}
686			}
687			if (hook) {
688				ARMRunFake(cpu, hook->patchedOpcode);
689			}
690		}
691		break;
692	default:
693		break;
694	}
695}
696
697void GBAFrameStarted(struct GBA* gba) {
698	UNUSED(gba);
699
700	struct GBAThread* thread = GBAThreadGetContext();
701	if (!thread) {
702		return;
703	}
704
705	if (thread->rewindBuffer) {
706		--thread->rewindBufferNext;
707		if (thread->rewindBufferNext <= 0) {
708			thread->rewindBufferNext = thread->rewindBufferInterval;
709			GBARecordFrame(thread);
710		}
711	}
712}
713
714void GBAFrameEnded(struct GBA* gba) {
715	if (gba->rr) {
716		gba->rr->nextFrame(gba->rr);
717	}
718
719	if (gba->cpu->components && gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
720		struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
721		size_t i;
722		for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
723			struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
724			if (!cheats->hook) {
725				GBACheatRefresh(device, cheats);
726			}
727		}
728	}
729
730
731	struct GBAThread* thread = GBAThreadGetContext();
732	if (!thread) {
733		return;
734	}
735
736	if (thread->stream) {
737		thread->stream->postVideoFrame(thread->stream, thread->renderer);
738	}
739
740	if (thread->frameCallback) {
741		thread->frameCallback(thread);
742	}
743}
744
745void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
746	size_t immediate;
747	for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
748		if (gba->cpu->components[immediate] == component) {
749			break;
750		}
751	}
752	if (immediate == gba->cpu->numComponents) {
753		return;
754	}
755	if (mode == MODE_ARM) {
756		int32_t value;
757		int32_t old;
758		value = 0xE1200070;
759		value |= immediate & 0xF;
760		value |= (immediate & 0xFFF0) << 4;
761		GBAPatch32(gba->cpu, address, value, &old);
762		*opcode = old;
763	} else {
764		int16_t value;
765		int16_t old;
766		value = 0xBE00;
767		value |= immediate & 0xFF;
768		GBAPatch16(gba->cpu, address, value, &old);
769		*opcode = (uint16_t) old;
770	}
771}
772
773void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
774	if (mode == MODE_ARM) {
775		GBAPatch32(gba->cpu, address, opcode, 0);
776	} else {
777		GBAPatch16(gba->cpu, address, opcode, 0);
778	}
779}
780
781static bool _setSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
782	GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d, address, mode, opcode);
783	return true;
784}
785
786static bool _clearSoftwareBreakpoint(struct ARMDebugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
787	GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
788	return true;
789}