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 if (!core->dirs.save) {
161 return false;
162 }
163 return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
164}
165
166bool mCoreAutoloadPatch(struct mCore* core) {
167 if (!core->dirs.patch) {
168 return false;
169 }
170 return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
171 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
172 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
173}
174
175bool mCoreAutoloadCheats(struct mCore* core) {
176 bool success = true;
177 int cheatAuto;
178 if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
179 struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
180 if (vf) {
181 struct mCheatDevice* device = core->cheatDevice(core);
182 success = mCheatParseFile(device, vf);
183 vf->close(vf);
184 }
185 }
186 if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
187 struct mCheatDevice* device = core->cheatDevice(core);
188 device->autosave = true;
189 }
190 return success;
191}
192
193bool mCoreSaveState(struct mCore* core, int slot, int flags) {
194 struct VFile* vf = mCoreGetState(core, slot, true);
195 if (!vf) {
196 return false;
197 }
198 bool success = mCoreSaveStateNamed(core, vf, flags);
199 vf->close(vf);
200 if (success) {
201 mLOG(STATUS, INFO, "State %i saved", slot);
202 } else {
203 mLOG(STATUS, INFO, "State %i failed to save", slot);
204 }
205
206 return success;
207}
208
209bool mCoreLoadState(struct mCore* core, int slot, int flags) {
210 struct VFile* vf = mCoreGetState(core, slot, false);
211 if (!vf) {
212 return false;
213 }
214 bool success = mCoreLoadStateNamed(core, vf, flags);
215 vf->close(vf);
216 if (success) {
217 mLOG(STATUS, INFO, "State %i loaded", slot);
218 } else {
219 mLOG(STATUS, INFO, "State %i failed to load", slot);
220 }
221
222 return success;
223}
224
225struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
226 if (!core->dirs.state) {
227 return NULL;
228 }
229 char name[PATH_MAX + 14]; // Quash warning
230 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
231 return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
232}
233
234void mCoreDeleteState(struct mCore* core, int slot) {
235 char name[PATH_MAX + 14]; // Quash warning
236 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
237 core->dirs.state->deleteFile(core->dirs.state, name);
238}
239
240void mCoreTakeScreenshot(struct mCore* core) {
241#ifdef USE_PNG
242 size_t stride;
243 const void* pixels = 0;
244 unsigned width, height;
245 core->desiredVideoDimensions(core, &width, &height);
246 struct VFile* vf;
247#ifndef PSP2
248 vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
249#else
250 vf = VFileMemChunk(0, 0);
251#endif
252 bool success = false;
253 if (vf) {
254 core->getPixels(core, &pixels, &stride);
255 png_structp png = PNGWriteOpen(vf);
256 png_infop info = PNGWriteHeader(png, width, height);
257 success = PNGWritePixels(png, width, height, stride, pixels);
258 PNGWriteClose(png, info);
259#ifdef PSP2
260 void* data = vf->map(vf, 0, 0);
261 PhotoExportParam param = {
262 0,
263 NULL,
264 NULL,
265 NULL,
266 { 0 }
267 };
268 scePhotoExportFromData(data, vf->size(vf), ¶m, NULL, NULL, NULL, NULL, 0);
269#endif
270 vf->close(vf);
271 }
272 if (success) {
273 mLOG(STATUS, INFO, "Screenshot saved");
274 return;
275 }
276#else
277 UNUSED(core);
278#endif
279 mLOG(STATUS, WARN, "Failed to take screenshot");
280}
281#endif
282
283void mCoreInitConfig(struct mCore* core, const char* port) {
284 mCoreConfigInit(&core->config, port);
285}
286
287void mCoreLoadConfig(struct mCore* core) {
288#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
289 mCoreConfigLoad(&core->config);
290#endif
291 mCoreLoadForeignConfig(core, &core->config);
292}
293
294void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
295 mCoreConfigMap(config, &core->opts);
296#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
297 mDirectorySetMapOptions(&core->dirs, &core->opts);
298#endif
299 if (core->opts.audioBuffers) {
300 core->setAudioBufferSize(core, core->opts.audioBuffers);
301 }
302
303 mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
304 mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
305
306 core->loadConfig(core, config);
307}
308
309void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
310 core->rtc.custom = rtc;
311 core->rtc.override = RTC_CUSTOM_START;
312}
313
314void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
315 const struct mCoreMemoryBlock* blocks;
316 size_t nBlocks = core->listMemoryBlocks(core, &blocks);
317 size_t i;
318 for (i = 0; i < nBlocks; ++i) {
319 if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
320 continue;
321 }
322 if (start < blocks[i].start) {
323 continue;
324 }
325 if (start >= blocks[i].start + blocks[i].size) {
326 continue;
327 }
328 uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
329 out += start - blocks[i].start;
330 *size -= start - blocks[i].start;
331 return out;
332 }
333 return NULL;
334}
335
336#ifdef USE_ELF
337bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
338 struct ELFProgramHeaders ph;
339 ELFProgramHeadersInit(&ph, 0);
340 ELFGetProgramHeaders(elf, &ph);
341 size_t i;
342 for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
343 size_t bsize, esize;
344 Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
345 void* block = mCoreGetMemoryBlock(core, phdr->p_paddr, &bsize);
346 char* bytes = ELFBytes(elf, &esize);
347 if (block && bsize >= phdr->p_filesz && esize >= phdr->p_filesz + phdr->p_offset) {
348 memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
349 } else {
350 return false;
351 }
352 }
353 return true;
354}
355
356#ifdef USE_DEBUGGERS
357void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
358 size_t symIndex = ELFFindSection(elf, ".symtab");
359 size_t names = ELFFindSection(elf, ".strtab");
360 Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
361 char* bytes = ELFBytes(elf, NULL);
362
363 Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
364 size_t i;
365 for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
366 if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
367 continue;
368 }
369 const char* name = ELFGetString(elf, names, syms[i].st_name);
370 if (name[0] == '$') {
371 continue;
372 }
373 mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
374 }
375}
376#endif
377#endif