all repos — mgba @ a71d1a13682aebc1be45be8bc8a96012e9c4730e

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	free(core);
 96}
 97
 98static enum mPlatform _GBACorePlatform(struct mCore* core) {
 99	UNUSED(core);
100	return PLATFORM_GBA;
101}
102
103static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
104	struct GBA* gba = core->board;
105	gba->sync = sync;
106}
107
108static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
109	struct GBA* gba = core->board;
110	if (core->opts.mute) {
111		gba->audio.masterVolume = 0;
112	} else {
113		gba->audio.masterVolume = core->opts.volume;
114	}
115	gba->video.frameskip = core->opts.frameskip;
116
117#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
118	struct GBACore* gbacore = (struct GBACore*) core;
119	gbacore->overrides = mCoreConfigGetOverridesConst(config);
120
121	struct VFile* bios = 0;
122	if (core->opts.useBios && core->opts.bios) {
123		bios = VFileOpen(core->opts.bios, O_RDONLY);
124	}
125	if (bios) {
126		GBALoadBIOS(gba, bios);
127	}
128#endif
129
130	const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
131	if (idleOptimization) {
132		if (strcasecmp(idleOptimization, "ignore") == 0) {
133			gba->idleOptimization = IDLE_LOOP_IGNORE;
134		} else if (strcasecmp(idleOptimization, "remove") == 0) {
135			gba->idleOptimization = IDLE_LOOP_REMOVE;
136		} else if (strcasecmp(idleOptimization, "detect") == 0) {
137			gba->idleOptimization = IDLE_LOOP_DETECT;
138		}
139	}
140
141#ifndef DISABLE_THREADING
142	mCoreConfigGetIntValue(config, "threadedVideo", &gbacore->threadedVideo);
143#endif
144}
145
146static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
147	UNUSED(core);
148	*width = VIDEO_HORIZONTAL_PIXELS;
149	*height = VIDEO_VERTICAL_PIXELS;
150}
151
152static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
153	struct GBACore* gbacore = (struct GBACore*) core;
154	gbacore->renderer.outputBuffer = buffer;
155	gbacore->renderer.outputBufferStride = stride;
156}
157
158static void _GBACoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
159	struct GBACore* gbacore = (struct GBACore*) core;
160	*buffer = gbacore->renderer.outputBuffer;
161	*stride = gbacore->renderer.outputBufferStride;
162}
163
164static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
165	struct GBA* gba = core->board;
166	switch (ch) {
167	case 0:
168		return gba->audio.psg.left;
169	case 1:
170		return gba->audio.psg.right;
171	default:
172		return NULL;
173	}
174}
175
176static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
177	struct GBA* gba = core->board;
178	GBAAudioResizeBuffer(&gba->audio, samples);
179}
180
181static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
182	struct GBA* gba = core->board;
183	return gba->audio.samples;
184}
185
186static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
187	struct GBA* gba = core->board;
188	gba->stream = stream;
189	if (stream && stream->videoDimensionsChanged) {
190		stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
191	}
192}
193
194static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
195	return GBALoadROM(core->board, vf);
196}
197
198static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
199	UNUSED(type);
200	if (!GBAIsBIOS(vf)) {
201		return false;
202	}
203	GBALoadBIOS(core->board, vf);
204	return true;
205}
206
207static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
208	return GBALoadSave(core->board, vf);
209}
210
211static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
212	GBASavedataMask(core->board, vf);
213	return true; // TODO: Return a real value
214}
215
216static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
217	if (!vf) {
218		return false;
219	}
220	struct Patch patch;
221	if (!loadPatch(vf, &patch)) {
222		return false;
223	}
224	GBAApplyPatch(core->board, &patch);
225	return true;
226}
227
228static void _GBACoreUnloadROM(struct mCore* core) {
229	return GBAUnloadROM(core->board);
230}
231
232static void _GBACoreReset(struct mCore* core) {
233	struct GBACore* gbacore = (struct GBACore*) core;
234	struct GBA* gba = (struct GBA*) core->board;
235	if (gbacore->renderer.outputBuffer) {
236		struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
237#ifndef DISABLE_THREADING
238		if (gbacore->threadedVideo) {
239			renderer = &gbacore->threadProxy.d;
240		}
241#endif
242		GBAVideoAssociateRenderer(&gba->video, renderer);
243	}
244	ARMReset(core->cpu);
245	if (core->opts.skipBios) {
246		GBASkipBIOS(core->board);
247	}
248
249	struct GBACartridgeOverride override;
250	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
251	if (cart) {
252		memcpy(override.id, &cart->id, sizeof(override.id));
253		if (GBAOverrideFind(gbacore->overrides, &override)) {
254			GBAOverrideApply(gba, &override);
255		}
256	}
257}
258
259static void _GBACoreRunFrame(struct mCore* core) {
260	struct GBA* gba = core->board;
261	int32_t frameCounter = gba->video.frameCounter;
262	while (gba->video.frameCounter == frameCounter) {
263		ARMRunLoop(core->cpu);
264	}
265}
266
267static void _GBACoreRunLoop(struct mCore* core) {
268	ARMRunLoop(core->cpu);
269}
270
271static void _GBACoreStep(struct mCore* core) {
272	ARMRun(core->cpu);
273}
274
275static size_t _GBACoreStateSize(struct mCore* core) {
276	UNUSED(core);
277	return sizeof(struct GBASerializedState);
278}
279
280static bool _GBACoreLoadState(struct mCore* core, const void* state) {
281	return GBADeserialize(core->board, state);
282}
283
284static bool _GBACoreSaveState(struct mCore* core, void* state) {
285	GBASerialize(core->board, state);
286	return true;
287}
288
289static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
290	struct GBACore* gbacore = (struct GBACore*) core;
291	gbacore->keys = keys;
292}
293
294static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
295	struct GBACore* gbacore = (struct GBACore*) core;
296	gbacore->keys |= keys;
297}
298
299static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
300	struct GBACore* gbacore = (struct GBACore*) core;
301	gbacore->keys &= ~keys;
302}
303
304static int32_t _GBACoreFrameCounter(struct mCore* core) {
305	struct GBA* gba = core->board;
306	return gba->video.frameCounter;
307}
308
309static int32_t _GBACoreFrameCycles(struct mCore* core) {
310	UNUSED(core);
311	return VIDEO_TOTAL_LENGTH;
312}
313
314static int32_t _GBACoreFrequency(struct mCore* core) {
315	UNUSED(core);
316	return GBA_ARM7TDMI_FREQUENCY;
317}
318
319static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
320	GBAGetGameTitle(core->board, title);
321}
322
323static void _GBACoreGetGameCode(struct mCore* core, char* title) {
324	GBAGetGameCode(core->board, title);
325}
326
327static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
328	struct GBA* gba = core->board;
329	gba->rtcSource = rtc;
330}
331
332static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
333	struct GBA* gba = core->board;
334	gba->rotationSource = rotation;
335}
336
337static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
338	struct GBA* gba = core->board;
339	gba->rumble = rumble;
340}
341
342static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
343	struct ARMCore* cpu = core->cpu;
344	return cpu->memory.load8(cpu, address, 0);
345}
346
347static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
348	struct ARMCore* cpu = core->cpu;
349	return cpu->memory.load16(cpu, address, 0);
350
351}
352
353static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
354	struct ARMCore* cpu = core->cpu;
355	return cpu->memory.load32(cpu, address, 0);
356}
357
358static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
359	struct ARMCore* cpu = core->cpu;
360	cpu->memory.store8(cpu, address, value, 0);
361}
362
363static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
364	struct ARMCore* cpu = core->cpu;
365	cpu->memory.store16(cpu, address, value, 0);
366}
367
368static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
369	struct ARMCore* cpu = core->cpu;
370	cpu->memory.store32(cpu, address, value, 0);
371}
372
373static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address) {
374	struct ARMCore* cpu = core->cpu;
375	return GBAView8(cpu, address);
376}
377
378static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address) {
379	struct ARMCore* cpu = core->cpu;
380	return GBAView16(cpu, address);
381}
382
383static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address) {
384	struct ARMCore* cpu = core->cpu;
385	return GBAView32(cpu, address);
386}
387
388static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
389	struct ARMCore* cpu = core->cpu;
390	GBAPatch8(cpu, address, value, NULL);
391}
392
393static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
394	struct ARMCore* cpu = core->cpu;
395	GBAPatch16(cpu, address, value, NULL);
396}
397
398static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
399	struct ARMCore* cpu = core->cpu;
400	GBAPatch32(cpu, address, value, NULL);
401}
402
403static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
404	UNUSED(core);
405	switch (type) {
406#ifdef USE_CLI_DEBUGGER
407	case DEBUGGER_CLI:
408		return true;
409#endif
410#ifdef USE_GDB_STUB
411	case DEBUGGER_GDB:
412		return true;
413#endif
414	default:
415		return false;
416	}
417}
418
419static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
420	struct GBACore* gbacore = (struct GBACore*) core;
421	if (!gbacore->debuggerPlatform) {
422		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
423	}
424	return gbacore->debuggerPlatform;
425}
426
427static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
428#ifdef USE_CLI_DEBUGGER
429	return &GBACLIDebuggerCreate(core)->d;
430#else
431	UNUSED(core);
432	return NULL;
433#endif
434}
435
436static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
437	if (core->debugger) {
438		GBADetachDebugger(core->board);
439	}
440	GBAAttachDebugger(core->board, debugger);
441	core->debugger = debugger;
442}
443
444static void _GBACoreDetachDebugger(struct mCore* core) {
445	GBADetachDebugger(core->board);
446	core->debugger = NULL;
447}
448
449static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
450	struct GBACore* gbacore = (struct GBACore*) core;
451	if (!gbacore->cheatDevice) {
452		gbacore->cheatDevice = GBACheatDeviceCreate();
453		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
454		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
455		gbacore->cheatDevice->p = core;
456	}
457	return gbacore->cheatDevice;
458}
459
460static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
461	struct GBA* gba = core->board;
462	size_t size = GBASavedataSize(&gba->memory.savedata);
463	if (!size) {
464		*sram = NULL;
465		return 0;
466	}
467	*sram = malloc(size);
468	struct VFile* vf = VFileFromMemory(*sram, size);
469	if (!vf) {
470		free(*sram);
471		*sram = NULL;
472		return 0;
473	}
474	bool success = GBASavedataClone(&gba->memory.savedata, vf);
475	vf->close(vf);
476	if (!success) {
477		free(*sram);
478		*sram = NULL;
479		return 0;
480	}
481	return size;
482}
483
484static bool _GBACoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
485	struct VFile* vf = VFileFromConstMemory(sram, size);
486	if (!vf) {
487		return false;
488	}
489	struct GBA* gba = core->board;
490	bool success = GBASavedataLoad(&gba->memory.savedata, vf);
491	vf->close(vf);
492	return success;
493}
494
495struct mCore* GBACoreCreate(void) {
496	struct GBACore* gbacore = malloc(sizeof(*gbacore));
497	struct mCore* core = &gbacore->d;
498	memset(&core->opts, 0, sizeof(core->opts));
499	core->cpu = NULL;
500	core->board = NULL;
501	core->debugger = NULL;
502	core->init = _GBACoreInit;
503	core->deinit = _GBACoreDeinit;
504	core->platform = _GBACorePlatform;
505	core->setSync = _GBACoreSetSync;
506	core->loadConfig = _GBACoreLoadConfig;
507	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
508	core->setVideoBuffer = _GBACoreSetVideoBuffer;
509	core->getVideoBuffer = _GBACoreGetVideoBuffer;
510	core->getAudioChannel = _GBACoreGetAudioChannel;
511	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
512	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
513	core->setAVStream = _GBACoreSetAVStream;
514	core->isROM = GBAIsROM;
515	core->loadROM = _GBACoreLoadROM;
516	core->loadBIOS = _GBACoreLoadBIOS;
517	core->loadSave = _GBACoreLoadSave;
518	core->loadTemporarySave = _GBACoreLoadTemporarySave;
519	core->loadPatch = _GBACoreLoadPatch;
520	core->unloadROM = _GBACoreUnloadROM;
521	core->reset = _GBACoreReset;
522	core->runFrame = _GBACoreRunFrame;
523	core->runLoop = _GBACoreRunLoop;
524	core->step = _GBACoreStep;
525	core->stateSize = _GBACoreStateSize;
526	core->loadState = _GBACoreLoadState;
527	core->saveState = _GBACoreSaveState;
528	core->setKeys = _GBACoreSetKeys;
529	core->addKeys = _GBACoreAddKeys;
530	core->clearKeys = _GBACoreClearKeys;
531	core->frameCounter = _GBACoreFrameCounter;
532	core->frameCycles = _GBACoreFrameCycles;
533	core->frequency = _GBACoreFrequency;
534	core->getGameTitle = _GBACoreGetGameTitle;
535	core->getGameCode = _GBACoreGetGameCode;
536	core->setRTC = _GBACoreSetRTC;
537	core->setRotation = _GBACoreSetRotation;
538	core->setRumble = _GBACoreSetRumble;
539	core->busRead8 = _GBACoreBusRead8;
540	core->busRead16 = _GBACoreBusRead16;
541	core->busRead32 = _GBACoreBusRead32;
542	core->busWrite8 = _GBACoreBusWrite8;
543	core->busWrite16 = _GBACoreBusWrite16;
544	core->busWrite32 = _GBACoreBusWrite32;
545	core->rawRead8 = _GBACoreRawRead8;
546	core->rawRead16 = _GBACoreRawRead16;
547	core->rawRead32 = _GBACoreRawRead32;
548	core->rawWrite8 = _GBACoreRawWrite8;
549	core->rawWrite16 = _GBACoreRawWrite16;
550	core->rawWrite32 = _GBACoreRawWrite32;
551	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
552	core->debuggerPlatform = _GBACoreDebuggerPlatform;
553	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
554	core->attachDebugger = _GBACoreAttachDebugger;
555	core->detachDebugger = _GBACoreDetachDebugger;
556	core->cheatDevice = _GBACoreCheatDevice;
557	core->savedataClone = _GBACoreSavedataClone;
558	core->savedataLoad = _GBACoreSavedataLoad;
559	return core;
560}