all repos — mgba @ 52152a3a12f36f91ace52b31ab9f0c129b8cd19e

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