all repos — mgba @ fa884d071ecaa3e05ff20b45a67bf9500dd3d6b6

mGBA Game Boy Advance Emulator

src/gba/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/gba/core.h>
  7
  8#include <mgba/core/core.h>
  9#include <mgba/core/log.h>
 10#include <mgba/internal/arm/debugger/debugger.h>
 11#include <mgba/internal/gba/cheats.h>
 12#include <mgba/internal/gba/gba.h>
 13#include <mgba/internal/gba/extra/cli.h>
 14#include <mgba/internal/gba/overrides.h>
 15#ifndef DISABLE_THREADING
 16#include <mgba/internal/gba/renderers/thread-proxy.h>
 17#endif
 18#include <mgba/internal/gba/renderers/video-software.h>
 19#include <mgba/internal/gba/savedata.h>
 20#include <mgba/internal/gba/serialize.h>
 21#include <mgba-util/memory.h>
 22#include <mgba-util/patch.h>
 23#include <mgba-util/vfs.h>
 24
 25struct GBACore {
 26	struct mCore d;
 27	struct GBAVideoSoftwareRenderer renderer;
 28#ifndef DISABLE_THREADING
 29	struct GBAVideoThreadProxyRenderer threadProxy;
 30	int threadedVideo;
 31#endif
 32	int keys;
 33	struct mCPUComponent* components[CPU_COMPONENT_MAX];
 34	const struct Configuration* overrides;
 35	struct mDebuggerPlatform* debuggerPlatform;
 36	struct mCheatDevice* cheatDevice;
 37};
 38
 39static bool _GBACoreInit(struct mCore* core) {
 40	struct GBACore* gbacore = (struct GBACore*) core;
 41
 42	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 43	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 44	if (!cpu || !gba) {
 45		free(cpu);
 46		free(gba);
 47		return false;
 48	}
 49	core->cpu = cpu;
 50	core->board = gba;
 51	core->debugger = NULL;
 52	gbacore->overrides = NULL;
 53	gbacore->debuggerPlatform = NULL;
 54	gbacore->cheatDevice = NULL;
 55
 56	GBACreate(gba);
 57	// TODO: Restore cheats
 58	memset(gbacore->components, 0, sizeof(gbacore->components));
 59	ARMSetComponents(cpu, &gba->d, CPU_COMPONENT_MAX, gbacore->components);
 60	ARMInit(cpu);
 61
 62	GBAVideoSoftwareRendererCreate(&gbacore->renderer);
 63	gbacore->renderer.outputBuffer = NULL;
 64
 65#ifndef DISABLE_THREADING
 66	gbacore->threadedVideo = false;
 67	GBAVideoThreadProxyRendererCreate(&gbacore->threadProxy, &gbacore->renderer.d);
 68#endif
 69
 70	gbacore->keys = 0;
 71	gba->keySource = &gbacore->keys;
 72
 73#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 74	mDirectorySetInit(&core->dirs);
 75#endif
 76	
 77	return true;
 78}
 79
 80static void _GBACoreDeinit(struct mCore* core) {
 81	ARMDeinit(core->cpu);
 82	GBADestroy(core->board);
 83	mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
 84	mappedMemoryFree(core->board, sizeof(struct GBA));
 85#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 86	mDirectorySetDeinit(&core->dirs);
 87#endif
 88
 89	struct GBACore* gbacore = (struct GBACore*) core;
 90	free(gbacore->debuggerPlatform);
 91	if (gbacore->cheatDevice) {
 92		mCheatDeviceDestroy(gbacore->cheatDevice);
 93	}
 94	free(gbacore->cheatDevice);
 95	mCoreConfigFreeOpts(&core->opts);
 96	free(core);
 97}
 98
 99static enum mPlatform _GBACorePlatform(const struct mCore* core) {
100	UNUSED(core);
101	return PLATFORM_GBA;
102}
103
104static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
105	struct GBA* gba = core->board;
106	gba->sync = sync;
107}
108
109static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
110	struct GBA* gba = core->board;
111	if (core->opts.mute) {
112		gba->audio.masterVolume = 0;
113	} else {
114		gba->audio.masterVolume = core->opts.volume;
115	}
116	gba->video.frameskip = core->opts.frameskip;
117
118#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
119	struct GBACore* gbacore = (struct GBACore*) core;
120	gbacore->overrides = mCoreConfigGetOverridesConst(config);
121#endif
122
123	const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
124	if (idleOptimization) {
125		if (strcasecmp(idleOptimization, "ignore") == 0) {
126			gba->idleOptimization = IDLE_LOOP_IGNORE;
127		} else if (strcasecmp(idleOptimization, "remove") == 0) {
128			gba->idleOptimization = IDLE_LOOP_REMOVE;
129		} else if (strcasecmp(idleOptimization, "detect") == 0) {
130			if (gba->idleLoop == IDLE_LOOP_NONE) {
131				gba->idleOptimization = IDLE_LOOP_DETECT;
132			} else {
133				gba->idleOptimization = IDLE_LOOP_REMOVE;
134			}
135		}
136	}
137
138#ifndef DISABLE_THREADING
139	mCoreConfigGetIntValue(config, "threadedVideo", &gbacore->threadedVideo);
140#endif
141}
142
143static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
144	UNUSED(core);
145	*width = VIDEO_HORIZONTAL_PIXELS;
146	*height = VIDEO_VERTICAL_PIXELS;
147}
148
149static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
150	struct GBACore* gbacore = (struct GBACore*) core;
151	gbacore->renderer.outputBuffer = buffer;
152	gbacore->renderer.outputBufferStride = stride;
153}
154
155static void _GBACoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
156	struct GBACore* gbacore = (struct GBACore*) core;
157	gbacore->renderer.d.getPixels(&gbacore->renderer.d, stride, buffer);
158}
159
160static void _GBACorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
161	struct GBACore* gbacore = (struct GBACore*) core;
162	gbacore->renderer.d.putPixels(&gbacore->renderer.d, stride, buffer);
163}
164
165static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
166	struct GBA* gba = core->board;
167	switch (ch) {
168	case 0:
169		return gba->audio.psg.left;
170	case 1:
171		return gba->audio.psg.right;
172	default:
173		return NULL;
174	}
175}
176
177static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
178	struct GBA* gba = core->board;
179	GBAAudioResizeBuffer(&gba->audio, samples);
180}
181
182static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
183	struct GBA* gba = core->board;
184	return gba->audio.samples;
185}
186
187static void _GBACoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
188	struct GBA* gba = core->board;
189	gba->coreCallbacks = coreCallbacks;
190}
191
192static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
193	struct GBA* gba = core->board;
194	gba->stream = stream;
195	if (stream && stream->videoDimensionsChanged) {
196		stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
197	}
198}
199
200static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
201	return GBALoadROM(core->board, vf);
202}
203
204static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
205	UNUSED(type);
206	if (!GBAIsBIOS(vf)) {
207		return false;
208	}
209	GBALoadBIOS(core->board, vf);
210	return true;
211}
212
213static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
214	return GBALoadSave(core->board, vf);
215}
216
217static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
218	struct GBA* gba = core->board;
219	GBASavedataMask(&gba->memory.savedata, vf, false);
220	return true; // TODO: Return a real value
221}
222
223static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
224	if (!vf) {
225		return false;
226	}
227	struct Patch patch;
228	if (!loadPatch(vf, &patch)) {
229		return false;
230	}
231	GBAApplyPatch(core->board, &patch);
232	return true;
233}
234
235static void _GBACoreUnloadROM(struct mCore* core) {
236	struct GBACore* gbacore = (struct GBACore*) core;
237	struct ARMCore* cpu = core->cpu;
238	if (gbacore->cheatDevice) {
239		ARMHotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
240		cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
241		mCheatDeviceDestroy(gbacore->cheatDevice);
242		gbacore->cheatDevice = NULL;
243	}
244	return GBAUnloadROM(core->board);
245}
246
247static void _GBACoreReset(struct mCore* core) {
248	struct GBACore* gbacore = (struct GBACore*) core;
249	struct GBA* gba = (struct GBA*) core->board;
250	if (gbacore->renderer.outputBuffer) {
251		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
252#ifndef DISABLE_THREADING
253		if (gbacore->threadedVideo) {
254			renderer = &gbacore->threadProxy.d;
255		}
256#endif
257		GBAVideoAssociateRenderer(&gba->video, renderer);
258	}
259
260	struct GBACartridgeOverride override;
261	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
262	if (cart) {
263		memcpy(override.id, &cart->id, sizeof(override.id));
264		if (GBAOverrideFind(gbacore->overrides, &override)) {
265			GBAOverrideApply(gba, &override);
266		}
267	}
268
269#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
270	struct VFile* bios = 0;
271	if (core->opts.useBios) {
272		if (!core->opts.bios) {
273			char path[PATH_MAX];
274			mCoreConfigDirectory(path, PATH_MAX);
275			strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
276			bios = VFileOpen(path, O_RDONLY);
277		} else {
278			bios = VFileOpen(core->opts.bios, O_RDONLY);
279		}
280	}
281	if (bios) {
282		GBALoadBIOS(gba, bios);
283	}
284#endif
285
286	ARMReset(core->cpu);
287	if (core->opts.skipBios && gba->pristineRom) {
288		GBASkipBIOS(core->board);
289	}
290}
291
292static void _GBACoreRunFrame(struct mCore* core) {
293	struct GBA* gba = core->board;
294	int32_t frameCounter = gba->video.frameCounter;
295	while (gba->video.frameCounter == frameCounter) {
296		ARMRunLoop(core->cpu);
297	}
298}
299
300static void _GBACoreRunLoop(struct mCore* core) {
301	ARMRunLoop(core->cpu);
302}
303
304static void _GBACoreStep(struct mCore* core) {
305	ARMRun(core->cpu);
306}
307
308static size_t _GBACoreStateSize(struct mCore* core) {
309	UNUSED(core);
310	return sizeof(struct GBASerializedState);
311}
312
313static bool _GBACoreLoadState(struct mCore* core, const void* state) {
314	return GBADeserialize(core->board, state);
315}
316
317static bool _GBACoreSaveState(struct mCore* core, void* state) {
318	GBASerialize(core->board, state);
319	return true;
320}
321
322static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
323	struct GBACore* gbacore = (struct GBACore*) core;
324	gbacore->keys = keys;
325}
326
327static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
328	struct GBACore* gbacore = (struct GBACore*) core;
329	gbacore->keys |= keys;
330}
331
332static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
333	struct GBACore* gbacore = (struct GBACore*) core;
334	gbacore->keys &= ~keys;
335}
336
337static int32_t _GBACoreFrameCounter(const struct mCore* core) {
338	const struct GBA* gba = core->board;
339	return gba->video.frameCounter;
340}
341
342static int32_t _GBACoreFrameCycles(const struct mCore* core) {
343	UNUSED(core);
344	return VIDEO_TOTAL_LENGTH;
345}
346
347static int32_t _GBACoreFrequency(const struct mCore* core) {
348	UNUSED(core);
349	return GBA_ARM7TDMI_FREQUENCY;
350}
351
352static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
353	GBAGetGameTitle(core->board, title);
354}
355
356static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
357	GBAGetGameCode(core->board, title);
358}
359
360static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
361	struct GBA* gba = core->board;
362	gba->rtcSource = rtc;
363}
364
365static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
366	struct GBA* gba = core->board;
367	gba->rotationSource = rotation;
368}
369
370static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
371	struct GBA* gba = core->board;
372	gba->rumble = rumble;
373}
374
375static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
376	struct ARMCore* cpu = core->cpu;
377	return cpu->memory.load8(cpu, address, 0);
378}
379
380static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
381	struct ARMCore* cpu = core->cpu;
382	return cpu->memory.load16(cpu, address, 0);
383
384}
385
386static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
387	struct ARMCore* cpu = core->cpu;
388	return cpu->memory.load32(cpu, address, 0);
389}
390
391static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
392	struct ARMCore* cpu = core->cpu;
393	cpu->memory.store8(cpu, address, value, 0);
394}
395
396static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
397	struct ARMCore* cpu = core->cpu;
398	cpu->memory.store16(cpu, address, value, 0);
399}
400
401static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
402	struct ARMCore* cpu = core->cpu;
403	cpu->memory.store32(cpu, address, value, 0);
404}
405
406static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
407	UNUSED(segment);
408	struct ARMCore* cpu = core->cpu;
409	return GBAView8(cpu, address);
410}
411
412static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
413	UNUSED(segment);
414	struct ARMCore* cpu = core->cpu;
415	return GBAView16(cpu, address);
416}
417
418static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
419	UNUSED(segment);
420	struct ARMCore* cpu = core->cpu;
421	return GBAView32(cpu, address);
422}
423
424static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
425	UNUSED(segment);
426	struct ARMCore* cpu = core->cpu;
427	GBAPatch8(cpu, address, value, NULL);
428}
429
430static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
431	UNUSED(segment);
432	struct ARMCore* cpu = core->cpu;
433	GBAPatch16(cpu, address, value, NULL);
434}
435
436static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
437	UNUSED(segment);
438	struct ARMCore* cpu = core->cpu;
439	GBAPatch32(cpu, address, value, NULL);
440}
441
442#ifdef USE_DEBUGGERS
443static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
444	UNUSED(core);
445	switch (type) {
446	case DEBUGGER_CLI:
447		return true;
448#ifdef USE_GDB_STUB
449	case DEBUGGER_GDB:
450		return true;
451#endif
452	default:
453		return false;
454	}
455}
456
457static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
458	struct GBACore* gbacore = (struct GBACore*) core;
459	if (!gbacore->debuggerPlatform) {
460		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
461	}
462	return gbacore->debuggerPlatform;
463}
464
465static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
466	return &GBACLIDebuggerCreate(core)->d;
467}
468
469static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
470	if (core->debugger) {
471		GBADetachDebugger(core->board);
472	}
473	GBAAttachDebugger(core->board, debugger);
474	core->debugger = debugger;
475}
476
477static void _GBACoreDetachDebugger(struct mCore* core) {
478	GBADetachDebugger(core->board);
479	core->debugger = NULL;
480}
481#endif
482
483static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
484	struct GBACore* gbacore = (struct GBACore*) core;
485	if (!gbacore->cheatDevice) {
486		gbacore->cheatDevice = GBACheatDeviceCreate();
487		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
488		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
489		gbacore->cheatDevice->p = core;
490	}
491	return gbacore->cheatDevice;
492}
493
494static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
495	struct GBA* gba = core->board;
496	size_t size = GBASavedataSize(&gba->memory.savedata);
497	if (!size) {
498		*sram = NULL;
499		return 0;
500	}
501	*sram = malloc(size);
502	struct VFile* vf = VFileFromMemory(*sram, size);
503	if (!vf) {
504		free(*sram);
505		*sram = NULL;
506		return 0;
507	}
508	bool success = GBASavedataClone(&gba->memory.savedata, vf);
509	vf->close(vf);
510	if (!success) {
511		free(*sram);
512		*sram = NULL;
513		return 0;
514	}
515	return size;
516}
517
518static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
519	struct VFile* vf = VFileMemChunk(sram, size);
520	if (!vf) {
521		return false;
522	}
523	struct GBA* gba = core->board;
524	bool success = true;
525	if (writeback) {
526		success = GBASavedataLoad(&gba->memory.savedata, vf);
527		vf->close(vf);
528	} else {
529		GBASavedataMask(&gba->memory.savedata, vf, true);
530	}
531	return success;
532}
533
534struct mCore* GBACoreCreate(void) {
535	struct GBACore* gbacore = malloc(sizeof(*gbacore));
536	struct mCore* core = &gbacore->d;
537	memset(&core->opts, 0, sizeof(core->opts));
538	core->cpu = NULL;
539	core->board = NULL;
540	core->debugger = NULL;
541	core->init = _GBACoreInit;
542	core->deinit = _GBACoreDeinit;
543	core->platform = _GBACorePlatform;
544	core->setSync = _GBACoreSetSync;
545	core->loadConfig = _GBACoreLoadConfig;
546	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
547	core->setVideoBuffer = _GBACoreSetVideoBuffer;
548	core->getPixels = _GBACoreGetPixels;
549	core->putPixels = _GBACorePutPixels;
550	core->getAudioChannel = _GBACoreGetAudioChannel;
551	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
552	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
553	core->setCoreCallbacks = _GBACoreSetCoreCallbacks;
554	core->setAVStream = _GBACoreSetAVStream;
555	core->isROM = GBAIsROM;
556	core->loadROM = _GBACoreLoadROM;
557	core->loadBIOS = _GBACoreLoadBIOS;
558	core->loadSave = _GBACoreLoadSave;
559	core->loadTemporarySave = _GBACoreLoadTemporarySave;
560	core->loadPatch = _GBACoreLoadPatch;
561	core->unloadROM = _GBACoreUnloadROM;
562	core->reset = _GBACoreReset;
563	core->runFrame = _GBACoreRunFrame;
564	core->runLoop = _GBACoreRunLoop;
565	core->step = _GBACoreStep;
566	core->stateSize = _GBACoreStateSize;
567	core->loadState = _GBACoreLoadState;
568	core->saveState = _GBACoreSaveState;
569	core->setKeys = _GBACoreSetKeys;
570	core->addKeys = _GBACoreAddKeys;
571	core->clearKeys = _GBACoreClearKeys;
572	core->frameCounter = _GBACoreFrameCounter;
573	core->frameCycles = _GBACoreFrameCycles;
574	core->frequency = _GBACoreFrequency;
575	core->getGameTitle = _GBACoreGetGameTitle;
576	core->getGameCode = _GBACoreGetGameCode;
577	core->setRTC = _GBACoreSetRTC;
578	core->setRotation = _GBACoreSetRotation;
579	core->setRumble = _GBACoreSetRumble;
580	core->busRead8 = _GBACoreBusRead8;
581	core->busRead16 = _GBACoreBusRead16;
582	core->busRead32 = _GBACoreBusRead32;
583	core->busWrite8 = _GBACoreBusWrite8;
584	core->busWrite16 = _GBACoreBusWrite16;
585	core->busWrite32 = _GBACoreBusWrite32;
586	core->rawRead8 = _GBACoreRawRead8;
587	core->rawRead16 = _GBACoreRawRead16;
588	core->rawRead32 = _GBACoreRawRead32;
589	core->rawWrite8 = _GBACoreRawWrite8;
590	core->rawWrite16 = _GBACoreRawWrite16;
591	core->rawWrite32 = _GBACoreRawWrite32;
592#ifdef USE_DEBUGGERS
593	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
594	core->debuggerPlatform = _GBACoreDebuggerPlatform;
595	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
596	core->attachDebugger = _GBACoreAttachDebugger;
597	core->detachDebugger = _GBACoreDetachDebugger;
598#endif
599	core->cheatDevice = _GBACoreCheatDevice;
600	core->savedataClone = _GBACoreSavedataClone;
601	core->savedataRestore = _GBACoreSavedataRestore;
602	return core;
603}