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