forked from FlatAssembler/PicoBlaze_Simulator_in_JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.js
916 lines (916 loc) · 33.9 KB
/
simulator.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
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
"use strict";
function simulateOneInstruction() {
try {
PC = PC %
4096; // If you are at the end of a program, and there is no "return"
// there, jump to the beginning of the program. I think that's
// how PicoBlaze behaves, though I haven't tried it.
if (breakpoints.includes(machineCode[PC].line)) {
alert("Reached breakpoint on the line #" + machineCode[PC].line + ".");
if (playing)
clearInterval(simulationThread);
playing = false;
document.getElementById("fastForwardButton").disabled = false;
document.getElementById("singleStepButton").disabled = false;
document.getElementById("UART_INPUT").disabled = false;
document.getElementById("playImage").style.display = "inline";
document.getElementById("pauseImage").style.display = "none";
}
document.getElementById("PC_label_" + formatAsAddress(PC)).innerHTML = "";
const currentDirective = parseInt(machineCode[PC].hex, 16);
// "bennyboy" from "atheistforums.org" thinks my program can be
// speeded up by using a switch-case instead of the large if-else (that a
// switch-case would compile into a more efficient assembly code), so it
// would be interesting to investigate whether that's true:
// https://atheistforums.org/thread-61911-post-2112817.html#pid2112817
let port, firstRegister, secondRegister, firstValue, secondValue, result,
value, registerIndex, registerValue;
switch (currentDirective & 0xff000) {
// if ((currentDirective & 0xff000) === 0x00000) {
case 0x00000:
// LOAD register,register
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
registers[regbank][parseInt(machineCode[PC].hex[3], 16)];
PC++;
// } else if ((currentDirective & 0xff000) === 0x01000) {
break;
case 0x01000:
// LOAD register,constant
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
parseInt(machineCode[PC].hex.substr(3), 16);
PC++;
// } else if ((currentDirective & 0xff000) === 0x17000) {
break;
case 0x17000:
// STAR register,constant ;Storing a constant into an inactive register
registers[!regbank | 0 /*That is how you convert a boolean to an integer
in JavaScript.*/
][parseInt(machineCode[PC].hex[2], 16)] =
parseInt(machineCode[PC].hex.substr(3), 16);
PC++;
// } else if ((currentDirective & 0xff000) === 0x16000) {
break;
case 0x16000:
// STAR register,register ;Copying from an active register into an
// inactive one.
registers[!regbank | 0][parseInt(machineCode[PC].hex[2], 16)] =
registers[regbank][parseInt(machineCode[PC].hex[3], 16)];
PC++;
// } else if ((currentDirective & 0xff000) === 0x2e000) {
break;
case 0x2e000:
// STORE register,(register) ;Store the first register at the memory
// location where the second register points to.
memory[registers[regbank][parseInt(machineCode[PC].hex[3], 16)]] =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
document
.getElementById(
"memory_" +
formatAsByte(
registers[regbank][parseInt(machineCode[PC].hex[3], 16)]))
.innerHTML = formatAsByte(
registers[regbank][parseInt(machineCode[PC].hex[2], 16)]);
PC++;
// } else if ((currentDirective & 0xff000) === 0x2f000) {
break;
case 0x2f000:
// STORE register,memory_address ;Copy a register onto a memory address.
memory[parseInt(machineCode[PC].hex.substr(3), 16)] =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
document.getElementById("memory_" + machineCode[PC].hex.substr(3))
.innerHTML = formatAsByte(
registers[regbank][parseInt(machineCode[PC].hex[2], 16)]);
PC++;
// } else if ((currentDirective & 0xff000) === 0x0a000) {
break;
case 0x0a000:
// FETCH register,(register) ;Dereference the pointer in the second
// register.
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
memory[registers[regbank][parseInt(machineCode[PC].hex[3], 16)]];
PC++;
// } else if ((currentDirective & 0xff000) === 0x0b000) {
break;
case 0x0b000:
// FETCH register,memory_address ;Copy the value at memory_address to the
// register.
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
memory[parseInt(machineCode[PC].hex.substr(3), 16)];
PC++;
// } else if ((currentDirective & 0xff000) === 0x08000) {
break;
case 0x08000:
// INPUT register,(register) ;Read a byte from a port specified by a
// register.
/*const*/ port = registers[regbank][parseInt(machineCode[PC].hex[3], 16)];
if ((port === 2 || port === 3) && is_UART_enabled) {
if (port === 3) {
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
document.getElementById("UART_INPUT")
.value.charCodeAt(currentlyReadCharacterInUART);
currentlyReadCharacterInUART++;
} else
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
currentlyReadCharacterInUART <
document.getElementById("UART_INPUT").value.length
? 0b00001000 /*U_RX_D*/
: 0;
} else
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] = parseInt(
document
.getElementById("input_" +
formatAsByte(registers[regbank][parseInt(
machineCode[PC].hex[3], 16)]))
.value,
16);
PC++;
// } else if ((currentDirective & 0xff000) === 0x09000) {
break;
case 0x09000:
// INPUT register, port_number
/*const*/ port = parseInt(machineCode[PC].hex.substr(3), 16);
if ((port === 2 || port === 3) && is_UART_enabled) {
if (port === 3) {
// UART_RX_PORT
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
document.getElementById("UART_INPUT")
.value.charCodeAt(currentlyReadCharacterInUART);
currentlyReadCharacterInUART++;
} else if (port === 2)
// UART_STATUS_PORT
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] =
currentlyReadCharacterInUART <
document.getElementById("UART_INPUT").value.length
? 0b00001000 /*U_RX_D*/
: 0;
else {
alert(
"Internal simulator error: The simulator got into a forbidden state!");
stopSimulation();
}
} else
registers[regbank][parseInt(machineCode[PC].hex[2], 16)] = parseInt(
document.getElementById("input_" + machineCode[PC].hex.substr(3))
.value,
16);
PC++;
// } else if ((currentDirective & 0xff000) === 0x2c000) {
break;
case 0x2c000:
// OUTPUT register,(register) ;Output the result of the first register to
// the port specified by the second register.
/*const*/ port = registers[regbank][parseInt(machineCode[PC].hex[3], 16)];
/*const*/ value =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
if ((port === 3 || port === 4) && is_UART_enabled) {
if (port === 3)
// UART_TX_PORT
document.getElementById("UART_OUTPUT").innerText +=
String.fromCharCode(value);
else if (port === 4)
// UART_RESET_PORT
document.getElementById("UART_OUTPUT").innerText = "";
else {
alert(
"Internal simulator error: The simulator got into a forbidden state!");
stopSimulation();
}
} else
output[registers[regbank][parseInt(machineCode[PC].hex[3], 16)]] =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
displayOutput();
PC++;
// } else if ((currentDirective & 0xff000) === 0x2d000) {
break;
case 0x2d000:
// OUTPUT register, port_number
/*const*/ port = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ value =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
if ((port === 3 || port === 4) && is_UART_enabled) {
if (port === 3)
// UART_TX_PORT
document.getElementById("UART_OUTPUT").innerText +=
String.fromCharCode(value);
else if (port === 4)
// UART_RESET_PORT
document.getElementById("UART_OUTPUT").innerText = "";
else {
alert(
"Internal simulator error: The simulator got into a forbidden state!");
stopSimulation();
}
} else {
output[parseInt(machineCode[PC].hex.substr(3), 16)] =
registers[regbank][parseInt(machineCode[PC].hex[2], 16)];
displayOutput();
}
PC++;
// } else if ((currentDirective & 0xff000) === 0x2b000) {
break;
case 0x2b000:
// OUTPUTK constant, port_number
/*const*/ value = parseInt(machineCode[PC].hex.substr(2, 2), 16);
/*const*/ port = parseInt(machineCode[PC].hex[4], 16);
if ((port === 3 || port === 4) && is_UART_enabled) {
if (port === 3)
// UART_TX_PORT
document.getElementById("UART_OUTPUT").innerText +=
String.fromCharCode(value);
else if (port === 4)
// UART_RESET_PORT
document.getElementById("UART_OUTPUT").innerText = "";
else {
alert(
"Internal simulator error: The simulator got into a forbidden state!");
stopSimulation();
}
} else {
output[parseInt(machineCode[PC].hex[4], 16)] =
parseInt(machineCode[PC].hex.substr(2, 2), 16);
displayOutput();
}
PC++;
/* } else if (currentDirective === 0x37000) {
// REGBANK A
regbank = 0;
PC++;
} else if (currentDirective === 0x37001) {
// REGBANK B
regbank = 1;
PC++;
*/
break;
case 0x37000:
if ((currentDirective & 0x00ff0) !== 0 || (currentDirective & 0xf) > 1) {
alert(
'Sorry about that, the simulator currently does not support the instruction "' +
machineCode[PC].hex + '" (' + currentDirective + " & " + 0xff000 +
" = " + (currentDirective & 0xff000) + "), assembled from line #" +
machineCode[PC].line + ".");
stopSimulation();
}
if (currentDirective % 2 === 0)
regbank = 0;
else
regbank = 1;
PC++;
// } else if ((currentDirective & 0xff000) === 0x22000) {
break;
case 0x22000:
// JUMP label
PC = parseInt(machineCode[PC].hex.substr(2), 16);
/* } else if ((currentDirective & 0xff0ff) == 0x14080) {
// HWBUILD register
flagC[regbank] =
1; // Have a better idea? We can't simulate all of what this
directive
// does, but we can simulate this part of it.
PC++;
*/ // Moved to bit-shifting operations...
// } else if ((currentDirective & 0xff000) === 0x10000) {
break;
case 0x10000:
// ADD register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
if ((firstValue + secondValue) % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (firstValue + secondValue > 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] += secondValue;
PC++;
// } else if ((currentDirective & 0xff000) === 0x11000) {
break;
case 0x11000:
// ADD register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
if ((firstValue + secondValue) % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (firstValue + secondValue > 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] += secondValue;
PC++;
// } else if ((currentDirective & 0xff000) === 0x12000) {
break;
case 0x12000:
// ADDCY register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue + secondValue + flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result > 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x13000) {
break;
case 0x13000:
// ADDCY register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue + secondValue + flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result > 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x18000) {
break;
case 0x18000:
// SUB register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue - secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x19000) {
break;
case 0x19000:
// SUB register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue - secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1a000) {
break;
case 0x1a000:
// SUBCY register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue - secondValue - flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1b000) {
break;
case 0x1b000:
// SUBCY register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue - secondValue - flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x03000) {
break;
case 0x03000:
// AND register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue & secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x02000) {
break;
case 0x02000:
// AND register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue & secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x04000) {
break;
case 0x04000:
// OR register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue | secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x05000) {
break;
case 0x05000:
// OR register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue | secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x06000) {
break;
case 0x06000:
// XOR register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue ^ secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x07000) {
break;
case 0x07000:
// XOR register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue ^ secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
registers[regbank][firstRegister] = result;
PC++;
/* } else if ((currentDirective & 0xff000) === 0x0c000 ||
(currentDirective & 0xff000) === 0x0e000) {
*/
break;
case 0x0c000:
case 0x0e000:
// TEST register, register ;The same as "AND", but does not store the
// result (only the flags). I am not sure if there is a difference between
// "0c" and "0e", they appear to be the same.
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue & secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
/* } else if ((currentDirective & 0xff000) === 0x0d000 ||
(currentDirective & 0xff000) === 0x0f000) {*/
break;
case 0x0d000:
case 0x0f000:
// TEST register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue & secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result % 256 === 255)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1c000) {
break;
case 0x1c000:
// COMPARE register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue - secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1d000) {
break;
case 0x1d000:
// COMPARE register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue - secondValue;
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1e000) {
break;
case 0x1e000:
// COMPARECY register, register
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
/*const*/ result = firstValue - secondValue - flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x1f000) {
break;
case 0x1f000:
// COMPARECY register, constant
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = parseInt(machineCode[PC].hex.substr(3), 16);
/*const*/ result = firstValue - secondValue - flagC[regbank];
if (result % 256 === 0)
flagZ[regbank] = 1;
else
flagZ[regbank] = 0;
if (result < 0)
flagC[regbank] = 1;
else
flagC[regbank] = 0;
// registers[regbank][firstRegister] = result;
PC++;
// } else if ((currentDirective & 0xff000) === 0x14000) {
break;
case 0x14000:
// Bit-shifting operations...
/*const*/ registerIndex = parseInt(machineCode[PC].hex[2], 16);
/*let*/ registerValue = registers[regbank][registerIndex];
console.log("DEBUG: Shifting the bits in register s" +
registerIndex.toString(16));
const set_flags_after_shift_left = () => {
flagC[regbank] = (registerValue > 255) | 0;
flagZ[regbank] = (registerValue % 256 === 0) | 0;
};
const set_flags_before_shift_right = () => {
flagC[regbank] = registerValue % 2;
flagZ[regbank] = (Math.floor(registerValue / 2) === 0) | 0;
};
switch (machineCode[PC].hex.substr(3)) {
case "06": // SL0
registerValue <<= 1;
set_flags_after_shift_left();
break;
case "07": // SL1
registerValue = (registerValue << 1) + 1;
set_flags_after_shift_left();
break;
case "04": // SLX
registerValue = (registerValue << 1) + (registerValue % 2);
set_flags_after_shift_left();
break;
case "00": // SLA
registerValue = (registerValue << 1) + flagC[regbank];
set_flags_after_shift_left();
break;
case "02": // RL
registerValue = (registerValue << 1) + Math.floor(registerValue / 128);
set_flags_after_shift_left();
break;
case "0e": // SR0
set_flags_before_shift_right();
registerValue >>= 1;
break;
case "0f": // SR1
set_flags_before_shift_right();
registerValue = (registerValue >> 1) + 128;
break;
case "0a": // SRX
set_flags_before_shift_right();
registerValue =
(registerValue >> 1) + Math.floor(registerValue / 128) * 128;
break;
case "08": // SRA
const oldFlagC = flagC[regbank];
set_flags_before_shift_right();
registerValue = (registerValue >> 1) | oldFlagC << 7;
break;
case "0c": // RR
set_flags_before_shift_right();
registerValue = (registerValue >> 1) + 128 * (registerValue % 2);
break;
case "80": // HWBUILD (not a bit-shifting operation)
flagC[regbank] = 1;
break;
default:
alert('The instruction "' + machineCode[PC].hex +
'", assembled from line #' + machineCode[PC].line +
", hasn't been implemented yet, sorry about that!");
}
registers[regbank][registerIndex] = registerValue;
PC++;
// } else if ((currentDirective & 0xff000) === 0x32000) {
break;
case 0x32000:
// JUMP Z, label
if (flagZ[regbank])
PC = parseInt(machineCode[PC].hex.substr(2), 16);
else
PC++;
// } else if ((currentDirective & 0xff000) === 0x36000) {
break;
case 0x36000:
// JUMP NZ, label
if (!flagZ[regbank])
PC = parseInt(machineCode[PC].hex.substr(2), 16);
else
PC++;
// } else if ((currentDirective & 0xff000) === 0x3a000) {
break;
case 0x3a000:
// JUMP C, label
if (flagC[regbank])
PC = parseInt(machineCode[PC].hex.substr(2), 16);
else
PC++;
// } else if ((currentDirective & 0xff000) === 0x3e000) {
break;
case 0x3e000:
// JUMP NC, label
if (!flagC[regbank])
PC = parseInt(machineCode[PC].hex.substr(2), 16);
else
PC++;
// } else if ((currentDirective & 0xff000) === 0x26000) {
break;
case 0x26000:
// JUMP@ (register, register) ; Jump to the address pointed by the
// registers (something like function pointers, except that "return" won't
// work).
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
PC = (firstValue % 16) * 256 + secondValue;
// } else if ((currentDirective & 0xff000) === 0x20000) {
break;
case 0x20000:
// CALL functionName
callStack.push(PC);
PC = parseInt(machineCode[PC].hex.substr(2), 16);
// } else if ((currentDirective & 0xff000) === 0x30000) {
break;
case 0x30000:
// CALL Z, functionName ; Call the function only if the Zero Flag is set.
if (flagZ[regbank]) {
callStack.push(PC);
PC = parseInt(machineCode[PC].hex.substr(2), 16);
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x34000) {
break;
case 0x34000:
// CALL NZ, functionName ; Call the function only if the Zero Flag is not
// set.
if (!flagZ[regbank]) {
callStack.push(PC);
PC = parseInt(machineCode[PC].hex.substr(2), 16);
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x38000) {
break;
case 0x38000:
// CALL C, functionName ; Call the function only if the Carry Flag is set.
if (flagC[regbank]) {
callStack.push(PC);
PC = parseInt(machineCode[PC].hex.substr(2), 16);
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x3c000) {
break;
case 0x3c000:
// CALL NC, functionName ; Call the function only if the Carry Flag is not
// set.
if (!flagC[regbank]) {
callStack.push(PC);
PC = parseInt(machineCode[PC].hex.substr(2), 16);
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x24000) {
break;
case 0x24000:
// CALL@ (register, register) ; Jump the function pointed by the function
// pointer stored in the registers.
/*const*/ firstRegister = parseInt(machineCode[PC].hex[2], 16);
/*const*/ secondRegister = parseInt(machineCode[PC].hex[3], 16);
/*const*/ firstValue = registers[regbank][firstRegister];
/*const*/ secondValue = registers[regbank][secondRegister];
callStack.push(PC);
PC = (firstValue % 16) * 256 + secondValue;
// } else if ((currentDirective & 0xff000) === 0x25000) {
break;
case 0x25000:
// RETURN
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
// } else if ((currentDirective & 0xff000) === 0x31000) {
break;
case 0x31000:
// RETURN Z ; Return from a function only if the Zero Flag is set.
if (flagZ[regbank]) {
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x35000) {
break;
case 0x35000:
// RETURN NZ ; Return from a function only if the Zero Flag is not set.
if (!flagZ[regbank]) {
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x39000) {
break;
case 0x39000:
// RETURN C ; Return from a function only if the Carry Flag is set.
if (flagC[regbank]) {
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x3d000) {
break;
case 0x3d000:
// RETURN NC ; Return from a function only if the Carry Flag is not set.
if (!flagC[regbank]) {
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
} else
PC++;
// } else if ((currentDirective & 0xff000) === 0x28000) {
break;
case 0x28000:
// INTERRUPT ENABLE|DISABLE
flagIE = machineCode[PC].hex[4] | 0;
PC++;
// } else if ((currentDirective & 0xff000) === 0x29000) {
break;
case 0x29000:
// RETURNI ENABLE|DISABLE
flagIE = machineCode[PC].hex[4] | 0;
if (callStack.length)
PC = callStack.pop() + 1;
else {
if (playing)
clearInterval(simulationThread);
alert("The program exited!");
}
// } else {
break;
default:
alert(
'Sorry about that, the simulator currently does not support the instruction "' +
machineCode[PC].hex + '" (' + currentDirective + " & " + 0xff000 +
" = " + (currentDirective & 0xff000) + "), assembled from line #" +
machineCode[PC].line + ".");
stopSimulation();
}
displayRegistersAndFlags();
document.getElementById("PC_label_" + formatAsAddress(PC)).innerHTML =
"->";
} catch (error) {
if (playing)
clearInterval(simulationThread);
alert("The simulator crashed! Error: " + error.message);
}
}