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