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 PLATFORM_NONE;
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#ifdef FIXED_ROM_BUFFER
118 return mCorePreloadFile(core, path);
119#else
120 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
121 if (!rom) {
122 return false;
123 }
124
125 bool ret = core->loadROM(core, rom);
126 if (!ret) {
127 rom->close(rom);
128 }
129 return ret;
130#endif
131}
132
133bool mCorePreloadVF(struct mCore* core, struct VFile* vf) {
134 return mCorePreloadVFCB(core, vf, NULL, NULL);
135}
136
137bool mCorePreloadFile(struct mCore* core, const char* path) {
138 return mCorePreloadFileCB(core, path, NULL, NULL);
139}
140
141bool mCorePreloadVFCB(struct mCore* core, struct VFile* vf, void (cb)(size_t, size_t, void*), void* context) {
142 struct VFile* vfm;
143 size_t size = vf->size(vf);
144
145#ifdef FIXED_ROM_BUFFER
146 extern uint32_t* romBuffer;
147 extern size_t romBufferSize;
148 if (size > romBufferSize) {
149 size = romBufferSize;
150 }
151 vfm = VFileFromMemory(romBuffer, size);
152#else
153 vfm = VFileMemChunk(NULL, size);
154#endif
155
156 uint8_t buffer[2048];
157 ssize_t read;
158 size_t total = 0;
159 vf->seek(vf, 0, SEEK_SET);
160 while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
161 vfm->write(vfm, buffer, read);
162 total += read;
163 if (cb) {
164 cb(total, size, context);
165 }
166 }
167 vf->close(vf);
168 if (read < 0) {
169 vfm->close(vfm);
170 return false;
171 }
172 bool ret = core->loadROM(core, vfm);
173 if (!ret) {
174 vfm->close(vfm);
175 }
176 return ret;
177}
178
179bool mCorePreloadFileCB(struct mCore* core, const char* path, void (cb)(size_t, size_t, void*), void* context) {
180 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
181 if (!rom) {
182 return false;
183 }
184
185 bool ret = mCorePreloadVFCB(core, rom, cb, context);
186 if (!ret) {
187 rom->close(rom);
188 }
189 return ret;
190}
191
192bool mCoreAutoloadSave(struct mCore* core) {
193 if (!core->dirs.save) {
194 return false;
195 }
196 return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
197}
198
199bool mCoreAutoloadPatch(struct mCore* core) {
200 if (!core->dirs.patch) {
201 return false;
202 }
203 return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
204 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
205 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
206}
207
208bool mCoreAutoloadCheats(struct mCore* core) {
209 bool success = true;
210 int cheatAuto;
211 if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
212 struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
213 if (vf) {
214 struct mCheatDevice* device = core->cheatDevice(core);
215 success = mCheatParseFile(device, vf);
216 vf->close(vf);
217 }
218 }
219 if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
220 struct mCheatDevice* device = core->cheatDevice(core);
221 device->autosave = true;
222 }
223 return success;
224}
225
226bool mCoreSaveState(struct mCore* core, int slot, int flags) {
227 struct VFile* vf = mCoreGetState(core, slot, true);
228 if (!vf) {
229 return false;
230 }
231 bool success = mCoreSaveStateNamed(core, vf, flags);
232 vf->close(vf);
233 if (success) {
234 mLOG(STATUS, INFO, "State %i saved", slot);
235 } else {
236 mLOG(STATUS, INFO, "State %i failed to save", slot);
237 }
238
239 return success;
240}
241
242bool mCoreLoadState(struct mCore* core, int slot, int flags) {
243 struct VFile* vf = mCoreGetState(core, slot, false);
244 if (!vf) {
245 return false;
246 }
247 bool success = mCoreLoadStateNamed(core, vf, flags);
248 vf->close(vf);
249 if (success) {
250 mLOG(STATUS, INFO, "State %i loaded", slot);
251 } else {
252 mLOG(STATUS, INFO, "State %i failed to load", slot);
253 }
254
255 return success;
256}
257
258struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
259 if (!core->dirs.state) {
260 return NULL;
261 }
262 char name[PATH_MAX + 14]; // Quash warning
263 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
264 return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
265}
266
267void mCoreDeleteState(struct mCore* core, int slot) {
268 char name[PATH_MAX + 14]; // Quash warning
269 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
270 core->dirs.state->deleteFile(core->dirs.state, name);
271}
272
273void mCoreTakeScreenshot(struct mCore* core) {
274#ifdef USE_PNG
275 size_t stride;
276 const void* pixels = 0;
277 unsigned width, height;
278 core->desiredVideoDimensions(core, &width, &height);
279 struct VFile* vf;
280#ifndef PSP2
281 vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
282#else
283 vf = VFileMemChunk(0, 0);
284#endif
285 bool success = false;
286 if (vf) {
287 core->getPixels(core, &pixels, &stride);
288 png_structp png = PNGWriteOpen(vf);
289 png_infop info = PNGWriteHeader(png, width, height);
290 success = PNGWritePixels(png, width, height, stride, pixels);
291 PNGWriteClose(png, info);
292#ifdef PSP2
293 void* data = vf->map(vf, 0, 0);
294 PhotoExportParam param = {
295 0,
296 NULL,
297 NULL,
298 NULL,
299 { 0 }
300 };
301 scePhotoExportFromData(data, vf->size(vf), ¶m, NULL, NULL, NULL, NULL, 0);
302#endif
303 vf->close(vf);
304 }
305 if (success) {
306 mLOG(STATUS, INFO, "Screenshot saved");
307 return;
308 }
309#else
310 UNUSED(core);
311#endif
312 mLOG(STATUS, WARN, "Failed to take screenshot");
313}
314#endif
315
316void mCoreInitConfig(struct mCore* core, const char* port) {
317 mCoreConfigInit(&core->config, port);
318}
319
320void mCoreLoadConfig(struct mCore* core) {
321#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
322 mCoreConfigLoad(&core->config);
323#endif
324 mCoreLoadForeignConfig(core, &core->config);
325}
326
327void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
328 mCoreConfigMap(config, &core->opts);
329#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
330 mDirectorySetMapOptions(&core->dirs, &core->opts);
331#endif
332 if (core->opts.audioBuffers) {
333 core->setAudioBufferSize(core, core->opts.audioBuffers);
334 }
335
336 mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
337 mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
338
339 core->loadConfig(core, config);
340}
341
342void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
343 core->rtc.custom = rtc;
344 core->rtc.override = RTC_CUSTOM_START;
345}
346
347void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
348 return mCoreGetMemoryBlockMasked(core, start, size, mCORE_MEMORY_MAPPED);
349}
350
351void* mCoreGetMemoryBlockMasked(struct mCore* core, uint32_t start, size_t* size, uint32_t mask) {
352 const struct mCoreMemoryBlock* blocks;
353 size_t nBlocks = core->listMemoryBlocks(core, &blocks);
354 size_t i;
355 for (i = 0; i < nBlocks; ++i) {
356 if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
357 continue;
358 }
359 if (!(blocks[i].flags & mask)) {
360 continue;
361 }
362 if (start < blocks[i].start) {
363 continue;
364 }
365 if (start >= blocks[i].start + blocks[i].size) {
366 continue;
367 }
368 uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
369 out += start - blocks[i].start;
370 *size -= start - blocks[i].start;
371 return out;
372 }
373 return NULL;
374}
375
376#ifdef USE_ELF
377bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
378 struct ELFProgramHeaders ph;
379 ELFProgramHeadersInit(&ph, 0);
380 ELFGetProgramHeaders(elf, &ph);
381 size_t i;
382 for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
383 size_t bsize, esize;
384 Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
385 void* block = mCoreGetMemoryBlockMasked(core, phdr->p_paddr, &bsize, mCORE_MEMORY_WRITE | mCORE_MEMORY_WORM);
386 char* bytes = ELFBytes(elf, &esize);
387 if (block && bsize >= phdr->p_filesz && esize > phdr->p_offset && esize >= phdr->p_filesz + phdr->p_offset) {
388 memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
389 } else {
390 return false;
391 }
392 }
393 return true;
394}
395
396#ifdef USE_DEBUGGERS
397void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
398 size_t symIndex = ELFFindSection(elf, ".symtab");
399 size_t names = ELFFindSection(elf, ".strtab");
400 Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
401 char* bytes = ELFBytes(elf, NULL);
402
403 Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
404 size_t i;
405 for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
406 if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
407 continue;
408 }
409 const char* name = ELFGetString(elf, names, syms[i].st_name);
410 if (name[0] == '$') {
411 continue;
412 }
413 mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
414 }
415}
416#endif
417#endif