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