src/gba/debugger/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 <mgba/internal/gba/extra/cli.h>
7
8#include <mgba/core/core.h>
9#include <mgba/core/serialize.h>
10#include <mgba/internal/gba/gba.h>
11#include <mgba/internal/gba/io.h>
12#include <mgba/internal/gba/video.h>
13#include <mgba/internal/arm/debugger/cli-debugger.h>
14
15static void _GBACLIDebuggerInit(struct CLIDebuggerSystem*);
16static bool _GBACLIDebuggerCustom(struct CLIDebuggerSystem*);
17
18static void _frame(struct CLIDebugger*, struct CLIDebugVector*);
19static void _load(struct CLIDebugger*, struct CLIDebugVector*);
20static void _save(struct CLIDebugger*, struct CLIDebugVector*);
21
22struct CLIDebuggerCommandSummary _GBACLIDebuggerCommands[] = {
23 { "frame", _frame, "", "Frame advance" },
24 { "load", _load, "*", "Load a savestate" },
25 { "save", _save, "*", "Save a savestate" },
26 { 0, 0, 0, 0 }
27};
28
29struct GBACLIDebugger* GBACLIDebuggerCreate(struct mCore* core) {
30 struct GBACLIDebugger* debugger = malloc(sizeof(struct GBACLIDebugger));
31 ARMCLIDebuggerCreate(&debugger->d);
32 debugger->d.init = _GBACLIDebuggerInit;
33 debugger->d.deinit = NULL;
34 debugger->d.custom = _GBACLIDebuggerCustom;
35
36 debugger->d.name = "Game Boy Advance";
37 debugger->d.commands = _GBACLIDebuggerCommands;
38
39 debugger->core = core;
40
41 return debugger;
42}
43
44static void _GBACLIDebuggerInit(struct CLIDebuggerSystem* debugger) {
45 struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger;
46
47 gbaDebugger->frameAdvance = false;
48}
49
50static bool _GBACLIDebuggerCustom(struct CLIDebuggerSystem* debugger) {
51 struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger;
52
53 if (gbaDebugger->frameAdvance) {
54 if (!gbaDebugger->inVblank && GBARegisterDISPSTATIsInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1])) {
55 mDebuggerEnter(&gbaDebugger->d.p->d, DEBUGGER_ENTER_MANUAL, 0);
56 gbaDebugger->frameAdvance = false;
57 return false;
58 }
59 gbaDebugger->inVblank = GBARegisterDISPSTATGetInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1]);
60 return true;
61 }
62 return true;
63}
64
65static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
66 UNUSED(dv);
67 debugger->d.state = DEBUGGER_CUSTOM;
68
69 struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
70 gbaDebugger->frameAdvance = true;
71 gbaDebugger->inVblank = GBARegisterDISPSTATGetInVblank(((struct GBA*) gbaDebugger->core->board)->memory.io[REG_DISPSTAT >> 1]);
72}
73
74static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
75 struct CLIDebuggerBackend* be = debugger->backend;
76 if (!dv || dv->type != CLIDV_INT_TYPE) {
77 be->printf(be, "%s\n", ERROR_MISSING_ARGS);
78 return;
79 }
80
81 int state = dv->intValue;
82 if (state < 1 || state > 9) {
83 be->printf(be, "State %u out of range", state);
84 }
85
86 struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
87
88 mCoreLoadState(gbaDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
89}
90
91// TODO: Put back rewind
92
93static void _save(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
94 struct CLIDebuggerBackend* be = debugger->backend;
95 if (!dv || dv->type != CLIDV_INT_TYPE) {
96 be->printf(be, "%s\n", ERROR_MISSING_ARGS);
97 return;
98 }
99
100 int state = dv->intValue;
101 if (state < 1 || state > 9) {
102 be->printf(be, "State %u out of range", state);
103 }
104
105 struct GBACLIDebugger* gbaDebugger = (struct GBACLIDebugger*) debugger->system;
106
107 mCoreSaveState(gbaDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC | SAVESTATE_METADATA);
108}