all repos — mgba @ f3b66397a2a598e5cfb63acd0c0b84f5796d0802

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