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