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