all repos — mgba @ d25ba2ec59b940940950aa1330fe563bced278bd

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