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/cheats.h>
9#include <mgba/core/log.h>
10#include <mgba/core/serialize.h>
11#include <mgba-util/vfs.h>
12#include <mgba/internal/debugger/symbols.h>
13
14#ifdef USE_ELF
15#include <mgba-util/elf-read.h>
16#endif
17
18#ifdef M_CORE_GB
19#include <mgba/gb/core.h>
20// TODO: Fix layering violation
21#include <mgba/internal/gb/gb.h>
22#endif
23#ifdef M_CORE_GBA
24#include <mgba/gba/core.h>
25#include <mgba/internal/gba/gba.h>
26#endif
27#ifdef M_CORE_DS
28#include <mgba/ds/core.h>
29#include <mgba/internal/ds/ds.h>
30#endif
31#ifndef MINIMAL_CORE
32#include <mgba/feature/video-logger.h>
33#endif
34
35static const struct mCoreFilter {
36 bool (*filter)(struct VFile*);
37 struct mCore* (*open)(void);
38 enum mPlatform platform;
39} _filters[] = {
40#ifdef M_CORE_DS
41 { DSIsROM, DSCoreCreate, PLATFORM_DS },
42#endif
43#ifdef M_CORE_GBA
44 { GBAIsROM, GBACoreCreate, PLATFORM_GBA },
45#endif
46#ifdef M_CORE_GB
47 { GBIsROM, GBCoreCreate, PLATFORM_GB },
48#endif
49 { 0, 0, PLATFORM_NONE }
50};
51
52struct mCore* mCoreFindVF(struct VFile* vf) {
53 if (!vf) {
54 return NULL;
55 }
56 const struct mCoreFilter* filter;
57 for (filter = &_filters[0]; filter->filter; ++filter) {
58 if (filter->filter(vf)) {
59 break;
60 }
61 }
62 if (filter->open) {
63 return filter->open();
64 }
65#ifndef MINIMAL_CORE
66 return mVideoLogCoreFind(vf);
67#endif
68 return NULL;
69}
70
71enum mPlatform mCoreIsCompatible(struct VFile* vf) {
72 if (!vf) {
73 return false;
74 }
75 const struct mCoreFilter* filter;
76 for (filter = &_filters[0]; filter->filter; ++filter) {
77 if (filter->filter(vf)) {
78 return filter->platform;
79 }
80 }
81 return PLATFORM_NONE;
82}
83
84#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
85#include <mgba-util/png-io.h>
86
87#ifdef PSP2
88#include <psp2/photoexport.h>
89#endif
90
91struct mCore* mCoreFind(const char* path) {
92 struct VDir* archive = VDirOpenArchive(path);
93 struct mCore* core = NULL;
94 if (archive) {
95 struct VDirEntry* dirent = archive->listNext(archive);
96 while (dirent) {
97 struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY);
98 if (!vf) {
99 dirent = archive->listNext(archive);
100 continue;
101 }
102 core = mCoreFindVF(vf);
103 vf->close(vf);
104 if (core) {
105 break;
106 }
107 dirent = archive->listNext(archive);
108 }
109 archive->close(archive);
110 } else {
111 struct VFile* vf = VFileOpen(path, O_RDONLY);
112 if (!vf) {
113 return NULL;
114 }
115 core = mCoreFindVF(vf);
116 vf->close(vf);
117 }
118 if (core) {
119 return core;
120 }
121 return NULL;
122}
123
124bool mCoreLoadFile(struct mCore* core, const char* path) {
125 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
126 if (!rom) {
127 return false;
128 }
129
130 bool ret = core->loadROM(core, rom);
131 if (!ret) {
132 rom->close(rom);
133 }
134 return ret;
135}
136
137bool mCorePreloadVF(struct mCore* core, struct VFile* vf) {
138 struct VFile* vfm = VFileMemChunk(NULL, vf->size(vf));
139 uint8_t buffer[2048];
140 ssize_t read;
141 vf->seek(vf, 0, SEEK_SET);
142 while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
143 vfm->write(vfm, buffer, read);
144 }
145 vf->close(vf);
146 bool ret = core->loadROM(core, vfm);
147 if (!ret) {
148 vfm->close(vfm);
149 }
150 return ret;
151}
152
153bool mCorePreloadFile(struct mCore* core, const char* path) {
154 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
155 if (!rom) {
156 return false;
157 }
158
159 bool ret = mCorePreloadVF(core, rom);
160 if (!ret) {
161 rom->close(rom);
162 }
163 return ret;
164}
165
166bool mCoreAutoloadSave(struct mCore* core) {
167 return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
168}
169
170bool mCoreAutoloadPatch(struct mCore* core) {
171 return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
172 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
173 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
174}
175
176bool mCoreAutoloadCheats(struct mCore* core) {
177 bool success = true;
178 int cheatAuto;
179 if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
180 struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
181 if (vf) {
182 struct mCheatDevice* device = core->cheatDevice(core);
183 if (!device) {
184 success = false;
185 } else {
186 success = mCheatParseFile(device, vf);
187 }
188 vf->close(vf);
189 }
190 }
191 if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
192 struct mCheatDevice* device = core->cheatDevice(core);
193 if (device) {
194 device->autosave = true;
195 }
196 }
197 return success;
198}
199
200bool mCoreSaveState(struct mCore* core, int slot, int flags) {
201 struct VFile* vf = mCoreGetState(core, slot, true);
202 if (!vf) {
203 return false;
204 }
205 bool success = mCoreSaveStateNamed(core, vf, flags);
206 vf->close(vf);
207 if (success) {
208 mLOG(STATUS, INFO, "State %i saved", slot);
209 } else {
210 mLOG(STATUS, INFO, "State %i failed to save", slot);
211 }
212
213 return success;
214}
215
216bool mCoreLoadState(struct mCore* core, int slot, int flags) {
217 struct VFile* vf = mCoreGetState(core, slot, false);
218 if (!vf) {
219 return false;
220 }
221 bool success = mCoreLoadStateNamed(core, vf, flags);
222 vf->close(vf);
223 if (success) {
224 mLOG(STATUS, INFO, "State %i loaded", slot);
225 } else {
226 mLOG(STATUS, INFO, "State %i failed to load", slot);
227 }
228
229 return success;
230}
231
232struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
233 char name[PATH_MAX + 14]; // Quash warning
234 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
235 return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
236}
237
238void mCoreDeleteState(struct mCore* core, int slot) {
239 char name[PATH_MAX + 14]; // Quash warning
240 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
241 core->dirs.state->deleteFile(core->dirs.state, name);
242}
243
244void mCoreTakeScreenshot(struct mCore* core) {
245#ifdef USE_PNG
246 size_t stride;
247 const void* pixels = 0;
248 unsigned width, height;
249 core->desiredVideoDimensions(core, &width, &height);
250 struct VFile* vf;
251#ifndef PSP2
252 vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
253#else
254 vf = VFileMemChunk(0, 0);
255#endif
256 bool success = false;
257 if (vf) {
258 core->getPixels(core, &pixels, &stride);
259 png_structp png = PNGWriteOpen(vf);
260 png_infop info = PNGWriteHeader(png, width, height);
261 success = PNGWritePixels(png, width, height, stride, pixels);
262 PNGWriteClose(png, info);
263#ifdef PSP2
264 void* data = vf->map(vf, 0, 0);
265 PhotoExportParam param = {
266 0,
267 NULL,
268 NULL,
269 NULL,
270 { 0 }
271 };
272 scePhotoExportFromData(data, vf->size(vf), ¶m, NULL, NULL, NULL, NULL, 0);
273#endif
274 vf->close(vf);
275 }
276 if (success) {
277 mLOG(STATUS, INFO, "Screenshot saved");
278 return;
279 }
280#else
281 UNUSED(core);
282#endif
283 mLOG(STATUS, WARN, "Failed to take screenshot");
284}
285#endif
286
287void mCoreInitConfig(struct mCore* core, const char* port) {
288 mCoreConfigInit(&core->config, port);
289}
290
291void mCoreLoadConfig(struct mCore* core) {
292#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
293 mCoreConfigLoad(&core->config);
294#endif
295 mCoreLoadForeignConfig(core, &core->config);
296}
297
298void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
299 mCoreConfigMap(config, &core->opts);
300#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
301 mDirectorySetMapOptions(&core->dirs, &core->opts);
302#endif
303 if (core->opts.audioBuffers) {
304 core->setAudioBufferSize(core, core->opts.audioBuffers);
305 }
306
307 mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
308 mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
309
310 core->loadConfig(core, config);
311}
312
313void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
314 core->rtc.custom = rtc;
315 core->rtc.override = RTC_CUSTOM_START;
316}
317
318void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
319 const struct mCoreMemoryBlock* blocks;
320 size_t nBlocks = core->listMemoryBlocks(core, &blocks);
321 size_t i;
322 for (i = 0; i < nBlocks; ++i) {
323 if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
324 continue;
325 }
326 if (start < blocks[i].start) {
327 continue;
328 }
329 if (start >= blocks[i].start + blocks[i].size) {
330 continue;
331 }
332 uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
333 out += start - blocks[i].start;
334 *size -= start - blocks[i].start;
335 return out;
336 }
337 return NULL;
338}
339
340#ifdef USE_ELF
341bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
342 struct ELFProgramHeaders ph;
343 ELFProgramHeadersInit(&ph, 0);
344 ELFGetProgramHeaders(elf, &ph);
345 size_t i;
346 for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
347 size_t bsize, esize;
348 Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
349 void* block = mCoreGetMemoryBlock(core, phdr->p_paddr, &bsize);
350 char* bytes = ELFBytes(elf, &esize);
351 if (block && bsize >= phdr->p_filesz && esize >= phdr->p_filesz + phdr->p_offset) {
352 memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
353 } else {
354 return false;
355 }
356 }
357 return true;
358}
359
360#ifdef USE_DEBUGGERS
361void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
362 size_t symIndex = ELFFindSection(elf, ".symtab");
363 size_t names = ELFFindSection(elf, ".strtab");
364 Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
365 char* bytes = ELFBytes(elf, NULL);
366
367 Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
368 size_t i;
369 for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
370 if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
371 continue;
372 }
373 const char* name = ELFGetString(elf, names, syms[i].st_name);
374 if (name[0] == '$') {
375 continue;
376 }
377 mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
378 }
379}
380#endif
381#endif