all repos — mgba @ 77ec5e6e91dec006cc9975315a04c5be40f4782b

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