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