all repos — mgba @ 5ea104844d58095e3dc002d93074d3302976cae6

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