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