all repos — mgba @ 09cd56820f9445fc9d9f5cefe3a56408b4d654ab

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