src/gb/debugger/cli.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/internal/gb/extra/cli.h>
7
8#include <mgba/core/core.h>
9#include <mgba/core/serialize.h>
10#include <mgba/internal/gb/gb.h>
11#include <mgba/internal/gb/io.h>
12#include <mgba/internal/gb/video.h>
13#include <mgba/internal/sm83/debugger/cli-debugger.h>
14
15static void _GBCLIDebuggerInit(struct CLIDebuggerSystem*);
16static bool _GBCLIDebuggerCustom(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 _GBCLIDebuggerCommands[] = {
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 CLIDebuggerSystem* GBCLIDebuggerCreate(struct mCore* core) {
30 UNUSED(core);
31 struct GBCLIDebugger* debugger = malloc(sizeof(struct GBCLIDebugger));
32 SM83CLIDebuggerCreate(&debugger->d);
33 debugger->d.init = _GBCLIDebuggerInit;
34 debugger->d.deinit = NULL;
35 debugger->d.custom = _GBCLIDebuggerCustom;
36
37 debugger->d.name = "Game Boy";
38 debugger->d.commands = _GBCLIDebuggerCommands;
39 debugger->d.commandAliases = NULL;
40
41 debugger->core = core;
42
43 return &debugger->d;
44}
45
46static void _GBCLIDebuggerInit(struct CLIDebuggerSystem* debugger) {
47 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger;
48
49 gbDebugger->frameAdvance = false;
50}
51
52static bool _GBCLIDebuggerCustom(struct CLIDebuggerSystem* debugger) {
53 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger;
54
55 if (gbDebugger->frameAdvance) {
56 if (!gbDebugger->inVblank && GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1) {
57 mDebuggerEnter(&gbDebugger->d.p->d, DEBUGGER_ENTER_MANUAL, 0);
58 gbDebugger->frameAdvance = false;
59 return false;
60 }
61 gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
62 return true;
63 }
64 return true;
65}
66
67static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
68 UNUSED(dv);
69 debugger->d.state = DEBUGGER_CUSTOM;
70
71 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
72 gbDebugger->frameAdvance = true;
73 gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
74}
75
76static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
77 struct CLIDebuggerBackend* be = debugger->backend;
78 if (!dv || dv->type != CLIDV_INT_TYPE) {
79 be->printf(be, "%s\n", ERROR_MISSING_ARGS);
80 return;
81 }
82
83 int state = dv->intValue;
84 if (state < 1 || state > 9) {
85 be->printf(be, "State %u out of range", state);
86 }
87
88 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
89
90 mCoreLoadState(gbDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
91}
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 GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
106
107 mCoreSaveState(gbDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC | SAVESTATE_METADATA);
108}