all repos — mgba @ 9dfcef3f45efe580e252ec8bc3852257e9de9fd6

mGBA Game Boy Advance Emulator

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