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#ifndef MINIMAL_CORE
22#include <mgba/feature/video-logger.h>
23#endif
24
25static const 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 { 0, 0, PLATFORM_NONE }
37};
38
39struct mCore* mCoreFindVF(struct VFile* vf) {
40 if (!vf) {
41 return NULL;
42 }
43 const struct mCoreFilter* filter;
44 for (filter = &_filters[0]; filter->filter; ++filter) {
45 if (filter->filter(vf)) {
46 break;
47 }
48 }
49 if (filter->open) {
50 return filter->open();
51 }
52#ifndef MINIMAL_CORE
53 return mVideoLogCoreFind(vf);
54#endif
55 return NULL;
56}
57
58enum mPlatform mCoreIsCompatible(struct VFile* vf) {
59 if (!vf) {
60 return false;
61 }
62 const 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 <mgba-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 mCorePreloadVF(struct mCore* core, struct VFile* vf) {
125 struct VFile* vfm = VFileMemChunk(NULL, vf->size(vf));
126 uint8_t buffer[2048];
127 ssize_t read;
128 vf->seek(vf, 0, SEEK_SET);
129 while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
130 vfm->write(vfm, buffer, read);
131 }
132 vf->close(vf);
133 bool ret = core->loadROM(core, vfm);
134 if (!ret) {
135 vfm->close(vfm);
136 }
137 return ret;
138}
139
140bool mCorePreloadFile(struct mCore* core, const char* path) {
141 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
142 if (!rom) {
143 return false;
144 }
145
146 bool ret = mCorePreloadVF(core, rom);
147 if (!ret) {
148 rom->close(rom);
149 }
150 return ret;
151}
152
153bool mCoreAutoloadSave(struct mCore* core) {
154 return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
155}
156
157bool mCoreAutoloadPatch(struct mCore* core) {
158 return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
159 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
160 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
161}
162
163bool mCoreSaveState(struct mCore* core, int slot, int flags) {
164 struct VFile* vf = mCoreGetState(core, slot, true);
165 if (!vf) {
166 return false;
167 }
168 bool success = mCoreSaveStateNamed(core, vf, flags);
169 vf->close(vf);
170 if (success) {
171 mLOG(STATUS, INFO, "State %i saved", slot);
172 } else {
173 mLOG(STATUS, INFO, "State %i failed to save", slot);
174 }
175
176 return success;
177}
178
179bool mCoreLoadState(struct mCore* core, int slot, int flags) {
180 struct VFile* vf = mCoreGetState(core, slot, false);
181 if (!vf) {
182 return false;
183 }
184 bool success = mCoreLoadStateNamed(core, vf, flags);
185 vf->close(vf);
186 if (success) {
187 mLOG(STATUS, INFO, "State %i loaded", slot);
188 } else {
189 mLOG(STATUS, INFO, "State %i failed to load", slot);
190 }
191
192 return success;
193}
194
195struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
196 char name[PATH_MAX];
197 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
198 return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
199}
200
201void mCoreDeleteState(struct mCore* core, int slot) {
202 char name[PATH_MAX];
203 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
204 core->dirs.state->deleteFile(core->dirs.state, name);
205}
206
207void mCoreTakeScreenshot(struct mCore* core) {
208#ifdef USE_PNG
209 size_t stride;
210 const void* pixels = 0;
211 unsigned width, height;
212 core->desiredVideoDimensions(core, &width, &height);
213 struct VFile* vf;
214#ifndef PSP2
215 vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
216#else
217 vf = VFileMemChunk(0, 0);
218#endif
219 bool success = false;
220 if (vf) {
221 core->getPixels(core, &pixels, &stride);
222 png_structp png = PNGWriteOpen(vf);
223 png_infop info = PNGWriteHeader(png, width, height);
224 success = PNGWritePixels(png, width, height, stride, pixels);
225 PNGWriteClose(png, info);
226#ifdef PSP2
227 void* data = vf->map(vf, 0, 0);
228 PhotoExportParam param = {
229 0,
230 NULL,
231 NULL,
232 NULL,
233 { 0 }
234 };
235 scePhotoExportFromData(data, vf->size(vf), ¶m, NULL, NULL, NULL, NULL, 0);
236#endif
237 vf->close(vf);
238 }
239 if (success) {
240 mLOG(STATUS, INFO, "Screenshot saved");
241 return;
242 }
243#else
244 UNUSED(core);
245#endif
246 mLOG(STATUS, WARN, "Failed to take screenshot");
247}
248#endif
249
250void mCoreInitConfig(struct mCore* core, const char* port) {
251 mCoreConfigInit(&core->config, port);
252}
253
254void mCoreLoadConfig(struct mCore* core) {
255#ifndef MINIMAL_CORE
256 mCoreConfigLoad(&core->config);
257#endif
258 mCoreLoadForeignConfig(core, &core->config);
259}
260
261void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
262 mCoreConfigMap(config, &core->opts);
263#ifndef MINIMAL_CORE
264 mDirectorySetMapOptions(&core->dirs, &core->opts);
265#endif
266 if (core->opts.audioBuffers) {
267 core->setAudioBufferSize(core, core->opts.audioBuffers);
268 }
269 core->loadConfig(core, config);
270}
271
272void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
273 core->rtc.custom = rtc;
274 core->rtc.override = RTC_CUSTOM_START;
275}