all repos — mgba @ 86571c8496d3b9d1257aa449e5eebcee7137c297

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