all repos — mgba @ 10e794341aaee827cf9b2e839038fdff067cbdef

mGBA Game Boy Advance Emulator

src/gba/supervisor/context.c (view raw)

  1/* Copyright (c) 2013-2015 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 "gba/supervisor/context.h"
  7
  8#include "gba/supervisor/overrides.h"
  9
 10#include "util/memory.h"
 11#include "util/vfs.h"
 12
 13bool GBAContextInit(struct GBAContext* context, const char* port) {
 14	context->gba = anonymousMemoryMap(sizeof(struct GBA));
 15	context->cpu = anonymousMemoryMap(sizeof(struct ARMCore));
 16	context->rom = 0;
 17	context->save = 0;
 18	context->renderer = 0;
 19	memset(context->components, 0, sizeof(context->components));
 20
 21	if (!context->gba || !context->cpu) {
 22		if (context->gba) {
 23			mappedMemoryFree(context->gba, sizeof(struct GBA));
 24		}
 25		if (context->cpu) {
 26			mappedMemoryFree(context->cpu, sizeof(struct ARMCore));
 27		}
 28		return false;
 29	}
 30
 31	GBAConfigInit(&context->config, port);
 32	if (port) {
 33		GBAConfigLoad(&context->config);
 34	}
 35
 36	GBACreate(context->gba);
 37	ARMSetComponents(context->cpu, &context->gba->d, 0, context->components);
 38	ARMInit(context->cpu);
 39
 40	context->gba->sync = 0;
 41	return true;
 42}
 43
 44void GBAContextDeinit(struct GBAContext* context) {
 45	if (context->bios) {
 46		context->bios->close(context->bios);
 47		context->bios = 0;
 48	}
 49	if (context->rom) {
 50		context->rom->close(context->rom);
 51		context->rom = 0;
 52	}
 53	if (context->save) {
 54		context->save->close(context->save);
 55		context->save = 0;
 56	}
 57	ARMDeinit(context->cpu);
 58	GBADestroy(context->gba);
 59	mappedMemoryFree(context->gba, 0);
 60	mappedMemoryFree(context->cpu, 0);
 61	GBAConfigDeinit(&context->config);
 62}
 63
 64bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) {
 65	context->rom = VFileOpen(path, O_RDONLY);
 66	if (!context->rom) {
 67		return false;
 68	}
 69
 70	if (!GBAIsROM(context->rom)) {
 71		context->rom->close(context->rom);
 72		context->rom = 0;
 73		return false;
 74	}
 75
 76	if (autoloadSave) {
 77		context->save = VDirOptionalOpenFile(0, path, 0, ".sav", O_RDWR | O_CREAT);
 78	}
 79	return true;
 80}
 81
 82bool GBAContextLoadROMFromVFile(struct GBAContext* context, struct VFile* rom, struct VFile* save) {
 83	context->rom = rom;
 84	if (!GBAIsROM(context->rom)) {
 85		context->rom = 0;
 86		return false;
 87	}
 88	context->save = save;
 89	return true;
 90}
 91
 92bool GBAContextLoadBIOS(struct GBAContext* context, const char* path) {
 93	context->bios = VFileOpen(path, O_RDONLY);
 94	if (!context->bios) {
 95		return false;
 96	}
 97
 98	if (!GBAIsBIOS(context->bios)) {
 99		context->bios->close(context->bios);
100		context->bios = 0;
101		return false;
102	}
103	return true;
104}
105
106bool GBAContextLoadBIOSFromVFile(struct GBAContext* context, struct VFile* bios) {
107	context->bios = bios;
108	if (!GBAIsBIOS(context->bios)) {
109		context->bios = 0;
110		return false;
111	}
112	return true;
113}
114
115bool GBAContextStart(struct GBAContext* context) {
116	struct GBAOptions opts = {};
117	GBAConfigMap(&context->config, &opts);
118
119	if (context->renderer) {
120		GBAVideoAssociateRenderer(&context->gba->video, context->renderer);
121	}
122
123	GBALoadROM(context->gba, context->rom, context->save, 0);
124	if (opts.useBios && context->bios) {
125		GBALoadBIOS(context->gba, context->bios);
126	}
127
128	ARMReset(context->cpu);
129
130	if (opts.skipBios) {
131		GBASkipBIOS(context->cpu);
132	}
133
134	struct GBACartridgeOverride override;
135	const struct GBACartridge* cart = (const struct GBACartridge*) context->gba->memory.rom;
136	memcpy(override.id, &cart->id, sizeof(override.id));
137	if (GBAOverrideFind(GBAConfigGetOverrides(&context->config), &override)) {
138		GBAOverrideApply(context->gba, &override);
139	}
140	GBAConfigFreeOpts(&opts);
141	return true;
142}
143
144void GBAContextStop(struct GBAContext* context) {
145	UNUSED(context);
146	// TODO?
147}
148
149void GBAContextFrame(struct GBAContext* context, uint16_t keys) {
150	int activeKeys = keys;
151	context->gba->keySource = &activeKeys;
152
153	int frameCounter = context->gba->video.frameCounter;
154	while (frameCounter == context->gba->video.frameCounter) {
155		ARMRunLoop(context->cpu);
156	}
157}