-
Notifications
You must be signed in to change notification settings - Fork 29
/
debug.js
244 lines (216 loc) · 6 KB
/
debug.js
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
var debugSerial = 0;
var annotations = null;
function debug_init(romName) {
debugSerial = (romBanks[1][0x3ffc] << 8) | romBanks[1][0x3ffd];
if (!localStorage[debugSerial]) {
annotations = {'romName': romName, 'labels': {}};
} else {
annotations = JSON.parse(localStorage[debugSerial]);
}
console.log('Debug initialised for ' + romName + ' serial 0x' + hexword(debugSerial), annotations);
}
function persistAnnotations() {
localStorage[debugSerial] = JSON.stringify(annotations);
}
function setLabel(virtual, name) {
if (!name || name.match(/^[0-9]/)) {
delete annotations.labels[virtual];
} else {
annotations.labels[virtual] = name;
}
persistAnnotations();
updateDebug();
}
function addressName(addr) {
var virtual = virtualAddress(addr);
if (annotations.labels[virtual]) {
return '<span class="addr label" title="' + hexword(addr) + '">' + annotations.labels[virtual] + '</span>';
}
return null;
}
function addressHtml(addr) {
var name = addressName(addr);
if (name) {
return name + ' (0x' + hexword(addr) + ')';
} else {
return '<span class="addr">0x' + hexword(addr) + '</span>';
}
}
function labelHtml(addr) {
var name = addressName(addr);
if (name) {
return name + ':';
} else {
return '<span class="addr">' + hexword(addr) + '</span>';
}
}
function hexbyte(value) {
return ((value >> 4) & 0xf).toString(16) +
(value & 0xf).toString(16);
}
function endLabelEdit(content) {
var addr = $(this).attr('title') || content.previous;
var virtual = virtualAddress(parseInt(addr, 16));
setLabel(virtual, content.current);
}
var disassPc = 0;
function updateDisassembly(address) {
var disass = $('#disassembly');
disassPc = address;
disass.children().each(function () {
var result = disassemble(address);
var hex = "";
for (var i = address; i < result[1]; ++i) {
if (hex !== "") hex += " ";
hex += hexbyte(readbyte(i));
}
$(this).find('.dis_addr').html(labelHtml(address));
$(this).toggleClass('current', address === z80.pc);
$(this).find('.instr_bytes').text(hex);
$(this).find('.disassembly').html(result[0]);
$(this).find('.addr')
.editable({editBy: 'dblclick', editClass: 'editable', onSubmit: endLabelEdit})
.keypress(function (e) {
if (e.which === 13) $(this).blur();
});
address = result[1];
});
}
function prevInstruction(address) {
for (var startingPoint = address - 20; startingPoint !== address; startingPoint++) {
var addr = startingPoint & 0xffff;
while (addr < address) {
var result = disassemble(addr);
if (result[1] === address) {
return addr;
}
addr = result[1];
}
}
return 0;
}
function nextInstruction(address) {
return disassemble(address)[1] & 0xffff;
}
function updateElement(elem, newVal) {
elem.toggleClass('changed', newVal !== elem.text()).text(newVal);
}
function updateFlags(f) {
var string = 'cnp_h_zs';
for (var i = 0; i < 8; ++i) {
var r = string[i];
var elem = $('#z80_flag_' + r);
if (f & 1) {
r = r.toUpperCase();
}
if (elem) {
updateElement(elem, r);
}
f >>= 1;
}
}
function showDebug(pc) {
$('#debug').show(200);
for (var i = 0; i < $('#disassembly').children().length / 2; i++) {
pc = prevInstruction(pc);
}
updateDebug(pc);
}
function updateDebug(pcOrNone) {
if (pcOrNone === null) {
pcOrNone = disassPc;
}
updateDisassembly(pcOrNone);
for (var reg in z80) {
var elem = $('#z80_' + reg);
if (elem) {
if (reg.length > 1 && reg[reg.length - 1] !== 'h' && reg[reg.length - 1] !== 'l') {
updateElement(elem, hexword(z80[reg]));
} else {
updateElement(elem, hexbyte(z80[reg]));
}
}
}
var i = 0;
$('#vdp_registers > div:visible .value').each(function () {
updateElement($(this), hexbyte(vdp_regs[i++]));
});
i = 0;
$('#pages .value').each(function () {
updateElement($(this), hexbyte(pages[i++]));
});
updateFlags(z80.f);
}
function stepUntil(f) {
audio_enable(true);
breakpointHit = false;
for (var i = 0; i < 65536; i++) {
tstates = 0;
event_next_event = 1;
z80_do_opcodes(cycleCallback);
if (f()) break;
}
showDebug(z80.pc);
audio_enable(false);
}
function step() {
var curpc = z80.pc;
stepUntil(function () {
return z80.pc !== curpc;
});
}
function isUnconditionalJump(addr) {
var result = disassemble(addr);
return !!result[0].match(/^(JR 0x|JP|RET|RST)/);
}
function stepOver() {
if (isUnconditionalJump(z80.pc)) {
return step();
}
var nextPc = nextInstruction(z80.pc);
stepUntil(function () {
return z80.pc === nextPc;
});
}
function isReturn(addr) {
var result = disassemble(addr);
return !!result[0].match(/^RET/);
}
function stepOut() {
var sp = z80.sp;
stepUntil(function () {
if (z80.sp >= sp && isReturn(z80.pc)) {
var nextInstr = nextInstruction(z80.pc);
step();
return z80.pc !== nextInstr;
}
return false;
});
}
function debugKeyPress(key) {
if ($('input:visible').length) {
return true;
}
var keyStr = String.fromCharCode(key);
switch (keyStr) {
case 'k':
updateDisassembly(prevInstruction(disassPc));
break;
case 'j':
updateDisassembly(nextInstruction(disassPc));
break;
case 'n':
step();
break;
case 'm':
stepOver();
break;
case 'u':
stepOut();
break;
case 'g':
start();
break;
}
return true;
}