all repos — mgba @ e75cb6f7b4cdea9ee94c9af590172acb33a02ae6

mGBA Game Boy Advance Emulator

src/core/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/log.h"
  9#include "core/serialize.h"
 10#include "util/vfs.h"
 11
 12#ifdef M_CORE_GB
 13#include "gb/core.h"
 14#include "gb/gb.h"
 15#endif
 16#ifdef M_CORE_GBA
 17#include "gba/core.h"
 18#include "gba/gba.h"
 19#endif
 20#ifdef M_CORE_DS
 21#include "ds/core.h"
 22#include "ds/ds.h"
 23#endif
 24
 25static struct mCoreFilter {
 26	bool (*filter)(struct VFile*);
 27	struct mCore* (*open)(void);
 28	enum mPlatform platform;
 29} _filters[] = {
 30#ifdef M_CORE_GBA
 31	{ GBAIsROM, GBACoreCreate, PLATFORM_GBA },
 32#endif
 33#ifdef M_CORE_GB
 34	{ GBIsROM, GBCoreCreate, PLATFORM_GB },
 35#endif
 36#ifdef M_CORE_DS
 37	{ DSIsROM, DSCoreCreate, PLATFORM_DS },
 38#endif
 39	{ 0, 0, PLATFORM_NONE }
 40};
 41
 42struct mCore* mCoreFindVF(struct VFile* vf) {
 43	if (!vf) {
 44		return NULL;
 45	}
 46	struct mCoreFilter* filter;
 47	for (filter = &_filters[0]; filter->filter; ++filter) {
 48		if (filter->filter(vf)) {
 49			break;
 50		}
 51	}
 52	if (filter->open) {
 53		return filter->open();
 54	}
 55	return NULL;
 56}
 57
 58enum mPlatform mCoreIsCompatible(struct VFile* vf) {
 59	if (!vf) {
 60		return false;
 61	}
 62	struct mCoreFilter* filter;
 63	for (filter = &_filters[0]; filter->filter; ++filter) {
 64		if (filter->filter(vf)) {
 65			return filter->platform;
 66		}
 67	}
 68	return PLATFORM_NONE;
 69}
 70
 71#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 72#include "util/png-io.h"
 73
 74#ifdef PSP2
 75#include <psp2/photoexport.h>
 76#endif
 77
 78struct mCore* mCoreFind(const char* path) {
 79	struct VDir* archive = VDirOpenArchive(path);
 80	struct mCore* core = NULL;
 81	if (archive) {
 82		struct VDirEntry* dirent = archive->listNext(archive);
 83		while (dirent) {
 84			struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY);
 85			if (!vf) {
 86				dirent = archive->listNext(archive);
 87				continue;
 88			}
 89			core = mCoreFindVF(vf);
 90			vf->close(vf);
 91			if (core) {
 92				break;
 93			}
 94			dirent = archive->listNext(archive);
 95		}
 96		archive->close(archive);
 97	} else {
 98		struct VFile* vf = VFileOpen(path, O_RDONLY);
 99		if (!vf) {
100			return NULL;
101		}
102		core = mCoreFindVF(vf);
103		vf->close(vf);
104	}
105	if (core) {
106		return core;
107	}
108	return NULL;
109}
110
111bool mCoreLoadFile(struct mCore* core, const char* path) {
112	struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
113	if (!rom) {
114		return false;
115	}
116
117	bool ret = core->loadROM(core, rom);
118	if (!ret) {
119		rom->close(rom);
120	}
121	return ret;
122}
123
124bool mCoreAutoloadSave(struct mCore* core) {
125	return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
126}
127
128bool mCoreAutoloadPatch(struct mCore* core) {
129	return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
130	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
131	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
132}
133
134bool mCoreSaveState(struct mCore* core, int slot, int flags) {
135	struct VFile* vf = mCoreGetState(core, slot, true);
136	if (!vf) {
137		return false;
138	}
139	bool success = mCoreSaveStateNamed(core, vf, flags);
140	vf->close(vf);
141	if (success) {
142		mLOG(STATUS, INFO, "State %i saved", slot);
143	} else {
144		mLOG(STATUS, INFO, "State %i failed to save", slot);
145	}
146
147	return success;
148}
149
150bool mCoreLoadState(struct mCore* core, int slot, int flags) {
151	struct VFile* vf = mCoreGetState(core, slot, false);
152	if (!vf) {
153		return false;
154	}
155	bool success = mCoreLoadStateNamed(core, vf, flags);
156	vf->close(vf);
157	if (success) {
158		mLOG(STATUS, INFO, "State %i loaded", slot);
159	} else {
160		mLOG(STATUS, INFO, "State %i failed to loaded", slot);
161	}
162
163	return success;
164}
165
166struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
167	char name[PATH_MAX];
168	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
169	return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
170}
171
172void mCoreDeleteState(struct mCore* core, int slot) {
173	char name[PATH_MAX];
174	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
175	core->dirs.state->deleteFile(core->dirs.state, name);
176}
177
178void mCoreTakeScreenshot(struct mCore* core) {
179#ifdef USE_PNG
180	size_t stride;
181	const void* pixels = 0;
182	unsigned width, height;
183	core->desiredVideoDimensions(core, &width, &height);
184	struct VFile* vf;
185#ifndef PSP2
186	vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
187#else
188	vf = VFileMemChunk(0, 0);
189#endif
190	bool success = false;
191	if (vf) {
192		core->getPixels(core, &pixels, &stride);
193		png_structp png = PNGWriteOpen(vf);
194		png_infop info = PNGWriteHeader(png, width, height);
195		success = PNGWritePixels(png, width, height, stride, pixels);
196		PNGWriteClose(png, info);
197#ifdef PSP2
198		void* data = vf->map(vf, 0, 0);
199		PhotoExportParam param = {
200			0,
201			NULL,
202			NULL,
203			NULL,
204			{ 0 }
205		};
206		scePhotoExportFromData(data, vf->size(vf), &param, NULL, NULL, NULL, NULL, 0);
207#endif
208		vf->close(vf);
209	}
210	if (success) {
211		mLOG(STATUS, INFO, "Screenshot saved");
212		return;
213	}
214#else
215	UNUSED(core);
216#endif
217	mLOG(STATUS, WARN, "Failed to take screenshot");
218}
219#endif
220
221void mCoreInitConfig(struct mCore* core, const char* port) {
222	mCoreConfigInit(&core->config, port);
223}
224
225void mCoreLoadConfig(struct mCore* core) {
226#ifndef MINIMAL_CORE
227	mCoreConfigLoad(&core->config);
228#endif
229	mCoreLoadForeignConfig(core, &core->config);
230}
231
232void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
233	mCoreConfigMap(config, &core->opts);
234#ifndef MINIMAL_CORE
235	mDirectorySetMapOptions(&core->dirs, &core->opts);
236#endif
237	if (core->opts.audioBuffers) {
238		core->setAudioBufferSize(core, core->opts.audioBuffers);
239	}
240	core->loadConfig(core, config);
241}