all repos — mgba @ 6c67389e9c84846dbaac59f468d785984d854067

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 "gba/gba.h"
 11#include "gba/context/overrides.h"
 12#include "gba/renderers/video-software.h"
 13#include "gba/serialize.h"
 14#include "util/memory.h"
 15#include "util/patch.h"
 16#include "util/vfs.h"
 17
 18struct GBACore {
 19	struct mCore d;
 20	struct GBAVideoSoftwareRenderer renderer;
 21	int keys;
 22	struct mCPUComponent* components[GBA_COMPONENT_MAX];
 23	const struct Configuration* overrides;
 24};
 25
 26static bool _GBACoreInit(struct mCore* core) {
 27	struct GBACore* gbacore = (struct GBACore*) core;
 28
 29	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 30	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 31	if (!cpu || !gba) {
 32		free(cpu);
 33		free(gba);
 34		return false;
 35	}
 36	core->cpu = cpu;
 37	core->board = gba;
 38	gbacore->overrides = 0;
 39
 40	GBACreate(gba);
 41	// TODO: Restore debugger and cheats
 42	memset(gbacore->components, 0, sizeof(gbacore->components));
 43	ARMSetComponents(cpu, &gba->d, GBA_COMPONENT_MAX, gbacore->components);
 44	ARMInit(cpu);
 45
 46	GBAVideoSoftwareRendererCreate(&gbacore->renderer);
 47	gbacore->renderer.outputBuffer = NULL;
 48
 49	gbacore->keys = 0;
 50	gba->keySource = &gbacore->keys;
 51
 52#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 53	mDirectorySetInit(&core->dirs);
 54#endif
 55	
 56	return true;
 57}
 58
 59static void _GBACoreDeinit(struct mCore* core) {
 60	ARMDeinit(core->cpu);
 61	GBADestroy(core->board);
 62	mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
 63	mappedMemoryFree(core->board, sizeof(struct GBA));
 64#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 65	mDirectorySetDeinit(&core->dirs);
 66#endif
 67}
 68
 69static enum mPlatform _GBACorePlatform(struct mCore* core) {
 70	UNUSED(core);
 71	return PLATFORM_GBA;
 72}
 73
 74static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 75	struct GBA* gba = core->board;
 76	gba->sync = sync;
 77}
 78
 79static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 80	struct GBA* gba = core->board;
 81	if (core->opts.mute) {
 82		gba->audio.masterVolume = 0;
 83	} else {
 84		gba->audio.masterVolume = core->opts.volume;
 85	}
 86	gba->video.frameskip = core->opts.frameskip;
 87
 88#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 89	struct GBACore* gbacore = (struct GBACore*) core;
 90	gbacore->overrides = mCoreConfigGetOverridesConst(config);
 91
 92	struct VFile* bios = 0;
 93	if (core->opts.useBios && core->opts.bios) {
 94		bios = VFileOpen(core->opts.bios, O_RDONLY);
 95	}
 96	if (bios) {
 97		GBALoadBIOS(gba, bios);
 98	}
 99#endif
100
101	const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
102	if (idleOptimization) {
103		if (strcasecmp(idleOptimization, "ignore") == 0) {
104			gba->idleOptimization = IDLE_LOOP_IGNORE;
105		} else if (strcasecmp(idleOptimization, "remove") == 0) {
106			gba->idleOptimization = IDLE_LOOP_REMOVE;
107		} else if (strcasecmp(idleOptimization, "detect") == 0) {
108			gba->idleOptimization = IDLE_LOOP_DETECT;
109		}
110	}
111}
112
113static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
114	UNUSED(core);
115	*width = VIDEO_HORIZONTAL_PIXELS;
116	*height = VIDEO_VERTICAL_PIXELS;
117}
118
119static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
120	struct GBACore* gbacore = (struct GBACore*) core;
121	gbacore->renderer.outputBuffer = buffer;
122	gbacore->renderer.outputBufferStride = stride;
123}
124
125static void _GBACoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
126	struct GBACore* gbacore = (struct GBACore*) core;
127	*buffer = gbacore->renderer.outputBuffer;
128	*stride = gbacore->renderer.outputBufferStride;
129}
130
131static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
132	struct GBA* gba = core->board;
133	switch (ch) {
134	case 0:
135		return gba->audio.psg.left;
136	case 1:
137		return gba->audio.psg.right;
138	default:
139		return NULL;
140	}
141}
142
143static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
144	struct GBA* gba = core->board;
145	GBAAudioResizeBuffer(&gba->audio, samples);
146}
147
148static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
149	struct GBA* gba = core->board;
150	return gba->audio.samples;
151}
152
153static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
154	struct GBA* gba = core->board;
155	gba->stream = stream;
156	if (stream && stream->videoDimensionsChanged) {
157		stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
158	}
159}
160
161static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
162	return GBALoadROM(core->board, vf);
163}
164
165static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
166	UNUSED(type);
167	if (!GBAIsBIOS(vf)) {
168		return false;
169	}
170	GBALoadBIOS(core->board, vf);
171	return true;
172}
173
174static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
175	return GBALoadSave(core->board, vf);
176}
177
178static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
179	if (!vf) {
180		return false;
181	}
182	struct Patch patch;
183	if (!loadPatch(vf, &patch)) {
184		return false;
185	}
186	GBAApplyPatch(core->board, &patch);
187	return true;
188}
189
190static void _GBACoreUnloadROM(struct mCore* core) {
191	return GBAUnloadROM(core->board);
192}
193
194static void _GBACoreReset(struct mCore* core) {
195	struct GBACore* gbacore = (struct GBACore*) core;
196	struct GBA* gba = (struct GBA*) core->board;
197	if (gbacore->renderer.outputBuffer) {
198		GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
199	}
200	ARMReset(core->cpu);
201	if (core->opts.skipBios) {
202		GBASkipBIOS(core->board);
203	}
204
205	struct GBACartridgeOverride override;
206	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
207	memcpy(override.id, &cart->id, sizeof(override.id));
208	if (GBAOverrideFind(gbacore->overrides, &override)) {
209		GBAOverrideApply(gba, &override);
210	}
211}
212
213static void _GBACoreRunFrame(struct mCore* core) {
214	struct GBA* gba = core->board;
215	int32_t frameCounter = gba->video.frameCounter;
216	while (gba->video.frameCounter == frameCounter) {
217		ARMRunLoop(core->cpu);
218	}
219}
220
221static void _GBACoreRunLoop(struct mCore* core) {
222	ARMRunLoop(core->cpu);
223}
224
225static void _GBACoreStep(struct mCore* core) {
226	ARMRun(core->cpu);
227}
228
229static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
230	return GBALoadStateNamed(core->board, vf, flags);
231}
232
233static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
234	return GBASaveStateNamed(core->board, vf, flags);
235}
236
237static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
238	struct GBACore* gbacore = (struct GBACore*) core;
239	gbacore->keys = keys;
240}
241
242static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
243	struct GBACore* gbacore = (struct GBACore*) core;
244	gbacore->keys |= keys;
245}
246
247static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
248	struct GBACore* gbacore = (struct GBACore*) core;
249	gbacore->keys &= ~keys;
250}
251
252static int32_t _GBACoreFrameCounter(struct mCore* core) {
253	struct GBA* gba = core->board;
254	return gba->video.frameCounter;
255}
256
257static int32_t _GBACoreFrameCycles(struct mCore* core) {
258	UNUSED(core);
259	return VIDEO_TOTAL_LENGTH;
260}
261
262static int32_t _GBACoreFrequency(struct mCore* core) {
263	UNUSED(core);
264	return GBA_ARM7TDMI_FREQUENCY;
265}
266
267static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
268	GBAGetGameTitle(core->board, title);
269}
270
271static void _GBACoreGetGameCode(struct mCore* core, char* title) {
272	GBAGetGameCode(core->board, title);
273}
274
275static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
276	struct GBA* gba = core->board;
277	gba->rtcSource = rtc;
278}
279
280static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
281	struct GBA* gba = core->board;
282	gba->rotationSource = rotation;
283}
284
285static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
286	struct GBA* gba = core->board;
287	gba->rumble = rumble;
288}
289
290struct mCore* GBACoreCreate(void) {
291	struct GBACore* gbacore = malloc(sizeof(*gbacore));
292	struct mCore* core = &gbacore->d;
293	memset(&core->opts, 0, sizeof(core->opts));
294	core->cpu = 0;
295	core->board = 0;
296	core->init = _GBACoreInit;
297	core->deinit = _GBACoreDeinit;
298	core->platform = _GBACorePlatform;
299	core->setSync = _GBACoreSetSync;
300	core->loadConfig = _GBACoreLoadConfig;
301	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
302	core->setVideoBuffer = _GBACoreSetVideoBuffer;
303	core->getVideoBuffer = _GBACoreGetVideoBuffer;
304	core->getAudioChannel = _GBACoreGetAudioChannel;
305	core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
306	core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
307	core->setAVStream = _GBACoreSetAVStream;
308	core->isROM = GBAIsROM;
309	core->loadROM = _GBACoreLoadROM;
310	core->loadBIOS = _GBACoreLoadBIOS;
311	core->loadSave = _GBACoreLoadSave;
312	core->loadPatch = _GBACoreLoadPatch;
313	core->unloadROM = _GBACoreUnloadROM;
314	core->reset = _GBACoreReset;
315	core->runFrame = _GBACoreRunFrame;
316	core->runLoop = _GBACoreRunLoop;
317	core->step = _GBACoreStep;
318	core->loadState = _GBACoreLoadState;
319	core->saveState = _GBACoreSaveState;
320	core->setKeys = _GBACoreSetKeys;
321	core->addKeys = _GBACoreAddKeys;
322	core->clearKeys = _GBACoreClearKeys;
323	core->frameCounter = _GBACoreFrameCounter;
324	core->frameCycles = _GBACoreFrameCycles;
325	core->frequency = _GBACoreFrequency;
326	core->getGameTitle = _GBACoreGetGameTitle;
327	core->getGameCode = _GBACoreGetGameCode;
328	core->setRTC = _GBACoreSetRTC;
329	core->setRotation = _GBACoreSetRotation;
330	core->setRumble = _GBACoreSetRumble;
331	return core;
332}