all repos — mgba @ 803b7c9859e2f6385bb1295f711a28e2b2c52599

mGBA Game Boy Advance Emulator

src/gb/gb.c (view raw)

  1/* Copyright (c) 2013-2016 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 <mgba/internal/gb/gb.h>
  7
  8#include <mgba/internal/gb/io.h>
  9#include <mgba/internal/gb/mbc.h>
 10#include <mgba/internal/lr35902/lr35902.h>
 11
 12#include <mgba/core/core.h>
 13#include <mgba/core/cheats.h>
 14#include <mgba-util/crc32.h>
 15#include <mgba-util/memory.h>
 16#include <mgba-util/math.h>
 17#include <mgba-util/patch.h>
 18#include <mgba-util/vfs.h>
 19
 20#define CLEANUP_THRESHOLD 15
 21
 22const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
 23const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
 24
 25const uint32_t GB_COMPONENT_MAGIC = 0x400000;
 26
 27static const uint8_t _knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
 28
 29#define DMG_BIOS_CHECKSUM 0xC2F5CC97
 30#define DMG_2_BIOS_CHECKSUM 0x59C8598E
 31#define CGB_BIOS_CHECKSUM 0x41884E46
 32
 33mLOG_DEFINE_CATEGORY(GB, "GB");
 34
 35static void GBInit(void* cpu, struct mCPUComponent* component);
 36static void GBDeinit(struct mCPUComponent* component);
 37static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
 38static void GBProcessEvents(struct LR35902Core* cpu);
 39static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
 40static void GBIllegal(struct LR35902Core* cpu);
 41static void GBStop(struct LR35902Core* cpu);
 42
 43static void _enableInterrupts(struct mTiming* timing, void* user, uint32_t cyclesLate);
 44
 45#ifdef _3DS
 46extern uint32_t* romBuffer;
 47extern size_t romBufferSize;
 48#endif
 49
 50void GBCreate(struct GB* gb) {
 51	gb->d.id = GB_COMPONENT_MAGIC;
 52	gb->d.init = GBInit;
 53	gb->d.deinit = GBDeinit;
 54}
 55
 56static void GBInit(void* cpu, struct mCPUComponent* component) {
 57	struct GB* gb = (struct GB*) component;
 58	gb->cpu = cpu;
 59	gb->sync = NULL;
 60
 61	GBInterruptHandlerInit(&gb->cpu->irqh);
 62	GBMemoryInit(gb);
 63
 64	gb->video.p = gb;
 65	GBVideoInit(&gb->video);
 66
 67	gb->audio.p = gb;
 68	GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52], GB_AUDIO_DMG); // TODO: Remove magic constant
 69
 70	gb->sio.p = gb;
 71	GBSIOInit(&gb->sio);
 72
 73	gb->timer.p = gb;
 74
 75	gb->model = GB_MODEL_AUTODETECT;
 76
 77	gb->biosVf = NULL;
 78	gb->romVf = NULL;
 79	gb->sramVf = NULL;
 80	gb->sramRealVf = NULL;
 81
 82	gb->pristineRom = 0;
 83	gb->pristineRomSize = 0;
 84	gb->yankedRomSize = 0;
 85
 86	gb->coreCallbacks = NULL;
 87	gb->stream = NULL;
 88
 89	mTimingInit(&gb->timing, &gb->cpu->cycles, &gb->cpu->nextEvent);
 90	gb->audio.timing = &gb->timing;
 91
 92	gb->eiPending.name = "GB EI";
 93	gb->eiPending.callback = _enableInterrupts;
 94	gb->eiPending.context = gb;
 95	gb->eiPending.priority = 0;
 96}
 97
 98static void GBDeinit(struct mCPUComponent* component) {
 99	struct GB* gb = (struct GB*) component;
100	mTimingDeinit(&gb->timing);
101}
102
103bool GBLoadROM(struct GB* gb, struct VFile* vf) {
104	if (!vf) {
105		return false;
106	}
107	GBUnloadROM(gb);
108	gb->romVf = vf;
109	gb->pristineRomSize = vf->size(vf);
110	vf->seek(vf, 0, SEEK_SET);
111#ifdef _3DS
112	gb->pristineRom = 0;
113	if (gb->pristineRomSize <= romBufferSize) {
114		gb->pristineRom = romBuffer;
115		vf->read(vf, romBuffer, gb->pristineRomSize);
116	}
117#else
118	gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
119#endif
120	if (!gb->pristineRom) {
121		return false;
122	}
123	gb->yankedRomSize = 0;
124	gb->memory.rom = gb->pristineRom;
125	gb->memory.romBase = gb->memory.rom;
126	gb->memory.romSize = gb->pristineRomSize;
127	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
128	GBMBCSwitchBank(&gb->memory, gb->memory.currentBank);
129
130	if (gb->cpu) {
131		struct LR35902Core* cpu = gb->cpu;
132		cpu->memory.setActiveRegion(cpu, cpu->pc);
133	}
134
135	// TODO: error check
136	return true;
137}
138
139bool GBLoadSave(struct GB* gb, struct VFile* vf) {
140	gb->sramVf = vf;
141	gb->sramRealVf = vf;
142	return vf;
143}
144
145static void GBSramDeinit(struct GB* gb) {
146	if (gb->sramVf) {
147		gb->sramVf->unmap(gb->sramVf, gb->memory.sram, gb->sramSize);
148		if (gb->memory.mbcType == GB_MBC3_RTC && gb->sramVf == gb->sramRealVf) {
149			GBMBCRTCWrite(gb);
150		}
151		gb->sramVf = NULL;
152	} else if (gb->memory.sram) {
153		mappedMemoryFree(gb->memory.sram, gb->sramSize);
154	}
155	gb->memory.sram = 0;
156}
157
158void GBResizeSram(struct GB* gb, size_t size) {
159	if (gb->memory.sram && size <= gb->sramSize) {
160		return;
161	}
162	mLOG(GB, INFO, "Resizing SRAM to %"PRIz"u bytes", size);
163	struct VFile* vf = gb->sramVf;
164	if (vf) {
165		if (vf == gb->sramRealVf) {
166			ssize_t vfSize = vf->size(vf);
167			if (vfSize >= 0 && (size_t) vfSize < size) {
168				uint8_t extdataBuffer[0x100];
169				if (vfSize & 0xFF) {
170					vf->seek(vf, -(vfSize & 0xFF), SEEK_END);
171					vf->read(vf, extdataBuffer, vfSize & 0xFF);
172				}
173				if (gb->memory.sram) {
174					vf->unmap(vf, gb->memory.sram, gb->sramSize);
175				}
176				vf->truncate(vf, size + (vfSize & 0xFF));
177				if (vfSize & 0xFF) {
178					vf->seek(vf, size, SEEK_SET);
179					vf->write(vf, extdataBuffer, vfSize & 0xFF);
180				}
181				gb->memory.sram = vf->map(vf, size, MAP_WRITE);
182				memset(&gb->memory.sram[gb->sramSize], 0xFF, size - gb->sramSize);
183			} else if (size > gb->sramSize || !gb->memory.sram) {
184				if (gb->memory.sram) {
185					vf->unmap(vf, gb->memory.sram, gb->sramSize);
186				}
187				gb->memory.sram = vf->map(vf, size, MAP_WRITE);
188			}
189		} else {
190			if (gb->memory.sram) {
191				vf->unmap(vf, gb->memory.sram, gb->sramSize);
192			}
193			gb->memory.sram = vf->map(vf, size, MAP_READ);
194		}
195		if (gb->memory.sram == (void*) -1) {
196			gb->memory.sram = NULL;
197		}
198	} else {
199		uint8_t* newSram = anonymousMemoryMap(size);
200		if (gb->memory.sram) {
201			if (size > gb->sramSize) {
202				memcpy(newSram, gb->memory.sram, gb->sramSize);
203				memset(&newSram[gb->sramSize], 0xFF, size - gb->sramSize);
204			} else {
205				memcpy(newSram, gb->memory.sram, size);
206			}
207			mappedMemoryFree(gb->memory.sram, gb->sramSize);
208		} else {
209			memset(newSram, 0xFF, size);
210		}
211		gb->memory.sram = newSram;
212	}
213	if (gb->sramSize < size) {
214		gb->sramSize = size;
215	}
216}
217
218void GBSramClean(struct GB* gb, uint32_t frameCount) {
219	// TODO: Share with GBASavedataClean
220	if (!gb->sramVf || gb->sramVf != gb->sramRealVf) {
221		return;
222	}
223	if (gb->sramDirty & GB_SRAM_DIRT_NEW) {
224		gb->sramDirtAge = frameCount;
225		gb->sramDirty &= ~GB_SRAM_DIRT_NEW;
226		if (!(gb->sramDirty & GB_SRAM_DIRT_SEEN)) {
227			gb->sramDirty |= GB_SRAM_DIRT_SEEN;
228		}
229	} else if ((gb->sramDirty & GB_SRAM_DIRT_SEEN) && frameCount - gb->sramDirtAge > CLEANUP_THRESHOLD) {
230		if (gb->memory.mbcType == GB_MBC3_RTC) {
231			GBMBCRTCWrite(gb);
232		}
233		gb->sramDirty = 0;
234		if (gb->memory.sram && gb->sramVf->sync(gb->sramVf, gb->memory.sram, gb->sramSize)) {
235			mLOG(GB_MEM, INFO, "Savedata synced");
236		} else {
237			mLOG(GB_MEM, INFO, "Savedata failed to sync!");
238		}
239	}
240}
241
242void GBSavedataMask(struct GB* gb, struct VFile* vf, bool writeback) {
243	GBSramDeinit(gb);
244	gb->sramVf = vf;
245	gb->sramMaskWriteback = writeback;
246	gb->memory.sram = vf->map(vf, gb->sramSize, MAP_READ);
247	GBMBCSwitchSramBank(gb, gb->memory.sramCurrentBank);
248}
249
250void GBSavedataUnmask(struct GB* gb) {
251	if (gb->sramVf == gb->sramRealVf) {
252		return;
253	}
254	struct VFile* vf = gb->sramVf;
255	GBSramDeinit(gb);
256	gb->sramVf = gb->sramRealVf;
257	gb->memory.sram = gb->sramVf->map(gb->sramVf, gb->sramSize, MAP_WRITE);
258	if (gb->sramMaskWriteback) {
259		vf->read(vf, gb->memory.sram, gb->sramSize);
260	}
261	vf->close(vf);
262}
263
264void GBUnloadROM(struct GB* gb) {
265	// TODO: Share with GBAUnloadROM
266	if (gb->memory.rom && gb->memory.romBase != gb->memory.rom && gb->memory.romBase != gb->pristineRom) {
267		free(gb->memory.romBase);
268	}
269	if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
270		if (gb->yankedRomSize) {
271			gb->yankedRomSize = 0;
272		}
273		mappedMemoryFree(gb->memory.rom, GB_SIZE_CART_MAX);
274		gb->memory.rom = gb->pristineRom;
275	}
276	gb->memory.rom = 0;
277
278	if (gb->romVf) {
279#ifndef _3DS
280		gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
281#endif
282		gb->romVf->close(gb->romVf);
283		gb->romVf = 0;
284	}
285	gb->pristineRom = 0;
286
287	GBSavedataUnmask(gb);
288	GBSramDeinit(gb);
289	if (gb->sramRealVf) {
290		gb->sramRealVf->close(gb->sramRealVf);
291	}
292	gb->sramRealVf = NULL;
293	gb->sramVf = NULL;
294}
295
296void GBSynthesizeROM(struct VFile* vf) {
297	if (!vf) {
298		return;
299	}
300	const struct GBCartridge cart = {
301		.logo = { _knownHeader[0], _knownHeader[1], _knownHeader[2], _knownHeader[3]}
302	};
303
304	vf->seek(vf, 0x100, SEEK_SET);
305	vf->write(vf, &cart, sizeof(cart));
306}
307
308void GBLoadBIOS(struct GB* gb, struct VFile* vf) {
309	gb->biosVf = vf;
310}
311
312void GBApplyPatch(struct GB* gb, struct Patch* patch) {
313	size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
314	if (!patchedSize) {
315		return;
316	}
317	if (patchedSize > GB_SIZE_CART_MAX) {
318		patchedSize = GB_SIZE_CART_MAX;
319	}
320	gb->memory.rom = anonymousMemoryMap(GB_SIZE_CART_MAX);
321	if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
322		mappedMemoryFree(gb->memory.rom, patchedSize);
323		gb->memory.rom = gb->pristineRom;
324		return;
325	}
326	gb->memory.romSize = patchedSize;
327	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
328}
329
330void GBDestroy(struct GB* gb) {
331	GBUnloadROM(gb);
332
333	if (gb->biosVf) {
334		gb->biosVf->close(gb->biosVf);
335		gb->biosVf = 0;
336	}
337
338	GBMemoryDeinit(gb);
339	GBAudioDeinit(&gb->audio);
340	GBVideoDeinit(&gb->video);
341	GBSIODeinit(&gb->sio);
342}
343
344void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
345	irqh->reset = GBReset;
346	irqh->processEvents = GBProcessEvents;
347	irqh->setInterrupts = GBSetInterrupts;
348	irqh->hitIllegal = GBIllegal;
349	irqh->stop = GBStop;
350	irqh->halt = GBHalt;
351}
352
353static uint32_t _GBBiosCRC32(struct VFile* vf) {
354	ssize_t size = vf->size(vf);
355	if (size <= 0 || size > GB_SIZE_CART_BANK0) {
356		return 0;
357	}
358	void* bios = vf->map(vf, size, MAP_READ);
359	uint32_t biosCrc = doCrc32(bios, size);
360	vf->unmap(vf, bios, size);
361	return biosCrc;
362}
363
364bool GBIsBIOS(struct VFile* vf) {
365	switch (_GBBiosCRC32(vf)) {
366	case DMG_BIOS_CHECKSUM:
367	case DMG_2_BIOS_CHECKSUM:
368	case CGB_BIOS_CHECKSUM:
369		return true;
370	default:
371		return false;
372	}
373}
374
375void GBReset(struct LR35902Core* cpu) {
376	struct GB* gb = (struct GB*) cpu->master;
377	GBDetectModel(gb);
378	if (gb->biosVf) {
379		if (!GBIsBIOS(gb->biosVf)) {
380			gb->biosVf->close(gb->biosVf);
381			gb->biosVf = NULL;
382		} else {
383			gb->biosVf->seek(gb->biosVf, 0, SEEK_SET);
384			gb->memory.romBase = malloc(GB_SIZE_CART_BANK0);
385			ssize_t size = gb->biosVf->read(gb->biosVf, gb->memory.romBase, GB_SIZE_CART_BANK0);
386			memcpy(&gb->memory.romBase[size], &gb->memory.rom[size], GB_SIZE_CART_BANK0 - size);
387			if (size > 0x100) {
388				memcpy(&gb->memory.romBase[0x100], &gb->memory.rom[0x100], sizeof(struct GBCartridge));
389			}
390
391			cpu->a = 0;
392			cpu->f.packed = 0;
393			cpu->c = 0;
394			cpu->e = 0;
395			cpu->h = 0;
396			cpu->l = 0;
397			cpu->sp = 0;
398			cpu->pc = 0;
399		}
400	}
401
402	cpu->b = 0;
403	cpu->d = 0;
404
405	if (!gb->biosVf) {
406		switch (gb->model) {
407		case GB_MODEL_DMG:
408			// TODO: SGB
409		case GB_MODEL_SGB:
410		case GB_MODEL_AUTODETECT: // Silence warnings
411			gb->model = GB_MODEL_DMG;
412			cpu->a = 1;
413			cpu->f.packed = 0xB0;
414			cpu->c = 0x13;
415			cpu->e = 0xD8;
416			cpu->h = 1;
417			cpu->l = 0x4D;
418			break;
419		case GB_MODEL_AGB:
420			cpu->b = 1;
421			// Fall through
422		case GB_MODEL_CGB:
423			cpu->a = 0x11;
424			cpu->f.packed = 0x80;
425			cpu->c = 0;
426			cpu->e = 0x08;
427			cpu->h = 0;
428			cpu->l = 0x7C;
429			break;
430		}
431
432		cpu->sp = 0xFFFE;
433		cpu->pc = 0x100;
434	}
435
436	gb->cpuBlocked = false;
437	gb->earlyExit = false;
438	gb->doubleSpeed = 0;
439
440	cpu->memory.setActiveRegion(cpu, cpu->pc);
441
442	if (gb->yankedRomSize) {
443		gb->memory.romSize = gb->yankedRomSize;
444		gb->yankedRomSize = 0;
445	}
446
447	mTimingClear(&gb->timing);
448
449	GBMemoryReset(gb);
450	GBVideoReset(&gb->video);
451	GBTimerReset(&gb->timer);
452	mTimingSchedule(&gb->timing, &gb->timer.event, GB_DMG_DIV_PERIOD);
453
454	GBAudioReset(&gb->audio);
455	GBIOReset(gb);
456	GBSIOReset(&gb->sio);
457
458	GBSavedataUnmask(gb);
459}
460
461void GBDetectModel(struct GB* gb) {
462	if (gb->model != GB_MODEL_AUTODETECT) {
463		return;
464	}
465	if (gb->biosVf) {
466		switch (_GBBiosCRC32(gb->biosVf)) {
467		case DMG_BIOS_CHECKSUM:
468		case DMG_2_BIOS_CHECKSUM:
469			gb->model = GB_MODEL_DMG;
470			break;
471		case CGB_BIOS_CHECKSUM:
472			gb->model = GB_MODEL_CGB;
473			break;
474		default:
475			gb->biosVf->close(gb->biosVf);
476			gb->biosVf = NULL;
477		}
478	}
479	if (gb->model == GB_MODEL_AUTODETECT && gb->memory.rom) {
480		const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
481		if (cart->cgb & 0x80) {
482			gb->model = GB_MODEL_CGB;
483		} else {
484			gb->model = GB_MODEL_DMG;
485		}
486	}
487
488	switch (gb->model) {
489	case GB_MODEL_DMG:
490	case GB_MODEL_SGB:
491	case GB_MODEL_AUTODETECT: //Silence warnings
492		gb->audio.style = GB_AUDIO_DMG;
493		break;
494	case GB_MODEL_AGB:
495	case GB_MODEL_CGB:
496		gb->audio.style = GB_AUDIO_CGB;
497		break;
498	}
499}
500
501void GBUpdateIRQs(struct GB* gb) {
502	int irqs = gb->memory.ie & gb->memory.io[REG_IF];
503	if (!irqs) {
504		return;
505	}
506	gb->cpu->halted = false;
507
508	if (!gb->memory.ime || gb->cpu->irqPending) {
509		return;
510	}
511
512	if (irqs & (1 << GB_IRQ_VBLANK)) {
513		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
514		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
515		return;
516	}
517	if (irqs & (1 << GB_IRQ_LCDSTAT)) {
518		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
519		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
520		return;
521	}
522	if (irqs & (1 << GB_IRQ_TIMER)) {
523		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
524		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
525		return;
526	}
527	if (irqs & (1 << GB_IRQ_SIO)) {
528		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
529		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
530		return;
531	}
532	if (irqs & (1 << GB_IRQ_KEYPAD)) {
533		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
534		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
535	}
536}
537
538void GBProcessEvents(struct LR35902Core* cpu) {
539	struct GB* gb = (struct GB*) cpu->master;
540	do {
541		int32_t cycles = cpu->cycles;
542		int32_t nextEvent;
543
544		cpu->cycles = 0;
545		cpu->nextEvent = INT_MAX;
546
547		nextEvent = cycles;
548		do {
549			nextEvent = mTimingTick(&gb->timing, nextEvent);
550		} while (gb->cpuBlocked);
551		cpu->nextEvent = nextEvent;
552
553		if (gb->earlyExit) {
554			gb->earlyExit = false;
555			break;
556		}
557		if (cpu->halted) {
558			cpu->cycles = cpu->nextEvent;
559			if (!gb->memory.ie || !gb->memory.ime) {
560				break;
561			}
562		}
563	} while (cpu->cycles >= cpu->nextEvent);
564}
565
566void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
567	struct GB* gb = (struct GB*) cpu->master;
568	if (!enable) {
569		gb->memory.ime = enable;
570		mTimingDeschedule(&gb->timing, &gb->eiPending);
571		GBUpdateIRQs(gb);
572	} else {
573		mTimingDeschedule(&gb->timing, &gb->eiPending);
574		mTimingSchedule(&gb->timing, &gb->eiPending, 4);
575	}
576}
577
578static void _enableInterrupts(struct mTiming* timing, void* user, uint32_t cyclesLate) {
579	UNUSED(timing);
580	UNUSED(cyclesLate);
581	struct GB* gb = user;
582	gb->memory.ime = true;
583	GBUpdateIRQs(gb);
584}
585
586void GBHalt(struct LR35902Core* cpu) {
587	if (!cpu->irqPending) {
588		cpu->cycles = cpu->nextEvent;
589		cpu->halted = true;
590	}
591}
592
593void GBStop(struct LR35902Core* cpu) {
594	struct GB* gb = (struct GB*) cpu->master;
595	if (cpu->bus) {
596		mLOG(GB, GAME_ERROR, "Hit illegal stop at address %04X:%02X\n", cpu->pc, cpu->bus);
597	}
598	if (gb->memory.io[REG_KEY1] & 1) {
599		gb->doubleSpeed ^= 1;
600		gb->audio.timingFactor = gb->doubleSpeed + 1;
601		gb->memory.io[REG_KEY1] = 0;
602		gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
603	} else if (cpu->bus) {
604#ifdef USE_DEBUGGERS
605		if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
606			struct mDebuggerEntryInfo info = {
607				.address = cpu->pc - 1,
608				.opcode = 0x1000 | cpu->bus
609			};
610			mDebuggerEnter((struct mDebugger*) cpu->components[CPU_COMPONENT_DEBUGGER], DEBUGGER_ENTER_ILLEGAL_OP, &info);
611		}
612#endif
613		// Hang forever
614		gb->memory.ime = 0;
615		cpu->pc -= 2;
616	}
617	// TODO: Actually stop
618}
619
620void GBIllegal(struct LR35902Core* cpu) {
621	struct GB* gb = (struct GB*) cpu->master;
622	mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
623#ifdef USE_DEBUGGERS
624	if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
625		struct mDebuggerEntryInfo info = {
626			.address = cpu->pc,
627			.opcode = cpu->bus
628		};
629		mDebuggerEnter((struct mDebugger*) cpu->components[CPU_COMPONENT_DEBUGGER], DEBUGGER_ENTER_ILLEGAL_OP, &info);
630	}
631#endif
632	// Hang forever
633	gb->memory.ime = 0;
634	--cpu->pc;
635}
636
637bool GBIsROM(struct VFile* vf) {
638	vf->seek(vf, 0x104, SEEK_SET);
639	uint8_t header[4];
640
641	if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
642		return false;
643	}
644	if (memcmp(header, _knownHeader, sizeof(header))) {
645		return false;
646	}
647	return true;
648}
649
650void GBGetGameTitle(const struct GB* gb, char* out) {
651	const struct GBCartridge* cart = NULL;
652	if (gb->memory.rom) {
653		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
654	}
655	if (gb->pristineRom) {
656		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
657	}
658	if (!cart) {
659		return;
660	}
661	if (cart->oldLicensee != 0x33) {
662		memcpy(out, cart->titleLong, 16);
663	} else {
664		memcpy(out, cart->titleShort, 11);
665	}
666}
667
668void GBGetGameCode(const struct GB* gb, char* out) {
669	memset(out, 0, 8);
670	const struct GBCartridge* cart = NULL;
671	if (gb->memory.rom) {
672		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
673	}
674	if (gb->pristineRom) {
675		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
676	}
677	if (!cart) {
678		return;
679	}
680	if (cart->cgb == 0xC0) {
681		memcpy(out, "CGB-????", 8);
682	} else {
683		memcpy(out, "DMG-????", 8);
684	}
685	if (cart->oldLicensee == 0x33) {
686		memcpy(&out[4], cart->maker, 4);
687	}
688}
689
690void GBFrameEnded(struct GB* gb) {
691	GBSramClean(gb, gb->video.frameCounter);
692
693	if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
694		struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
695		size_t i;
696		for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
697			struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
698			mCheatRefresh(device, cheats);
699		}
700	}
701}