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 "gb.h"
7
8#include "gb/io.h"
9#include "gb/mbc.h"
10
11#include "core/core.h"
12#include "core/cheats.h"
13#include "util/crc32.h"
14#include "util/memory.h"
15#include "util/math.h"
16#include "util/patch.h"
17#include "util/vfs.h"
18
19#define CLEANUP_THRESHOLD 15
20
21const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
22const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
23
24const uint32_t GB_COMPONENT_MAGIC = 0x400000;
25
26static const uint8_t _knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
27
28#define DMG_BIOS_CHECKSUM 0xC2F5CC97
29#define DMG_2_BIOS_CHECKSUM 0x59C8598E
30#define CGB_BIOS_CHECKSUM 0x41884E46
31
32mLOG_DEFINE_CATEGORY(GB, "GB");
33
34static void GBInit(void* cpu, struct mCPUComponent* component);
35static void GBDeinit(struct mCPUComponent* component);
36static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
37static void GBProcessEvents(struct LR35902Core* cpu);
38static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
39static void GBIllegal(struct LR35902Core* cpu);
40static void GBStop(struct LR35902Core* cpu);
41
42static void _enableInterrupts(struct mTiming* timing, void* user, uint32_t cyclesLate);
43
44#ifdef _3DS
45extern uint32_t* romBuffer;
46extern size_t romBufferSize;
47#endif
48
49void GBCreate(struct GB* gb) {
50 gb->d.id = GB_COMPONENT_MAGIC;
51 gb->d.init = GBInit;
52 gb->d.deinit = GBDeinit;
53}
54
55static void GBInit(void* cpu, struct mCPUComponent* component) {
56 struct GB* gb = (struct GB*) component;
57 gb->cpu = cpu;
58 gb->sync = NULL;
59
60 GBInterruptHandlerInit(&gb->cpu->irqh);
61 GBMemoryInit(gb);
62
63 gb->video.p = gb;
64 GBVideoInit(&gb->video);
65
66 gb->audio.p = gb;
67 GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52], GB_AUDIO_DMG); // TODO: Remove magic constant
68
69 gb->sio.p = gb;
70 GBSIOInit(&gb->sio);
71
72 gb->timer.p = gb;
73
74 gb->model = GB_MODEL_AUTODETECT;
75
76 gb->biosVf = NULL;
77 gb->romVf = NULL;
78 gb->sramVf = NULL;
79 gb->sramRealVf = NULL;
80
81 gb->pristineRom = 0;
82 gb->pristineRomSize = 0;
83 gb->yankedRomSize = 0;
84
85 gb->coreCallbacks = NULL;
86 gb->stream = NULL;
87
88 mTimingInit(&gb->timing, &gb->cpu->cycles, &gb->cpu->nextEvent);
89 gb->audio.timing = &gb->timing;
90
91 gb->eiPending.name = "GB EI";
92 gb->eiPending.callback = _enableInterrupts;
93 gb->eiPending.context = gb;
94 gb->eiPending.priority = 0;
95}
96
97static void GBDeinit(struct mCPUComponent* component) {
98 struct GB* gb = (struct GB*) component;
99 mTimingDeinit(&gb->timing);
100}
101
102bool GBLoadROM(struct GB* gb, struct VFile* vf) {
103 if (!vf) {
104 return false;
105 }
106 GBUnloadROM(gb);
107 gb->romVf = vf;
108 gb->pristineRomSize = vf->size(vf);
109 vf->seek(vf, 0, SEEK_SET);
110#ifdef _3DS
111 gb->pristineRom = 0;
112 if (gb->pristineRomSize <= romBufferSize) {
113 gb->pristineRom = romBuffer;
114 vf->read(vf, romBuffer, gb->pristineRomSize);
115 }
116#else
117 gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
118#endif
119 if (!gb->pristineRom) {
120 return false;
121 }
122 gb->yankedRomSize = 0;
123 gb->memory.rom = gb->pristineRom;
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->memory, 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->memory.romBase != gb->pristineRom) {
266 free(gb->memory.romBase);
267 }
268 if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
269 if (gb->yankedRomSize) {
270 gb->yankedRomSize = 0;
271 }
272 mappedMemoryFree(gb->memory.rom, GB_SIZE_CART_MAX);
273 gb->memory.rom = gb->pristineRom;
274 }
275 gb->memory.rom = 0;
276
277 if (gb->romVf) {
278#ifndef _3DS
279 gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
280#endif
281 gb->romVf->close(gb->romVf);
282 gb->romVf = 0;
283 }
284 gb->pristineRom = 0;
285
286 GBSavedataUnmask(gb);
287 GBSramDeinit(gb);
288 if (gb->sramRealVf) {
289 gb->sramRealVf->close(gb->sramRealVf);
290 }
291 gb->sramRealVf = NULL;
292 gb->sramVf = NULL;
293}
294
295void GBSynthesizeROM(struct VFile* vf) {
296 if (!vf) {
297 return;
298 }
299 const struct GBCartridge cart = {
300 .logo = { _knownHeader[0], _knownHeader[1], _knownHeader[2], _knownHeader[3]}
301 };
302
303 vf->seek(vf, 0x100, SEEK_SET);
304 vf->write(vf, &cart, sizeof(cart));
305}
306
307void GBLoadBIOS(struct GB* gb, struct VFile* vf) {
308 gb->biosVf = vf;
309}
310
311void GBApplyPatch(struct GB* gb, struct Patch* patch) {
312 size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
313 if (!patchedSize) {
314 return;
315 }
316 if (patchedSize > GB_SIZE_CART_MAX) {
317 patchedSize = GB_SIZE_CART_MAX;
318 }
319 gb->memory.rom = anonymousMemoryMap(GB_SIZE_CART_MAX);
320 if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
321 mappedMemoryFree(gb->memory.rom, patchedSize);
322 gb->memory.rom = gb->pristineRom;
323 return;
324 }
325 gb->memory.romSize = patchedSize;
326 gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
327}
328
329void GBDestroy(struct GB* gb) {
330 GBUnloadROM(gb);
331
332 if (gb->biosVf) {
333 gb->biosVf->close(gb->biosVf);
334 gb->biosVf = 0;
335 }
336
337 GBMemoryDeinit(gb);
338 GBAudioDeinit(&gb->audio);
339 GBVideoDeinit(&gb->video);
340 GBSIODeinit(&gb->sio);
341}
342
343void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
344 irqh->reset = GBReset;
345 irqh->processEvents = GBProcessEvents;
346 irqh->setInterrupts = GBSetInterrupts;
347 irqh->hitIllegal = GBIllegal;
348 irqh->stop = GBStop;
349 irqh->halt = GBHalt;
350}
351
352static uint32_t _GBBiosCRC32(struct VFile* vf) {
353 ssize_t size = vf->size(vf);
354 if (size <= 0 || size > GB_SIZE_CART_BANK0) {
355 return 0;
356 }
357 void* bios = vf->map(vf, size, MAP_READ);
358 uint32_t biosCrc = doCrc32(bios, size);
359 vf->unmap(vf, bios, size);
360 return biosCrc;
361}
362
363bool GBIsBIOS(struct VFile* vf) {
364 switch (_GBBiosCRC32(vf)) {
365 case DMG_BIOS_CHECKSUM:
366 case DMG_2_BIOS_CHECKSUM:
367 case CGB_BIOS_CHECKSUM:
368 return true;
369 default:
370 return false;
371 }
372}
373
374void GBReset(struct LR35902Core* cpu) {
375 struct GB* gb = (struct GB*) cpu->master;
376 GBDetectModel(gb);
377 if (gb->biosVf) {
378 if (!GBIsBIOS(gb->biosVf)) {
379 gb->biosVf->close(gb->biosVf);
380 gb->biosVf = NULL;
381 } else {
382 gb->biosVf->seek(gb->biosVf, 0, SEEK_SET);
383 gb->memory.romBase = malloc(GB_SIZE_CART_BANK0);
384 ssize_t size = gb->biosVf->read(gb->biosVf, gb->memory.romBase, GB_SIZE_CART_BANK0);
385 memcpy(&gb->memory.romBase[size], &gb->memory.rom[size], GB_SIZE_CART_BANK0 - size);
386 if (size > 0x100) {
387 memcpy(&gb->memory.romBase[0x100], &gb->memory.rom[0x100], sizeof(struct GBCartridge));
388 }
389
390 cpu->a = 0;
391 cpu->f.packed = 0;
392 cpu->c = 0;
393 cpu->e = 0;
394 cpu->h = 0;
395 cpu->l = 0;
396 cpu->sp = 0;
397 cpu->pc = 0;
398 }
399 }
400
401 cpu->b = 0;
402 cpu->d = 0;
403
404 if (!gb->biosVf) {
405 switch (gb->model) {
406 case GB_MODEL_DMG:
407 // TODO: SGB
408 case GB_MODEL_SGB:
409 case GB_MODEL_AUTODETECT: // Silence warnings
410 gb->model = GB_MODEL_DMG;
411 cpu->a = 1;
412 cpu->f.packed = 0xB0;
413 cpu->c = 0x13;
414 cpu->e = 0xD8;
415 cpu->h = 1;
416 cpu->l = 0x4D;
417 break;
418 case GB_MODEL_AGB:
419 cpu->b = 1;
420 // Fall through
421 case GB_MODEL_CGB:
422 cpu->a = 0x11;
423 cpu->f.packed = 0x80;
424 cpu->c = 0;
425 cpu->e = 0x08;
426 cpu->h = 0;
427 cpu->l = 0x7C;
428 break;
429 }
430
431 cpu->sp = 0xFFFE;
432 cpu->pc = 0x100;
433 }
434
435 gb->cpuBlocked = false;
436 gb->doubleSpeed = 0;
437
438 cpu->memory.setActiveRegion(cpu, cpu->pc);
439
440 if (gb->yankedRomSize) {
441 gb->memory.romSize = gb->yankedRomSize;
442 gb->yankedRomSize = 0;
443 }
444
445 mTimingClear(&gb->timing);
446
447 GBMemoryReset(gb);
448 GBVideoReset(&gb->video);
449 GBTimerReset(&gb->timer);
450 mTimingSchedule(&gb->timing, &gb->timer.event, GB_DMG_DIV_PERIOD);
451
452 GBAudioReset(&gb->audio);
453 GBIOReset(gb);
454 GBSIOReset(&gb->sio);
455
456 GBSavedataUnmask(gb);
457}
458
459void GBDetectModel(struct GB* gb) {
460 if (gb->model != GB_MODEL_AUTODETECT) {
461 return;
462 }
463 if (gb->biosVf) {
464 switch (_GBBiosCRC32(gb->biosVf)) {
465 case DMG_BIOS_CHECKSUM:
466 case DMG_2_BIOS_CHECKSUM:
467 gb->model = GB_MODEL_DMG;
468 break;
469 case CGB_BIOS_CHECKSUM:
470 gb->model = GB_MODEL_CGB;
471 break;
472 default:
473 gb->biosVf->close(gb->biosVf);
474 gb->biosVf = NULL;
475 }
476 }
477 if (gb->model == GB_MODEL_AUTODETECT && gb->memory.rom) {
478 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
479 if (cart->cgb & 0x80) {
480 gb->model = GB_MODEL_CGB;
481 } else {
482 gb->model = GB_MODEL_DMG;
483 }
484 }
485
486 switch (gb->model) {
487 case GB_MODEL_DMG:
488 case GB_MODEL_SGB:
489 case GB_MODEL_AUTODETECT: //Silence warnings
490 gb->audio.style = GB_AUDIO_DMG;
491 break;
492 case GB_MODEL_AGB:
493 case GB_MODEL_CGB:
494 gb->audio.style = GB_AUDIO_CGB;
495 break;
496 }
497}
498
499void GBUpdateIRQs(struct GB* gb) {
500 int irqs = gb->memory.ie & gb->memory.io[REG_IF];
501 if (!irqs) {
502 return;
503 }
504 gb->cpu->halted = false;
505
506 if (!gb->memory.ime || gb->cpu->irqPending) {
507 return;
508 }
509
510 if (irqs & (1 << GB_IRQ_VBLANK)) {
511 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
512 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
513 return;
514 }
515 if (irqs & (1 << GB_IRQ_LCDSTAT)) {
516 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
517 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
518 return;
519 }
520 if (irqs & (1 << GB_IRQ_TIMER)) {
521 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
522 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
523 return;
524 }
525 if (irqs & (1 << GB_IRQ_SIO)) {
526 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
527 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
528 return;
529 }
530 if (irqs & (1 << GB_IRQ_KEYPAD)) {
531 LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
532 gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
533 }
534}
535
536void GBProcessEvents(struct LR35902Core* cpu) {
537 struct GB* gb = (struct GB*) cpu->master;
538 do {
539 int32_t cycles = cpu->cycles;
540 int32_t nextEvent;
541
542 cpu->cycles = 0;
543 cpu->nextEvent = INT_MAX;
544
545 nextEvent = cycles;
546 do {
547 nextEvent = mTimingTick(&gb->timing, nextEvent);
548 } while (gb->cpuBlocked);
549 cpu->nextEvent = nextEvent;
550
551 if (cpu->halted) {
552 cpu->cycles = cpu->nextEvent;
553 if (!gb->memory.ie || !gb->memory.ime) {
554 break;
555 }
556 }
557 } while (cpu->cycles >= cpu->nextEvent);
558}
559
560void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
561 struct GB* gb = (struct GB*) cpu->master;
562 if (!enable) {
563 gb->memory.ime = enable;
564 mTimingDeschedule(&gb->timing, &gb->eiPending);
565 GBUpdateIRQs(gb);
566 } else {
567 mTimingDeschedule(&gb->timing, &gb->eiPending);
568 mTimingSchedule(&gb->timing, &gb->eiPending, 4);
569 }
570}
571
572static void _enableInterrupts(struct mTiming* timing, void* user, uint32_t cyclesLate) {
573 UNUSED(timing);
574 UNUSED(cyclesLate);
575 struct GB* gb = user;
576 gb->memory.ime = true;
577 GBUpdateIRQs(gb);
578}
579
580void GBHalt(struct LR35902Core* cpu) {
581 if (!cpu->irqPending) {
582 cpu->cycles = cpu->nextEvent;
583 cpu->halted = true;
584 }
585}
586
587void GBStop(struct LR35902Core* cpu) {
588 struct GB* gb = (struct GB*) cpu->master;
589 if (cpu->bus) {
590 mLOG(GB, GAME_ERROR, "Hit illegal stop at address %04X:%02X\n", cpu->pc, cpu->bus);
591 }
592 if (gb->memory.io[REG_KEY1] & 1) {
593 gb->doubleSpeed ^= 1;
594 gb->audio.timingFactor = gb->doubleSpeed + 1;
595 gb->memory.io[REG_KEY1] = 0;
596 gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
597 } else if (cpu->bus) {
598#ifdef USE_DEBUGGERS
599 if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
600 struct mDebuggerEntryInfo info = {
601 .address = cpu->pc - 1,
602 .opcode = 0x1000 | cpu->bus
603 };
604 mDebuggerEnter((struct mDebugger*) cpu->components[CPU_COMPONENT_DEBUGGER], DEBUGGER_ENTER_ILLEGAL_OP, &info);
605 }
606#endif
607 // Hang forever
608 gb->memory.ime = 0;
609 cpu->pc -= 2;
610 }
611 // TODO: Actually stop
612}
613
614void GBIllegal(struct LR35902Core* cpu) {
615 struct GB* gb = (struct GB*) cpu->master;
616 mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
617#ifdef USE_DEBUGGERS
618 if (cpu->components && cpu->components[CPU_COMPONENT_DEBUGGER]) {
619 struct mDebuggerEntryInfo info = {
620 .address = cpu->pc,
621 .opcode = 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;
629}
630
631bool GBIsROM(struct VFile* vf) {
632 vf->seek(vf, 0x104, SEEK_SET);
633 uint8_t header[4];
634
635 if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
636 return false;
637 }
638 if (memcmp(header, _knownHeader, sizeof(header))) {
639 return false;
640 }
641 return true;
642}
643
644void GBGetGameTitle(const struct GB* gb, char* out) {
645 const struct GBCartridge* cart = NULL;
646 if (gb->memory.rom) {
647 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
648 }
649 if (gb->pristineRom) {
650 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
651 }
652 if (!cart) {
653 return;
654 }
655 if (cart->oldLicensee != 0x33) {
656 memcpy(out, cart->titleLong, 16);
657 } else {
658 memcpy(out, cart->titleShort, 11);
659 }
660}
661
662void GBGetGameCode(const struct GB* gb, char* out) {
663 memset(out, 0, 8);
664 const struct GBCartridge* cart = NULL;
665 if (gb->memory.rom) {
666 cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
667 }
668 if (gb->pristineRom) {
669 cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
670 }
671 if (!cart) {
672 return;
673 }
674 if (cart->cgb == 0xC0) {
675 memcpy(out, "CGB-????", 8);
676 } else {
677 memcpy(out, "DMG-????", 8);
678 }
679 if (cart->oldLicensee == 0x33) {
680 memcpy(&out[4], cart->maker, 4);
681 }
682}
683
684void GBFrameEnded(struct GB* gb) {
685 GBSramClean(gb, gb->video.frameCounter);
686
687 if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
688 struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
689 size_t i;
690 for (i = 0; i < mCheatSetsSize(&device->cheats); ++i) {
691 struct mCheatSet* cheats = *mCheatSetsGetPointer(&device->cheats, i);
692 mCheatRefresh(device, cheats);
693 }
694 }
695}