all repos — mgba @ 3a3b7dffdb9e69763a9567119e45312d4b1453df

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