-
Notifications
You must be signed in to change notification settings - Fork 4
/
debugger.c
101 lines (87 loc) · 3.08 KB
/
debugger.c
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
#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "debugger.h"
#include "disassembler.h"
#include "mmu.h"
void dbg_print_regs(struct gb_state *s) {
printf("\n\tAF\tBC\tDE\tHL\tSP\tPC\t\tLY\tZNHC\n");
printf("\t%04x\t%04x\t%04x\t%04x\t%04x\t%04x\t\t%04x\t%d%d%d%d\n",
s->reg16.AF, s->reg16.BC, s->reg16.DE, s->reg16.HL, s->sp, s->pc,
s->io_lcd_LY, s->flags.ZF, s->flags.NF, s->flags.HF, s->flags.CF);
}
int dbg_run_debugger(struct gb_state *s) {
static char last_exec_cmd = 0;
printf("Break, next instruction: ");
disassemble(s);
while (1) {
char *raw_input = readline("(GBd) ");
if (!raw_input) /* EOF */
return 1;
add_history(raw_input);
/* Copy into buffer that is automatically free'd on function exit. */
char input[32];
strncpy(input, raw_input, 32);
input[31] = '\0';
free(raw_input);
if (strlen(input) == 0) {
if (last_exec_cmd == 's')
s->emu_state->dbg_break_next = 1;
else if (last_exec_cmd == 'c')
s->emu_state->dbg_break_next = 0;
else
continue;
return 0;
}
switch (input[0]) {
case 'r': /* Regs - Print all regs */
case 'p':
dbg_print_regs(s);
break;
case 'x': /* eXamine - dump memory of given address */
{
u16 loc = strtol(&input[2], NULL, 16);
printf("%.4x: %.2x\n", loc, mmu_read(s, loc));
break;
}
case 'd': /* Disassemble - print instruction at given location */
{
u16 loc = strtol(&input[2], NULL, 16);
disassemble_pc(s, loc);
break;
}
case 's': /* Step - execute one instruction */
s->emu_state->dbg_break_next = 1;
last_exec_cmd = 's';
return 0;
case 'c': /* Continue - continue execution until breakpoint */
s->emu_state->dbg_break_next = 0;
last_exec_cmd = 'c';
return 0;
case 'b': /* Breakpoint - place new breakpoint */
{
u16 loc = strtol(&input[2], NULL, 16);
s->emu_state->dbg_breakpoint = loc;
break;
}
case 'q': /* Quit */
s->emu_state->quit = 1;
return 1;
case 'h': /* Help */
printf("GBd debugger commands:\n");
printf(" r - [P]rint all [r]egisters (alias: p)\n");
printf(" x loc - E[x]amine memory at `loc`\n");
printf(" d loc - [D]isassemble memory at `loc`\n");
printf(" b loc - Place a [b]reakpoint at PC `loc`\n");
printf(" s - [S]tep: execute single instruction\n");
printf(" c - [C]ontinue executiong until next breakpoint\n");
printf(" q - [Q]uit emulator\n");
printf(" h - Show [h]elp\n");
break;
default:
printf("Unknown command: '%s'\n", input);
break;
}
}
}