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