-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.c
170 lines (153 loc) · 6.59 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
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
#include <SDL2/SDL.h>
#include <gamegirl.h>
#include <opcodes.h>
#include <debugger.h>
#include <cb_opcodes.h>
#include <ppu.h>
#include <signal.h>
#define NEXT_BYTE m_dmg->m_cpu->m_boperand
#define NEXT_WORD m_dmg->m_cpu->m_woperand
int m_run_debugger(m_dmg_t *m_dmg)
{
printf("\e[1;1H\e[2J");
printf("\033[1;32mEntered Debugging Step Mode!\033[0;0m");
uint8_t m_dbgopc = READB(PC);
m_printregs(m_dmg);
printf("Legend: \033[0;34mPrevious Instruction\033[0;0m, \033[0;33mCurrent Instruction\033[0;0m\n\n");
signal(SIGINT, m_gamegirl_exit);
// Check if we're on the start of the BootROM (Don't print previous opcode as it doesn't exist)
if (PC)
{
/* Enter Debug First Opcode (Previous) */
if (m_dmg->m_cpu->m_opcode != 0xCB)
{
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 0)
{
printf("\033[0;34m00:%04X %02X \033[0;0m %s\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr);
}
else
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 1)
{
if ((m_dmg->m_cpu->m_opcode == 0x18) | (m_dmg->m_cpu->m_opcode == 0x20) | (m_dmg->m_cpu->m_opcode == 0x28))
{
printf("\033[0;34m00:%04X %02X %02X \033[0;0m %s$%04X (%d)\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr, ((m_dmg->m_cpu->prev_pc + 2) + (int8_t) READB(m_dmg->m_cpu->prev_pc + 1)),(int8_t) READB(m_dmg->m_cpu->prev_pc + 1));
}
else
{
printf("\033[0;34m00:%04X %02X \033[0;0m %s\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr);
}
}
else
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 2)
{
printf("\033[0;34m00:%04X %02X %02X %02X\033[0;0m %s$%04X\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), READB(m_dmg->m_cpu->prev_pc + 2), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr, ((READB(m_dmg->m_cpu->prev_pc + 2) << 8) | (READB(m_dmg->m_cpu->prev_pc + 1))));
}
}
else
{
// Extended (0xCB) Opcode Handling
printf("\033[0;34m00:%04X %02X %02X \033[0;0m %s\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), m_gb_instr_cb[READB(m_dmg->m_cpu->prev_pc + 1)].m_instr);
}
}
if (m_dbgopc != 0xCB)
{
/* Enter Debug Second Opcode (Current) */
if (m_gb_instr[m_dbgopc].m_operand == 0)
{
printf("\033[0;33m00:%04X %02X ->\033[0;0m %s\n\n", PC, READB(PC), m_gb_instr[m_dbgopc].m_instr);
}
else
if (m_gb_instr[m_dbgopc].m_operand == 1)
{
if ((m_dbgopc == 0x18) | (m_dbgopc == 0x20) | (m_dbgopc == 0x28))
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s$%04X (%d)\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr[m_dbgopc].m_instr, ((PC + 2) + (int8_t) READB(PC + 1)),(int8_t) READB(PC + 1));
}
else
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s$%02X\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr[m_dbgopc].m_instr, READB(PC + 1));
}
}
else
if (m_gb_instr[m_dbgopc].m_operand == 2)
{
printf("\033[0;33m00:%04X %02X %02X %02X ->\033[0;0m %s$%04X\n\n", PC, READB(PC), READB(PC + 1), READB(PC + 2), m_gb_instr[m_dbgopc].m_instr, ((READB(PC + 2) << 8) | (READB(PC + 1))));
}
}
else
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr_cb[READB(PC + 1)].m_instr);
}
printf("Press Enter to Step...\n");
while (true)
{
printf("\e[1;1H\e[2J");
m_dmg->m_cpu->prev_pc = PC;
size_t m_cycles = m_exec(m_dmg);
m_ppu_step(m_dmg, m_cycles);
m_interrupt_check(m_dmg);
m_printregs(m_dmg);
printf("Legend: \033[0;34mPrevious Instruction\033[0;0m, \033[0;33mCurrent Instruction\033[0;0m\n\n");
uint8_t m_dbgopc = READB(PC);
if (m_dmg->m_cpu->m_opcode != 0xCB)
{
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 0)
{
printf("\033[0;34m00:%04X %02X \033[0;0m %s\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr);
}
else
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 1)
{
if ((m_dmg->m_cpu->m_opcode == 0x18) | (m_dmg->m_cpu->m_opcode == 0x20) | (m_dmg->m_cpu->m_opcode == 0x28))
{
printf("\033[0;34m00:%04X %02X %02X \033[0;0m %s$%04X (%d)\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr, ((m_dmg->m_cpu->prev_pc + 2) + (int8_t) READB(m_dmg->m_cpu->prev_pc + 1)),(int8_t) READB(m_dmg->m_cpu->prev_pc + 1));
}
else
{
printf("\033[0;34m00:%04X %02X %02X \033[0;0m %s$%02X\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr, READB(m_dmg->m_cpu->prev_pc + 1));
}
}
else
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 2)
{
printf("\033[0;34m00:%04X %02X %02X %02X\033[0;0m %s$%04X\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), READB(m_dmg->m_cpu->prev_pc + 2), m_gb_instr[m_dmg->m_cpu->m_opcode].m_instr, ((READB(m_dmg->m_cpu->prev_pc + 2) << 8) | (READB(m_dmg->m_cpu->prev_pc + 1))));
}
}
else
{
// Extended (0xCB) Opcode Handling
printf("\033[0;34m00:%04X %02X %02X \033[0;0m %s\n", m_dmg->m_cpu->prev_pc, READB(m_dmg->m_cpu->prev_pc), READB(m_dmg->m_cpu->prev_pc + 1), m_gb_instr_cb[READB(m_dmg->m_cpu->prev_pc + 1)].m_instr);
}
if (m_dbgopc != 0xCB)
{
/* Enter Debug Second Opcode (Current) */
if (m_gb_instr[m_dbgopc].m_operand == 0)
{
printf("\033[0;33m00:%04X %02X ->\033[0;0m %s\n\n", PC, READB(PC), m_gb_instr[m_dbgopc].m_instr);
}
else
if (m_gb_instr[m_dbgopc].m_operand == 1)
{
if ((m_dbgopc == 0x18) | (m_dbgopc == 0x20) | (m_dbgopc == 0x28))
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s$%04X (%d)\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr[m_dbgopc].m_instr, ((PC + 2) + (int8_t) READB(PC + 1)),(int8_t) READB(PC + 1));
}
else
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s$%02X\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr[m_dbgopc].m_instr, READB(PC + 1));
}
}
else
if (m_gb_instr[m_dbgopc].m_operand == 2)
{
printf("\033[0;33m00:%04X %02X %02X %02X ->\033[0;0m %s$%04X\n\n", PC, READB(PC), READB(PC + 1), READB(PC + 2), m_gb_instr[m_dbgopc].m_instr, ((READB(PC + 2) << 8) | (READB(PC + 1))));
}
}
else
{
printf("\033[0;33m00:%04X %02X %02X ->\033[0;0m %s\n\n", PC, READB(PC), READB(PC + 1), m_gb_instr_cb[READB(PC + 1)].m_instr);
}
printf("Press Enter to Step...\n");
getchar();
}
}