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