src/gb/core.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/gb/core.h>
7
8#include <mgba/core/core.h>
9#include <mgba/internal/debugger/symbols.h>
10#include <mgba/internal/gb/cheats.h>
11#include <mgba/internal/gb/debugger/symbols.h>
12#include <mgba/internal/gb/extra/cli.h>
13#include <mgba/internal/gb/io.h>
14#include <mgba/internal/gb/gb.h>
15#include <mgba/internal/gb/mbc.h>
16#include <mgba/internal/gb/overrides.h>
17#include <mgba/internal/gb/renderers/software.h>
18#include <mgba/internal/gb/renderers/proxy.h>
19#include <mgba/internal/gb/serialize.h>
20#include <mgba/internal/lr35902/lr35902.h>
21#include <mgba/internal/lr35902/debugger/debugger.h>
22#include <mgba-util/crc32.h>
23#include <mgba-util/memory.h>
24#include <mgba-util/patch.h>
25#include <mgba-util/vfs.h>
26
27const static struct mCoreChannelInfo _GBVideoLayers[] = {
28 { 0, "bg", "Background", NULL },
29 { 1, "obj", "Objects", NULL },
30 { 2, "win", "Window", NULL },
31};
32
33const static struct mCoreChannelInfo _GBAudioChannels[] = {
34 { 0, "ch0", "Channel 0", "Square/Sweep" },
35 { 1, "ch1", "Channel 1", "Square" },
36 { 2, "ch2", "Channel 2", "PCM" },
37 { 3, "ch3", "Channel 3", "Noise" },
38};
39
40const static struct LR35902Segment _GBSegments[] = {
41 { .name = "ROM", .start = GB_BASE_CART_BANK1, .end = GB_BASE_VRAM },
42 { .name = "RAM", .start = GB_BASE_EXTERNAL_RAM, .end = GB_BASE_WORKING_RAM_BANK0 },
43 { 0 }
44};
45
46const static struct LR35902Segment _GBCSegments[] = {
47 { .name = "ROM", .start = GB_BASE_CART_BANK1, .end = GB_BASE_VRAM },
48 { .name = "RAM", .start = GB_BASE_EXTERNAL_RAM, .end = GB_BASE_WORKING_RAM_BANK0 },
49 { .name = "WRAM", .start = GB_BASE_WORKING_RAM_BANK1, .end = 0xE000 },
50 { .name = "VRAM", .start = GB_BASE_VRAM, .end = GB_BASE_EXTERNAL_RAM },
51 { 0 }
52};
53
54struct mVideoLogContext;
55struct GBCore {
56 struct mCore d;
57 struct GBVideoSoftwareRenderer renderer;
58 struct GBVideoProxyRenderer proxyRenderer;
59 struct mVideoLogContext* logContext;
60 struct mCoreCallbacks logCallbacks;
61 uint8_t keys;
62 struct mCPUComponent* components[CPU_COMPONENT_MAX];
63 const struct Configuration* overrides;
64 struct mDebuggerPlatform* debuggerPlatform;
65 struct mCheatDevice* cheatDevice;
66};
67
68static bool _GBCoreInit(struct mCore* core) {
69 struct GBCore* gbcore = (struct GBCore*) core;
70
71 struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
72 struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
73 if (!cpu || !gb) {
74 free(cpu);
75 free(gb);
76 return false;
77 }
78 core->cpu = cpu;
79 core->board = gb;
80 gbcore->overrides = NULL;
81 gbcore->debuggerPlatform = NULL;
82 gbcore->cheatDevice = NULL;
83
84 GBCreate(gb);
85 memset(gbcore->components, 0, sizeof(gbcore->components));
86 LR35902SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
87 LR35902Init(cpu);
88 mRTCGenericSourceInit(&core->rtc, core);
89 gb->memory.rtc = &core->rtc.d;
90
91 GBVideoSoftwareRendererCreate(&gbcore->renderer);
92 gbcore->renderer.outputBuffer = NULL;
93
94 gbcore->keys = 0;
95 gb->keySource = &gbcore->keys;
96
97#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
98 mDirectorySetInit(&core->dirs);
99#endif
100
101 return true;
102}
103
104static void _GBCoreDeinit(struct mCore* core) {
105 LR35902Deinit(core->cpu);
106 GBDestroy(core->board);
107 mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
108 mappedMemoryFree(core->board, sizeof(struct GB));
109#if defined USE_DEBUGGERS && (!defined(MINIMAL_CORE) || MINIMAL_CORE < 2)
110 mDirectorySetDeinit(&core->dirs);
111 if (core->symbolTable) {
112 mDebuggerSymbolTableDestroy(core->symbolTable);
113 }
114#endif
115
116 struct GBCore* gbcore = (struct GBCore*) core;
117 free(gbcore->debuggerPlatform);
118 if (gbcore->cheatDevice) {
119 mCheatDeviceDestroy(gbcore->cheatDevice);
120 }
121 free(gbcore->cheatDevice);
122 mCoreConfigFreeOpts(&core->opts);
123 free(core);
124}
125
126static enum mPlatform _GBCorePlatform(const struct mCore* core) {
127 UNUSED(core);
128 return PLATFORM_GB;
129}
130
131static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
132 struct GB* gb = core->board;
133 gb->sync = sync;
134}
135
136static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
137 UNUSED(config);
138
139 struct GB* gb = core->board;
140 if (core->opts.mute) {
141 gb->audio.masterVolume = 0;
142 } else {
143 gb->audio.masterVolume = core->opts.volume;
144 }
145 gb->video.frameskip = core->opts.frameskip;
146
147 int color;
148 if (mCoreConfigGetIntValue(&core->config, "gb.pal[0]", &color)) {
149 GBVideoSetPalette(&gb->video, 0, color);
150 }
151 if (mCoreConfigGetIntValue(&core->config, "gb.pal[1]", &color)) {
152 GBVideoSetPalette(&gb->video, 1, color);
153 }
154 if (mCoreConfigGetIntValue(&core->config, "gb.pal[2]", &color)) {
155 GBVideoSetPalette(&gb->video, 2, color);
156 }
157 if (mCoreConfigGetIntValue(&core->config, "gb.pal[3]", &color)) {
158 GBVideoSetPalette(&gb->video, 3, color);
159 }
160
161 mCoreConfigCopyValue(&core->config, config, "gb.bios");
162 mCoreConfigCopyValue(&core->config, config, "gbc.bios");
163
164#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
165 struct GBCore* gbcore = (struct GBCore*) core;
166 gbcore->overrides = mCoreConfigGetOverridesConst(config);
167#endif
168}
169
170static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
171 UNUSED(core);
172 *width = GB_VIDEO_HORIZONTAL_PIXELS;
173 *height = GB_VIDEO_VERTICAL_PIXELS;
174}
175
176static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
177 struct GBCore* gbcore = (struct GBCore*) core;
178 gbcore->renderer.outputBuffer = buffer;
179 gbcore->renderer.outputBufferStride = stride;
180}
181
182static void _GBCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
183 struct GBCore* gbcore = (struct GBCore*) core;
184 gbcore->renderer.d.getPixels(&gbcore->renderer.d, stride, buffer);
185}
186
187static void _GBCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
188 struct GBCore* gbcore = (struct GBCore*) core;
189 gbcore->renderer.d.putPixels(&gbcore->renderer.d, stride, buffer);
190}
191
192static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
193 struct GB* gb = core->board;
194 switch (ch) {
195 case 0:
196 return gb->audio.left;
197 case 1:
198 return gb->audio.right;
199 default:
200 return NULL;
201 }
202}
203
204static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
205 struct GB* gb = core->board;
206 GBAudioResizeBuffer(&gb->audio, samples);
207}
208
209static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
210 struct GB* gb = core->board;
211 return gb->audio.samples;
212}
213
214static void _GBCoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
215 struct GB* gb = core->board;
216 *mCoreCallbacksListAppend(&gb->coreCallbacks) = *coreCallbacks;
217}
218
219static void _GBCoreClearCoreCallbacks(struct mCore* core) {
220 struct GB* gb = core->board;
221 mCoreCallbacksListClear(&gb->coreCallbacks);
222}
223
224static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
225 struct GB* gb = core->board;
226 gb->stream = stream;
227 if (stream && stream->videoDimensionsChanged) {
228 stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
229 }
230}
231
232static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
233 return GBLoadROM(core->board, vf);
234}
235
236static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
237 UNUSED(type);
238 GBLoadBIOS(core->board, vf);
239 return true;
240}
241
242static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
243 return GBLoadSave(core->board, vf);
244}
245
246static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
247 struct GB* gb = core->board;
248 GBSavedataMask(gb, vf, false);
249 return true; // TODO: Return a real value
250}
251
252static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
253 if (!vf) {
254 return false;
255 }
256 struct Patch patch;
257 if (!loadPatch(vf, &patch)) {
258 return false;
259 }
260 GBApplyPatch(core->board, &patch);
261 return true;
262}
263
264static void _GBCoreUnloadROM(struct mCore* core) {
265 struct GBCore* gbcore = (struct GBCore*) core;
266 struct LR35902Core* cpu = core->cpu;
267 if (gbcore->cheatDevice) {
268 LR35902HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
269 cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
270 mCheatDeviceDestroy(gbcore->cheatDevice);
271 gbcore->cheatDevice = NULL;
272 }
273 return GBUnloadROM(core->board);
274}
275
276static void _GBCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
277 struct GB* gb = (struct GB*) core->board;
278 switch (type) {
279 case CHECKSUM_CRC32:
280 memcpy(data, &gb->romCrc32, sizeof(gb->romCrc32));
281 break;
282 }
283 return;
284}
285
286static void _GBCoreReset(struct mCore* core) {
287 struct GBCore* gbcore = (struct GBCore*) core;
288 struct GB* gb = (struct GB*) core->board;
289 if (gbcore->renderer.outputBuffer) {
290 GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
291 }
292
293 if (gb->memory.rom) {
294 struct GBCartridgeOverride override;
295 const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
296 override.headerCrc32 = doCrc32(cart, sizeof(*cart));
297 if (GBOverrideFind(gbcore->overrides, &override)) {
298 GBOverrideApply(gb, &override);
299 }
300 }
301
302#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
303 if (!gb->biosVf && core->opts.useBios) {
304 struct VFile* bios = NULL;
305 bool found = false;
306 if (core->opts.bios) {
307 bios = VFileOpen(core->opts.bios, O_RDONLY);
308 if (bios && GBIsBIOS(bios)) {
309 found = true;
310 } else if (bios) {
311 bios->close(bios);
312 bios = NULL;
313 }
314 }
315 if (!found) {
316 GBDetectModel(gb);
317 const char* configPath = NULL;
318
319 switch (gb->model) {
320 case GB_MODEL_DMG:
321 case GB_MODEL_SGB: // TODO
322 configPath = mCoreConfigGetValue(&core->config, "gb.bios");
323 break;
324 case GB_MODEL_CGB:
325 case GB_MODEL_AGB:
326 configPath = mCoreConfigGetValue(&core->config, "gbc.bios");
327 break;
328 default:
329 break;
330 };
331 if (configPath) {
332 bios = VFileOpen(configPath, O_RDONLY);
333 }
334 if (bios && GBIsBIOS(bios)) {
335 found = true;
336 } else if (bios) {
337 bios->close(bios);
338 bios = NULL;
339 }
340 }
341 if (!found) {
342 char path[PATH_MAX];
343 mCoreConfigDirectory(path, PATH_MAX);
344 switch (gb->model) {
345 case GB_MODEL_DMG:
346 case GB_MODEL_SGB: // TODO
347 strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
348 break;
349 case GB_MODEL_CGB:
350 case GB_MODEL_AGB:
351 strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
352 break;
353 default:
354 break;
355 };
356 bios = VFileOpen(path, O_RDONLY);
357 if (bios && GBIsBIOS(bios)) {
358 found = true;
359 } else if (bios) {
360 bios->close(bios);
361 bios = NULL;
362 }
363 }
364 if (bios) {
365 GBLoadBIOS(gb, bios);
366 }
367 }
368#endif
369
370 LR35902Reset(core->cpu);
371}
372
373static void _GBCoreRunFrame(struct mCore* core) {
374 struct GB* gb = core->board;
375 int32_t frameCounter = gb->video.frameCounter;
376 while (gb->video.frameCounter == frameCounter) {
377 LR35902Run(core->cpu);
378 }
379}
380
381static void _GBCoreRunLoop(struct mCore* core) {
382 LR35902Run(core->cpu);
383}
384
385static void _GBCoreStep(struct mCore* core) {
386 struct LR35902Core* cpu = core->cpu;
387 do {
388 LR35902Tick(cpu);
389 } while (cpu->executionState != LR35902_CORE_FETCH);
390}
391
392static size_t _GBCoreStateSize(struct mCore* core) {
393 UNUSED(core);
394 return sizeof(struct GBSerializedState);
395}
396
397static bool _GBCoreLoadState(struct mCore* core, const void* state) {
398 return GBDeserialize(core->board, state);
399}
400
401static bool _GBCoreSaveState(struct mCore* core, void* state) {
402 struct LR35902Core* cpu = core->cpu;
403 while (cpu->executionState != LR35902_CORE_FETCH) {
404 LR35902Tick(cpu);
405 }
406 GBSerialize(core->board, state);
407 return true;
408}
409
410static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
411 struct GBCore* gbcore = (struct GBCore*) core;
412 gbcore->keys = keys;
413}
414
415static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
416 struct GBCore* gbcore = (struct GBCore*) core;
417 gbcore->keys |= keys;
418}
419
420static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
421 struct GBCore* gbcore = (struct GBCore*) core;
422 gbcore->keys &= ~keys;
423}
424
425static int32_t _GBCoreFrameCounter(const struct mCore* core) {
426 const struct GB* gb = core->board;
427 return gb->video.frameCounter;
428}
429
430static int32_t _GBCoreFrameCycles(const struct mCore* core) {
431 UNUSED(core);
432 return GB_VIDEO_TOTAL_LENGTH;
433}
434
435static int32_t _GBCoreFrequency(const struct mCore* core) {
436 UNUSED(core);
437 // TODO: GB differences
438 return DMG_LR35902_FREQUENCY;
439}
440
441static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
442 GBGetGameTitle(core->board, title);
443}
444
445static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
446 GBGetGameCode(core->board, title);
447}
448
449static void _GBCoreSetPeripheral(struct mCore* core, int type, void* periph) {
450 struct GB* gb = core->board;
451 switch (type) {
452 case mPERIPH_ROTATION:
453 gb->memory.rotation = periph;
454 break;
455 case mPERIPH_RUMBLE:
456 gb->memory.rumble = periph;
457 break;
458 default:
459 return;
460 }
461}
462
463static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
464 struct LR35902Core* cpu = core->cpu;
465 return cpu->memory.load8(cpu, address);
466}
467
468static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
469 struct LR35902Core* cpu = core->cpu;
470 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
471}
472
473static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
474 struct LR35902Core* cpu = core->cpu;
475 return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
476 (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
477}
478
479static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
480 struct LR35902Core* cpu = core->cpu;
481 cpu->memory.store8(cpu, address, value);
482}
483
484static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
485 struct LR35902Core* cpu = core->cpu;
486 cpu->memory.store8(cpu, address, value);
487 cpu->memory.store8(cpu, address + 1, value >> 8);
488}
489
490static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
491 struct LR35902Core* cpu = core->cpu;
492 cpu->memory.store8(cpu, address, value);
493 cpu->memory.store8(cpu, address + 1, value >> 8);
494 cpu->memory.store8(cpu, address + 2, value >> 16);
495 cpu->memory.store8(cpu, address + 3, value >> 24);
496}
497
498static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
499 struct LR35902Core* cpu = core->cpu;
500 return GBView8(cpu, address, segment);
501}
502
503static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
504 struct LR35902Core* cpu = core->cpu;
505 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
506}
507
508static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
509 struct LR35902Core* cpu = core->cpu;
510 return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
511 (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
512}
513
514static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
515 struct LR35902Core* cpu = core->cpu;
516 GBPatch8(cpu, address, value, NULL, segment);
517}
518
519static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
520 struct LR35902Core* cpu = core->cpu;
521 GBPatch8(cpu, address, value, NULL, segment);
522 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
523}
524
525static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
526 struct LR35902Core* cpu = core->cpu;
527 GBPatch8(cpu, address, value, NULL, segment);
528 GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
529 GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
530 GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
531}
532
533#ifdef USE_DEBUGGERS
534static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
535 UNUSED(core);
536 switch (type) {
537 case DEBUGGER_CLI:
538 return true;
539 default:
540 return false;
541 }
542}
543
544static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
545 struct GBCore* gbcore = (struct GBCore*) core;
546 struct GB* gb = core->board;
547 if (!gbcore->debuggerPlatform) {
548 struct LR35902Debugger* platform = (struct LR35902Debugger*) LR35902DebuggerPlatformCreate();
549 if (gb->model >= GB_MODEL_CGB) {
550 platform->segments = _GBCSegments;
551 } else {
552 platform->segments = _GBSegments;
553 }
554 gbcore->debuggerPlatform = &platform->d;
555 }
556 return gbcore->debuggerPlatform;
557}
558
559static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
560 return GBCLIDebuggerCreate(core);
561}
562
563static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
564 struct LR35902Core* cpu = core->cpu;
565 if (core->debugger) {
566 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
567 }
568 cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
569 LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
570 core->debugger = debugger;
571}
572
573static void _GBCoreDetachDebugger(struct mCore* core) {
574 struct LR35902Core* cpu = core->cpu;
575 if (core->debugger) {
576 LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
577 }
578 cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
579 core->debugger = NULL;
580}
581
582static void _GBCoreLoadSymbols(struct mCore* core, struct VFile* vf) {
583 core->symbolTable = mDebuggerSymbolTableCreate();
584#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
585 if (!vf) {
586 vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
587 }
588#endif
589 if (!vf) {
590 return;
591 }
592 GBLoadSymbols(core->symbolTable, vf);
593}
594#endif
595
596static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
597 struct GBCore* gbcore = (struct GBCore*) core;
598 if (!gbcore->cheatDevice) {
599 gbcore->cheatDevice = GBCheatDeviceCreate();
600 ((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
601 LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
602 gbcore->cheatDevice->p = core;
603 }
604 return gbcore->cheatDevice;
605}
606
607static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
608 struct GB* gb = core->board;
609 struct VFile* vf = gb->sramVf;
610 if (vf) {
611 *sram = malloc(vf->size(vf));
612 vf->seek(vf, 0, SEEK_SET);
613 return vf->read(vf, *sram, vf->size(vf));
614 }
615 *sram = malloc(gb->sramSize);
616 memcpy(*sram, gb->memory.sram, gb->sramSize);
617 return gb->sramSize;
618}
619
620static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
621 struct GB* gb = core->board;
622 if (!writeback) {
623 struct VFile* vf = VFileMemChunk(sram, size);
624 GBSavedataMask(gb, vf, true);
625 return true;
626 }
627 struct VFile* vf = gb->sramVf;
628 if (vf) {
629 vf->seek(vf, 0, SEEK_SET);
630 return vf->write(vf, sram, size) > 0;
631 }
632 if (size > 0x20000) {
633 size = 0x20000;
634 }
635 GBResizeSram(gb, size);
636 memcpy(gb->memory.sram, sram, size);
637 return true;
638}
639
640static size_t _GBCoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
641 UNUSED(core);
642 *info = _GBVideoLayers;
643 return sizeof(_GBVideoLayers) / sizeof(*_GBVideoLayers);
644}
645
646static size_t _GBCoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
647 UNUSED(core);
648 *info = _GBAudioChannels;
649 return sizeof(_GBAudioChannels) / sizeof(*_GBAudioChannels);
650}
651
652static void _GBCoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
653 struct GB* gb = core->board;
654 switch (id) {
655 case 0:
656 gb->video.renderer->disableBG = !enable;
657 break;
658 case 1:
659 gb->video.renderer->disableOBJ = !enable;
660 break;
661 case 2:
662 gb->video.renderer->disableWIN = !enable;
663 break;
664 default:
665 break;
666 }
667}
668
669static void _GBCoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
670 struct GB* gb = core->board;
671 switch (id) {
672 case 0:
673 case 1:
674 case 2:
675 case 3:
676 gb->audio.forceDisableCh[id] = !enable;
677 break;
678 default:
679 break;
680 }
681}
682
683static void _GBCoreStartVideoLog(struct mCore* core, struct mVideoLogContext* context) {
684 struct GBCore* gbcore = (struct GBCore*) core;
685 struct GB* gb = core->board;
686 gbcore->logContext = context;
687
688 int channelId = mVideoLoggerAddChannel(context);
689 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
690 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, false);
691 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, context, channelId);
692 gbcore->proxyRenderer.logger->block = false;
693
694 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, &gbcore->renderer.d);
695 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
696}
697
698static void _GBCoreEndVideoLog(struct mCore* core) {
699 struct GBCore* gbcore = (struct GBCore*) core;
700 struct GB* gb = core->board;
701 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
702 free(gbcore->proxyRenderer.logger);
703 gbcore->proxyRenderer.logger = NULL;
704}
705
706struct mCore* GBCoreCreate(void) {
707 struct GBCore* gbcore = malloc(sizeof(*gbcore));
708 struct mCore* core = &gbcore->d;
709 memset(&core->opts, 0, sizeof(core->opts));
710 core->cpu = NULL;
711 core->board = NULL;
712 core->debugger = NULL;
713 core->symbolTable = NULL;
714 core->init = _GBCoreInit;
715 core->deinit = _GBCoreDeinit;
716 core->platform = _GBCorePlatform;
717 core->setSync = _GBCoreSetSync;
718 core->loadConfig = _GBCoreLoadConfig;
719 core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
720 core->setVideoBuffer = _GBCoreSetVideoBuffer;
721 core->getPixels = _GBCoreGetPixels;
722 core->putPixels = _GBCorePutPixels;
723 core->getAudioChannel = _GBCoreGetAudioChannel;
724 core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
725 core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
726 core->setAVStream = _GBCoreSetAVStream;
727 core->addCoreCallbacks = _GBCoreAddCoreCallbacks;
728 core->clearCoreCallbacks = _GBCoreClearCoreCallbacks;
729 core->isROM = GBIsROM;
730 core->loadROM = _GBCoreLoadROM;
731 core->loadBIOS = _GBCoreLoadBIOS;
732 core->loadSave = _GBCoreLoadSave;
733 core->loadTemporarySave = _GBCoreLoadTemporarySave;
734 core->loadPatch = _GBCoreLoadPatch;
735 core->unloadROM = _GBCoreUnloadROM;
736 core->checksum = _GBCoreChecksum;
737 core->reset = _GBCoreReset;
738 core->runFrame = _GBCoreRunFrame;
739 core->runLoop = _GBCoreRunLoop;
740 core->step = _GBCoreStep;
741 core->stateSize = _GBCoreStateSize;
742 core->loadState = _GBCoreLoadState;
743 core->saveState = _GBCoreSaveState;
744 core->setKeys = _GBCoreSetKeys;
745 core->addKeys = _GBCoreAddKeys;
746 core->clearKeys = _GBCoreClearKeys;
747 core->frameCounter = _GBCoreFrameCounter;
748 core->frameCycles = _GBCoreFrameCycles;
749 core->frequency = _GBCoreFrequency;
750 core->getGameTitle = _GBCoreGetGameTitle;
751 core->getGameCode = _GBCoreGetGameCode;
752 core->setPeripheral = _GBCoreSetPeripheral;
753 core->busRead8 = _GBCoreBusRead8;
754 core->busRead16 = _GBCoreBusRead16;
755 core->busRead32 = _GBCoreBusRead32;
756 core->busWrite8 = _GBCoreBusWrite8;
757 core->busWrite16 = _GBCoreBusWrite16;
758 core->busWrite32 = _GBCoreBusWrite32;
759 core->rawRead8 = _GBCoreRawRead8;
760 core->rawRead16 = _GBCoreRawRead16;
761 core->rawRead32 = _GBCoreRawRead32;
762 core->rawWrite8 = _GBCoreRawWrite8;
763 core->rawWrite16 = _GBCoreRawWrite16;
764 core->rawWrite32 = _GBCoreRawWrite32;
765#ifdef USE_DEBUGGERS
766 core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
767 core->debuggerPlatform = _GBCoreDebuggerPlatform;
768 core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
769 core->attachDebugger = _GBCoreAttachDebugger;
770 core->detachDebugger = _GBCoreDetachDebugger;
771 core->loadSymbols = _GBCoreLoadSymbols;
772#endif
773 core->cheatDevice = _GBCoreCheatDevice;
774 core->savedataClone = _GBCoreSavedataClone;
775 core->savedataRestore = _GBCoreSavedataRestore;
776 core->listVideoLayers = _GBCoreListVideoLayers;
777 core->listAudioChannels = _GBCoreListAudioChannels;
778 core->enableVideoLayer = _GBCoreEnableVideoLayer;
779 core->enableAudioChannel = _GBCoreEnableAudioChannel;
780#ifndef MINIMAL_CORE
781 core->startVideoLog = _GBCoreStartVideoLog;
782 core->endVideoLog = _GBCoreEndVideoLog;
783#endif
784 return core;
785}
786
787#ifndef MINIMAL_CORE
788static void _GBVLPStartFrameCallback(void *context) {
789 struct mCore* core = context;
790 struct GBCore* gbcore = (struct GBCore*) core;
791 struct GB* gb = core->board;
792
793 if (!mVideoLoggerRendererRun(gbcore->proxyRenderer.logger, true)) {
794 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
795 mVideoLogContextRewind(gbcore->logContext, core);
796 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
797 }
798}
799
800static bool _GBVLPInit(struct mCore* core) {
801 struct GBCore* gbcore = (struct GBCore*) core;
802 if (!_GBCoreInit(core)) {
803 return false;
804 }
805 gbcore->proxyRenderer.logger = malloc(sizeof(struct mVideoLogger));
806 mVideoLoggerRendererCreate(gbcore->proxyRenderer.logger, true);
807 GBVideoProxyRendererCreate(&gbcore->proxyRenderer, NULL);
808 memset(&gbcore->logCallbacks, 0, sizeof(gbcore->logCallbacks));
809 gbcore->logCallbacks.videoFrameStarted = _GBVLPStartFrameCallback;
810 gbcore->logCallbacks.context = core;
811 core->addCoreCallbacks(core, &gbcore->logCallbacks);
812 return true;
813}
814
815static void _GBVLPDeinit(struct mCore* core) {
816 struct GBCore* gbcore = (struct GBCore*) core;
817 if (gbcore->logContext) {
818 mVideoLogContextDestroy(core, gbcore->logContext);
819 }
820 _GBCoreDeinit(core);
821}
822
823static void _GBVLPReset(struct mCore* core) {
824 struct GBCore* gbcore = (struct GBCore*) core;
825 struct GB* gb = (struct GB*) core->board;
826 if (gb->video.renderer == &gbcore->proxyRenderer.d) {
827 GBVideoProxyRendererUnshim(&gb->video, &gbcore->proxyRenderer);
828 } else if (gbcore->renderer.outputBuffer) {
829 struct GBVideoRenderer* renderer = &gbcore->renderer.d;
830 GBVideoAssociateRenderer(&gb->video, renderer);
831 }
832
833 LR35902Reset(core->cpu);
834 mVideoLogContextRewind(gbcore->logContext, core);
835 GBVideoProxyRendererShim(&gb->video, &gbcore->proxyRenderer);
836
837 // Make sure CPU loop never spins
838 GBHalt(gb->cpu);
839 gb->memory.ie = 0;
840 gb->memory.ime = false;
841}
842
843static bool _GBVLPLoadROM(struct mCore* core, struct VFile* vf) {
844 struct GBCore* gbcore = (struct GBCore*) core;
845 gbcore->logContext = mVideoLogContextCreate(NULL);
846 if (!mVideoLogContextLoad(gbcore->logContext, vf)) {
847 mVideoLogContextDestroy(core, gbcore->logContext);
848 gbcore->logContext = NULL;
849 return false;
850 }
851 mVideoLoggerAttachChannel(gbcore->proxyRenderer.logger, gbcore->logContext, 0);
852 return true;
853}
854
855static bool _GBVLPLoadState(struct mCore* core, const void* buffer) {
856 struct GB* gb = (struct GB*) core->board;
857 const struct GBSerializedState* state = buffer;
858
859 gb->timing.root = NULL;
860 gb->model = state->model;
861
862 gb->cpu->pc = GB_BASE_HRAM;
863 gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
864
865 GBVideoDeserialize(&gb->video, state);
866 GBIODeserialize(gb, state);
867 GBAudioReset(&gb->audio);
868
869 // Make sure CPU loop never spins
870 GBHalt(gb->cpu);
871 gb->memory.ie = 0;
872 gb->memory.ime = false;
873
874 return true;
875}
876
877static bool _returnTrue(struct VFile* vf) {
878 UNUSED(vf);
879 return true;
880}
881
882struct mCore* GBVideoLogPlayerCreate(void) {
883 struct mCore* core = GBCoreCreate();
884 core->init = _GBVLPInit;
885 core->deinit = _GBVLPDeinit;
886 core->reset = _GBVLPReset;
887 core->loadROM = _GBVLPLoadROM;
888 core->loadState = _GBVLPLoadState;
889 core->isROM = _returnTrue;
890 return core;
891}
892#else
893struct mCore* GBVideoLogPlayerCreate(void) {
894 return false;
895}
896#endif