all repos — mgba @ 91491e704e21343f8d7a908d1a8119cafecd6de2

mGBA Game Boy Advance Emulator

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