-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChip8Sim.html
More file actions
4848 lines (4511 loc) · 152 KB
/
Chip8Sim.html
File metadata and controls
4848 lines (4511 loc) · 152 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>CHIP-8 Development Environment</title>
<meta charset="utf-8"/>
</head>
<style>
table.reg,table.mem {
font-size: 110%;
border: 1px solid black;
border-collapse: collapse;
/* border-spacing: 0px; */
}
tr.reg,tr.mem {
font-size: 110%;
background-color: #000000;
border: none;
padding: 0px;
margin: 0px;
}
td.reg,td.mem {
font-size: 70%;
background-color: #ffffff;
border: 1px solid black;
padding: 0px;
padding-left: 4px;
padding-right: 4px;
margin: 0px;
text-align: center;
/*outline-width: 0px;*/
}
input.reg,input.mem {
font-size: 110%;
font-family: "Lucida Console", "Courier New", monospace;
text-align: right;
background-color: #ffffff;
border: none;
padding: 0px;
margin: 0px;
height: 12px;
/*outline-width: 0px;*/
}
input.regleft {
font-size: 110%;
font-family: "Lucida Console", "Courier New", monospace;
text-align: left;
background-color: #ffffff;
border: none;
padding: 0px;
margin: 0px;
height: 12px;
/*outline-width: 0px;*/
}
table.ex, th.ex, tr.ex, td.ex {
border: 2px solid black;
font-family: monospace;
font-weight: bold;
font-size: 100%;
padding-top: 3px;
padding-left: 5px;
padding-right: 5px;
padding-bottom: 1px;
border-collapse: collapse;
}
th.ex {
font-size: 120%;
}
</style>
<script>
debugging = false; // Creates a text area during onload and fills while running
mem = [];
mem_top = 0x0200; // Address of memory shown at top of the memory list
V = [];
PC = 0x0200; // Program Counter typically starts at 0x0200
SP = 0x0001; // Set stack pointer at 1 since it is incremented before use
I = 0x0000; // Index register
N = 0; // Number of instructions since reset
T = 0; // Timer Register
CT = 1; // Clock Time (time per clock tick)
ST = 15; // Steps per clock tick
DT = 11; // Delay Time (time per delay count)
num_rows = 32;
num_cols = 64;
screen = [];
fg_color = "#AAAAff";
bg_color = "#000044";
font_addr = 0x0FA0; // This could be anywhere but stack or program memory
assem_loaded_once = false;
function log_entry() {
if (debugging) {
logtextarea = document.getElementById("log");
logtextarea.value += " PC:"+hex4(PC);
logtextarea.value += " SP:"+hex4(SP);
logtextarea.value += " I: "+hex4(I);
logtextarea.value += " V:["+V.toString()+"]";
logtextarea.value += "\n";
}
}
function replaceAll ( s, t, r ) {
new_s = (s + " ").trim();
do {
last_s = (new_s + " ").trim();
new_s = last_s.replace(t,r);
} while ( ! (new_s === last_s) );
return ( new_s );
}
function pad0 (val, n) {
v = val;
while (v.length < n) {
v = "0" + v;
}
return ( v );
}
function hex (val) {
return ( val.toString(16).toUpperCase() );
}
function hex2 (val) {
return ( pad0 ( val.toString(16).toUpperCase(), 2 ) );
}
function hex3 (val) {
return ( pad0 ( val.toString(16).toUpperCase(), 3 ) );
}
function hex4 (val) {
return ( pad0 ( val.toString(16).toUpperCase(), 4 ) );
}
function hex6 (val) {
return ( pad0 ( val.toString(16).toUpperCase(), 6 ) );
}
function clear_screen() {
// Clear the display memory
for (r=0; r<num_rows; r++) {
for (c=0; c<num_cols; c++) {
screen[r][c] = 0;
}
}
draw_screen();
}
function draw_screen() {
da = document.getElementById ("drawing_area");
ctxt = da.getContext('2d');
ctxt.fillStyle = bg_color;
ctxt.fillRect(0, 0, da.width, da.height);
ctxt.fillStyle = fg_color;
for (r=0; r<num_rows; r++) {
for (c=0; c<num_cols; c++) {
if (screen[r][c] != 0) {
ctxt.fillRect(5*c, 5*r, 5, 5);
}
}
}
}
function update_display() {
update_variables();
// Show the Memory starting with mem_top (even addresses)
base_addr = 2 * Math.floor(mem_top/2);
for (i=0; i<16; i++) {
i_text = i.toString(16).toUpperCase().trim();
addr = base_addr + (2*i);
mem_val = (mem[addr] << 8) + mem[addr+1];
document.getElementById("A"+i_text).innerHTML = hex4 ( addr );
document.getElementById("M"+i_text).value = hex4 ( mem_val );
}
// Show the register values
for (i=0; i<16; i++) {
i_text = i.toString(16).toUpperCase().trim();
v = "V" + i_text;
document.getElementById(v).value = hex2 ( V[i] );
}
document.getElementById("PC").value = hex4 ( PC );
document.getElementById("SP").value = hex4 ( SP );
opcode = hex2(mem[PC]) + hex2(mem[PC+1])
document.getElementById("PCINST").value = disassemble_line( opcode.trim() );
document.getElementById("[I]").value = hex4 ( I );
document.getElementById("N").value = N.toString();
document.getElementById("ST").value = ST.toString();
document.getElementById("CT").value = CT.toString();
document.getElementById("DT").value = DT.toString();
}
function init_machine() {
// Initialize the memory
mem = [];
for (i=0; i<0x10000; i++) {
mem.push(0xFF);
}
// Load the Font data (80 bytes total)
// Each entry is 4 columns x 5 rows
// Sprites are 8 columns wide but
// these only use the first 4 bits
font_data = [
[0xF0, 0x90, 0x90, 0x90, 0xF0], // 0
[0x20, 0x60, 0x20, 0x20, 0x70], // 1
[0xF0, 0x10, 0xF0, 0x80, 0xF0], // 2
[0xF0, 0x10, 0xF0, 0x10, 0xF0], // 3
[0x90, 0x90, 0xF0, 0x10, 0x10], // 4
[0xF0, 0x80, 0xF0, 0x10, 0xF0], // 5
[0xF0, 0x80, 0xF0, 0x90, 0xF0], // 6
[0xF0, 0x10, 0x20, 0x40, 0x40], // 7
[0xF0, 0x90, 0xF0, 0x90, 0xF0], // 8
[0xF0, 0x90, 0xF0, 0x10, 0xF0], // 9
[0xF0, 0x90, 0xF0, 0x90, 0x90], // A
[0xE0, 0x90, 0xE0, 0x90, 0xE0], // B
[0xF0, 0x80, 0x80, 0x80, 0xF0], // C
[0xE0, 0x90, 0x90, 0x90, 0xE0], // D
[0xF0, 0x80, 0xF0, 0x80, 0xF0], // E
[0xF0, 0x80, 0xF0, 0x80, 0x80] // F
];
i = font_addr;
// console.log ( "Loading Font Data" );
for (r=0; r<font_data.length; r++) {
for (c=0; c<font_data[r].length; c++) {
mem[i] = font_data[r][c];
i+=1;
}
}
// Initialize the "V" registers
V = [];
for (i=0; i<16; i++) {
V.push(0);
}
// Initialize the display memory
screen = [];
for (r=0; r<num_rows; r++) {
row = [];
for (c=0; c<num_cols; c++) {
row.push(0);
}
screen.push(row)
}
document.getElementById("ST").value = ST.toString();
document.getElementById("CT").value = CT.toString();
document.getElementById("DT").value = DT.toString();
update_display();
draw_screen();
}
function load_menus() {
example_menu = document.getElementById("example_select");
ex = document.getElementsByClassName("exsrc");
for (i=0; i<ex.length; i++) {
opt = document.createElement ( "option" );
opt.value = ex[i].id;
opt.innerHTML = ex[i].id;
example_menu.appendChild ( opt );
// console.log ( "Example: " + ex[i].id );
}
ex = document.getElementsByClassName("exhex");
for (i=0; i<ex.length; i++) {
opt = document.createElement ( "option" );
opt.value = "Hex:"+ex[i].id;
opt.innerHTML = "Hex:"+ex[i].id;
example_menu.appendChild ( opt );
// console.log ( "Example: " + ex[i].id );
}
}
function onload() {
load_menus();
selector = document.getElementById("example_select");
selector[selector.selectedIndex].value = "Generate a Maze";
on_example_change();
init_machine();
clear_screen();
window.addEventListener("keyup", handle_keyup);
window.addEventListener("keydown", handle_keydn);
// window.addEventListener("keypress", log);
if (debugging) {
plog = document.getElementById("plog");
plog.innerHTML = '<textarea id="log" rows="1" cols="60" ' +
'autocomplete="off" autocorrect="off" autocapitalize="off" ' +
'spellcheck="false"></textarea><p/><hr/><p/>';
logtextarea = document.getElementById("log");
logtextarea.value = "";
}
}
// Keymap starting at 7 (has problems with / key being intercepted)
keymap7 = { '7':0, '8':1, '9':2, '0':3,
'u':4, 'i':5, 'o':6, 'p':7,
'j':8, 'k':9, 'l':10,';':11,
'm':12,',':13,'.':14,'/':15 };
// Keymap starting at 6 (seems to work)
keymap = { '6':0, '7':1, '8':2, '9':3,
'y':4, 'u':5, 'i':6, 'o':7,
'h':8, 'j':9, 'k':10,'l':11,
'n':12,'m':13,',':14,'.':15 };
keydown = [ 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0 ];
lastkey = null;
new_key = null;
key_wait = null;
function wait_for_key() {
if (key_wait == null) {
// First call
lastkey = null;
key_wait = setTimeout ( "wait_for_key()", 10 ); // pause_time_ms );
} else {
clearTimeout ( key_wait );
if (lastkey == null) {
// Keep waiting
key_wait = setTimeout ( "wait_for_key()", 10 ); // pause_time_ms );
} else {
// Got a key
new_key = lastkey;
lastkey = null;
key_wait = null;
}
}
}
function bd(n,v) {
// console.log ( "Button " + n + " is " + v );
lastkey = n;
keydown[n] = v;
}
function kd(n,v) {
// console.log ( "Key " + n + " is " + v );
lastkey = n;
keydown[n] = v;
}
function handle_keydn(event) {
// console.log ( "Key down is " + event.key );
k = event.key.toUpperCase();
kv = 0;
if ('abcdefABCDEF'.indexOf(k) >= 0) {
kv = 9 + (k.codePointAt() % 16);
} else {
kv = Number(k);
}
kd(kv,1); // Call with key down (1)
}
function handle_keyup(event) {
// console.log ( "Key up is " + event.key );
k = event.key.toUpperCase();
kv = 0;
if ('abcdefABCDEF'.indexOf(k) >= 0) {
kv = 9 + (k.codePointAt() % 16);
} else {
kv = Number(k);
}
kd(kv,0); // Call with key up (0)
}
function showkeys() {
console.log ( "====================================" );
for (j=0; j<4; j++) {
console.log ( j + ": " + keydown[(4*j)+0] + " " + keydown[(4*j)+1] + " " + keydown[(4*j)+2] + " " + keydown[(4*j)+3] );
}
}
function changed_format() {
// fmt = document.getElementById ("format_to").value;
// console.log ("Format changed to " + fmt);
}
function get_b_comma_vx ( s ) {
result = []
comma = s.indexOf(",");
p1 = s.slice(0,comma).trim();
p2 = s.slice(comma+1).trim();
result.push (p1);
result.push (p2.slice(1));
// console.log ("get_vx_comma_b (" + s + ") = " + result );
return ( result );
}
function get_vx_comma_b ( s ) {
result = []
comma = s.indexOf(",");
p1 = s.slice(0,comma).trim();
p2 = s.slice(comma+1).trim();
result.push (p1.slice(1));
result.push (p2);
// console.log ("get_vx_comma_b (" + s + ") = " + result );
return ( result );
}
function get_vx_comma_vy ( s ) {
result = []
comma = s.indexOf(",");
p1 = s.slice(0,comma).trim();
p2 = s.slice(comma+1).trim();
result.push (p1.slice(1));
result.push (p2.slice(1));
// console.log ("get_vx_comma_vy (" + s + ") = " + result );
return ( result );
}
function assemble_text ( srctxt ) {
// console.log ("Assemble code");
// srctxt = document.getElementById ( "source" ).value;
// console.log ( "Text = " + srctxt );
if (srctxt.length == 0) return;
// Initialize the binary text output
bintxt = "";
// Normalize line endings and remove empty lines
text = replaceAll( srctxt, "\r", "\n");
while (text.indexOf("\n\n") >= 0) {
text = replaceAll(text, "\n\n", "\n");
}
// console.log ( text );
// Convert all white space to spaces
// text = text.replace(/\s+/g,' ');
// Remove all double spaces
while (text.indexOf(" ") >= 0) {
text = replaceAll(text, " ", " ");
}
// console.log ( text );
// Split the text into an array of lines
lines = text.split("\n");
// console.log ( "lines=" + lines );
// Remove all comments and empty lines
new_lines = [];
for (l=0; l<lines.length; l++) {
line = lines[l].toUpperCase().trim();
if (line.indexOf(";") >= 0) {
// Remove the comment
line = line.substr(0,line.indexOf(";")).trim();
}
if (line.trim().length > 0) {
new_lines.push ( line.trim() );
}
}
lines = new_lines;
// Remove any and all last lines containing empty labels
while ( lines[lines.length-1].indexOf(":") == (lines[lines.length-1].length-1) ) {
// Copy all but the last line
new_lines = [];
for (l=0; l<(lines.length-1); l++) {
new_lines[l] = lines[l];
}
lines = new_lines;
}
// Merge empty labels with subsequent lines
new_lines = [];
l = 0;
while (l<lines.length) {
line = lines[l];
if (line.indexOf(":") == (line.length-1)) {
// This is an empty label so join it with the next line
l = l + 1;
new_lines.push ( line + " " + lines[l] );
} else {
// Just push this line by itself
new_lines.push ( line );
}
l = l + 1;
}
lines = new_lines;
// Compute the label addresses
current_addr = 0x200;
label_dict = {};
for (l=0; l<lines.length; l++) {
line = lines[l].toUpperCase().trim();
if (line.indexOf(":") >= 0) {
// Store the label and its location
label = line.substr(0,line.indexOf(":"));
// label_dict[label] = ((2*l)+0x200).toString(16); ////// INCORRECT for byte addressing
label_dict[label] = current_addr.toString(16);
while (label_dict[label].length < 3) {
label_dict[label] = '0' + label_dict[label];
}
// Remove the label from the line of code for assembly
lines[l] = lines[l].substr(line.indexOf(":")+1).trim();
}
// Increment the current address
if (lines[l].startsWith("BYTE")) {
// Increment by 1 for a single data byte
current_addr += 1;
} else if ( lines[l].startsWith("DATA") || lines[l].startsWith("WORD") ) {
// Increment by 2 for an instruction
current_addr += 2;
} else {
current_addr += 2;
}
}
keys = Object.keys(label_dict);
// console.log ( "labels = " + keys );
for (l=0; l<lines.length; l++) {
for (ki=0; ki<keys.length; ki++) {
// console.log ( "Searching for " + keys[ki] );
lines[l] = replaceAll( lines[l].toUpperCase(), keys[ki], label_dict[keys[ki]]);
}
}
// Process the lines one by one
for (l=0; l<lines.length; l++) {
line = lines[l].toUpperCase().trim();
// line = lines[l].replace(/\s+/g,''); // Remove all white space
if (line.indexOf(";") >= 0) {
// Remove the comment
line = line.substr(0,line.indexOf(";")).trim();
}
if (line.length > 0) {
if (line.indexOf(":") >= 0) {
// Throw away the labels for now
line = line.substr(line.indexOf(":")+1).trim();
}
if (line.slice(0,5) == "DATA ") {
bintxt += line.slice(5).trim() + "\n";
} else if (line.slice(0,5) == "WORD ") {
bintxt += line.slice(5).trim() + "\n";
} else if (line.slice(0,5) == "BYTE ") {
bintxt += line.slice(5).trim() + "\n";
} else if (line.slice(0,4) == "SYS ") {
bintxt += "0" + line.slice(4).trim() + "\n";
} else if (line.slice(0,3) == "CLS") {
bintxt += "00E0\n";
} else if (line.slice(0,3) == "RET") {
bintxt += "00EE\n";
} else if (line.slice(0,3) == "JP ") {
// JP can take the following forms:
// JP nnn --> 1nnn
// JP V0,nnn --> Bnnn
if (line.indexOf(",") >= 0) {
bintxt += "B" + line.slice(line.indexOf(",")+1).trim() + "\n";
} else {
bintxt += "1" + line.slice(2).trim() + "\n";
}
} else if (line.slice(0,5) == "CALL ") {
// CALL takes the following form:
// CALL nnn
bintxt += "2" + line.slice(5).trim() + "\n";
} else if (line.slice(0,3) == "SE ") {
// SE (Skip when Equal) can take the following forms:
// SE Vx,byte -> 3xbb
// SE Vx,Vy -> 5xy0
if (line.split(",")[1].indexOf("V") < 0) {
// SE Vx,byte -> 3xbb
vb = get_vx_comma_b ( line.slice(3) );
bintxt += "3" + vb[0] + vb[1] + "\n";
} else {
// SE Vx,Vy -> 5xy0
vv = get_vx_comma_vy ( line.slice(3) );
bintxt += "5" + vv[0] + vv[1] + "0" + "\n";
}
} else if (line.slice(0,4) == "SNE ") {
// SNE (Skip when Not Equal) can take the following forms:
// SNE Vx,byte -> 4xbb
// SNE Vx,Vy -> 9xy0
if (line.split(",")[1].indexOf("V") < 0) {
// SNE Vx,byte -> 4xbb
vb = get_vx_comma_b ( line.slice(3) );
bintxt += "4" + vb[0] + vb[1] + "\n";
} else {
// SNE Vx,Vy -> 9xy0
vv = get_vx_comma_vy ( line.slice(3) );
bintxt += "9" + vv[0] + vv[1] + "0" + "\n";
}
} else if (line.slice(0,3) == "LD ") {
// LD (Load) can take the following forms:
// LD Vx,byte -> 6xbb
// LD Vx,Vy -> 8xy0
// LD I,nnn -> Annn
// LD Vx,DT -> Fx07
// LD Vx,K -> Fx0A
// LD DT,Vx -> Fx15
// LD ST,Vx -> Fx18
// LD F,Vx -> Fx29
// LD B,Vx -> Fx33
// LD [I],Vx -> Fx55
// LD Vx,[I] -> Fx65
pars = line.slice(3).trim().split(",");
pars[0] = pars[0].trim();
pars[1] = pars[1].trim();
// Handle the easy ones first
if (pars[0] == "I") {
// LD I,nnn -> Annn
bintxt += "A" + pars[1].trim() + "\n";
} else if (pars[0] == "DT") {
// LD DT,Vx -> Fx15
bintxt += "F" + pars[1].slice(1).trim() + "15\n";
} else if (pars[0] == "ST") {
// LD ST,Vx -> Fx18
bintxt += "F" + pars[1].slice(1).trim() + "18\n";
} else if (pars[0] == "F") {
// LD F,Vx -> Fx29
bintxt += "F" + pars[1].slice(1).trim() + "29\n";
} else if (pars[0] == "B") {
// LD B,Vx -> Fx33
bintxt += "F" + pars[1].slice(1).trim() + "33\n";
} else if (pars[0] == "[I]") {
// LD [I],Vx -> Fx55
bintxt += "F" + pars[1].slice(1).trim() + "55\n";
} else if (pars[1] == "DT") {
// LD Vx,DT -> Fx07
bintxt += "F" + pars[0].slice(1).trim() + "07\n";
} else if (pars[1] == "K") {
// LD Vx,K -> Fx0A
} else if (pars[1] == "[I]") {
// LD Vx,[I] -> Fx65
bintxt += "F" + pars[0].slice(1).trim() + "65\n";
} else {
if (line.split(",")[1].indexOf("V") < 0) {
// LD Vx,byte -> 6xbb
vb = get_vx_comma_b ( line.slice(3) );
bintxt += "6" + vb[0] + vb[1] + "\n";
} else {
// LD Vx,Vy -> 8xy0
vv = get_vx_comma_vy ( line.slice(3) );
bintxt += "8" + vv[0] + vv[1] + "0" + "\n";
}
}
} else if (line.slice(0,4) == "ADD ") {
// ADD can take the following forms:
// ADD Vx,byte -> 7xbb
// ADD Vx,Vy -> 8xy4
// ADD I,Vx -> Fx1E
pars = line.slice(3).trim().split(",");
pars[0] = pars[0].trim();
pars[1] = pars[1].trim();
if (pars[0] == "I") {
// ADD I,Vx -> Fx1E
bintxt += "F" + pars[1].slice(1).trim() + "1E\n";
} else {
if (pars[1].indexOf("V") < 0) {
// ADD Vx,byte -> 7xbb
vb = get_vx_comma_b ( line.slice(3) );
bintxt += "7" + vb[0] + vb[1] + "\n";
} else {
// ADD Vx,Vy -> 8xy0
vv = get_vx_comma_vy ( line.slice(3) );
bintxt += "8" + vv[0] + vv[1] + "4" + "\n";
}
}
} else if (line.slice(0,3) == "OR ") {
vv = get_vx_comma_vy ( line.slice(2).trim() );
bintxt += "8" + vv[0] + vv[1] + "1" + "\n";
} else if (line.slice(0,4) == "AND ") {
vv = get_vx_comma_vy ( line.slice(3).trim() );
bintxt += "8" + vv[0] + vv[1] + "2" + "\n";
} else if (line.slice(0,4) == "XOR ") {
vv = get_vx_comma_vy ( line.slice(3).trim() );
bintxt += "8" + vv[0] + vv[1] + "3" + "\n";
} else if (line.slice(0,4) == "SUB ") {
vv = get_vx_comma_vy ( line.slice(3).trim() );
bintxt += "8" + vv[0] + vv[1] + "5" + "\n";
} else if (line.slice(0,4) == "SHR ") {
if (line.indexOf(",") >= 0) {
// SHR Vx,Vy
vv = get_vx_comma_vy ( line.slice(3).trim() );
if (vv[0].length <= 0) {
vv[0] = "?";
}
} else {
// SHR Vx
vv = [line.slice(3).trim()[1],"1"];
}
bintxt += "8" + vv[0] + vv[1] + "6" + "\n";
} else if (line.slice(0,5) == "SUBN ") {
vv = get_vx_comma_vy ( line.slice(4).trim() );
bintxt += "8" + vv[0] + vv[1] + "7" + "\n";
} else if (line.slice(0,4) == "SHL ") {
if (line.indexOf(",") >= 0) {
// SHL Vx,Vy
vv = get_vx_comma_vy ( line.slice(3).trim() );
if (vv[0].length <= 0) {
vv[0] = "?";
}
} else {
// SHL Vx
vv = [line.slice(3).trim()[1],"1"];
}
bintxt += "8" + vv[0] + vv[1] + "E" + "\n";
} else if (line.slice(0,4) == "RND ") {
vb = get_vx_comma_b ( line.slice(3) );
bintxt += "C" + vb[0] + vb[1] + "\n";
} else if (line.slice(0,4) == "DRW ") {
pars = line.slice(3).split(",");
pars[0] = pars[0].trim();
pars[1] = pars[1].trim();
pars[2] = pars[2].trim();
bintxt += "D" + pars[0][1] + pars[1][1] + pars[2][0] + "\n";
} else if (line.slice(0,4) == "SKP ") {
vx = line.slice(3).trim();
bintxt += "E" + vx[1] + "9E" + "\n";
} else if (line.slice(0,5) == "SKNP ") {
vx = line.slice(4).trim();
bintxt += "E" + vx[1] + "A1" + "\n";
} else {
bintxt += "[??]" + line + "\n";
}
}
}
return ( bintxt );
}
function assemble() {
// console.log ("Assemble source code window to machine code window");
srctxt = document.getElementById ( "source" ).value;
document.getElementById ( "machine" ).value = assemble_text ( srctxt );
}
hex_lines = null;
hex_bytes = null;
instr_types = null;
byte_types = null;
function get_machine_lines ( text_in ) {
// Normalize line endings and remove empty lines
text = replaceAll( text_in, "\r", "\n");
while (text.indexOf("\n\n") >= 0) {
text = replaceAll(text, "\n\n", "\n");
}
// Convert to upper case
text = "" + text.toUpperCase();
// Remove all line endings
while (text.indexOf("\n") >= 0) {
text = replaceAll(text, "\n", "");
}
// Remove all spaces
while (text.indexOf(" ") >= 0) {
text = replaceAll(text, " ", "");
}
text_bytes = text;
// Split by 4 digits per instruction
hex_lines = [];
while (text.length > 0) {
hex_lines.push ( text.slice(0,4) );
text = text.slice(4)
}
// Split by 2 digits per byte
hex_bytes = [];
while (text_bytes.length > 0) {
hex_bytes.push ( text_bytes.slice(0,2) );
text_bytes = text_bytes.slice(2)
}
return ( hex_lines );
}
function get_machine_bytes ( text_in ) {
// Normalize line endings and remove empty lines
text = replaceAll( text_in, "\r", "\n");
while (text.indexOf("\n\n") >= 0) {
text = replaceAll(text, "\n\n", "\n");
}
// Convert to upper case
text = "" + text.toUpperCase();
// Remove all line endings
while (text.indexOf("\n") >= 0) {
text = replaceAll(text, "\n", "");
}
// Remove all spaces
while (text.indexOf(" ") >= 0) {
text = replaceAll(text, " ", "");
}
text_bytes = text;
// Split by 4 digits per instruction
hex_lines = [];
while (text.length > 0) {
hex_lines.push ( text.slice(0,4) );
text = text.slice(4)
}
// Split by 2 digits per byte
hex_bytes = [];
while (text_bytes.length > 0) {
hex_bytes.push ( text_bytes.slice(0,2) );
text_bytes = text_bytes.slice(2)
}
return ( hex_bytes );
}
function disassemble_line ( line ) {
result = "";
// #### : 00E0 00EE
if (line == "00E0") {
result += "CLS";
} else if (line == "00EE") {
result += "RET";
// #nnn : 0 1 2 A B
} else if (line[0] == "0") {
result += "SYS " + line.slice(1);
} else if (line[0] == "1") {
// result += "JP L" + line.slice(1);
result += "JP " + line.slice(1);
} else if (line[0] == "2") {
// result += "CALL L" + line.slice(1);
result += "CALL " + line.slice(1);
} else if (line[0] == "A") {
result += "LD I," + line.slice(1);
} else if (line[0] == "B") {
result += "JP V0," + line.slice(1);
// #nkk or #xkk : 3 4 6 7 C
} else if (line[0] == "3") {
result += "SE V" + line[1] + "," + line.slice(2);
} else if (line[0] == "4") {
result += "SNE V" + line[1] + "," + line.slice(2);
} else if (line[0] == "6") {
result += "LD V" + line[1] + "," + line.slice(2);
} else if (line[0] == "7") {
result += "ADD V" + line[1] + "," + line.slice(2);
} else if (line[0] == "C") {
result += "RND V" + line[1] + "," + line.slice(2);
// #xy# : 5, 8, 9, D
} else if (line[0] == "5") {
if (line[3] == "0") {
result += "SE V" + line[1] + ",V" + line[2];
} else {
result += "DATA " + line;
}
} else if (line[0] == "8") {
if (line[3] == "0") {
result += "LD V" + line[1] + ",V" + line[2];
} else if (line[3] == "1") {
result += "OR V" + line[1] + ",V" + line[2];
} else if (line[3] == "2") {
result += "AND V" + line[1] + ",V" + line[2];
} else if (line[3] == "3") {
result += "XOR V" + line[1] + ",V" + line[2];
} else if (line[3] == "4") {
result += "ADD V" + line[1] + ",V" + line[2];
} else if (line[3] == "5") {
result += "SUB V" + line[1] + ",V" + line[2];
} else if (line[3] == "6") {
result += "SHR V" + line[1] + ",V" + line[2];
} else if (line[3] == "7") {
result += "SUBN V" + line[1] + ",V" + line[2];
} else if (line[3] == "E") {
result += "SHL V" + line[1] + ",V" + line[2];
} else {
result += "DATA " + line;
}
} else if (line[0] == "9") {
if (line[3] == "0") {
result += "SNE V" + line[1] + ",V" + line[2];
} else {
result += "DATA " + line;
}
} else if (line[0] == "D") {
result += "DRW V" + line[1] + ",V" + line[2] + "," + line[3];
// #x## : E, F
} else if (line[0] == "E") {
if (line.slice(2) == "9E") {
result += "SKP V" + line[1];
} else if (line.slice(2) == "A1") {
result += "SKNP V" + line[1];
} else {
result += "DATA " + line;
}
} else if (line[0] == "F") {
if (line.slice(2) == "07") {
result += "LD V" + line[1] + ",DT";
} else if (line.slice(2) == "0A") {
result += "LD V" + line[1] + ",K";
} else if (line.slice(2) == "15") {
result += "LD DT,V" + line[1];
} else if (line.slice(2) == "18") {
result += "LD ST,V" + line[1];
} else if (line.slice(2) == "1E") {
result += "ADD I,V" + line[1];
} else if (line.slice(2) == "29") {
result += "LD F,V" + line[1];
} else if (line.slice(2) == "33") {
result += "LD B,V" + line[1];
} else if (line.slice(2) == "55") {
result += "LD [I],V" + line[1];
} else if (line.slice(2) == "65") {
result += "LD V" + line[1] + ",[I]";
} else {
result += "DATA " + line;
}
// Unknown
} else {
result += "DATA " + line;
}
return ( result );
}
function trace_from ( start_byte ) {
try {
start_addr_hex = (start_byte+0x200).toString(16).toUpperCase();
// console.log ( "Tracing from " + start_addr_hex );
// Check to be sure we're not done
if (start_byte < (hex_bytes.length-1)) {
// Check to ensure that this instruction hasn't already been traced
if (byte_types[start_byte] != "c") {
// Always assume this is code, and mark as such
byte_types[start_byte] = "c";
byte_types[start_byte+1] = "c";
// Get the instruction
instr = hex_bytes[start_byte].toUpperCase() + hex_bytes[start_byte+1].toUpperCase();
// Get the opcode
opcode = instr.substr(0,1);
if (instr == '00EE') { // RET
// console.log ( start_addr_hex + ": RET" );
} else if (opcode == '1') { // JP 1nnn
addr = parseInt ( instr.substr(1), 16 );
// console.log ( start_addr_hex + ": JP " + instr.substr(1) + " = " + addr.toString(16).toUpperCase() );
trace_from ( addr - 0x200 );
} else if (opcode == '2') { // CALL 2nnn
addr = parseInt ( instr.substr(1), 16 );
// console.log ( start_addr_hex + ": CALL " + instr.substr(1) + " = " + addr.toString(16).toUpperCase() );
trace_from ( addr - 0x200 );
trace_from ( start_byte + 2 );
} else if (opcode == '3') { // SE 3xkk
// console.log ( start_addr_hex + ": SE " + instr.substr(1) );
trace_from ( start_byte + 2 );
trace_from ( start_byte + 4 );
} else if (opcode == '4') { // SNE 4xkk
// console.log ( start_addr_hex + ": SNE " + instr.substr(1) );
trace_from ( start_byte + 2 );
trace_from ( start_byte + 4 );
} else if (opcode == '5') { // SE 5xy0
// console.log ( start_addr_hex + ": SE " + instr.substr(1) );
trace_from ( start_byte + 2 );
trace_from ( start_byte + 4 );
} else if (opcode == 'B') { // JP Bnnn (offset from V0)
// This instruction cannot be statically traced
console.log ( start_addr_hex + ": JP " + instr.substr(1) + " (" + instr + ")" );
console.log ( "Warning: This instruction cannot be statically traced." );
trace_from ( start_byte + 2 );
} else if (opcode == 'E') {
// console.log ( start_addr_hex + ": SKP " + instr.substr(1) );
trace_from ( start_byte + 2 );
trace_from ( start_byte + 4 );
} else {
// console.log ( start_addr_hex + ": Other: " + disassemble_line(instr) );
trace_from ( start_byte + 2 );
}
}
}
} catch (ex) {
window.alert ( "Exception while tracing from " + start_byte + ": " + ex );
}
}
function trace_code() {
bintxt = document.getElementById ( "machine" ).value;
// Convert the text into lines
hex_lines = get_machine_lines ( bintxt );
hex_bytes = get_machine_bytes ( bintxt );
instr_types = Array.from("d".repeat(hex_lines.length));
byte_types = Array.from("d".repeat(hex_bytes.length));
trace_from ( 0 );
}
function disassemble() {
bintxt = document.getElementById ( "machine" ).value;
// console.log ( "Text = " + bintxt );
// Convert the text into bytes
hex_bytes = get_machine_bytes ( bintxt );
// console.log ( "Bytes = " + hex_bytes );
byte_types = Array.from("d".repeat(hex_bytes.length));
trace_from ( 0 );
// console.log ( "Text = " + bintxt );
opts = document.getElementById ( "disassem_opts" ).value;
// console.log ( "Disassemble with options: " + opts );
// Clear the source window
document.getElementById ( "source" ).value = "";
// Process the bytes one by one
byte_index = 0;
//for (l=0; l<hex_lines.length; l++) {
while (byte_index < hex_bytes.length) {
// Handle an instruction or data
if ( (opts == "Std") || (opts.indexOf("L") >= 0) ) {
// Include the hex address of this instruction