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/lr35902/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 LR35902CLIDebuggerCreate(&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
40 debugger->core = core;
41
42 return &debugger->d;
43}
44
45static void _GBCLIDebuggerInit(struct CLIDebuggerSystem* debugger) {
46 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger;
47
48 gbDebugger->frameAdvance = false;
49}
50
51static bool _GBCLIDebuggerCustom(struct CLIDebuggerSystem* debugger) {
52 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger;
53
54 if (gbDebugger->frameAdvance) {
55 if (!gbDebugger->inVblank && GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1) {
56 mDebuggerEnter(&gbDebugger->d.p->d, DEBUGGER_ENTER_MANUAL, 0);
57 gbDebugger->frameAdvance = false;
58 return false;
59 }
60 gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
61 return true;
62 }
63 return true;
64}
65
66static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
67 UNUSED(dv);
68 debugger->d.state = DEBUGGER_CUSTOM;
69
70 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
71 gbDebugger->frameAdvance = true;
72 gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
73}
74
75static void _load(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
76 struct CLIDebuggerBackend* be = debugger->backend;
77 if (!dv || dv->type != CLIDV_INT_TYPE) {
78 be->printf(be, "%s\n", ERROR_MISSING_ARGS);
79 return;
80 }
81
82 int state = dv->intValue;
83 if (state < 1 || state > 9) {
84 be->printf(be, "State %u out of range", state);
85 }
86
87 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
88
89 mCoreLoadState(gbDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
90}
91
92static void _save(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
93 struct CLIDebuggerBackend* be = debugger->backend;
94 if (!dv || dv->type != CLIDV_INT_TYPE) {
95 be->printf(be, "%s\n", ERROR_MISSING_ARGS);
96 return;
97 }
98
99 int state = dv->intValue;
100 if (state < 1 || state > 9) {
101 be->printf(be, "State %u out of range", state);
102 }
103
104 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
105
106 mCoreSaveState(gbDebugger->core, dv->intValue, SAVESTATE_SCREENSHOT | SAVESTATE_RTC | SAVESTATE_METADATA);
107}