all repos — mgba @ 4b957cb66d51cec6dd99d2c753a20d00529cfd5b

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