all repos — mgba @ 24f0be2862fe39219c4e2dcc615b0c76cbb700b6

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