all repos — mgba @ f491196bc4fcc6c5c51c1fecff1db78b2070bb81

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) {
375	struct ARMCore* cpu = core->cpu;
376	return GBAView8(cpu, address);
377}
378
379static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address) {
380	struct ARMCore* cpu = core->cpu;
381	return GBAView16(cpu, address);
382}
383
384static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address) {
385	struct ARMCore* cpu = core->cpu;
386	return GBAView32(cpu, address);
387}
388
389static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
390	struct ARMCore* cpu = core->cpu;
391	GBAPatch8(cpu, address, value, NULL);
392}
393
394static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
395	struct ARMCore* cpu = core->cpu;
396	GBAPatch16(cpu, address, value, NULL);
397}
398
399static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
400	struct ARMCore* cpu = core->cpu;
401	GBAPatch32(cpu, address, value, NULL);
402}
403
404static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
405	UNUSED(core);
406	switch (type) {
407#ifdef USE_CLI_DEBUGGER
408	case DEBUGGER_CLI:
409		return true;
410#endif
411#ifdef USE_GDB_STUB
412	case DEBUGGER_GDB:
413		return true;
414#endif
415	default:
416		return false;
417	}
418}
419
420static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
421	struct GBACore* gbacore = (struct GBACore*) core;
422	if (!gbacore->debuggerPlatform) {
423		gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
424	}
425	return gbacore->debuggerPlatform;
426}
427
428static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
429#ifdef USE_CLI_DEBUGGER
430	return &GBACLIDebuggerCreate(core)->d;
431#else
432	UNUSED(core);
433	return NULL;
434#endif
435}
436
437static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
438	if (core->debugger) {
439		GBADetachDebugger(core->board);
440	}
441	GBAAttachDebugger(core->board, debugger);
442	core->debugger = debugger;
443}
444
445static void _GBACoreDetachDebugger(struct mCore* core) {
446	GBADetachDebugger(core->board);
447	core->debugger = NULL;
448}
449
450static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
451	struct GBACore* gbacore = (struct GBACore*) core;
452	if (!gbacore->cheatDevice) {
453		gbacore->cheatDevice = GBACheatDeviceCreate();
454		((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
455		ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
456		gbacore->cheatDevice->p = core;
457	}
458	return gbacore->cheatDevice;
459}
460
461static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
462	struct GBA* gba = core->board;
463	size_t size = GBASavedataSize(&gba->memory.savedata);
464	if (!size) {
465		*sram = NULL;
466		return 0;
467	}
468	*sram = malloc(size);
469	struct VFile* vf = VFileFromMemory(*sram, size);
470	if (!vf) {
471		free(*sram);
472		*sram = NULL;
473		return 0;
474	}
475	bool success = GBASavedataClone(&gba->memory.savedata, vf);
476	vf->close(vf);
477	if (!success) {
478		free(*sram);
479		*sram = NULL;
480		return 0;
481	}
482	return size;
483}
484
485static bool _GBACoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
486	struct VFile* vf = VFileFromConstMemory(sram, size);
487	if (!vf) {
488		return false;
489	}
490	struct GBA* gba = core->board;
491	bool success = GBASavedataLoad(&gba->memory.savedata, vf);
492	vf->close(vf);
493	return success;
494}
495
496struct mCore* GBACoreCreate(void) {
497	struct GBACore* gbacore = malloc(sizeof(*gbacore));
498	struct mCore* core = &gbacore->d;
499	memset(&core->opts, 0, sizeof(core->opts));
500	core->cpu = NULL;
501	core->board = NULL;
502	core->debugger = NULL;
503	core->init = _GBACoreInit;
504	core->deinit = _GBACoreDeinit;
505	core->platform = _GBACorePlatform;
506	core->setSync = _GBACoreSetSync;
507	core->loadConfig = _GBACoreLoadConfig;
508	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
509	core->setVideoBuffer = _GBACoreSetVideoBuffer;
510	core->getVideoBuffer = _GBACoreGetVideoBuffer;
511	core->getAudioChannel = _GBACoreGetAudioChannel;
512	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
513	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
514	core->setAVStream = _GBACoreSetAVStream;
515	core->isROM = GBAIsROM;
516	core->loadROM = _GBACoreLoadROM;
517	core->loadBIOS = _GBACoreLoadBIOS;
518	core->loadSave = _GBACoreLoadSave;
519	core->loadTemporarySave = _GBACoreLoadTemporarySave;
520	core->loadPatch = _GBACoreLoadPatch;
521	core->unloadROM = _GBACoreUnloadROM;
522	core->reset = _GBACoreReset;
523	core->runFrame = _GBACoreRunFrame;
524	core->runLoop = _GBACoreRunLoop;
525	core->step = _GBACoreStep;
526	core->stateSize = _GBACoreStateSize;
527	core->loadState = _GBACoreLoadState;
528	core->saveState = _GBACoreSaveState;
529	core->setKeys = _GBACoreSetKeys;
530	core->addKeys = _GBACoreAddKeys;
531	core->clearKeys = _GBACoreClearKeys;
532	core->frameCounter = _GBACoreFrameCounter;
533	core->frameCycles = _GBACoreFrameCycles;
534	core->frequency = _GBACoreFrequency;
535	core->getGameTitle = _GBACoreGetGameTitle;
536	core->getGameCode = _GBACoreGetGameCode;
537	core->setRTC = _GBACoreSetRTC;
538	core->setRotation = _GBACoreSetRotation;
539	core->setRumble = _GBACoreSetRumble;
540	core->busRead8 = _GBACoreBusRead8;
541	core->busRead16 = _GBACoreBusRead16;
542	core->busRead32 = _GBACoreBusRead32;
543	core->busWrite8 = _GBACoreBusWrite8;
544	core->busWrite16 = _GBACoreBusWrite16;
545	core->busWrite32 = _GBACoreBusWrite32;
546	core->rawRead8 = _GBACoreRawRead8;
547	core->rawRead16 = _GBACoreRawRead16;
548	core->rawRead32 = _GBACoreRawRead32;
549	core->rawWrite8 = _GBACoreRawWrite8;
550	core->rawWrite16 = _GBACoreRawWrite16;
551	core->rawWrite32 = _GBACoreRawWrite32;
552	core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
553	core->debuggerPlatform = _GBACoreDebuggerPlatform;
554	core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
555	core->attachDebugger = _GBACoreAttachDebugger;
556	core->detachDebugger = _GBACoreDetachDebugger;
557	core->cheatDevice = _GBACoreCheatDevice;
558	core->savedataClone = _GBACoreSavedataClone;
559	core->savedataLoad = _GBACoreSavedataLoad;
560	return core;
561}