all repos — mgba @ 84844232ecc9175dc90e2ab2ccff380c6edd502d

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