all repos — mgba @ 9b0a5e566ec2de97baf5abbe0807922420e34006

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