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