all repos — mgba @ 8aee10486211a8b549dcc2a9d42a4174b2ded1a0

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 "core.h"
  7
  8#include "core/core.h"
  9#include "core/log.h"
 10#include "arm/debugger/debugger.h"
 11#include "gba/cheats.h"
 12#include "gba/gba.h"
 13#include "gba/extra/cli.h"
 14#include "gba/overrides.h"
 15#ifndef DISABLE_THREADING
 16#include "gba/renderers/thread-proxy.h"
 17#endif
 18#include "gba/renderers/video-software.h"
 19#include "gba/savedata.h"
 20#include "gba/serialize.h"
 21#include "util/memory.h"
 22#include "util/patch.h"
 23#include "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
442static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
443	UNUSED(core);
444	switch (type) {
445#ifdef USE_CLI_DEBUGGER
446	case DEBUGGER_CLI:
447		return true;
448#endif
449#ifdef USE_GDB_STUB
450	case DEBUGGER_GDB:
451		return true;
452#endif
453	default:
454		return false;
455	}
456}
457
458static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
459	struct GBACore* gbacore = (struct GBACore*) core;
460	if (!gbacore->debuggerPlatform) {
461		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
462	}
463	return gbacore->debuggerPlatform;
464}
465
466static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
467#ifdef USE_CLI_DEBUGGER
468	return &GBACLIDebuggerCreate(core)->d;
469#else
470	UNUSED(core);
471	return NULL;
472#endif
473}
474
475static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
476	if (core->debugger) {
477		GBADetachDebugger(core->board);
478	}
479	GBAAttachDebugger(core->board, debugger);
480	core->debugger = debugger;
481}
482
483static void _GBACoreDetachDebugger(struct mCore* core) {
484	GBADetachDebugger(core->board);
485	core->debugger = NULL;
486}
487
488static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
489	struct GBACore* gbacore = (struct GBACore*) core;
490	if (!gbacore->cheatDevice) {
491		gbacore->cheatDevice = GBACheatDeviceCreate();
492		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
493		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
494		gbacore->cheatDevice->p = core;
495	}
496	return gbacore->cheatDevice;
497}
498
499static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
500	struct GBA* gba = core->board;
501	size_t size = GBASavedataSize(&gba->memory.savedata);
502	if (!size) {
503		*sram = NULL;
504		return 0;
505	}
506	*sram = malloc(size);
507	struct VFile* vf = VFileFromMemory(*sram, size);
508	if (!vf) {
509		free(*sram);
510		*sram = NULL;
511		return 0;
512	}
513	bool success = GBASavedataClone(&gba->memory.savedata, vf);
514	vf->close(vf);
515	if (!success) {
516		free(*sram);
517		*sram = NULL;
518		return 0;
519	}
520	return size;
521}
522
523static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
524	struct VFile* vf = VFileMemChunk(sram, size);
525	if (!vf) {
526		return false;
527	}
528	struct GBA* gba = core->board;
529	bool success = true;
530	if (writeback) {
531		success = GBASavedataLoad(&gba->memory.savedata, vf);
532		vf->close(vf);
533	} else {
534		GBASavedataMask(&gba->memory.savedata, vf, true);
535	}
536	return success;
537}
538
539struct mCore* GBACoreCreate(void) {
540	struct GBACore* gbacore = malloc(sizeof(*gbacore));
541	struct mCore* core = &gbacore->d;
542	memset(&core->opts, 0, sizeof(core->opts));
543	core->cpu = NULL;
544	core->board = NULL;
545	core->debugger = NULL;
546	core->init = _GBACoreInit;
547	core->deinit = _GBACoreDeinit;
548	core->platform = _GBACorePlatform;
549	core->setSync = _GBACoreSetSync;
550	core->loadConfig = _GBACoreLoadConfig;
551	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
552	core->setVideoBuffer = _GBACoreSetVideoBuffer;
553	core->getPixels = _GBACoreGetPixels;
554	core->putPixels = _GBACorePutPixels;
555	core->getAudioChannel = _GBACoreGetAudioChannel;
556	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
557	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
558	core->setCoreCallbacks = _GBACoreSetCoreCallbacks;
559	core->setAVStream = _GBACoreSetAVStream;
560	core->isROM = GBAIsROM;
561	core->loadROM = _GBACoreLoadROM;
562	core->loadBIOS = _GBACoreLoadBIOS;
563	core->loadSave = _GBACoreLoadSave;
564	core->loadTemporarySave = _GBACoreLoadTemporarySave;
565	core->loadPatch = _GBACoreLoadPatch;
566	core->unloadROM = _GBACoreUnloadROM;
567	core->reset = _GBACoreReset;
568	core->runFrame = _GBACoreRunFrame;
569	core->runLoop = _GBACoreRunLoop;
570	core->step = _GBACoreStep;
571	core->stateSize = _GBACoreStateSize;
572	core->loadState = _GBACoreLoadState;
573	core->saveState = _GBACoreSaveState;
574	core->setKeys = _GBACoreSetKeys;
575	core->addKeys = _GBACoreAddKeys;
576	core->clearKeys = _GBACoreClearKeys;
577	core->frameCounter = _GBACoreFrameCounter;
578	core->frameCycles = _GBACoreFrameCycles;
579	core->frequency = _GBACoreFrequency;
580	core->getGameTitle = _GBACoreGetGameTitle;
581	core->getGameCode = _GBACoreGetGameCode;
582	core->setRTC = _GBACoreSetRTC;
583	core->setRotation = _GBACoreSetRotation;
584	core->setRumble = _GBACoreSetRumble;
585	core->busRead8 = _GBACoreBusRead8;
586	core->busRead16 = _GBACoreBusRead16;
587	core->busRead32 = _GBACoreBusRead32;
588	core->busWrite8 = _GBACoreBusWrite8;
589	core->busWrite16 = _GBACoreBusWrite16;
590	core->busWrite32 = _GBACoreBusWrite32;
591	core->rawRead8 = _GBACoreRawRead8;
592	core->rawRead16 = _GBACoreRawRead16;
593	core->rawRead32 = _GBACoreRawRead32;
594	core->rawWrite8 = _GBACoreRawWrite8;
595	core->rawWrite16 = _GBACoreRawWrite16;
596	core->rawWrite32 = _GBACoreRawWrite32;
597	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
598	core->debuggerPlatform = _GBACoreDebuggerPlatform;
599	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
600	core->attachDebugger = _GBACoreAttachDebugger;
601	core->detachDebugger = _GBACoreDetachDebugger;
602	core->cheatDevice = _GBACoreCheatDevice;
603	core->savedataClone = _GBACoreSavedataClone;
604	core->savedataRestore = _GBACoreSavedataRestore;
605	return core;
606}