all repos — mgba @ eccddde2834544da2ce62dfb3abe2c596207bd5f

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