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