all repos — mgba @ d232a538cc74fac4846ef3aa790eced33b61c919

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