all repos — mgba @ 458fd58985a23210cd288cc8c988b6b8707f8c14

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