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