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", "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 mCoreCallbacksListInit(&gb->coreCallbacks, 0);
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 mCoreCallbacksListDeinit(&gb->coreCallbacks);
353}
354
355void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
356 irqh->reset = GBReset;
357 irqh->processEvents = GBProcessEvents;
358 irqh->setInterrupts = GBSetInterrupts;
359 irqh->hitIllegal = GBIllegal;
360 irqh->stop = GBStop;
361 irqh->halt = GBHalt;
362}
363
364static uint32_t _GBBiosCRC32(struct VFile* vf) {
365 ssize_t size = vf->size(vf);
366 if (size <= 0 || size > GB_SIZE_CART_BANK0) {
367 return 0;
368 }
369 void* bios = vf->map(vf, size, MAP_READ);
370 uint32_t biosCrc = doCrc32(bios, size);
371 vf->unmap(vf, bios, size);
372 return biosCrc;
373}
374
375bool GBIsBIOS(struct VFile* vf) {
376 switch (_GBBiosCRC32(vf)) {
377 case DMG_BIOS_CHECKSUM:
378 case DMG_2_BIOS_CHECKSUM:
379 case CGB_BIOS_CHECKSUM:
380 return true;
381 default:
382 return false;
383 }
384}
385
386void GBReset(struct LR35902Core* cpu) {
387 struct GB* gb = (struct GB*) cpu->master;
388 GBDetectModel(gb);
389 if (gb->biosVf) {
390 if (!GBIsBIOS(gb->biosVf)) {
391 gb->biosVf->close(gb->biosVf);
392 gb->biosVf = NULL;
393 } else {
394 gb->biosVf->seek(gb->biosVf, 0, SEEK_SET);
395 gb->memory.romBase = malloc(GB_SIZE_CART_BANK0);
396 ssize_t size = gb->biosVf->read(gb->biosVf, gb->memory.romBase, GB_SIZE_CART_BANK0);
397 memcpy(&gb->memory.romBase[size], &gb->memory.rom[size], GB_SIZE_CART_BANK0 - size);
398 if (size > 0x100) {
399 memcpy(&gb->memory.romBase[0x100], &gb->memory.rom[0x100], sizeof(struct GBCartridge));
400 }
401
402 cpu->a = 0;
403 cpu->f.packed = 0;
404 cpu->c = 0;
405 cpu->e = 0;
406 cpu->h = 0;
407 cpu->l = 0;
408 cpu->sp = 0;
409 cpu->pc = 0;
410 }
411 }
412
413 cpu->b = 0;
414 cpu->d = 0;
415
416 if (!gb->biosVf) {
417 switch (gb->model) {
418 case GB_MODEL_DMG:
419 // TODO: SGB
420 case GB_MODEL_SGB:
421 case GB_MODEL_AUTODETECT: // Silence warnings
422 gb->model = GB_MODEL_DMG;
423 cpu->a = 1;
424 cpu->f.packed = 0xB0;
425 cpu->c = 0x13;
426 cpu->e = 0xD8;
427 cpu->h = 1;
428 cpu->l = 0x4D;
429 break;
430 case GB_MODEL_AGB:
431 cpu->b = 1;
432 // Fall through
433 case GB_MODEL_CGB:
434 cpu->a = 0x11;
435 cpu->f.packed = 0x80;
436 cpu->c = 0;
437 cpu->e = 0x08;
438 cpu->h = 0;
439 cpu->l = 0x7C;
440 break;
441 }
442
443 cpu->sp = 0xFFFE;
444 cpu->pc = 0x100;
445 }
446
447 gb->cpuBlocked = false;
448 gb->earlyExit = false;
449 gb->doubleSpeed = 0;
450
451 cpu->memory.setActiveRegion(cpu, cpu->pc);
452
453 if (gb->yankedRomSize) {
454 gb->memory.romSize = gb->yankedRomSize;
455 gb->yankedRomSize = 0;
456 }
457
458 mTimingClear(&gb->timing);
459
460 GBMemoryReset(gb);
461 GBVideoReset(&gb->video);
462 GBTimerReset(&gb->timer);
463 mTimingSchedule(&gb->timing, &gb->timer.event, GB_DMG_DIV_PERIOD);
464
465 GBAudioReset(&gb->audio);
466 GBIOReset(gb);
467 GBSIOReset(&gb->sio);
468
469 GBSavedataUnmask(gb);
470}
471
472void GBDetectModel(struct GB* gb) {
473 if (gb->model != GB_MODEL_AUTODETECT) {
474 return;
475 }
476 if (gb->biosVf) {
477 switch (_GBBiosCRC32(gb->biosVf)) {
478 case DMG_BIOS_CHECKSUM:
479 case DMG_2_BIOS_CHECKSUM:
480 gb->model = GB_MODEL_DMG;
481 break;
482 case CGB_BIOS_CHECKSUM:
483 gb->model = GB_MODEL_CGB;
484 break;
485 default:
486 gb->biosVf->close(gb->biosVf);
487 gb->biosVf = NULL;
488 }
489 }
490 if (gb->model == GB_MODEL_AUTODETECT && gb->memory.rom) {
491 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
492 if (cart->cgb & 0x80) {
493 gb->model = GB_MODEL_CGB;
494 } else {
495 gb->model = GB_MODEL_DMG;
496 }
497 }
498
499 switch (gb->model) {
500 case GB_MODEL_DMG:
501 case GB_MODEL_SGB:
502 case GB_MODEL_AUTODETECT: //Silence warnings
503 gb->audio.style = GB_AUDIO_DMG;
504 break;
505 case GB_MODEL_AGB:
506 case GB_MODEL_CGB:
507 gb->audio.style = GB_AUDIO_CGB;
508 break;
509 }
510}
511
512void GBUpdateIRQs(struct GB* gb) {
513 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
514 if (!irqs) {
515 return;
516 }
517 gb->cpu->halted = false;
518
519 if (!gb->memory.ime || gb->cpu->irqPending) {
520 return;
521 }
522
523 if (irqs & (1 << GB_IRQ_VBLANK)) {
524 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
525 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
526 return;
527 }
528 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
529 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
530 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
531 return;
532 }
533 if (irqs & (1 << GB_IRQ_TIMER)) {
534 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
535 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
536 return;
537 }
538 if (irqs & (1 << GB_IRQ_SIO)) {
539 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
540 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
541 return;
542 }
543 if (irqs & (1 << GB_IRQ_KEYPAD)) {
544 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
545 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
546 }
547}
548
549void GBProcessEvents(struct LR35902Core* cpu) {
550 struct GB* gb = (struct GB*) cpu->master;
551 do {
552 int32_t cycles = cpu->cycles;
553 int32_t nextEvent;
554
555 cpu->cycles = 0;
556 cpu->nextEvent = INT_MAX;
557
558 nextEvent = cycles;
559 do {
560 nextEvent = mTimingTick(&gb->timing, nextEvent);
561 } while (gb->cpuBlocked);
562 cpu->nextEvent = nextEvent;
563
564 if (gb->earlyExit) {
565 gb->earlyExit = false;
566 break;
567 }
568 if (cpu->halted) {
569 cpu->cycles = cpu->nextEvent;
570 if (!gb->memory.ie || !gb->memory.ime) {
571 break;
572 }
573 }
574 } while (cpu->cycles >= cpu->nextEvent);
575}
576
577void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
578 struct GB* gb = (struct GB*) cpu->master;
579 if (!enable) {
580 gb->memory.ime = enable;
581 mTimingDeschedule(&gb->timing, &gb->eiPending);
582 GBUpdateIRQs(gb);
583 } else {
584 mTimingDeschedule(&gb->timing, &gb->eiPending);
585 mTimingSchedule(&gb->timing, &gb->eiPending, 4);
586 }
587}
588
589static void _enableInterrupts(struct mTiming* timing, void* user, uint32_t cyclesLate) {
590 UNUSED(timing);
591 UNUSED(cyclesLate);
592 struct GB* gb = user;
593 gb->memory.ime = true;
594 GBUpdateIRQs(gb);
595}
596
597void GBHalt(struct LR35902Core* cpu) {
598 if (!cpu->irqPending) {
599 cpu->cycles = cpu->nextEvent;
600 cpu->halted = true;
601 }
602}
603
604void GBStop(struct LR35902Core* cpu) {
605 struct GB* gb = (struct GB*) cpu->master;
606 if (cpu->bus) {
607 mLOG(GB, GAME_ERROR, "Hit illegal stop at address %04X:%02X\n", cpu->pc, cpu->bus);
608 }
609 if (gb->memory.io[REG_KEY1] & 1) {
610 gb->doubleSpeed ^= 1;
611 gb->audio.timingFactor = gb->doubleSpeed + 1;
612 gb->memory.io[REG_KEY1] = 0;
613 gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
614 } else if (cpu->bus) {
615#ifdef USE_DEBUGGERS
616 if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
617 struct mDebuggerEntryInfo info = {
618 .address = cpu->pc - 1,
619 .opcode = 0x1000 | cpu->bus
620 };
621 mDebuggerEnter((struct mDebugger*) cpu->components[CPU_COMPONENT_DEBUGGER], DEBUGGER_ENTER_ILLEGAL_OP, &info);
622 }
623#endif
624 // Hang forever
625 gb->memory.ime = 0;
626 cpu->pc -= 2;
627 }
628 // TODO: Actually stop
629}
630
631void GBIllegal(struct LR35902Core* cpu) {
632 struct GB* gb = (struct GB*) cpu->master;
633 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
634#ifdef USE_DEBUGGERS
635 if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
636 struct mDebuggerEntryInfo info = {
637 .address = cpu->pc,
638 .opcode = cpu->bus
639 };
640 mDebuggerEnter((struct mDebugger*) cpu->components[CPU_COMPONENT_DEBUGGER], DEBUGGER_ENTER_ILLEGAL_OP, &info);
641 }
642#endif
643 // Hang forever
644 gb->memory.ime = 0;
645 --cpu->pc;
646}
647
648bool GBIsROM(struct VFile* vf) {
649 vf->seek(vf, 0x104, SEEK_SET);
650 uint8_t header[4];
651
652 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
653 return false;
654 }
655 if (memcmp(header, _knownHeader, sizeof(header))) {
656 return false;
657 }
658 return true;
659}
660
661void GBGetGameTitle(const struct GB* gb, char* out) {
662 const struct GBCartridge* cart = NULL;
663 if (gb->memory.rom) {
664 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
665 }
666 if (!cart) {
667 return;
668 }
669 if (cart->oldLicensee != 0x33) {
670 memcpy(out, cart->titleLong, 16);
671 } else {
672 memcpy(out, cart->titleShort, 11);
673 }
674}
675
676void GBGetGameCode(const struct GB* gb, char* out) {
677 memset(out, 0, 8);
678 const struct GBCartridge* cart = NULL;
679 if (gb->memory.rom) {
680 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
681 }
682 if (!cart) {
683 return;
684 }
685 if (cart->cgb == 0xC0) {
686 memcpy(out, "CGB-????", 8);
687 } else {
688 memcpy(out, "DMG-????", 8);
689 }
690 if (cart->oldLicensee == 0x33) {
691 memcpy(&out[4], cart->maker, 4);
692 }
693}
694
695void GBFrameEnded(struct GB* gb) {
696 GBSramClean(gb, gb->video.frameCounter);
697
698 if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
699 struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
700 size_t i;
701 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
702 struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
703 mCheatRefresh(device, cheats);
704 }
705 }
706}