| 9120e874 Andy McCall |
1 |
import socket, json, time, sys, base64 |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 9009 |
| 7 |
|
| 8 |
|
| 9 |
def send_dap(sock, msg): |
| 10 |
body = json.dumps(msg) |
| 11 |
header = "Content-Length: %d\r\n\r\n" % len(body) |
| 12 |
sock.sendall((header + body).encode()) |
| 13 |
|
| 14 |
def recv_dap(sock, timeout=3): |
| 15 |
sock.settimeout(timeout) |
| 16 |
data = b'' |
| 17 |
results = [] |
| 18 |
try: |
| 19 |
while True: |
| 20 |
chunk = sock.recv(4096) |
| 21 |
if not chunk: |
| 22 |
break |
| 23 |
data += chunk |
| 24 |
while b'\r\n\r\n' in data: |
| 25 |
header_end = data.index(b'\r\n\r\n') |
| 26 |
header = data[:header_end].decode() |
| 27 |
for line in header.split('\r\n'): |
| 28 |
if line.startswith('Content-Length:'): |
| 29 |
cl = int(line.split(':')[1].strip()) |
| 30 |
break |
| 31 |
body_start = header_end + 4 |
| 32 |
if len(data) >= body_start + cl: |
| 33 |
body = data[body_start:body_start + cl].decode() |
| 34 |
data = data[body_start + cl:] |
| 35 |
results.append(json.loads(body)) |
| 36 |
else: |
| 37 |
break |
| 38 |
except socket.timeout: |
| 39 |
pass |
| 40 |
return results |
| 41 |
|
| 42 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 43 |
try: |
| 44 |
sock.connect(('127.0.0.1', PORT)) |
| 45 |
except ConnectionRefusedError: |
| 46 |
print("ERROR: Cannot connect to DAP server on port %d. Is emulator running with -debugport?" % PORT) |
| 47 |
sys.exit(1) |
| 48 |
print("Connected to DAP server on port %d" % PORT) |
| 49 |
|
| 50 |
passed = 0 |
| 51 |
failed = 0 |
| 52 |
|
| 53 |
def check(name, msgs, expect_type=None, expect_cmd=None, expect_success=True): |
| 54 |
global passed, failed |
| 55 |
if not msgs: |
| 56 |
print(" FAIL %s: no response" % name) |
| 57 |
failed += 1 |
| 58 |
return None |
| 59 |
for m in msgs: |
| 60 |
mtype = m.get("type", "") |
| 61 |
mcmd = m.get("command", m.get("event", "")) |
| 62 |
ok = True |
| 63 |
if expect_type and mtype != expect_type: |
| 64 |
ok = False |
| 65 |
if expect_cmd and mcmd != expect_cmd: |
| 66 |
ok = False |
| 67 |
if expect_type == "response" and expect_success is not None: |
| 68 |
if m.get("success") != expect_success: |
| 69 |
ok = False |
| 70 |
if ok: |
| 71 |
print(" PASS %s" % name) |
| 72 |
passed += 1 |
| 73 |
return m |
| 74 |
print(" FAIL %s: unexpected response: %s" % (name, json.dumps(msgs[0])[:100])) |
| 75 |
failed += 1 |
| 76 |
return msgs[0] |
| 77 |
|
| 78 |
|
| 79 |
send_dap(sock, {"seq": 1, "type": "request", "command": "initialize", "arguments": {"adapterID": "test"}}) |
| 80 |
time.sleep(0.5) |
| 81 |
msgs = recv_dap(sock, 1) |
| 82 |
m = check("initialize response", [m for m in msgs if m.get("type") == "response"], "response", "initialize") |
| 83 |
if m and m.get("body", {}).get("supportsConfigurationDoneRequest"): |
| 84 |
print(" Capabilities OK") |
| 85 |
check("initialized event", [m for m in msgs if m.get("event") == "initialized"], "event", "initialized") |
| 86 |
|
| 87 |
|
| 88 |
send_dap(sock, {"seq": 2, "type": "request", "command": "launch", "arguments": {"stopOnEntry": True}}) |
| 89 |
time.sleep(0.5) |
| 90 |
msgs = recv_dap(sock, 1) |
| 91 |
check("launch response", [m for m in msgs if m.get("type") == "response"], "response", "launch") |
| 92 |
check("thread event", [m for m in msgs if m.get("event") == "thread"], "event", "thread") |
| 93 |
|
| 94 |
|
| 95 |
send_dap(sock, {"seq": 3, "type": "request", "command": "configurationDone", "arguments": {}}) |
| 96 |
time.sleep(0.5) |
| 97 |
msgs = recv_dap(sock, 1) |
| 98 |
check("configDone response", [m for m in msgs if m.get("type") == "response"], "response", "configurationDone") |
| 99 |
check("stopped event (entry)", [m for m in msgs if m.get("event") == "stopped"], "event", "stopped") |
| 100 |
|
| 101 |
|
| 102 |
send_dap(sock, {"seq": 4, "type": "request", "command": "threads"}) |
| 103 |
time.sleep(0.3) |
| 104 |
msgs = recv_dap(sock, 1) |
| 105 |
m = check("threads response", msgs, "response", "threads") |
| 106 |
if m: |
| 107 |
threads = m.get("body", {}).get("threads", []) |
| 108 |
if len(threads) == 1 and threads[0].get("name") == "6502 CPU": |
| 109 |
print(" Thread: %s (id=%d)" % (threads[0]["name"], threads[0]["id"])) |
| 110 |
|
| 111 |
|
| 112 |
send_dap(sock, {"seq": 5, "type": "request", "command": "stackTrace", "arguments": {"threadId": 1}}) |
| 113 |
time.sleep(0.3) |
| 114 |
msgs = recv_dap(sock, 1) |
| 115 |
m = check("stackTrace response", msgs, "response", "stackTrace") |
| 116 |
if m: |
| 117 |
frames = m.get("body", {}).get("stackFrames", []) |
| 118 |
if frames: |
| 119 |
print(" Frame: %s" % frames[0].get("name", "?")) |
| 120 |
|
| 121 |
|
| 122 |
send_dap(sock, {"seq": 6, "type": "request", "command": "scopes", "arguments": {"frameId": 1}}) |
| 123 |
time.sleep(0.3) |
| 124 |
msgs = recv_dap(sock, 1) |
| 125 |
m = check("scopes response", msgs, "response", "scopes") |
| 126 |
if m: |
| 127 |
scopes = m.get("body", {}).get("scopes", []) |
| 128 |
print(" Scopes: %s" % ", ".join(s["name"] for s in scopes)) |
| 129 |
|
| 130 |
|
| 131 |
send_dap(sock, {"seq": 7, "type": "request", "command": "variables", "arguments": {"variablesReference": 1}}) |
| 132 |
time.sleep(0.3) |
| 133 |
msgs = recv_dap(sock, 1) |
| 134 |
m = check("variables (registers)", msgs, "response", "variables") |
| 135 |
if m: |
| 136 |
vlist = m.get("body", {}).get("variables", []) |
| 137 |
for v in vlist[:3]: |
| 138 |
print(" %s = %s" % (v["name"], v["value"])) |
| 139 |
|
| 140 |
|
| 141 |
send_dap(sock, {"seq": 8, "type": "request", "command": "stepIn", "arguments": {"threadId": 1}}) |
| 142 |
time.sleep(0.5) |
| 143 |
msgs = recv_dap(sock, 1) |
| 144 |
check("stepIn response", [m for m in msgs if m.get("type") == "response"], "response", "stepIn") |
| 145 |
check("stopped event (step)", [m for m in msgs if m.get("event") == "stopped"], "event", "stopped") |
| 146 |
|
| 147 |
|
| 148 |
send_dap(sock, {"seq": 9, "type": "request", "command": "evaluate", "arguments": {"expression": "PC"}}) |
| 149 |
time.sleep(0.3) |
| 150 |
msgs = recv_dap(sock, 1) |
| 151 |
m = check("evaluate (PC)", msgs, "response", "evaluate") |
| 152 |
if m: |
| 153 |
print(" PC = %s" % m.get("body", {}).get("result", "?")) |
| 154 |
|
| 155 |
|
| 156 |
send_dap(sock, {"seq": 10, "type": "request", "command": "evaluate", "arguments": {"expression": "$0000"}}) |
| 157 |
time.sleep(0.3) |
| 158 |
msgs = recv_dap(sock, 1) |
| 159 |
m = check("evaluate ($0000)", msgs, "response", "evaluate") |
| 160 |
if m: |
| 161 |
print(" [$0000] = %s" % m.get("body", {}).get("result", "?")) |
| 162 |
|
| 163 |
|
| 164 |
send_dap(sock, {"seq": 11, "type": "request", "command": "setExceptionBreakpoints", "arguments": {"filters": []}}) |
| 165 |
time.sleep(0.3) |
| 166 |
msgs = recv_dap(sock, 1) |
| 167 |
check("setExceptionBreakpoints", msgs, "response", "setExceptionBreakpoints") |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
body_a = json.dumps({"seq": 90, "type": "request", "command": "threads"}) |
| 178 |
body_b = json.dumps({"seq": 91, "type": "request", "command": "evaluate", |
| 179 |
"arguments": {"expression": "PC"}}) |
| 180 |
blob = ("Content-Length: %d\r\n\r\n%s" % (len(body_a), body_a) + |
| 181 |
"Content-Length: %d\r\n\r\n%s" % (len(body_b), body_b)) |
| 182 |
sock.sendall(blob.encode()) |
| 183 |
time.sleep(0.5) |
| 184 |
msgs = recv_dap(sock, 2) |
| 185 |
seqs = sorted(m.get("request_seq") for m in msgs if m.get("type") == "response") |
| 186 |
if seqs == [90, 91]: |
| 187 |
print(" PASS both pipelined requests answered") |
| 188 |
passed += 1 |
| 189 |
else: |
| 190 |
print(" FAIL both pipelined requests answered: got %r" % (seqs,)) |
| 191 |
failed += 1 |
| 192 |
|
| 193 |
|
| 194 |
send_dap(sock, {"seq": 92, "type": "request", "command": "threads"}) |
| 195 |
time.sleep(0.3) |
| 196 |
msgs = recv_dap(sock, 1) |
| 197 |
check("connection still live after pipelining", msgs, "response", "threads") |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
def joystick(seq, args, pause=0.3): |
| 209 |
send_dap(sock, {"seq": seq, "type": "request", "command": "x16/joystick", |
| 210 |
"arguments": args}) |
| 211 |
time.sleep(pause) |
| 212 |
for m in recv_dap(sock, 1): |
| 213 |
if m.get("type") == "response" and m.get("command") == "x16/joystick": |
| 214 |
return m |
| 215 |
return None |
| 216 |
|
| 217 |
|
| 218 |
def check_joystick(name, resp, want): |
| 219 |
global passed, failed |
| 220 |
if resp is None or not resp.get("success"): |
| 221 |
print(" FAIL %s: no successful response" % name) |
| 222 |
failed += 1 |
| 223 |
return None |
| 224 |
body = resp.get("body", {}) |
| 225 |
for key, expected in want.items(): |
| 226 |
got = body.get(key) |
| 227 |
if key == "buttons": |
| 228 |
got = sorted(got or []) |
| 229 |
expected = sorted(expected) |
| 230 |
if got != expected: |
| 231 |
print(" FAIL %s: %s was %r, expected %r" % (name, key, got, expected)) |
| 232 |
failed += 1 |
| 233 |
return body |
| 234 |
print(" PASS %s" % name) |
| 235 |
passed += 1 |
| 236 |
return body |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
check_joystick("x16/joystick holds a button", |
| 242 |
joystick(93, {"index": 0, "buttons": ["a"]}), |
| 243 |
{"index": 0, "mask": 0xFFFE, "buttons": ["a"], "enabled": True, |
| 244 |
"slotEnabled": True}) |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
check_joystick("x16/joystick enables the port it drives", |
| 249 |
joystick(94, {"index": 2, "buttons": ["left", "b"]}), |
| 250 |
{"index": 2, "slotEnabled": True, "enabled": True, |
| 251 |
"buttons": ["left", "b"]}) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
check_joystick("x16/joystick queries without changing state", |
| 256 |
joystick(95, {"index": 0}), |
| 257 |
{"index": 0, "mask": 0xFFFE, "buttons": ["a"], "enabled": True}) |
| 258 |
|
| 259 |
|
| 260 |
check_joystick("x16/joystick releases buttons left off the list", |
| 261 |
joystick(96, {"index": 0, "buttons": ["start"]}), |
| 262 |
{"index": 0, "buttons": ["start"], "enabled": True}) |
| 263 |
|
| 264 |
|
| 265 |
check_joystick("x16/joystick accepts a raw mask", |
| 266 |
joystick(97, {"index": 1, "mask": 0xFFFF & ~(1 << 4)}), |
| 267 |
{"index": 1, "buttons": ["up"], "enabled": True}) |
| 268 |
|
| 269 |
check_joystick("x16/joystick releases the port", |
| 270 |
joystick(98, {"index": 0, "enabled": False}), |
| 271 |
{"index": 0, "mask": 0xFFFF, "buttons": [], "enabled": False}) |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
check_joystick("x16/joystick connects a controller with nothing held", |
| 277 |
joystick(99, {"index": 0, "enabled": True}), |
| 278 |
{"index": 0, "mask": 0xFFFF, "buttons": [], "enabled": True, |
| 279 |
"slotEnabled": True}) |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
for _slot in (0, 1, 2): |
| 284 |
joystick(100 + _slot, {"index": _slot, "enabled": False}) |
| 285 |
|
| 286 |
m = joystick(102, {"index": 9, "buttons": ["a"]}) |
| 287 |
if m is not None and not m.get("success"): |
| 288 |
print(" PASS x16/joystick rejects a port that does not exist") |
| 289 |
passed += 1 |
| 290 |
else: |
| 291 |
print(" FAIL x16/joystick rejects a port that does not exist") |
| 292 |
failed += 1 |
| 293 |
|
| 294 |
m = joystick(103, {"index": 0, "buttons": ["turbo"]}) |
| 295 |
if m is not None and not m.get("success"): |
| 296 |
print(" PASS x16/joystick rejects an unknown button") |
| 297 |
passed += 1 |
| 298 |
else: |
| 299 |
print(" FAIL x16/joystick rejects an unknown button") |
| 300 |
failed += 1 |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
def read_mem(seq, ref, count): |
| 310 |
send_dap(sock, {"seq": seq, "type": "request", "command": "readMemory", |
| 311 |
"arguments": {"memoryReference": ref, "count": count}}) |
| 312 |
time.sleep(0.3) |
| 313 |
for m in recv_dap(sock, 1): |
| 314 |
if m.get("type") == "response" and m.get("command") == "readMemory": |
| 315 |
return m |
| 316 |
return None |
| 317 |
|
| 318 |
|
| 319 |
def write_mem(seq, ref, data): |
| 320 |
send_dap(sock, {"seq": seq, "type": "request", "command": "writeMemory", |
| 321 |
"arguments": {"memoryReference": ref, |
| 322 |
"data": base64.b64encode(bytes(data)).decode()}}) |
| 323 |
time.sleep(0.3) |
| 324 |
for m in recv_dap(sock, 1): |
| 325 |
if m.get("type") == "response" and m.get("command") == "writeMemory": |
| 326 |
return m |
| 327 |
return None |
| 328 |
|
| 329 |
|
| 330 |
def mem_bytes(resp): |
| 331 |
if not resp or not resp.get("success"): |
| 332 |
return None |
| 333 |
return base64.b64decode(resp.get("body", {}).get("data", "")) |
| 334 |
|
| 335 |
|
| 336 |
def expect(name, cond): |
| 337 |
global passed, failed |
| 338 |
if cond: |
| 339 |
print(" PASS %s" % name) |
| 340 |
passed += 1 |
| 341 |
else: |
| 342 |
print(" FAIL %s" % name) |
| 343 |
failed += 1 |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
cpu = mem_bytes(read_mem(110, "0x1B000", 16)) |
| 349 |
vram = mem_bytes(read_mem(111, "vram:1B000", 16)) |
| 350 |
expect("readMemory reads VRAM", vram is not None and len(vram) == 16) |
| 351 |
expect("VRAM and the same CPU address are different memory", cpu is not None and vram != cpu) |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
r = read_mem(112, "vram:1B000", 4) |
| 356 |
expect("the echoed address keeps the vram: prefix", |
| 357 |
r is not None and r.get("body", {}).get("address", "").lower().startswith("vram:")) |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
scratch = "vram:1F9C0" |
| 362 |
before = mem_bytes(read_mem(113, scratch, 4)) |
| 363 |
write_mem(114, scratch, [0xDE, 0xAD, 0xBE, 0xEF]) |
| 364 |
after = mem_bytes(read_mem(115, scratch, 4)) |
| 365 |
expect("writeMemory writes VRAM", after == b"\xde\xad\xbe\xef") |
| 366 |
if before is not None: |
| 367 |
write_mem(116, scratch, list(before)) |
| 368 |
expect("the scratch bytes restore", mem_bytes(read_mem(117, scratch, 4)) == before) |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
a = mem_bytes(read_mem(118, "vram:$1B000", 8)) |
| 373 |
b = mem_bytes(read_mem(119, "vram:0x1B000", 8)) |
| 374 |
expect("vram: accepts $ and 0x alike", a is not None and a == b == vram[:8]) |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
for bad in ("vram:zzz", "notanaddress", "vram:"): |
| 380 |
r = read_mem(120, bad, 4) |
| 381 |
expect("readMemory rejects %r instead of reading address 0" % bad, |
| 382 |
r is not None and not r.get("success")) |
| 383 |
|
| 384 |
r = read_mem(121, "$C000", 4) |
| 385 |
expect("readMemory accepts a $-prefixed CPU address", r is not None and r.get("success")) |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
def perf(seq, args=None): |
| 396 |
send_dap(sock, {"seq": seq, "type": "request", "command": "x16/perfConfig", |
| 397 |
"arguments": args if args is not None else {}}) |
| 398 |
time.sleep(0.3) |
| 399 |
for m in recv_dap(sock, 1): |
| 400 |
if m.get("command") == "x16/perfConfig": |
| 401 |
return m |
| 402 |
return None |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
send_dap(sock, {"seq": 129, "type": "request", "command": "continue", |
| 408 |
"arguments": {"threadId": 1}}) |
| 409 |
time.sleep(0.5) |
| 410 |
recv_dap(sock, 1) |
| 411 |
|
| 412 |
r = perf(130, {"enabled": True, "targetFps": 60, "idleMode": "auto", "reset": True}) |
| 413 |
expect("x16/perfConfig arms profiling", r is not None and r.get("success")) |
| 414 |
budget = r["body"]["budget"] if r and r.get("success") else {} |
| 415 |
expect("profiling reports itself enabled", budget.get("enabled") is True) |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
expect("a VGA frame at 8 MHz is 134400 cycles", budget.get("cyclesPerFrame") == 134400) |
| 421 |
expect("which is 256 cycles per scanline", budget.get("cyclesPerScanline") == 256) |
| 422 |
expect("the 60 fps budget is one whole frame", |
| 423 |
budget.get("budgetCycles") == 134400 and budget.get("framesPerBudget") == 1) |
| 424 |
|
| 425 |
|
| 426 |
r = perf(131, {"targetFps": 30}) |
| 427 |
b30 = r["body"]["budget"] if r and r.get("success") else {} |
| 428 |
expect("30 fps spans two vsyncs", b30.get("framesPerBudget") == 2) |
| 429 |
expect("and doubles the budget", b30.get("budgetCycles") == 268800) |
| 430 |
expect("without changing the frame period", b30.get("cyclesPerFrame") == 134400) |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
r = perf(132, {"targetFps": 120}) |
| 436 |
b120 = r["body"]["budget"] if r and r.get("success") else {} |
| 437 |
expect("a sub-frame target gets a fraction of a frame", |
| 438 |
b120.get("budgetCycles", 0) < 134400 * 0.6) |
| 439 |
|
| 440 |
perf(133, {"targetFps": 60}) |
| 441 |
|
| 442 |
time.sleep(1.0) |
| 443 |
send_dap(sock, {"seq": 134, "type": "request", "command": "x16/perfStats", |
| 444 |
"arguments": {"windows": [1, 0], "includeFrames": 3}}) |
| 445 |
time.sleep(0.5) |
| 446 |
stats = None |
| 447 |
for m in recv_dap(sock, 2): |
| 448 |
if m.get("command") == "x16/perfStats": |
| 449 |
stats = m |
| 450 |
expect("x16/perfStats responds", stats is not None and stats.get("success")) |
| 451 |
|
| 452 |
if stats and stats.get("success"): |
| 453 |
body = stats["body"] |
| 454 |
cur = body.get("current", {}) |
| 455 |
expect("a completed frame is reported", bool(cur)) |
| 456 |
expect("the frame holds a frame's worth of cycles", |
| 457 |
130000 <= cur.get("totalCycles", 0) <= 140000) |
| 458 |
|
| 459 |
expect("work and idle account for every cycle", |
| 460 |
cur.get("workCycles", 0) + cur.get("idleCycles", -1) == cur.get("totalCycles")) |
| 461 |
|
| 462 |
wins = body.get("windows", []) |
| 463 |
expect("both requested windows came back", len(wins) == 2) |
| 464 |
if wins: |
| 465 |
bw = wins[0].get("budgetWork", {}) |
| 466 |
expect("percentiles are ordered", |
| 467 |
bw.get("min", 0) <= bw.get("p50", 0) <= bw.get("p95", 0) <= bw.get("max", 0)) |
| 468 |
expect("the window reports overrun accounting", |
| 469 |
"overruns" in wins[0] and "overrunPct" in wins[0]) |
| 470 |
frames = body.get("frames", []) |
| 471 |
expect("raw frame samples came back", len(frames) == 3) |
| 472 |
if len(frames) >= 2: |
| 473 |
expect("raw frames are oldest first", frames[0]["frame"] < frames[-1]["frame"]) |
| 474 |
|
| 475 |
|
| 476 |
r = perf(135, {"reset": True, |
| 477 |
"zones": [{"name": "kernal", "start": 0xC000, "end": 0xFFFF}]}) |
| 478 |
expect("zones can be declared over DAP", r is not None and r.get("success")) |
| 479 |
time.sleep(0.8) |
| 480 |
send_dap(sock, {"seq": 136, "type": "request", "command": "x16/perfStats", |
| 481 |
"arguments": {"windows": [1]}}) |
| 482 |
time.sleep(0.5) |
| 483 |
for m in recv_dap(sock, 2): |
| 484 |
if m.get("command") == "x16/perfStats": |
| 485 |
zones = m["body"].get("zones", []) |
| 486 |
expect("the declared zone came back", len(zones) == 1) |
| 487 |
if zones: |
| 488 |
expect("the zone is named as declared", zones[0].get("name") == "kernal") |
| 489 |
expect("and has window statistics", "window30s" in zones[0]) |
| 490 |
|
| 491 |
|
| 492 |
r = perf(137, {"enabled": False, "zones": []}) |
| 493 |
expect("profiling can be switched back off", |
| 494 |
r is not None and r["body"]["budget"].get("enabled") is False) |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
send_dap(sock, {"seq": 138, "type": "request", "command": "x16/perfConfig", |
| 504 |
"arguments": {"enabled": True, "reset": True}}) |
| 505 |
time.sleep(1.0) |
| 506 |
send_dap(sock, {"seq": 139, "type": "request", "command": "x16/perfStats", |
| 507 |
"arguments": {"windows": [1]}}) |
| 508 |
time.sleep(0.5) |
| 509 |
bwstats = None |
| 510 |
for m in recv_dap(sock, 2): |
| 511 |
if m.get("command") == "x16/perfStats": |
| 512 |
bwstats = m |
| 513 |
|
| 514 |
expect("x16/perfStats carries a bandwidth object", |
| 515 |
bwstats is not None and "bandwidth" in bwstats.get("body", {})) |
| 516 |
|
| 517 |
if bwstats and bwstats.get("success"): |
| 518 |
bwb = bwstats["body"]["bandwidth"] |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
expect("a scanline is 800 clocks", bwb.get("lineClocks") == 800) |
| 524 |
expect("a VRAM access is four bytes", bwb.get("bytesPerAccess") == 4) |
| 525 |
expect("and costs two clocks", bwb.get("accessClocks") == 2) |
| 526 |
expect("VGA scans 525 lines", bwb.get("scanlinesPerFrame") == 525) |
| 527 |
|
| 528 |
expect("bandwidth accounting reports itself enabled", bwb.get("enabled") is True) |
| 529 |
expect("a frame has been accounted", bwb.get("linesActive", 0) > 0) |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
layers = bwb.get("layers", []) |
| 534 |
expect("both layers are reported", len(layers) == 2) |
| 535 |
if len(layers) == 2: |
| 536 |
expect("layer 1 is fetching at the BASIC prompt", |
| 537 |
layers[1].get("fetches", 0) > 0) |
| 538 |
expect("its bytes are its fetches times four", |
| 539 |
layers[1].get("bytes") == layers[1].get("fetches", 0) * 4) |
| 540 |
|
| 541 |
|
| 542 |
peak = layers[1].get("peakFetches", 0) |
| 543 |
expect("a text-mode line fetches about 120 words", 118 <= peak <= 126) |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
peak_clocks = bwb.get("peakLineClocks", 0) |
| 548 |
expect("the peak scanline is a real cost", peak_clocks > 0) |
| 549 |
expect("and fits inside the scanline", peak_clocks <= 800) |
| 550 |
expect("so no line is over budget", bwb.get("linesOverBudget", -1) == 0) |
| 551 |
expect("percentile and mean are ordered", |
| 552 |
bwb.get("meanLineClocks", 0) <= bwb.get("p95LineClocks", 0) <= peak_clocks) |
| 553 |
expect("sprites are left the rest of the scanline", |
| 554 |
bwb.get("spriteBusHeadroomClocksAtPeak", -1) == 800 - peak_clocks) |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
expect("and the clock unit is declared", bwb.get("clockUnits") == "bus") |
| 559 |
|
| 560 |
port = bwb.get("port", {}) |
| 561 |
expect("the data port is reported", isinstance(port, dict) and "writeBytes" in port) |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
send_dap(sock, {"seq": 140, "type": "request", "command": "x16/perfConfig", |
| 579 |
"arguments": {"reset": True}}) |
| 580 |
time.sleep(0.3) |
| 581 |
recv_dap(sock, 1) |
| 582 |
send_dap(sock, {"seq": 141, "type": "request", "command": "writeMemory", |
| 583 |
"arguments": {"memoryReference": "vram:1B000", |
| 584 |
"data": base64.b64encode(bytes(64)).decode()}}) |
| 585 |
time.sleep(0.5) |
| 586 |
recv_dap(sock, 1) |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
debugger_charged = False |
| 599 |
guest_port_bytes = 0 |
| 600 |
seq = 142 |
| 601 |
for _ in range(5): |
| 602 |
for _ in range(20): |
| 603 |
send_dap(sock, {"seq": seq, "type": "request", "command": "x16/perfStats", |
| 604 |
"arguments": {"windows": [1], "includeZones": False}}) |
| 605 |
seq += 1 |
| 606 |
time.sleep(0.03) |
| 607 |
for m in recv_dap(sock, 1): |
| 608 |
if m.get("command") != "x16/perfStats": |
| 609 |
continue |
| 610 |
p = m.get("body", {}).get("bandwidth", {}).get("port", {}) |
| 611 |
guest_port_bytes = max(guest_port_bytes, |
| 612 |
p.get("writeBytes", 0) + p.get("readBytes", 0)) |
| 613 |
|
| 614 |
if p.get("writeBytes", 0) >= 64: |
| 615 |
debugger_charged = True |
| 616 |
|
| 617 |
expect("a debugger VRAM write is not charged to the guest", not debugger_charged) |
| 618 |
expect("but the guest's own data-port traffic is", guest_port_bytes > 0) |
| 619 |
|
| 620 |
|
| 621 |
perf(seq, {"enabled": False}) |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
def evaluate(s, expr, pause=0.3): |
| 636 |
global _eval_seq |
| 637 |
_eval_seq += 1 |
| 638 |
send_dap(s, {"seq": 500 + _eval_seq, "type": "request", "command": "evaluate", |
| 639 |
"arguments": {"expression": expr}}) |
| 640 |
time.sleep(pause) |
| 641 |
for m in recv_dap(s, 1): |
| 642 |
if m.get("type") == "response" and m.get("command") == "evaluate": |
| 643 |
return m.get("body", {}).get("result", "") |
| 644 |
return "" |
| 645 |
|
| 646 |
def bp_addrs(s): |
| 647 |
|
| 648 |
out = evaluate(s, "bp_list") |
| 649 |
return [t for t in out.split() if t.startswith("$")] |
| 650 |
|
| 651 |
_eval_seq = 0 |
| 652 |
|
| 653 |
baseline = bp_addrs(sock) |
| 654 |
print(" breakpoints before this session: %s" % (" ".join(baseline) or "(none)")) |
| 655 |
|
| 656 |
|
| 657 |
scratch = "3F00" |
| 658 |
evaluate(sock, "bp_add " + scratch) |
| 659 |
if ("$" + scratch) in bp_addrs(sock): |
| 660 |
print(" PASS console bp_add arms a breakpoint") |
| 661 |
passed += 1 |
| 662 |
else: |
| 663 |
print(" FAIL console bp_add arms a breakpoint") |
| 664 |
failed += 1 |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
shared = baseline[0][1:] if baseline else None |
| 669 |
if shared: |
| 670 |
evaluate(sock, "bp_add " + shared) |
| 671 |
|
| 672 |
send_dap(sock, {"seq": 13, "type": "request", "command": "disconnect", |
| 673 |
"arguments": {"terminateDebuggee": False}}) |
| 674 |
time.sleep(0.3) |
| 675 |
msgs = recv_dap(sock, 1) |
| 676 |
check("disconnect response", msgs, "response", "disconnect") |
| 677 |
sock.close() |
| 678 |
time.sleep(0.5) |
| 679 |
|
| 680 |
after_session = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 681 |
after_session.connect(('127.0.0.1', PORT)) |
| 682 |
send_dap(after_session, {"seq": 1, "type": "request", "command": "initialize", |
| 683 |
"arguments": {"adapterID": "test"}}) |
| 684 |
time.sleep(0.5) |
| 685 |
recv_dap(after_session, 1) |
| 686 |
|
| 687 |
remaining = bp_addrs(after_session) |
| 688 |
print(" breakpoints after teardown: %s" % (" ".join(remaining) or "(none)")) |
| 689 |
|
| 690 |
if ("$" + scratch) not in remaining: |
| 691 |
print(" PASS the session's own breakpoint went with it") |
| 692 |
passed += 1 |
| 693 |
else: |
| 694 |
print(" FAIL the session's own breakpoint went with it: still armed, and now " |
| 695 |
"owned by nobody") |
| 696 |
failed += 1 |
| 697 |
|
| 698 |
if not baseline: |
| 699 |
print(" SKIP a pre-existing breakpoint survives a session " |
| 700 |
"(start the emulator with -bp to check this)") |
| 701 |
elif all(a in remaining for a in baseline): |
| 702 |
print(" PASS every pre-existing breakpoint survived the session") |
| 703 |
passed += 1 |
| 704 |
else: |
| 705 |
print(" FAIL every pre-existing breakpoint survived the session: had %s, left %s" |
| 706 |
% (" ".join(baseline), " ".join(remaining) or "(none)")) |
| 707 |
failed += 1 |
| 708 |
|
| 709 |
after_session.close() |
| 710 |
time.sleep(0.3) |
| 711 |
|
| 712 |
|
| 713 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 714 |
sock.connect(('127.0.0.1', PORT)) |
| 715 |
send_dap(sock, {"seq": 1, "type": "request", "command": "initialize", |
| 716 |
"arguments": {"adapterID": "test"}}) |
| 717 |
time.sleep(0.4) |
| 718 |
recv_dap(sock, 1) |
| 719 |
send_dap(sock, {"seq": 12, "type": "request", "command": "disconnect", "arguments": {"terminateDebuggee": False}}) |
| 720 |
time.sleep(0.3) |
| 721 |
msgs = recv_dap(sock, 1) |
| 722 |
check("disconnect response", msgs, "response", "disconnect") |
| 723 |
|
| 724 |
sock.close() |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 734 |
probe.connect(('127.0.0.1', PORT)) |
| 735 |
probe.sendall(b"Content-Length: 100000\r\n\r\n") |
| 736 |
time.sleep(0.5) |
| 737 |
probe.close() |
| 738 |
time.sleep(0.5) |
| 739 |
|
| 740 |
try: |
| 741 |
after = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 742 |
after.connect(('127.0.0.1', PORT)) |
| 743 |
time.sleep(0.5) |
| 744 |
send_dap(after, {"seq": 1, "type": "request", "command": "initialize", |
| 745 |
"arguments": {"adapterID": "test"}}) |
| 746 |
time.sleep(0.5) |
| 747 |
msgs = recv_dap(after, 2) |
| 748 |
check("server still usable after an oversized frame", msgs, "response", "initialize") |
| 749 |
after.close() |
| 750 |
except OSError as e: |
| 751 |
print(" FAIL server still usable after an oversized frame: %s" % e) |
| 752 |
failed += 1 |
| 753 |
|
| 754 |
print("\n%d passed, %d failed" % (passed, failed)) |
| 755 |
sys.exit(0 if failed == 0 else 1) |