mgba/src/core/core.c

368 lines
9.3 KiB
C
Raw Normal View History

2016-02-05 05:58:45 +00:00
/* Copyright (c) 2013-2016 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba/core/core.h>
2016-02-05 05:58:45 +00:00
2017-11-11 20:30:04 +00:00
#include <mgba/core/cheats.h>
#include <mgba/core/log.h>
#include <mgba/core/serialize.h>
#include <mgba-util/vfs.h>
2017-07-16 16:44:58 +00:00
#include <mgba/internal/debugger/symbols.h>
#ifdef USE_ELF
#include <mgba-util/elf-read.h>
#endif
2016-02-05 05:58:45 +00:00
2016-02-08 06:24:25 +00:00
#ifdef M_CORE_GB
#include <mgba/gb/core.h>
// TODO: Fix layering violation
#include <mgba/internal/gb/gb.h>
2016-02-08 06:24:25 +00:00
#endif
#ifdef M_CORE_GBA
#include <mgba/gba/core.h>
#include <mgba/internal/gba/gba.h>
2016-02-08 06:24:25 +00:00
#endif
2017-04-17 04:12:27 +00:00
#ifndef MINIMAL_CORE
#include <mgba/feature/video-logger.h>
2017-04-17 04:12:27 +00:00
#endif
2016-02-08 06:24:25 +00:00
static const struct mCoreFilter {
2016-02-08 06:24:25 +00:00
bool (*filter)(struct VFile*);
struct mCore* (*open)(void);
2016-08-27 07:56:25 +00:00
enum mPlatform platform;
2016-02-08 06:24:25 +00:00
} _filters[] = {
#ifdef M_CORE_GBA
2016-08-27 07:56:25 +00:00
{ GBAIsROM, GBACoreCreate, PLATFORM_GBA },
2016-02-08 06:24:25 +00:00
#endif
#ifdef M_CORE_GB
2016-08-27 07:56:25 +00:00
{ GBIsROM, GBCoreCreate, PLATFORM_GB },
2016-02-08 06:24:25 +00:00
#endif
2016-08-27 07:56:25 +00:00
{ 0, 0, PLATFORM_NONE }
2016-02-08 06:24:25 +00:00
};
2016-08-27 07:56:25 +00:00
struct mCore* mCoreFindVF(struct VFile* vf) {
if (!vf) {
return NULL;
}
2017-04-17 04:12:27 +00:00
const struct mCoreFilter* filter;
2016-08-27 07:56:25 +00:00
for (filter = &_filters[0]; filter->filter; ++filter) {
if (filter->filter(vf)) {
break;
}
}
if (filter->open) {
return filter->open();
}
2017-04-17 04:12:27 +00:00
#ifndef MINIMAL_CORE
return mVideoLogCoreFind(vf);
#endif
2016-08-27 07:56:25 +00:00
return NULL;
}
enum mPlatform mCoreIsCompatible(struct VFile* vf) {
if (!vf) {
return false;
}
2017-04-17 04:12:27 +00:00
const struct mCoreFilter* filter;
2016-08-27 07:56:25 +00:00
for (filter = &_filters[0]; filter->filter; ++filter) {
if (filter->filter(vf)) {
return filter->platform;
}
}
return PLATFORM_NONE;
}
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
#include <mgba-util/png-io.h>
2016-08-27 07:56:25 +00:00
#ifdef PSP2
#include <psp2/photoexport.h>
#endif
2016-02-08 06:24:25 +00:00
struct mCore* mCoreFind(const char* path) {
struct VDir* archive = VDirOpenArchive(path);
2016-08-27 07:56:25 +00:00
struct mCore* core = NULL;
2016-02-08 06:24:25 +00:00
if (archive) {
struct VDirEntry* dirent = archive->listNext(archive);
while (dirent) {
struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY);
if (!vf) {
dirent = archive->listNext(archive);
continue;
}
2016-08-27 07:56:25 +00:00
core = mCoreFindVF(vf);
2016-02-08 06:24:25 +00:00
vf->close(vf);
2016-08-27 07:56:25 +00:00
if (core) {
2016-02-08 06:24:25 +00:00
break;
}
dirent = archive->listNext(archive);
}
archive->close(archive);
} else {
struct VFile* vf = VFileOpen(path, O_RDONLY);
if (!vf) {
return NULL;
}
2016-08-27 07:56:25 +00:00
core = mCoreFindVF(vf);
2016-02-08 06:24:25 +00:00
vf->close(vf);
}
2016-08-27 07:56:25 +00:00
if (core) {
return core;
2016-02-08 06:24:25 +00:00
}
return NULL;
}
2016-02-05 05:58:45 +00:00
bool mCoreLoadFile(struct mCore* core, const char* path) {
struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
if (!rom) {
return false;
}
bool ret = core->loadROM(core, rom);
if (!ret) {
rom->close(rom);
}
return ret;
}
bool mCorePreloadVF(struct mCore* core, struct VFile* vf) {
struct VFile* vfm = VFileMemChunk(NULL, vf->size(vf));
uint8_t buffer[2048];
ssize_t read;
vf->seek(vf, 0, SEEK_SET);
while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
vfm->write(vfm, buffer, read);
}
vf->close(vf);
bool ret = core->loadROM(core, vfm);
if (!ret) {
vfm->close(vfm);
}
return ret;
}
bool mCorePreloadFile(struct mCore* core, const char* path) {
struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
if (!rom) {
return false;
}
bool ret = mCorePreloadVF(core, rom);
if (!ret) {
rom->close(rom);
}
return ret;
}
2016-02-05 05:58:45 +00:00
bool mCoreAutoloadSave(struct mCore* core) {
2016-02-17 06:17:21 +00:00
return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
2016-02-05 05:58:45 +00:00
}
bool mCoreAutoloadPatch(struct mCore* core) {
2016-02-17 06:17:21 +00:00
return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
2016-02-05 05:58:45 +00:00
}
2017-11-11 20:30:04 +00:00
bool mCoreAutoloadCheats(struct mCore* core) {
bool success = true;
int cheatAuto;
if (!mCoreConfigGetIntValue(&core->config, "cheatAutoload", &cheatAuto) || cheatAuto) {
struct VFile* vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.cheats, ".cheats", O_RDONLY);
if (vf) {
struct mCheatDevice* device = core->cheatDevice(core);
success = mCheatParseFile(device, vf);
vf->close(vf);
}
}
if (!mCoreConfigGetIntValue(&core->config, "cheatAutosave", &cheatAuto) || cheatAuto) {
struct mCheatDevice* device = core->cheatDevice(core);
device->autosave = true;
}
return success;
}
2016-02-05 05:58:45 +00:00
bool mCoreSaveState(struct mCore* core, int slot, int flags) {
struct VFile* vf = mCoreGetState(core, slot, true);
if (!vf) {
return false;
}
2016-05-30 22:01:40 +00:00
bool success = mCoreSaveStateNamed(core, vf, flags);
2016-02-05 05:58:45 +00:00
vf->close(vf);
if (success) {
mLOG(STATUS, INFO, "State %i saved", slot);
} else {
mLOG(STATUS, INFO, "State %i failed to save", slot);
}
return success;
}
bool mCoreLoadState(struct mCore* core, int slot, int flags) {
struct VFile* vf = mCoreGetState(core, slot, false);
if (!vf) {
return false;
}
2016-05-30 22:01:40 +00:00
bool success = mCoreLoadStateNamed(core, vf, flags);
2016-02-05 05:58:45 +00:00
vf->close(vf);
if (success) {
mLOG(STATUS, INFO, "State %i loaded", slot);
} else {
2017-03-21 00:22:49 +00:00
mLOG(STATUS, INFO, "State %i failed to load", slot);
2016-02-05 05:58:45 +00:00
}
return success;
}
struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
char name[PATH_MAX];
snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
}
void mCoreDeleteState(struct mCore* core, int slot) {
char name[PATH_MAX];
snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
core->dirs.state->deleteFile(core->dirs.state, name);
}
2016-02-07 23:29:02 +00:00
void mCoreTakeScreenshot(struct mCore* core) {
#ifdef USE_PNG
size_t stride;
const void* pixels = 0;
2016-02-07 23:29:02 +00:00
unsigned width, height;
core->desiredVideoDimensions(core, &width, &height);
struct VFile* vf;
#ifndef PSP2
vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
#else
vf = VFileMemChunk(0, 0);
#endif
2016-02-07 23:29:02 +00:00
bool success = false;
if (vf) {
core->getPixels(core, &pixels, &stride);
2016-02-07 23:29:02 +00:00
png_structp png = PNGWriteOpen(vf);
png_infop info = PNGWriteHeader(png, width, height);
success = PNGWritePixels(png, width, height, stride, pixels);
PNGWriteClose(png, info);
#ifdef PSP2
void* data = vf->map(vf, 0, 0);
PhotoExportParam param = {
0,
NULL,
NULL,
NULL,
{ 0 }
};
scePhotoExportFromData(data, vf->size(vf), &param, NULL, NULL, NULL, NULL, 0);
#endif
2016-02-07 23:29:02 +00:00
vf->close(vf);
}
if (success) {
mLOG(STATUS, INFO, "Screenshot saved");
return;
}
#else
UNUSED(core);
#endif
mLOG(STATUS, WARN, "Failed to take screenshot");
}
2016-02-05 05:58:45 +00:00
#endif
void mCoreInitConfig(struct mCore* core, const char* port) {
mCoreConfigInit(&core->config, port);
}
void mCoreLoadConfig(struct mCore* core) {
2018-01-08 01:01:08 +00:00
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
2016-02-05 05:58:45 +00:00
mCoreConfigLoad(&core->config);
#endif
2016-02-08 04:07:08 +00:00
mCoreLoadForeignConfig(core, &core->config);
}
void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
mCoreConfigMap(config, &core->opts);
2018-01-08 01:01:08 +00:00
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
2016-02-05 05:58:45 +00:00
mDirectorySetMapOptions(&core->dirs, &core->opts);
#endif
2016-02-15 14:33:13 +00:00
if (core->opts.audioBuffers) {
core->setAudioBufferSize(core, core->opts.audioBuffers);
}
2017-11-11 20:30:04 +00:00
mCoreConfigCopyValue(&core->config, config, "cheatAutosave");
mCoreConfigCopyValue(&core->config, config, "cheatAutoload");
2016-02-08 04:07:08 +00:00
core->loadConfig(core, config);
2016-02-05 05:58:45 +00:00
}
void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
core->rtc.custom = rtc;
core->rtc.override = RTC_CUSTOM_START;
}
2017-07-16 16:44:58 +00:00
void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) {
const struct mCoreMemoryBlock* blocks;
size_t nBlocks = core->listMemoryBlocks(core, &blocks);
size_t i;
for (i = 0; i < nBlocks; ++i) {
if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) {
continue;
}
if (start < blocks[i].start) {
continue;
}
if (start >= blocks[i].start + blocks[i].size) {
continue;
}
uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size);
out += start - blocks[i].start;
*size -= start - blocks[i].start;
return out;
}
return NULL;
}
#ifdef USE_ELF
bool mCoreLoadELF(struct mCore* core, struct ELF* elf) {
struct ELFProgramHeaders ph;
ELFProgramHeadersInit(&ph, 0);
ELFGetProgramHeaders(elf, &ph);
size_t i;
for (i = 0; i < ELFProgramHeadersSize(&ph); ++i) {
size_t bsize, esize;
Elf32_Phdr* phdr = ELFProgramHeadersGetPointer(&ph, i);
void* block = mCoreGetMemoryBlock(core, phdr->p_paddr, &bsize);
char* bytes = ELFBytes(elf, &esize);
if (block && bsize >= phdr->p_filesz && esize >= phdr->p_filesz + phdr->p_offset) {
memcpy(block, &bytes[phdr->p_offset], phdr->p_filesz);
} else {
return false;
}
}
return true;
}
void mCoreLoadELFSymbols(struct mDebuggerSymbols* symbols, struct ELF* elf) {
size_t symIndex = ELFFindSection(elf, ".symtab");
size_t names = ELFFindSection(elf, ".strtab");
Elf32_Shdr* symHeader = ELFGetSectionHeader(elf, symIndex);
char* bytes = ELFBytes(elf, NULL);
Elf32_Sym* syms = (Elf32_Sym*) &bytes[symHeader->sh_offset];
size_t i;
for (i = 0; i * sizeof(*syms) < symHeader->sh_size; ++i) {
if (!syms[i].st_name || ELF32_ST_TYPE(syms[i].st_info) == STT_FILE) {
continue;
}
const char* name = ELFGetString(elf, names, syms[i].st_name);
if (name[0] == '$') {
continue;
}
mDebuggerSymbolAdd(symbols, name, syms[i].st_value, -1);
}
}
#endif