all repos — mgba @ 36ea5ea89c86b97c03729ecbc67725fba47f55a6

mGBA Game Boy Advance Emulator

src/gba/extra/cli.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 "cli.h"
  7
  8#include "arm/debugger/cli-debugger.h"
  9#include "core/serialize.h"
 10#include "gba/io.h"
 11#include "gba/serialize.h"
 12
 13#ifdef USE_CLI_DEBUGGER
 14
 15static void _GBACLIDebuggerInit(struct CLIDebuggerSystem*);
 16static bool _GBACLIDebuggerCustom(struct CLIDebuggerSystem*);
 17static uint32_t _GBACLIDebuggerLookupIdentifier(struct CLIDebuggerSystem*, const char* name, struct CLIDebugVector* dv);
 18
 19static void _frame(struct CLIDebugger*, struct CLIDebugVector*);
 20static void _load(struct CLIDebugger*, struct CLIDebugVector*);
 21static void _rewind(struct CLIDebugger*, struct CLIDebugVector*);
 22static void _save(struct CLIDebugger*, struct CLIDebugVector*);
 23
 24struct CLIDebuggerCommandSummary _GBACLIDebuggerCommands[] = {
 25	{ "frame", _frame, 0, "Frame advance" },
 26	{ "load", _load, CLIDVParse, "Load a savestate" },
 27	{ "rewind", _rewind, CLIDVParse, "Rewind the emulation a number of recorded intervals" },
 28	{ "save", _save, CLIDVParse, "Save a savestate" },
 29	{ 0, 0, 0, 0 }
 30};
 31
 32struct GBACLIDebugger* GBACLIDebuggerCreate(struct mCore* core) {
 33	struct GBACLIDebugger* debugger = malloc(sizeof(struct GBACLIDebugger));
 34	ARMCLIDebuggerCreate(&debugger->d);
 35	debugger->d.init = _GBACLIDebuggerInit;
 36	debugger->d.deinit = NULL;
 37	debugger->d.custom = _GBACLIDebuggerCustom;
 38	debugger->d.lookupIdentifier = _GBACLIDebuggerLookupIdentifier;
 39
 40	debugger->d.name = "Game Boy Advance";
 41	debugger->d.commands = _GBACLIDebuggerCommands;
 42
 43	debugger->core = core;
 44
 45	return debugger;
 46}
 47
 48static void _GBACLIDebuggerInit(struct CLIDebuggerSystem* debugger) {
 49	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger;
 50
 51	gbaDebugger->frameAdvance = false;
 52}
 53
 54static bool _GBACLIDebuggerCustom(struct CLIDebuggerSystem* debugger) {
 55	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger;
 56
 57	if (gbaDebugger->frameAdvance) {
 58		if (!gbaDebugger->inVblank && GBARegisterDISPSTATIsInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1])) {
 59			mDebuggerEnter(&gbaDebugger->d.p->d, DEBUGGER_ENTER_MANUAL, 0);
 60			gbaDebugger->frameAdvance = false;
 61			return false;
 62		}
 63		gbaDebugger->inVblank = GBARegisterDISPSTATGetInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1]);
 64		return true;
 65	}
 66	return false;
 67}
 68
 69static uint32_t _GBACLIDebuggerLookupIdentifier(struct CLIDebuggerSystem* debugger, const char* name, struct CLIDebugVector* dv) {
 70	UNUSED(debugger);
 71	int i;
 72	for (i = 0; i < REG_MAX; i += 2) {
 73		const char* reg = GBAIORegisterNames[i >> 1];
 74		if (reg && strcasecmp(reg, name) == 0) {
 75			return BASE_IO | i;
 76		}
 77	}
 78	dv->type = CLIDV_ERROR_TYPE;
 79	return 0;
 80}
 81
 82static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 83	UNUSED(dv);
 84	debugger->d.state = DEBUGGER_CUSTOM;
 85
 86	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
 87	gbaDebugger->frameAdvance = true;
 88	gbaDebugger->inVblank = GBARegisterDISPSTATGetInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1]);
 89}
 90
 91static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 92	if (!dv || dv->type != CLIDV_INT_TYPE) {
 93		printf("%s\n", ERROR_MISSING_ARGS);
 94		return;
 95	}
 96
 97	int state = dv->intValue;
 98	if (state < 1 || state > 9) {
 99		printf("State %u out of range", state);
100	}
101
102	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
103
104	mCoreLoadState(gbaDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT);
105}
106
107static void _rewind(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
108	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
109	if (!dv) {
110		// TODO: Put back rewind
111	} else if (dv->type != CLIDV_INT_TYPE) {
112		printf("%s\n", ERROR_MISSING_ARGS);
113	} else {
114		// TODO: Put back rewind
115	}
116}
117
118static void _save(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
119	if (!dv || dv->type != CLIDV_INT_TYPE) {
120		printf("%s\n", ERROR_MISSING_ARGS);
121		return;
122	}
123
124	int state = dv->intValue;
125	if (state < 1 || state > 9) {
126		printf("State %u out of range", state);
127	}
128
129	struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
130
131	mCoreSaveState(gbaDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT);
132}
133#endif