-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebugger.c
More file actions
330 lines (296 loc) · 6.68 KB
/
debugger.c
File metadata and controls
330 lines (296 loc) · 6.68 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "debugger.h"
#include "memory.h"
#include "lr35902.h"
#include "linenoise/linenoise.h"
//#define MEM_SIZE 0x10000
typedef struct {
char name[16];
int arg_a, arg_b;
} command_t;
typedef enum {
DBG_IDLE,
DBG_CONT,
DBG_STEP
} dbg_state_t;
const char *PROMPT = "miniBoy> ";
const int ARG_LEN = 16;
static uint8_t break_points[MEM_SIZE];
static regs_t *regs;
static dbg_state_t state;
static int rem_steps;
static command_t com;
typedef struct {
uint16_t addr;
unsigned int depth;
unsigned int rep;
} trace_t;
const unsigned int max_trace = 0x1000;
static trace_t call_trace[max_trace];
//static trace_t call_trace[0x1000];
static unsigned int call_trace_len;
static unsigned int call_trace_depth;
void com_help() {
printf("\n -= miniBoy debugger commands: =-\n\n"
"help\n"
"step [n]\n"
"regs\n"
"run\n"
"break n\n"
"continue\n"
"clear [n]\n"
"memory [start] [end]\n"
"disas [start] [end]\n"
"print [addr]\n"
"write [addr] [value]\n"
"io\n"
"trace\n"
"quit\n\n");
}
void com_clear(int n) {
int i;
if (n == -1) {
printf("- Clearing all breakpoints\n");
for (i = 0; i < MEM_SIZE; i++) {
break_points[i] = 0;
}
} else {
printf("- Clearing breakpoint 0x%02X\n", n);
break_points[n] = 0;
}
}
void com_break(int n) {
if (n < 0) {
printf("E) Breakpoint must be positive\n");
return;
} else {
printf("- Setting breakpoint at 0x%02X\n", n);
break_points[n] = 1;
}
}
void com_disas(int start, int end) {
if (start == -1) {
start = regs->PC;
end = start + 16;
} else if (end == -1) {
end = start + 16;
}
while (end > start) {
start += disas_op(start);
}
}
void com_step(int a) {
if (a < 0) {
a = 1;
}
rem_steps = a;
state = DBG_STEP;
}
void com_dump() {
FILE *fp;
fp = fopen("dump.bin", "wb");
fwrite(mem_get_mem(), MEM_SIZE, 1, fp);
fclose(fp);
}
void com_trace() {
int i, j;
for (i = 0; i < call_trace_len; i++) {
if (call_trace[i].depth > 10) {
printf("~~~~");
} else {
for (j = 0; j < call_trace[i].depth; j++) {
printf(" ");
}
}
printf("%04X", call_trace[i].addr);
if (call_trace[i].rep > 1) {
printf(" x %d\n", call_trace[i].rep);
} else {
printf("\n");
}
}
}
void debug_init() {
int i;
for (i = 0; i < MEM_SIZE; i++) {
break_points[i] = 0;
}
state = DBG_IDLE;
call_trace_len = 0;
call_trace_depth = 0;
}
int parse_com(char *buf, command_t *com, int arg_len) {
char *pch, *endptr;
int *args[2] = {&com->arg_a, &com->arg_b};
int i;
com->name[0] = '\0';
com->arg_a = -1;
com->arg_b = -1;
pch = strtok(buf, " ");
if (pch != NULL) {
strncpy(com->name, pch, arg_len);
com->name[arg_len - 1] = '\0';
} else {
return -1;
}
for (i = 0; i < 2; i++) {
pch = strtok(NULL, " ");
if (pch != NULL) {
if (pch[1] == 'x') {
*args[i] = strtol(pch, &endptr, 16);
} else {
*args[i] = strtol(pch, &endptr, 10);
}
if (endptr == pch) {
*args[i] = -2;
return -2;
}
} else {
return i;
}
}
return 2;
}
int run_com(command_t *com) {
char *name = com->name;
// TODO: Make this nicer: in a loop?
if (strncmp(name, "help", ARG_LEN) == 0 || name[0] == 'h') {
com_help();
} else if (strncmp(name, "step", ARG_LEN) == 0 || name[0] == 's') {
com_step(com->arg_a);
return 1;
} else if (strncmp(name, "run", ARG_LEN) == 0) {
return -1;
} else if (strncmp(name, "regs", ARG_LEN) == 0 || name[0] == 'r') {
cpu_dump_reg();
} else if (strncmp(name, "break", ARG_LEN) == 0 || name[0] == 'b') {
com_break(com->arg_a);
} else if (strncmp(name, "clear", ARG_LEN) == 0) {
com_clear(com->arg_a);
} else if (strncmp(name, "continue", ARG_LEN) == 0 || name[0] == 'c') {
state = DBG_CONT;
return 1;
} else if (strncmp(name, "memory", ARG_LEN) == 0 || name[0] == 'm') {
//printf("memory!\n");
mem_dump(com->arg_a, com->arg_b);
} else if (strncmp(name, "dump", ARG_LEN) == 0) {
com_dump();
} else if (strncmp(name, "trace", ARG_LEN) == 0) {
com_trace();
} else if (strncmp(name, "disas", ARG_LEN) == 0 || name[0] == 'd') {
//printf("memory!\n");
com_disas(com->arg_a, com->arg_b);
} else if (strncmp(name, "io", ARG_LEN) == 0 || name[0] == 'i') {
//printf("memory!\n");
mem_dump_io_regs();
} else if (strncmp(name, "print", ARG_LEN) == 0 || name[0] == 'p') {
printf("[%04X] %02X\n", (uint16_t)com->arg_a,
mem_read_8((uint16_t)com->arg_a));
} else if (strncmp(name, "write", ARG_LEN) == 0 || name[0] == 'w') {
mem_write_8((uint16_t)com->arg_a, (uint8_t)com->arg_b);
printf("[%04X] %02X\n", (uint16_t)com->arg_a,
mem_read_8((uint16_t)com->arg_a));
} else if (strncmp(name, "quit", ARG_LEN) == 0 || name[0] == 'q') {
exit(0);
} else {
printf("E) Unrecognized command: %s\n", name);
}
return 0;
}
int debug_cpu_step() {
int cycles;
int op;
int sp, pc;
sp = *(regs->SP);
pc = regs->PC;
op = mem_read_8(regs->PC);
cycles = cpu_step();
if (op == 0xC4 || op == 0xD4 || op == 0xCC || op == 0xDC ||
op == 0xCD) {
// CALL op
if (sp == *(regs->SP) + 2) {
if (call_trace_len > 0 &&
call_trace[call_trace_len - 1].addr == pc) {
call_trace[call_trace_len - 1].rep += 1;
call_trace_depth += 1;
} else {
call_trace[call_trace_len].addr = pc;
call_trace[call_trace_len].depth = call_trace_depth;
call_trace[call_trace_len].rep = 1;
call_trace_len = (call_trace_len + 1) % max_trace;
call_trace_depth += 1;
}
}
} else if (op == 0xC0 || op == 0xD0 || op == 0xC8 || op == 0xD8 ||
op == 0xC9 || op == 0xD9) {
// RET op
if (sp == *(regs->SP) - 2) {
call_trace_depth -= 1;
}
}
return cycles;
}
int debug_run(int *debug_flag, int *debug_pause) {
char *line;
int res;
regs = cpu_get_regs();
if (*debug_pause) {
state = DBG_IDLE;
*debug_pause = 0;
}
switch(state) {
case DBG_IDLE:
break;
case DBG_STEP:
if (rem_steps > 0) {
disas_op(regs->PC);
rem_steps--;
return debug_cpu_step();
} else {
state = DBG_IDLE;
}
break;
case DBG_CONT:
if (break_points[regs->PC]) {
disas_op(regs->PC);
state = DBG_IDLE;
break;
} else {
/*
if (mem_read_8(regs->PC) == 0x30) {
state = DBG_IDLE;
break;
}
*/
return debug_cpu_step();
}
break;
}
while((line = linenoise(PROMPT)) != NULL) {
if (line[0] != '\0') {
linenoiseHistoryAdd(line);
if (parse_com(line, &com, ARG_LEN) < 0) {
printf("E) Error parsing command: %s\n", line);
free(line);
continue;
}
}
free(line);
res = run_com(&com);
if (res < 0) {
*debug_flag = 0;
return 0;
} else if (res == 0) {
continue;
} else {
if (state == DBG_CONT) {
return debug_cpu_step();
}
return 0;
}
}
return 0;
}