-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.htm
More file actions
626 lines (495 loc) · 17.6 KB
/
debugger.htm
File metadata and controls
626 lines (495 loc) · 17.6 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<html>
<head>
<link href='https://schickt.de/font/px8x8.css' rel='stylesheet'>
<link rel="stylesheet" type="text/css" href="style/style.css" media="screen" />
<meta charset='utf-8' />
<style>
#display {
width: 100%;
height: 100%;
}
#rdb {
width: 50%;
height: 100%;
left: 100%;
top: 0;
position: absolute;
background-color: black;
transition: left 0.5s ease;
}
.command {
color: #b0b0b0;
}
#debugger-switch {
position: absolute;
margin-left: -24px;
width: 24px;
background-color: white;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
}
#debugger-output {
height: calc(100% - 32px);
width: calc(100% - 8px);
color: white;
overflow: scroll;
padding-left: 8px;
}
#debugger-input {
width: 100%;
}
body {
overflow: hidden;
}
</style>
<script src='rc-core.js'></script>
<script src='rc-rdb.js'></script>
<script src='rc-math.js'></script>
<script src='rc-address-modes.js'></script>
<script src='rc-preprocessor.js'></script>
<script src='rc-assembler.js'></script>
<script src='rc-classes.js'></script>
<script src='rc-instruction.js'></script>
<script src='rc-instruction-set.js'></script>
<script src='canvas-draw.js'></script>
<script src='canvas-font.js'></script>
<script>
var rdb
var environment
var memory
var core
var timer
var buttons
var scale = 1
var cycles = 1
function run() {
stop()
timer = setInterval(run_cycle, 20)
}
function stop() {
clearInterval(timer)
}
function run_cycle() {
for (var c = 0; c < cycles; c++) {
if (!core.step()) {
stop();
break;
}
}
draw()
}
function toggle_boost() {
cycles = cycles == 1 ? 1000 : 1
}
function draw_memory_1x1(w, h) {
for (var i = 0; i < w * h; i++) {
const idx = i % core.environment.core_size
const instruction = core.ram.memory[idx]
var color = kDarkGrey
if (instruction.execution_flag != -1) {
color = process_colors[0][instruction.execution_flag + 1]
} else if (instruction.write_flag != -1) {
color = process_colors[0][instruction.execution_flag + 1]
}
if (i >= core.environment.core_size) {
color = draw_darken(color)
}
x = i % w
y = ~~(i / w)
draw_pixel(x, y, color)
}
}
function draw_memory_medium(w, h, size) {
const cols = ~~(w / size)
const rows = ~~(h / size)
function location(index) {
return {
x: index % cols,
y: ~~(index / cols)
}
}
for (var i = 0; i < cols * rows; i++) {
const idx = i % core.environment.core_size
const instruction = core.ram.memory[idx]
var c1 = process_colors[1][instruction.write_flag + 1]
var c2 = process_colors[1][instruction.read_flag + 1]
var c3 = process_colors[1][instruction.execution_flag + 1]
const l = location(i)
const x = l.x * size
const y = l.y * size
if (i != idx) {
c1 = draw_darken(c1)
c2 = draw_darken(c2)
c3 = draw_darken(c3)
}
draw_triangles(x, y, size, size, c2, c1)
draw_rect(x + 1, y + 1, size - 2, size - 2, c3)
}
}
function draw_memory_big(w, h, num_visible) {
const size = row_height
const cols = ~~(w / size)
function location(index) {
return {
x: index % cols,
y: ~~(index / cols)
}
}
for (var i = 0; i < num_visible; i++) {
const dark = i >= core.environment.core_size
const idx = i % core.environment.core_size
const instruction = core.ram.memory[idx]
const i_hash = instruction.hash()
if (i_hash == memory[i]) {
continue
}
memory[i] = i_hash
var color = process_colors[0][instruction.execution_flag + 1]
var color1 = process_colors[2][instruction.read_flag + 1]
var color2 = process_colors[2][instruction.write_flag + 1]
if (dark) {
color = draw_darken(color)
color1 = draw_darken(color1)
color2 = draw_darken(color2)
}
var l = location(i)
const x = l.x * size
const y = l.y * size
draw_triangles(x, y, size, size, color1, color2)
draw_symbol(x, y, instruction.op.opcode, color)
}
}
function draw_memory(dx, dy, w, h) {
const size = row_height
const cols = ~~(w / size)
const rows = ~~(h / size)
draw_translate_to(dx, dy)
const num_visible = cols * rows
if (num_visible < core.environment.core_size) {
const sizes = [7, 6, 5, 4, 3, 1]
for (var s = 0; s < sizes.length; s++) {
const sz = sizes[s]
if ((~~(w/sz) * ~~(h/sz)) >= core.environment.core_size) {
if (sz == 1) {
draw_memory_1x1(w, h)
} else {
draw_memory_medium(w, h, sz)
}
break;
}
}
} else {
draw_memory_big(w, h, num_visible)
}
draw_translate_to(0, 0)
}
function draw_memory_dump(x, y, w, data, highlight) {
draw_rect(x, y, w, row_height * data.length, kBlack)
draw_rect(x, y + highlight * row_height, w, row_height, kDarkGrey)
for (var i = 0; i < data.length; i++) {
draw_text(x, y + i * row_height, data[i], (i == highlight) ? kWhite : kSilver)
}
return row_height * data.length
}
function draw(redraw) {
const start = performance.now()
if (redraw === undefined) {
redraw = false
}
const canvas = document.getElementById('display')
if (canvas.width != canvas.clientWidth || canvas.height != canvas.clientHeight || redraw) {
canvas.width = canvas.clientWidth
canvas.height = canvas.clientHeight
memory = []
draw_set_context(canvas, scale)
}
var width = ~~(canvas.width / scale / row_height) * row_height
var height = ~~(canvas.height / scale / row_height) * row_height
const max_width = 2 + ('' + (core.environment.core_size - 1)).length
var dump_width = 4 + 3 * max_width
var dx = row_height * dump_width
var dy = 0
draw_rect(0, 0, dx, height - 3 * row_height, kBlack);
const total_rows = ~~(canvas.height / scale / row_height)
const usable_rows = total_rows - 8 // \n, next, cycle, \n, toolbar
const rows_per_process = ~~(usable_rows / core.processes.length)
const memory_rows = rows_per_process - 2 /* title, spaver */
const look_behind = ~~(memory_rows / 4)
const look_ahead = memory_rows - 1 - look_behind
for (var pid = 0; pid < core.processes.length; pid++) {
const next = core.processes[pid].next()
var name = core.processes[pid].program.metadata.get('NAME')
const max_len = dump_width - 2 - (pid + '').length
if (name.length > max_len) {
name = name.substr(0, max_len - 1) + char_ellipsis
}
draw_text(0, dy, `#${pid} ${name}`, process_colors[1][pid + 1])
dy += row_height
dy += row_height + draw_memory_dump(0, dy, dx, core.memory_dump(next - look_behind, next + look_ahead), look_behind)
const n = core.processes[pid].instruction_pointers.length
title = `${n} THREAD${n == 1 ? '' : 'S'}: `
draw_text(0, dy, title, kWhite)
var tids = core.processes[pid].instruction_pointers.slice(0)
var s = tids.join(' ')
const available = 22 - title.length
if (s.length > available) {
s = s.substr(0, available - 1) + char_ellipsis
} else {
s = s.substr(0, available)
}
draw_text(title.length * row_height, dy, s, kWhite)
dy += row_height * 2
}
var c = process_colors[1][core.current_process_index + 1]
const ni = core.next_instruction();
if (ni !== null) {
var index = core.current_process().next() + ''
const l = (core.environment.core_size - 1 + '').length
index = index.padStart(l, '0')
draw_text(0, dy, `${index} ${ni.to_string(max_width - 1)}`, c)
}
dy += row_height
draw_text(0, dy, `CYCLE ${core.cycle}`, kWhite)
// graphical representation of the memory
draw_memory(dx, 0, width - dx, height - 3 * row_height)
// ui buttons
var by = height - 3 * row_height
draw_rect(0, by, width, 3 * row_height, kDarkestGrey)
buttons = []
var b
var bx = 0
b = draw_button(bx, by, String.fromCharCode(28))
bx += b.width + row_height
b.doit = run_cycle
buttons.push(b)
b = draw_button(bx, by, String.fromCharCode(30))
bx += b.width + row_height
b.doit = run
buttons.push(b)
b = draw_button(bx, by, String.fromCharCode(15))
bx += b.width + row_height
b.doit = stop
buttons.push(b)
b = draw_button(bx, by, '-')
bx += b.width
b.doit = function () {
set_size(-1)
}
buttons.push(b)
b = draw_button(bx, by, '+')
bx += b.width + row_height
b.doit = function () {
set_size(+1)
}
buttons.push(b)
b = draw_checkbox(bx, by, 'BOOST', (cycles > 1))
bx += b.width + row_height
b.doit = function () {
toggle_boost();
draw()
}
buttons.push(b)
// UI handler
canvas.onclick = function (e) {
var x = e.clientX / scale
var y = e.clientY / scale
for (var b = 0; b < buttons.length; b++) {
const btn = buttons[b]
if ((x >= btn.x1) && (y >= btn.y1) && (btn.x2 >= x) && (btn.y2 >= y)) {
btn.doit();
return;
}
}
}
draw_flip()
const elapsed = ~~(100 * (performance.now() - start)) / 100
document.title = elapsed + 'ms ' + (redraw ? 'full' : 'part')
}
function init_everything() {
environment = new Environment()
memory = new Array(environment.core_size)
core = new Core(environment)
rdb = new Debugger()
rdb.attach_to(core)
rdb.output_handler = function(text) {
const div = document.getElementById('debugger-output')
div.innerHTML += text + '\n'
div.scrollTop = div.scrollHeight
}
var code0 =
`
;name idle
idle jmp 0
`
var code_imp =
`
;name imp
imp mov 0 next
next dat 0 0
`
var code_dwarf =
`
;name dwarf
org jump
add #4, 3
mov 2, @2
JUMP jmp -2
dummy dat #42 #0
`
var code_stone =
`
;name stone
mov <2, 3
add 3, -1
jmp -2
dat #0 #0
dat #-5084, #5084
DAT 0 0
MOV 0 0
ADD 0 0
NOP 0 0
JMP 0 0
SPL 0 0
SUB 0 0
JMZ 0 0
JMN 0 0
CMP 0 0
SLT 0 0
DJN 0 0
NOP 0 0
`
var code_mice =
`
;name mice
org start
ptr dat #0 , #0
start mov #12 , ptr
loop mov @ptr , <copy
djn loop , ptr
spl @copy , 0
add #653 , copy
jmz start , ptr
copy dat #0 , #833
`
var test_code =
`
start CMP #0, 1
DAT #1, #0
CMP 4, 5
CMP 4, 5
DAT #1, #1
JMP start
DAT #0, #2
DAT #2, #2
DAT #2, #2
`
// init both prpgrams
core.load_program(new Program(code_imp, core.environment))
core.load_program(new Program(code_imp, core.environment))
draw(true)
// listen to messages from parent window
window.addEventListener('message', message_handler, false)
}
function set_size(diff) {
scale = Math.max(1.0, scale + diff / 2)
draw(true)
}
function message_handler(event) {
items = event.data.split('::')
action = items.shift(0)
switch (action) {
case 'launch':
load(items)
break
case 'environment':
core = new Core(JSON.parse(atob(items[0])))
draw(true)
break
}
}
function load(warriors) {
core.reset()
warriors.forEach(base64 => {
code = atob(base64)
core.load_program(new Program(code, core.environment))
})
draw(1)
}
function focus_dbg_input() {
const input = document.getElementById('debugger-input')
const pos = input.value.length - 1
input.focus()
input.selectionStart = pos
}
function rdb_run(instruction) {
rdb.command(instruction)
}
document.onkeydown = function(event) {
if (document.activeElement.nodeName == 'INPUT') {
const input = document.getElementById('debugger-input')
switch (event.keyCode) {
case 13:
const value = input.value
input.value = ''
const output = document.getElementById('debugger-output')
if (value == 'clear') {
output.innerHTML = ''
}
else if (value && value.length > 0) {
output.innerHTML += `<span class='command'>(rdb) ${value}</span>\n`
rdb_run(value)
}
break
case 38: // up arrow
input.value = rdb.history.get_previous()
focus_dbg_input()
break
case 40: // down arrow
input.value = rdb.history.get_next()
focus_dbg_input()
break
default:
console.log(event.keyCode)
}
return
}
event = event || window.event;
switch (event.keyCode) {
case 'S'.charCodeAt(0):
run_cycle()
break
case 109: // -
set_size(-1)
break;
case 107: // +
set_size(+1)
break
case 'D'.charCodeAt(0):
for (var i = 0; i < 10; i++) run_cycle()
break
default:
console.log(event.keyCode)
}
}
// TEMP
var visible = false
function toggle_debugger() {
const dgb = document.getElementById('rdb')
visible = !visible
if (visible) {
dgb.style.left = '50%'
} else {
dgb.style.left = '100%'
}
}
</script>
</head>
<body onload='init_everything()' onresize='draw()'>
<canvas id='display'></canvas>
<div id='rdb'>
<div id='debugger-switch' class='clickable' onclick='toggle_debugger()'><br> < <br> > <br> </div>
<pre id='debugger-output'></pre>
<input id='debugger-input' placeholder='(rdb)'>
</div>
</body>
</html>