all repos — mgba @ 41e00148bfa2e75ee72505030f4fd91fe5c13cfb

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