all repos — mgba @ 5486e013e0665e3d13168d1500c7f99a4af0ee44

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