all repos — mgba @ 193d2d1f4aa358b2454016d44cccae8316261b7f

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