all repos — mgba @ b6dbbb14abc159b9378353df2d978527f3414389

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