-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdebug.js
More file actions
278 lines (250 loc) · 6.36 KB
/
debug.js
File metadata and controls
278 lines (250 loc) · 6.36 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
import { hexbyte, hexword } from "./utils";
import { makeDisassembler } from "./z80/z80_dis";
// Module-level refs — set during debug_init from the SMS instance + callbacks.
let z80, bus, vdp;
let sms;
let disassemble;
let _audioEnable, _cycleCallback, _start;
let debugSerial = 0;
let annotations = null;
export function debug_init(romName, smsInstance, callbacks) {
sms = smsInstance;
z80 = sms.getZ80();
bus = sms.getBus();
vdp = sms.getVdp();
_audioEnable = callbacks.audioEnable;
_cycleCallback = callbacks.cycleCallback;
_start = callbacks.start;
const { disassemble: dis } = makeDisassembler(
(a) => bus.readbyte(a),
addressHtml,
);
disassemble = dis;
debugSerial = (bus.romBanks[1][0x3ffc] << 8) | bus.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) {
const virtual = bus.virtualAddress(addr);
if (annotations.labels[virtual]) {
return (
'<span class="addr label" title="' +
hexword(addr) +
'">' +
annotations.labels[virtual] +
"</span>"
);
}
return null;
}
export function addressHtml(addr) {
const name = addressName(addr);
if (name) {
return name + " (0x" + hexword(addr) + ")";
} else {
return '<span class="addr">0x' + hexword(addr) + "</span>";
}
}
function labelHtml(addr) {
const name = addressName(addr);
if (name) {
return name + ":";
} else {
return '<span class="addr">' + hexword(addr) + "</span>";
}
}
// TODO(#18) reinstate
// function endLabelEdit(content) {
// var addr = this.getAttribute("title") || content.previous;
// var virtual = bus.virtualAddress(parseInt(addr, 16));
// setLabel(virtual, content.current);
// }
let disassPc = 0;
function updateDisassembly(address) {
const disass = document.getElementById("disassembly");
disassPc = address;
for (const child of disass.children) {
const result = disassemble(address);
let hex = "";
for (let i = address; i < result[1]; ++i) {
if (hex !== "") hex += " ";
hex += hexbyte(bus.readbyte(i));
}
child.querySelector(".dis_addr").innerHTML = labelHtml(address);
child.classList.toggle("current", address === z80.pc);
child.querySelector(".instr_bytes").textContent = hex;
child.querySelector(".disassembly").innerHTML = result[0];
address = result[1];
}
}
function prevInstruction(address) {
for (
let startingPoint = address - 20;
startingPoint !== address;
startingPoint++
) {
let addr = startingPoint & 0xffff;
while (addr < address) {
const 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.classList.toggle("changed", newVal !== elem.textContent);
elem.textContent = newVal;
}
function updateFlags(f) {
const string = "cnp_h_zs";
for (let i = 0; i < 8; ++i) {
let r = string[i];
const elem = document.getElementById("z80_flag_" + r);
if (f & 1) {
r = r.toUpperCase();
}
if (elem) {
updateElement(elem, r);
}
f >>= 1;
}
}
export function showDebug(pc) {
document.getElementById("debug").style.display = "";
const disass = document.getElementById("disassembly");
for (let i = 0; i < disass.children.length / 2; i++) {
pc = prevInstruction(pc);
}
updateDebug(pc);
}
function updateDebug(pcOrNone) {
if (pcOrNone === null) {
pcOrNone = disassPc;
}
updateDisassembly(pcOrNone);
for (const reg in z80) {
const elem = document.getElementById("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]));
}
}
}
let i = 0;
for (const el of document.querySelectorAll("#vdp_registers > div .value")) {
if (el.offsetParent !== null) updateElement(el, hexbyte(vdp.vdp_regs[i++]));
}
i = 0;
for (const el of document.querySelectorAll("#pages .value")) {
updateElement(el, hexbyte(bus.pages[i++]));
}
updateFlags(z80.f);
}
export function stepUntil(f) {
_audioEnable(true);
sms.clearBreakpoint();
for (let i = 0; i < 65536; i++) {
z80.tstates = 0;
z80.eventNextEvent = 1;
sms.execOpcodes(_cycleCallback);
if (f()) break;
}
showDebug(z80.pc);
_audioEnable(false);
}
export function step() {
const curpc = z80.pc;
stepUntil(function () {
return z80.pc !== curpc;
});
}
function isUnconditionalJump(addr) {
const result = disassemble(addr);
return !!result[0].match(/^(JR 0x|JP|RET|RST)/);
}
export function stepOver() {
if (isUnconditionalJump(z80.pc)) {
return step();
}
const nextPc = nextInstruction(z80.pc);
stepUntil(function () {
return z80.pc === nextPc;
});
}
function isReturn(addr) {
const result = disassemble(addr);
return !!result[0].match(/^RET/);
}
export function stepOut() {
const sp = z80.sp;
stepUntil(function () {
if (z80.sp >= sp && isReturn(z80.pc)) {
const nextInstr = nextInstruction(z80.pc);
step();
return z80.pc !== nextInstr;
}
return false;
});
}
export function debugKeyPress(key) {
if (
Array.from(document.querySelectorAll("input")).some(
(el) => el.offsetParent !== null,
)
) {
return true;
}
const 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;
}