src/gb/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 "cli.h"
7
8#include "core/core.h"
9#include "core/serialize.h"
10#include "gb/gb.h"
11#include "gb/io.h"
12#include "gb/video.h"
13#include "lr35902/debugger/cli-debugger.h"
14
15#ifdef USE_CLI_DEBUGGER
16
17static void _GBCLIDebuggerInit(struct CLIDebuggerSystem*);
18static bool _GBCLIDebuggerCustom(struct CLIDebuggerSystem*);
19static uint32_t _GBCLIDebuggerLookupIdentifier(struct CLIDebuggerSystem*, const char* name, struct CLIDebugVector* dv);
20
21static void _frame(struct CLIDebugger*, struct CLIDebugVector*);
22
23struct CLIDebuggerCommandSummary _GBCLIDebuggerCommands[] = {
24 { "frame", _frame, 0, "Frame advance" },
25 { 0, 0, 0, 0 }
26};
27
28struct CLIDebuggerSystem* GBCLIDebuggerCreate(struct mCore* core) {
29 UNUSED(core);
30 struct GBCLIDebugger* debugger = malloc(sizeof(struct GBCLIDebugger));
31 LR35902CLIDebuggerCreate(&debugger->d);
32 debugger->d.init = _GBCLIDebuggerInit;
33 debugger->d.deinit = NULL;
34 debugger->d.custom = _GBCLIDebuggerCustom;
35 debugger->d.lookupIdentifier = _GBCLIDebuggerLookupIdentifier;
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 false;
64}
65
66static uint32_t _GBCLIDebuggerLookupIdentifier(struct CLIDebuggerSystem* debugger, const char* name, struct CLIDebugVector* dv) {
67 UNUSED(debugger);
68 int i;
69 for (i = 0; i < REG_MAX; ++i) {
70 const char* reg = GBIORegisterNames[i];
71 if (reg && strcasecmp(reg, name) == 0) {
72 return GB_BASE_IO | i;
73 }
74 }
75 dv->type = CLIDV_ERROR_TYPE;
76 return 0;
77}
78
79static void _frame(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
80 UNUSED(dv);
81 debugger->d.state = DEBUGGER_CUSTOM;
82
83 struct GBCLIDebugger* gbDebugger = (struct GBCLIDebugger*) debugger->system;
84 gbDebugger->frameAdvance = true;
85 gbDebugger->inVblank = GBRegisterSTATGetMode(((struct GB*) gbDebugger->core->board)->memory.io[REG_STAT]) == 1;
86}
87
88#endif