all repos — mgba @ e9fe4a1a9a7e005c8f846dc501798db8a628a574

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 ARMComponent* components[GBA_COMPONENT_MAX];
 23};
 24
 25static bool _GBACoreInit(struct mCore* core) {
 26	struct GBACore* gbacore = (struct GBACore*) core;
 27
 28	struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 29	struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
 30	if (!cpu || !gba) {
 31		free(cpu);
 32		free(gba);
 33		return false;
 34	}
 35	core->cpu = cpu;
 36	core->board = gba;
 37
 38	GBACreate(gba);
 39	// TODO: Restore debugger and cheats
 40	memset(gbacore->components, 0, sizeof(gbacore->components));
 41	ARMSetComponents(cpu, &gba->d, GBA_COMPONENT_MAX, gbacore->components);
 42	ARMInit(cpu);
 43
 44	GBAVideoSoftwareRendererCreate(&gbacore->renderer);
 45
 46	gba->keySource = &gbacore->keys;
 47
 48#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 49	mDirectorySetInit(&core->dirs);
 50#endif
 51	
 52	return true;
 53}
 54
 55static void _GBACoreDeinit(struct mCore* core) {
 56	ARMDeinit(core->cpu);
 57	GBADestroy(core->board);
 58	mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
 59	mappedMemoryFree(core->board, sizeof(struct GBA));
 60#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 61	mDirectorySetDeinit(&core->dirs);
 62#endif
 63}
 64
 65static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 66	struct GBA* gba = core->board;
 67	gba->sync = sync;
 68}
 69
 70static void _GBACoreLoadConfig(struct mCore* core) {
 71	struct GBA* gba = core->board;
 72
 73#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 74	struct VFile* bios = 0;
 75	if (core->opts.useBios) {
 76		bios = VFileOpen(core->opts.bios, O_RDONLY);
 77	}
 78	GBALoadBIOS(gba, bios);
 79#endif
 80
 81	const char* idleOptimization = mCoreConfigGetValue(&core->config, "idleOptimization");
 82	if (idleOptimization) {
 83		if (strcasecmp(idleOptimization, "ignore") == 0) {
 84			gba->idleOptimization = IDLE_LOOP_IGNORE;
 85		} else if (strcasecmp(idleOptimization, "remove") == 0) {
 86			gba->idleOptimization = IDLE_LOOP_REMOVE;
 87		} else if (strcasecmp(idleOptimization, "detect") == 0) {
 88			gba->idleOptimization = IDLE_LOOP_DETECT;
 89		}
 90	}
 91}
 92
 93static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 94	UNUSED(core);
 95	*width = VIDEO_HORIZONTAL_PIXELS;
 96	*height = VIDEO_VERTICAL_PIXELS;
 97}
 98
 99static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
100	struct GBACore* gbacore = (struct GBACore*) core;
101	gbacore->renderer.outputBuffer = buffer;
102	gbacore->renderer.outputBufferStride = stride;
103}
104
105static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
106	struct GBA* gba = core->board;
107	switch (ch) {
108	case 0:
109		return gba->audio.psg.left;
110	case 1:
111		return gba->audio.psg.right;
112	default:
113		return NULL;
114	}
115}
116
117static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
118	return GBALoadROM2(core->board, vf);
119}
120
121static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
122	UNUSED(type);
123	if (!GBAIsBIOS(vf)) {
124		return false;
125	}
126	GBALoadBIOS(core->board, vf);
127	return true;
128}
129
130static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
131	return GBALoadSave(core->board, vf);
132}
133
134static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
135	if (!vf) {
136		return false;
137	}
138	struct Patch patch;
139	if (!loadPatch(vf, &patch)) {
140		return false;
141	}
142	GBAApplyPatch(core->board, &patch);
143	return true;
144}
145
146static void _GBACoreUnloadROM(struct mCore* core) {
147	return GBAUnloadROM(core->board);
148}
149
150static void _GBACoreReset(struct mCore* core) {
151	struct GBACore* gbacore = (struct GBACore*) core;
152	struct GBA* gba = (struct GBA*) core->board;
153	GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
154	ARMReset(core->cpu);
155	if (core->opts.skipBios) {
156		GBASkipBIOS(core->board);
157	}
158
159	struct GBACartridgeOverride override;
160	const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
161	memcpy(override.id, &cart->id, sizeof(override.id));
162	struct Configuration* overrides = 0;
163#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
164	overrides = mCoreConfigGetOverrides(&core->config);
165#endif
166	if (GBAOverrideFind(overrides, &override)) {
167		GBAOverrideApply(gba, &override);
168	}
169}
170
171static void _GBACoreRunFrame(struct mCore* core) {
172	struct GBA* gba = core->board;
173	int32_t frameCounter = gba->video.frameCounter;
174	while (gba->video.frameCounter == frameCounter) {
175		ARMRunLoop(core->cpu);
176	}
177}
178
179static void _GBACoreRunLoop(struct mCore* core) {
180	ARMRunLoop(core->cpu);
181}
182
183static void _GBACoreStep(struct mCore* core) {
184	ARMRun(core->cpu);
185}
186
187static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
188	return GBALoadStateNamed(core->board, vf, flags);
189}
190
191static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
192	return GBASaveStateNamed(core->board, vf, flags);
193}
194
195static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
196	struct GBACore* gbacore = (struct GBACore*) core;
197	gbacore->keys = keys;
198}
199
200static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
201	struct GBACore* gbacore = (struct GBACore*) core;
202	gbacore->keys |= keys;
203}
204
205static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
206	struct GBACore* gbacore = (struct GBACore*) core;
207	gbacore->keys &= ~keys;
208}
209
210static int32_t _GBACoreFrameCounter(struct mCore* core) {
211	struct GBA* gba = core->board;
212	return gba->video.frameCounter;
213}
214
215static int32_t _GBACoreFrameCycles(struct mCore* core) {
216	UNUSED(core);
217	return VIDEO_TOTAL_LENGTH;
218}
219
220static int32_t _GBACoreFrequency(struct mCore* core) {
221	UNUSED(core);
222	return GBA_ARM7TDMI_FREQUENCY;
223}
224
225static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
226	struct GBA* gba = core->board;
227	gba->rtcSource = rtc;
228}
229
230struct mCore* GBACoreCreate(void) {
231	struct GBACore* gbacore = malloc(sizeof(*gbacore));
232	struct mCore* core = &gbacore->d;
233	memset(&core->opts, 0, sizeof(core->opts));
234	core->cpu = 0;
235	core->board = 0;
236	core->init = _GBACoreInit;
237	core->deinit = _GBACoreDeinit;
238	core->setSync = _GBACoreSetSync;
239	core->loadConfig = _GBACoreLoadConfig;
240	core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
241	core->setVideoBuffer = _GBACoreSetVideoBuffer;
242	core->getAudioChannel = _GBACoreGetAudioChannel;
243	core->isROM = GBAIsROM;
244	core->loadROM = _GBACoreLoadROM;
245	core->loadBIOS = _GBACoreLoadBIOS;
246	core->loadSave = _GBACoreLoadSave;
247	core->loadPatch = _GBACoreLoadPatch;
248	core->unloadROM = _GBACoreUnloadROM;
249	core->reset = _GBACoreReset;
250	core->runFrame = _GBACoreRunFrame;
251	core->runLoop = _GBACoreRunLoop;
252	core->step = _GBACoreStep;
253	core->loadState = _GBACoreLoadState;
254	core->saveState = _GBACoreSaveState;
255	core->setKeys = _GBACoreSetKeys;
256	core->addKeys = _GBACoreAddKeys;
257	core->clearKeys = _GBACoreClearKeys;
258	core->frameCounter = _GBACoreFrameCounter;
259	core->frameCycles = _GBACoreFrameCycles;
260	core->frequency = _GBACoreFrequency;
261	core->setRTC = _GBACoreSetRTC;
262	return core;
263}