all repos — mgba @ 5425cff3e27be9ec09828cd7967bbe604eaa9e60

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(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 _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
188	struct GBA* gba = core->board;
189	gba->stream = stream;
190	if (stream && stream->videoDimensionsChanged) {
191		stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
192	}
193}
194
195static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
196	return GBALoadROM(core->board, vf);
197}
198
199static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
200	UNUSED(type);
201	if (!GBAIsBIOS(vf)) {
202		return false;
203	}
204	GBALoadBIOS(core->board, vf);
205	return true;
206}
207
208static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
209	return GBALoadSave(core->board, vf);
210}
211
212static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
213	struct GBA* gba = core->board;
214	GBASavedataMask(&gba->memory.savedata, vf);
215	return true; // TODO: Return a real value
216}
217
218static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
219	if (!vf) {
220		return false;
221	}
222	struct Patch patch;
223	if (!loadPatch(vf, &patch)) {
224		return false;
225	}
226	GBAApplyPatch(core->board, &patch);
227	return true;
228}
229
230static void _GBACoreUnloadROM(struct mCore* core) {
231	return GBAUnloadROM(core->board);
232}
233
234static void _GBACoreReset(struct mCore* core) {
235	struct GBACore* gbacore = (struct GBACore*) core;
236	struct GBA* gba = (struct GBA*) core->board;
237	if (gbacore->renderer.outputBuffer) {
238		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
239#ifndef DISABLE_THREADING
240		if (gbacore->threadedVideo) {
241			renderer = &gbacore->threadProxy.d;
242		}
243#endif
244		GBAVideoAssociateRenderer(&gba->video, renderer);
245	}
246
247	struct GBACartridgeOverride override;
248	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
249	if (cart) {
250		memcpy(override.id, &cart->id, sizeof(override.id));
251		if (GBAOverrideFind(gbacore->overrides, &override)) {
252			GBAOverrideApply(gba, &override);
253		}
254	}
255
256#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
257	struct VFile* bios = 0;
258	if (core->opts.useBios) {
259		if (!core->opts.bios) {
260			char path[PATH_MAX];
261			mCoreConfigDirectory(path, PATH_MAX);
262			strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
263			bios = VFileOpen(path, O_RDONLY);
264		} else {
265			bios = VFileOpen(core->opts.bios, O_RDONLY);
266		}
267	}
268	if (bios) {
269		GBALoadBIOS(gba, bios);
270	}
271#endif
272
273	ARMReset(core->cpu);
274	if (core->opts.skipBios && gba->pristineRom) {
275		GBASkipBIOS(core->board);
276	}
277}
278
279static void _GBACoreRunFrame(struct mCore* core) {
280	struct GBA* gba = core->board;
281	int32_t frameCounter = gba->video.frameCounter;
282	while (gba->video.frameCounter == frameCounter) {
283		ARMRunLoop(core->cpu);
284	}
285}
286
287static void _GBACoreRunLoop(struct mCore* core) {
288	ARMRunLoop(core->cpu);
289}
290
291static void _GBACoreStep(struct mCore* core) {
292	ARMRun(core->cpu);
293}
294
295static size_t _GBACoreStateSize(struct mCore* core) {
296	UNUSED(core);
297	return sizeof(struct GBASerializedState);
298}
299
300static bool _GBACoreLoadState(struct mCore* core, const void* state) {
301	return GBADeserialize(core->board, state);
302}
303
304static bool _GBACoreSaveState(struct mCore* core, void* state) {
305	GBASerialize(core->board, state);
306	return true;
307}
308
309static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
310	struct GBACore* gbacore = (struct GBACore*) core;
311	gbacore->keys = keys;
312}
313
314static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
315	struct GBACore* gbacore = (struct GBACore*) core;
316	gbacore->keys |= keys;
317}
318
319static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
320	struct GBACore* gbacore = (struct GBACore*) core;
321	gbacore->keys &= ~keys;
322}
323
324static int32_t _GBACoreFrameCounter(struct mCore* core) {
325	struct GBA* gba = core->board;
326	return gba->video.frameCounter;
327}
328
329static int32_t _GBACoreFrameCycles(struct mCore* core) {
330	UNUSED(core);
331	return VIDEO_TOTAL_LENGTH;
332}
333
334static int32_t _GBACoreFrequency(struct mCore* core) {
335	UNUSED(core);
336	return GBA_ARM7TDMI_FREQUENCY;
337}
338
339static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
340	GBAGetGameTitle(core->board, title);
341}
342
343static void _GBACoreGetGameCode(struct mCore* core, char* title) {
344	GBAGetGameCode(core->board, title);
345}
346
347static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
348	struct GBA* gba = core->board;
349	gba->rtcSource = rtc;
350}
351
352static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
353	struct GBA* gba = core->board;
354	gba->rotationSource = rotation;
355}
356
357static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
358	struct GBA* gba = core->board;
359	gba->rumble = rumble;
360}
361
362static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
363	struct ARMCore* cpu = core->cpu;
364	return cpu->memory.load8(cpu, address, 0);
365}
366
367static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
368	struct ARMCore* cpu = core->cpu;
369	return cpu->memory.load16(cpu, address, 0);
370
371}
372
373static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
374	struct ARMCore* cpu = core->cpu;
375	return cpu->memory.load32(cpu, address, 0);
376}
377
378static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
379	struct ARMCore* cpu = core->cpu;
380	cpu->memory.store8(cpu, address, value, 0);
381}
382
383static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
384	struct ARMCore* cpu = core->cpu;
385	cpu->memory.store16(cpu, address, value, 0);
386}
387
388static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
389	struct ARMCore* cpu = core->cpu;
390	cpu->memory.store32(cpu, address, value, 0);
391}
392
393static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
394	UNUSED(segment);
395	struct ARMCore* cpu = core->cpu;
396	return GBAView8(cpu, address);
397}
398
399static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
400	UNUSED(segment);
401	struct ARMCore* cpu = core->cpu;
402	return GBAView16(cpu, address);
403}
404
405static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
406	UNUSED(segment);
407	struct ARMCore* cpu = core->cpu;
408	return GBAView32(cpu, address);
409}
410
411static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
412	UNUSED(segment);
413	struct ARMCore* cpu = core->cpu;
414	GBAPatch8(cpu, address, value, NULL);
415}
416
417static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
418	UNUSED(segment);
419	struct ARMCore* cpu = core->cpu;
420	GBAPatch16(cpu, address, value, NULL);
421}
422
423static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
424	UNUSED(segment);
425	struct ARMCore* cpu = core->cpu;
426	GBAPatch32(cpu, address, value, NULL);
427}
428
429static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
430	UNUSED(core);
431	switch (type) {
432#ifdef USE_CLI_DEBUGGER
433	case DEBUGGER_CLI:
434		return true;
435#endif
436#ifdef USE_GDB_STUB
437	case DEBUGGER_GDB:
438		return true;
439#endif
440	default:
441		return false;
442	}
443}
444
445static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
446	struct GBACore* gbacore = (struct GBACore*) core;
447	if (!gbacore->debuggerPlatform) {
448		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
449	}
450	return gbacore->debuggerPlatform;
451}
452
453static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
454#ifdef USE_CLI_DEBUGGER
455	return &GBACLIDebuggerCreate(core)->d;
456#else
457	UNUSED(core);
458	return NULL;
459#endif
460}
461
462static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
463	if (core->debugger) {
464		GBADetachDebugger(core->board);
465	}
466	GBAAttachDebugger(core->board, debugger);
467	core->debugger = debugger;
468}
469
470static void _GBACoreDetachDebugger(struct mCore* core) {
471	GBADetachDebugger(core->board);
472	core->debugger = NULL;
473}
474
475static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
476	struct GBACore* gbacore = (struct GBACore*) core;
477	if (!gbacore->cheatDevice) {
478		gbacore->cheatDevice = GBACheatDeviceCreate();
479		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
480		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
481		gbacore->cheatDevice->p = core;
482	}
483	return gbacore->cheatDevice;
484}
485
486static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
487	struct GBA* gba = core->board;
488	size_t size = GBASavedataSize(&gba->memory.savedata);
489	if (!size) {
490		*sram = NULL;
491		return 0;
492	}
493	*sram = malloc(size);
494	struct VFile* vf = VFileFromMemory(*sram, size);
495	if (!vf) {
496		free(*sram);
497		*sram = NULL;
498		return 0;
499	}
500	bool success = GBASavedataClone(&gba->memory.savedata, vf);
501	vf->close(vf);
502	if (!success) {
503		free(*sram);
504		*sram = NULL;
505		return 0;
506	}
507	return size;
508}
509
510static bool _GBACoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
511	struct VFile* vf = VFileFromConstMemory(sram, size);
512	if (!vf) {
513		return false;
514	}
515	struct GBA* gba = core->board;
516	bool success = GBASavedataLoad(&gba->memory.savedata, vf);
517	vf->close(vf);
518	return success;
519}
520
521struct mCore* GBACoreCreate(void) {
522	struct GBACore* gbacore = malloc(sizeof(*gbacore));
523	struct mCore* core = &gbacore->d;
524	memset(&core->opts, 0, sizeof(core->opts));
525	core->cpu = NULL;
526	core->board = NULL;
527	core->debugger = NULL;
528	core->init = _GBACoreInit;
529	core->deinit = _GBACoreDeinit;
530	core->platform = _GBACorePlatform;
531	core->setSync = _GBACoreSetSync;
532	core->loadConfig = _GBACoreLoadConfig;
533	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
534	core->setVideoBuffer = _GBACoreSetVideoBuffer;
535	core->getPixels = _GBACoreGetPixels;
536	core->putPixels = _GBACorePutPixels;
537	core->getAudioChannel = _GBACoreGetAudioChannel;
538	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
539	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
540	core->setAVStream = _GBACoreSetAVStream;
541	core->isROM = GBAIsROM;
542	core->loadROM = _GBACoreLoadROM;
543	core->loadBIOS = _GBACoreLoadBIOS;
544	core->loadSave = _GBACoreLoadSave;
545	core->loadTemporarySave = _GBACoreLoadTemporarySave;
546	core->loadPatch = _GBACoreLoadPatch;
547	core->unloadROM = _GBACoreUnloadROM;
548	core->reset = _GBACoreReset;
549	core->runFrame = _GBACoreRunFrame;
550	core->runLoop = _GBACoreRunLoop;
551	core->step = _GBACoreStep;
552	core->stateSize = _GBACoreStateSize;
553	core->loadState = _GBACoreLoadState;
554	core->saveState = _GBACoreSaveState;
555	core->setKeys = _GBACoreSetKeys;
556	core->addKeys = _GBACoreAddKeys;
557	core->clearKeys = _GBACoreClearKeys;
558	core->frameCounter = _GBACoreFrameCounter;
559	core->frameCycles = _GBACoreFrameCycles;
560	core->frequency = _GBACoreFrequency;
561	core->getGameTitle = _GBACoreGetGameTitle;
562	core->getGameCode = _GBACoreGetGameCode;
563	core->setRTC = _GBACoreSetRTC;
564	core->setRotation = _GBACoreSetRotation;
565	core->setRumble = _GBACoreSetRumble;
566	core->busRead8 = _GBACoreBusRead8;
567	core->busRead16 = _GBACoreBusRead16;
568	core->busRead32 = _GBACoreBusRead32;
569	core->busWrite8 = _GBACoreBusWrite8;
570	core->busWrite16 = _GBACoreBusWrite16;
571	core->busWrite32 = _GBACoreBusWrite32;
572	core->rawRead8 = _GBACoreRawRead8;
573	core->rawRead16 = _GBACoreRawRead16;
574	core->rawRead32 = _GBACoreRawRead32;
575	core->rawWrite8 = _GBACoreRawWrite8;
576	core->rawWrite16 = _GBACoreRawWrite16;
577	core->rawWrite32 = _GBACoreRawWrite32;
578	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
579	core->debuggerPlatform = _GBACoreDebuggerPlatform;
580	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
581	core->attachDebugger = _GBACoreAttachDebugger;
582	core->detachDebugger = _GBACoreDetachDebugger;
583	core->cheatDevice = _GBACoreCheatDevice;
584	core->savedataClone = _GBACoreSavedataClone;
585	core->savedataLoad = _GBACoreSavedataLoad;
586	return core;
587}