-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.cpp
422 lines (378 loc) · 10 KB
/
debugger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Debugger commands:
- state: print CPU state
- r addr [n]: read [n bytes of] memory at addr
- w addr v: write value v to addr
- step: step one CPU tick
- break: add breakpoint
- clear: remove breakpoint
- cont: continue execution, stopping at breakpoints
- shutdown: stop emulator
- exit: exit debugger
- blank: repeat previous command
*/
#include <array>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "debugger.hpp"
#include "cpu.hpp"
#include "opcodes.hpp"
#include "mem.hpp"
using namespace std;
// TODO fix include discipline so we can put this in the header
// without linker errors
bool DEBUG_SHUTDOWN_ON_EOF = 1;
vector<uint16_t> breakpoints;
bool read_addr(stringstream &cmdstream, uint16_t &addr){
int out;
cmdstream >> hex >> out;
if (!cmdstream) {
printf("Couldn't read address\n");
return 0;
}
if ((out < 0) || (out > 0xffff)) {
printf("Bad address 0x%x\n", out);
return 0;
}
addr = out;
return 1;
}
void cmd_state(CPU &cpu, stringstream &cmdstream) {
cpu.printState();
}
void cmd_interrupts(CPU &cpu, stringstream &cmdstream) {
cout << "Interrupts "
<< (cpu.interrupt_master_enable ? "enabled" : "disabled")
<< "\n";
cout << "Enabled interrupts: "
<< hex << setw(2) << setfill('0') << (int) cpu.interrupts_enabled
<< "\n";
cout << "Requested interrupts: "
<< hex << setw(2) << setfill('0') << (int) cpu.interrupts_raised
<< "\n";
printf("%02x %02x\n", cpu.interrupts_enabled, cpu.interrupts_raised);
}
void cmd_read(CPU &cpu, stringstream &cmdstream) {
// Read as hex by default
uint16_t addr;
if (!read_addr(cmdstream, addr)) {
return;
}
int bytes;
cmdstream >> dec >> bytes;
if (!cmdstream) {
bytes = 1;
}
cout << "["
<< hex << setw(4) << setfill('0') << addr
<< "] ";
for (int i = 0; i < bytes; i++) {
cout << hex << setw(2) << setfill('0')
<< (int) gb_mem_ptr(cpu, addr + i).read()
<< " ";
}
cout << "\n";
}
void cmd_write(CPU &cpu, stringstream &cmdstream) {
uint16_t addr;
if (!read_addr(cmdstream, addr)) {
return;
}
int val;
cmdstream >> hex >> val;
if (!cmdstream) {
printf("Couldn't read value\n");
return;
}
if ((val < 0) || (val > 0xff)) {
printf("Bad value %x\n", val);
return;
}
gb_mem_ptr(cpu, addr).write(val);
// confirm write
cout << "["
<< hex << setw(4) << setfill('0') << addr
<< "] "
<< hex << setw(2) << setfill('0')
<< (int) gb_mem_ptr(cpu, addr).read()
<< "\n";
}
void cmd_wreg(CPU &cpu, stringstream &cmdstream) {
string regname;
cmdstream >> regname;
if (!cmdstream) {
printf("Couldn't read register name\n");
return;
}
int val;
cmdstream >> hex >> val;
if (!cmdstream) {
printf("Couldn't read value\n");
return;
}
struct {string name; gb_ptr ptr;} regs_8[] = {
{"b", gb_reg_ptr(cpu, &cpu.bc.high)},
{"c", gb_reg_ptr(cpu, &cpu.bc.low)},
{"d", gb_reg_ptr(cpu, &cpu.de.high)},
{"e", gb_reg_ptr(cpu, &cpu.de.low)},
{"a", gb_reg_ptr(cpu, &cpu.af.high)},
{"f", gb_reg_ptr(cpu, &cpu.af.low)},
{"h", gb_reg_ptr(cpu, &cpu.hl.high)},
{"l", gb_reg_ptr(cpu, &cpu.hl.low)},
};
struct {string name; gb_ptr_16 ptr;} regs_16[] = {
{"af", gb_reg16_ptr(cpu, &cpu.af.full)},
{"bc", gb_reg16_ptr(cpu, &cpu.bc.full)},
{"de", gb_reg16_ptr(cpu, &cpu.de.full)},
{"hl", gb_reg16_ptr(cpu, &cpu.hl.full)},
{"pc", gb_reg16_ptr(cpu, &cpu.pc)},
{"sp", gb_reg16_ptr(cpu, &cpu.sp)},
};
bool recognized = 0;
for (int i = 0; i < sizeof(regs_8) / sizeof(regs_8[0]); i++){
if (regname.compare(regs_8[i].name) == 0) {
if ((val < 0) || (val > 0xff)) {
cout << "Bad value " << hex << val << " for register " << regname << "\n";
return;
}
regs_8[i].ptr.write(val);
recognized = 1;
break;
}
}
if (!recognized) {
for (int i = 0; i < sizeof(regs_16) / sizeof(regs_16[0]); i++){
if (regname.compare(regs_16[i].name) == 0) {
if ((val < 0) || (val > 0xffff)) {
cout << "Bad value " << hex << val << " for register " << regname << "\n";
return;
}
regs_8[i].ptr.write(val);
recognized = 1;
break;
}
}
}
if (recognized) {
// confirm write
cpu.printState();
} else {
cout << "Unrecognized register\n";
}
}
void cmd_step(CPU &cpu, stringstream &cmdstream) {
cpu.tick();
cpu.printState();
}
void cmd_break(CPU &cpu, stringstream &cmdstream) {
uint16_t addr;
if (!read_addr(cmdstream, addr)) {
return;
}
if (find(breakpoints.begin(), breakpoints.end(),
addr)
!= breakpoints.end()) {
cout << "Breakpoint already set at "
<< hex << setw(4) << setfill('0') << addr
<< "\n";
return;
}
breakpoints.push_back(addr);
cout << "Set breakpoint at "
<< hex << setw(4) << setfill('0') << addr
<< "\n";
}
void cmd_clear(CPU &cpu, stringstream &cmdstream) {
uint16_t addr;
if (!read_addr(cmdstream, addr)) {
return;
}
if ((addr < 0) || (addr > 0xffff)) {
printf("Bad address %x\n", addr);
return;
}
auto loc = find(breakpoints.begin(),
breakpoints.end(),
addr);
if (loc == breakpoints.end()) {
cout << "Breakpoint not found at "
<< hex << setw(4) << setfill('0') << addr
<< "\n";
return;
} else {
breakpoints.erase(loc);
cout << "Removed breakpoint at "
<< hex << setw(4) << setfill('0') << addr
<< "\n";
}
}
void cmd_continue(CPU &cpu, stringstream &cmdstream) {
cout << "Continuing\n";
cpu.install_sigint();
do {
cpu.tick();
} while (find(breakpoints.begin(), breakpoints.end(), cpu.pc)
== breakpoints.end());
cpu.uninstall_sigint();
cpu.printState();
}
void cmd_shutdown(CPU &cpu, stringstream &cmdstream) {
exit(0);
}
// tilea: print tile data from an address
void cmd_tilea(CPU &cpu, stringstream &cmdstream) {
uint16_t base;
if (!read_addr(cmdstream, base)) {
return;
}
for (int row = 0; row < 8; row++) {
uint8_t low_byte = gb_mem_ptr(cpu, base + row*2).read();
uint8_t high_byte = gb_mem_ptr(cpu, base + row*2 + 1).read();
for (int col = 0; col < 8; col++) {
int out = 0;
if (low_byte & (1<<(7-col))) {
out += 1;
}
if (high_byte & (1<<(7-col))) {
out += 2;
}
if (out) {
cout << out;
} else {
cout << '.';
}
}
cout << '\n';
}
}
void cmd_ascii_screen(CPU &cpu, stringstream &cmdstream) {
// Testing out some rendering code here before I move it elsewhere.
// For testing purposes, REG_LCD_CONTROL stores 0x91, so:
// BG on
// Sprites off
// Sprites 8x8
// BG code starts at 0x9800
// BG tiles start at 0x8000
// Window off
// Window code starts at 0x9800
// Screen on
for (int y = 0; y < 144; y++) {
int tileY = y / 8;
for (int x = 0; x < 160; x++) {
int tileX = x / 8;
int block = tileY * 32 + tileX;
uint16_t bg_base = 0x9800; // TODO confirm
uint8_t tile_n = gb_mem_ptr(cpu, bg_base + block).read();
uint16_t tile_base = 0x8000;
uint16_t tile_addr = tile_base + tile_n * 16;
uint8_t low_byte = gb_mem_ptr(cpu, tile_addr + (y%8)*2).read();
uint8_t high_byte = gb_mem_ptr(cpu, tile_addr + (y%8)*2 + 1).read();
int out = 0;
if (low_byte & (1<<(7-(x%8)))) {
out += 1;
}
if (high_byte & (1<<(7-(x%8)))) {
out += 2;
}
if (out) {
cout << out;
} else {
cout << '.';
}
}
cout << "\n";
}
cout << "\n";
}
void cmd_draw(CPU &cpu, stringstream &cmdstream) {
cpu.screen->draw();
}
void cmd_disasssemble_fun(CPU &cpu, stringstream &cmdstream) {
uint16_t addr;
if (!read_addr(cmdstream, addr)) {
return;
}
uint8_t op_first;
const char *opcode_name;
do {
op_first = gb_mem_ptr(cpu, addr).read();
opcode_name = op_first == 0xcb ?
CB_OPCODE_NAMES[(gb_mem_ptr(cpu, addr+1)).read()] :
OPCODE_NAMES[op_first];
printf("%04x: %s: %02x", addr, opcode_name, op_first);
for (int i = 0; i < OPCODE_LENGTHS[op_first] - 1; i++) {
uint8_t arg = gb_mem_ptr(cpu, addr+i+1).read();
printf(" %02x", arg);
}
printf("\n");
addr += OPCODE_LENGTHS[op_first];
// stop on return, absolute jump, or reset
// (will miss interrupts triggered by writing to the interrupt register)
} while ((strncmp(opcode_name, "RET", 3) != 0) &&
(strncmp(opcode_name, "JR", 2) != 0) &&
(strncmp(opcode_name, "JP", 2) != 0) &&
(strncmp(opcode_name, "RST", 2) != 0) &&
(OPCODE_LENGTHS[op_first] != 0));
}
const struct {string name; void (*cmd)(CPU &, stringstream &);} cmds[] = {
{"state", cmd_state},
{"read", cmd_read},
{"r", cmd_read},
{"write", cmd_write},
{"w", cmd_write},
{"wreg", cmd_wreg},
{"step", cmd_step},
{"s", cmd_step},
{"break", cmd_break},
{"b", cmd_break},
{"clear", cmd_clear},
{"cont", cmd_continue},
{"c", cmd_continue},
{"shutdown", cmd_shutdown},
{"sd", cmd_shutdown},
{"tilea", cmd_tilea},
{"screen", cmd_ascii_screen},
{"draw", cmd_draw},
{"disassemble_fun", cmd_disasssemble_fun},
{"interrupts", cmd_interrupts},
};
void run_debugger(CPU &cpu) {
cpu.printState();
string last_cmd;
while (1) {
string cmd_full;
cout << "> ";
getline(cin, cmd_full);
if (!cin) {
if (DEBUG_SHUTDOWN_ON_EOF) {
cout << "\n";
exit(0);
}
break;
}
if (cmd_full.empty()) {
cmd_full = last_cmd;
}
last_cmd = cmd_full;
stringstream cmdstream = stringstream(cmd_full);
string cmd;
cmdstream >> cmd;
bool recognized = 0;
if (cmd.compare("exit") == 0) {
break;
}
for (int i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++){
if (cmd.compare(cmds[i].name) == 0) {
cmds[i].cmd(cpu, cmdstream);
recognized = 1;
break;
}
}
if (!recognized) {
cout << "Unrecognized command\n";
}
}
cout << "\n";
}