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