all repos — mgba @ 7bb17bc99ddd1764c99ce209cc017442e30e339f

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