src/lr35902/debugger/cli-debugger.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/lr35902/debugger/cli-debugger.h>
7
8#include <mgba/core/core.h>
9#include <mgba/internal/debugger/cli-debugger.h>
10#include <mgba/internal/lr35902/decoder.h>
11#include <mgba/internal/lr35902/debugger/debugger.h>
12#include <mgba/internal/lr35902/lr35902.h>
13
14static void _printStatus(struct CLIDebuggerSystem*);
15
16static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv);
17static uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment);
18
19static struct CLIDebuggerCommandSummary _lr35902Commands[] = {
20 { 0, 0, 0, 0 }
21};
22
23static inline void _printFlags(struct CLIDebuggerBackend* be, union FlagRegister f) {
24 be->printf(be, "[%c%c%c%c]\n",
25 f.z ? 'Z' : '-',
26 f.n ? 'N' : '-',
27 f.h ? 'H' : '-',
28 f.c ? 'C' : '-');
29}
30
31static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv) {
32 struct LR35902Core* cpu = debugger->p->d.core->cpu;
33
34 uint16_t address;
35 int segment = -1;
36 size_t size;
37 if (!dv || dv->type != CLIDV_INT_TYPE) {
38 address = cpu->pc;
39 } else {
40 address = dv->intValue;
41 segment = dv->segmentValue;
42 dv = dv->next;
43 }
44
45 if (!dv || dv->type != CLIDV_INT_TYPE) {
46 size = 1;
47 } else {
48 size = dv->intValue;
49 dv = dv->next; // TODO: Check for excess args
50 }
51
52 size_t i;
53 for (i = 0; i < size; ++i) {
54 address = _printLine(debugger->p, address, segment);
55 }
56}
57
58static inline uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment) {
59 struct CLIDebuggerBackend* be = debugger->backend;
60 struct LR35902InstructionInfo info = {0};
61 char disassembly[48];
62 char* disPtr = disassembly;
63 if (segment >= 0) {
64 be->printf(be, "%02X:", segment);
65 }
66 be->printf(be, "%04X: ", address);
67 uint8_t instruction;
68 size_t bytesRemaining = 1;
69 for (bytesRemaining = 1; bytesRemaining; --bytesRemaining) {
70 instruction = debugger->d.core->rawRead8(debugger->d.core, address, segment);
71 disPtr += snprintf(disPtr, sizeof(disassembly) - (disPtr - disassembly), "%02X", instruction);
72 ++address;
73 bytesRemaining += LR35902Decode(instruction, &info);
74 };
75 disPtr[0] = '\t';
76 ++disPtr;
77 LR35902Disassemble(&info, disPtr, sizeof(disassembly) - (disPtr - disassembly));
78 be->printf(be, "%s\n", disassembly);
79 return address;
80}
81
82static void _printStatus(struct CLIDebuggerSystem* debugger) {
83 struct CLIDebuggerBackend* be = debugger->p->backend;
84 struct LR35902Core* cpu = debugger->p->d.core->cpu;
85 be->printf(be, "A: %02X F: %02X (AF: %04X)\n", cpu->a, cpu->f.packed, cpu->af);
86 be->printf(be, "B: %02X C: %02X (BC: %04X)\n", cpu->b, cpu->c, cpu->bc);
87 be->printf(be, "D: %02X E: %02X (DE: %04X)\n", cpu->d, cpu->e, cpu->de);
88 be->printf(be, "H: %02X L: %02X (HL: %04X)\n", cpu->h, cpu->l, cpu->hl);
89 be->printf(be, "PC: %04X SP: %04X\n", cpu->pc, cpu->sp);
90
91 struct LR35902Debugger* platDebugger = (struct LR35902Debugger*) debugger->p->d.platform;
92 size_t i;
93 for (i = 0; platDebugger->segments[i].name; ++i) {
94 be->printf(be, "%s%s: %02X", i ? " " : "", platDebugger->segments[i].name, cpu->memory.currentSegment(cpu, platDebugger->segments[i].start));
95 }
96 if (i) {
97 be->printf(be, "\n");
98 }
99 _printFlags(be, cpu->f);
100 _printLine(debugger->p, cpu->pc, cpu->memory.currentSegment(cpu, cpu->pc));
101}
102
103static uint32_t _lookupPlatformIdentifier(struct CLIDebuggerSystem* debugger, const char* name, struct CLIDebugVector* dv) {
104 struct LR35902Core* cpu = debugger->p->d.core->cpu;
105 if (strcmp(name, "a") == 0) {
106 return cpu->a;
107 }
108 if (strcmp(name, "b") == 0) {
109 return cpu->b;
110 }
111 if (strcmp(name, "c") == 0) {
112 return cpu->c;
113 }
114 if (strcmp(name, "d") == 0) {
115 return cpu->d;
116 }
117 if (strcmp(name, "e") == 0) {
118 return cpu->e;
119 }
120 if (strcmp(name, "h") == 0) {
121 return cpu->h;
122 }
123 if (strcmp(name, "l") == 0) {
124 return cpu->l;
125 }
126 if (strcmp(name, "bc") == 0) {
127 return cpu->bc;
128 }
129 if (strcmp(name, "de") == 0) {
130 return cpu->de;
131 }
132 if (strcmp(name, "hl") == 0) {
133 return cpu->hl;
134 }
135 if (strcmp(name, "af") == 0) {
136 return cpu->af;
137 }
138 if (strcmp(name, "pc") == 0) {
139 return cpu->pc;
140 }
141 if (strcmp(name, "sp") == 0) {
142 return cpu->sp;
143 }
144 if (strcmp(name, "f") == 0) {
145 return cpu->f.packed;
146 }
147 dv->type = CLIDV_ERROR_TYPE;
148 return 0;
149}
150
151void LR35902CLIDebuggerCreate(struct CLIDebuggerSystem* debugger) {
152 debugger->printStatus = _printStatus;
153 debugger->disassemble = _disassemble;
154 debugger->lookupPlatformIdentifier = _lookupPlatformIdentifier;
155 debugger->platformName = "GB-Z80";
156 debugger->platformCommands = _lr35902Commands;
157}