all repos — mgba @ 2a926e8dd50658b1761abb6f7f51f7a92fdd2696

mGBA Game Boy Advance Emulator

src/gba/context/context.c (view raw)

  1/* Copyright (c) 2013-2015 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 "gba/context/context.h"
  7
  8#include "gba/video.h"
  9#include "gba/context/overrides.h"
 10
 11#include "util/memory.h"
 12#include "util/vfs.h"
 13
 14static struct VFile* _logFile = 0;
 15static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 16
 17bool GBAContextInit(struct GBAContext* context, const char* port) {
 18	context->gba = anonymousMemoryMap(sizeof(struct GBA));
 19	context->cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 20	context->rom = 0;
 21	context->bios = 0;
 22	context->fname = 0;
 23	context->save = 0;
 24	context->renderer = 0;
 25#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 26	mDirectorySetInit(&context->dirs);
 27#endif
 28	memset(context->components, 0, sizeof(context->components));
 29
 30	if (!context->gba || !context->cpu) {
 31		if (context->gba) {
 32			mappedMemoryFree(context->gba, sizeof(struct GBA));
 33		}
 34		if (context->cpu) {
 35			mappedMemoryFree(context->cpu, sizeof(struct ARMCore));
 36		}
 37		return false;
 38	}
 39	GBACreate(context->gba);
 40	ARMSetComponents(context->cpu, &context->gba->d, GBA_COMPONENT_MAX, context->components);
 41	ARMInit(context->cpu);
 42
 43	mCoreConfigInit(&context->config, port);
 44#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 45	if (port) {
 46		if (!_logFile) {
 47			char logPath[PATH_MAX];
 48			mCoreConfigDirectory(logPath, PATH_MAX);
 49			strncat(logPath, PATH_SEP "log", PATH_MAX - strlen(logPath));
 50			_logFile = VFileOpen(logPath, O_WRONLY | O_CREAT | O_TRUNC);
 51		}
 52		context->gba->logHandler = _GBAContextLog;
 53
 54		char biosPath[PATH_MAX];
 55		mCoreConfigDirectory(biosPath, PATH_MAX);
 56		strncat(biosPath, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(biosPath));
 57
 58		struct mCoreOptions opts = {
 59			.bios = biosPath,
 60			.useBios = true,
 61			.logLevel = GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL | GBA_LOG_STATUS
 62		};
 63		mCoreConfigLoad(&context->config);
 64		mCoreConfigLoadDefaults(&context->config, &opts);
 65	}
 66#else
 67	UNUSED(port);
 68#endif
 69
 70	context->gba->sync = 0;
 71	return true;
 72}
 73
 74void GBAContextDeinit(struct GBAContext* context) {
 75	ARMDeinit(context->cpu);
 76	GBADestroy(context->gba);
 77	if (context->bios) {
 78		context->bios->close(context->bios);
 79		context->bios = 0;
 80	}
 81	mappedMemoryFree(context->gba, 0);
 82	mappedMemoryFree(context->cpu, 0);
 83	mCoreConfigDeinit(&context->config);
 84#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 85	mDirectorySetDeinit(&context->dirs);
 86#endif
 87}
 88
 89bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) {
 90#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 91	context->rom = mDirectorySetOpenPath(&context->dirs, path, GBAIsROM);
 92#else
 93	context->rom = VFileOpen(path, O_RDONLY);
 94#endif
 95	if (!context->rom) {
 96		return false;
 97	}
 98
 99	context->fname = path;
100#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
101	if (autoloadSave) {
102		char dirname[PATH_MAX];
103		char basename[PATH_MAX];
104		separatePath(context->fname, dirname, basename, 0);
105		mDirectorySetAttachBase(&context->dirs, VDirOpen(dirname));
106		strncat(basename, ".sav", PATH_MAX - strlen(basename) - 1);
107		context->save = context->dirs.save->openFile(context->dirs.save, basename, O_RDWR | O_CREAT);
108	}
109#else
110	UNUSED(autoloadSave);
111#endif
112	return true;
113}
114
115void GBAContextUnloadROM(struct GBAContext* context) {
116	GBAUnloadROM(context->gba);
117#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
118	mDirectorySetDetachBase(&context->dirs);
119#endif
120	if (context->rom) {
121		context->rom->close(context->rom);
122		context->rom = 0;
123	}
124	if (context->save) {
125		context->save->close(context->save);
126		context->save = 0;
127	}
128}
129
130bool GBAContextLoadROMFromVFile(struct GBAContext* context, struct VFile* rom, struct VFile* save) {
131	context->rom = rom;
132	if (!GBAIsROM(context->rom)) {
133		context->rom = 0;
134		return false;
135	}
136	context->save = save;
137	return true;
138}
139
140bool GBAContextLoadBIOS(struct GBAContext* context, const char* path) {
141	context->bios = VFileOpen(path, O_RDONLY);
142	if (!context->bios) {
143		return false;
144	}
145
146	if (!GBAIsBIOS(context->bios)) {
147		context->bios->close(context->bios);
148		context->bios = 0;
149		return false;
150	}
151	return true;
152}
153
154bool GBAContextLoadBIOSFromVFile(struct GBAContext* context, struct VFile* bios) {
155	context->bios = bios;
156	if (!GBAIsBIOS(context->bios)) {
157		context->bios = 0;
158		return false;
159	}
160	return true;
161}
162
163bool GBAContextStart(struct GBAContext* context) {
164	struct mCoreOptions opts = { .bios = 0 };
165
166	if (context->renderer) {
167		GBAVideoAssociateRenderer(&context->gba->video, context->renderer);
168	}
169
170	if (!GBALoadROM(context->gba, context->rom, context->save, context->fname)) {
171		return false;
172	}
173
174	mCoreConfigMap(&context->config, &opts);
175
176	if (!context->bios && opts.bios) {
177		GBAContextLoadBIOS(context, opts.bios);
178	}
179	if (opts.useBios && context->bios) {
180		GBALoadBIOS(context->gba, context->bios);
181	}
182	context->gba->logLevel = opts.logLevel;
183
184	GBAContextReset(context);
185
186	// TODO: Move this into GBAContextReset
187	if (opts.skipBios) {
188		GBASkipBIOS(context->gba);
189	}
190
191	struct GBACartridgeOverride override;
192	const struct GBACartridge* cart = (const struct GBACartridge*) context->gba->memory.rom;
193	memcpy(override.id, &cart->id, sizeof(override.id));
194	struct Configuration* overrides = 0;
195#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
196	overrides = mCoreConfigGetOverrides(&context->config);
197	mCoreConfigFreeOpts(&opts);
198#endif
199	if (GBAOverrideFind(overrides, &override)) {
200		GBAOverrideApply(context->gba, &override);
201	}
202	return true;
203}
204
205void GBAContextReset(struct GBAContext* context) {
206	ARMReset(context->cpu);
207}
208
209void GBAContextStop(struct GBAContext* context) {
210	UNUSED(context);
211	// TODO?
212}
213
214void GBAContextFrame(struct GBAContext* context, uint16_t keys) {
215	int activeKeys = keys;
216	context->gba->keySource = &activeKeys;
217
218	int frameCounter = context->gba->video.frameCounter;
219	while (frameCounter == context->gba->video.frameCounter) {
220		ARMRunLoop(context->cpu);
221	}
222}
223
224static void _GBAContextLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
225	UNUSED(thread);
226	UNUSED(level);
227	// TODO: Make this local
228	if (!_logFile) {
229		return;
230	}
231	char out[256];
232	size_t len = vsnprintf(out, sizeof(out), format, args);
233	if (len >= sizeof(out)) {
234		len = sizeof(out) - 1;
235	}
236	out[len] = '\n';
237	_logFile->write(_logFile, out, len + 1);
238}