all repos — mgba @ 498aa541fc660c8755cda99cf8889171b3bd5381

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