all repos — mgba @ 56d83bee11a478605a2d8d761975ae74348e8328

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