forked from izuzak/FRISCjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friscsim.js
1743 lines (1414 loc) · 52.4 KB
/
friscsim.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
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
//
// Helper functions
//
/* Converts integer value to binary, specifying the length in bits of output */
function convertIntToBinary(value, numberOfDigits) {
var retVal = new Array(numberOfDigits);
for (var i=0; i<numberOfDigits; i++) {
retVal[numberOfDigits-i-1] = (value & (1<<i)) ? 1 : 0;
}
return retVal.join('');
}
/* Returns the integer represented by 'value'.
* - value is a string of ones and zeroes. If it is empty or contains another digit, an exception is thrown.
* - [signed=false] can be either true, false, 0 or 1. For any other value, an exception is thrown as it is
* likely a silent error. */
function convertBinaryToInt(value, signed) {
if (value.length === 0) {
throw "'value' must be nonempty";
}
var retVal = 0, bit;
if (typeof signed === 'undefined') {
signed = 0;
}
if (signed!==0 && signed!==1 && signed!==true && signed!==false) {
throw "invalid 'signed' value " + signed.toString();
}
for (var i=0, numberOfBits=value.length; i<numberOfBits-signed; i++) {
bit = value[numberOfBits - 1 - i];
if (bit !== '0' && bit !== '1') {
throw "invalid bit in binary string 'value' at position " + (numberOfBits - 1 - i) + ' (' + bit + ')';
}
// using Math.pow here since 'i' can be >30
retVal += (value[numberOfBits - i - 1] === '1') * Math.pow(2, i);
}
return (signed && value[0] === '1') ? ( Math.pow(2, value.length-1) - retVal) * -1 : (retVal);
}
/* Returns a bit string representing bits from 'start' to 'end' (inclusive) of 'number'.
* The bits are counted from right to left, i.e. the LSB is the bit 0.
* The returned string contains bits with indices 'end', 'end'-1, ... 'start'.
* - number is either a bit string or a number object that is converted into a
* 32-bit bit string - otherwise, null is returned
* - start is a valid 0-based index of the lowest requested bit
* - end is a valid 0-based index of the highest requested bit */
function getBitString(number, start, end) {
if (typeof number === 'string') {
return number.substring(number.length - end - 1, number.length - start);
} else if (typeof number === 'number') {
return getBitString(convertIntToBinary(number, 32), start, end);
} else {
return null;
}
}
/* Returns 'binaryString' sign-extended to 'numberOfBits' bits.
* - binaryString is a string of ones and zeroes
* - numberOfBits is the desired number of bits for the extended number. If
* it is less than or eqaul to binaryString.length, binaryString is returned unchanged.
* - [signed=false] is a flag signaling if binaryString is signed or unsigned */
function extend(binaryString, numberOfBits, signed) {
var bit = signed ? binaryString[0] : '0';
var len = binaryString.length;
var prefix = '';
for (var i=0; i<numberOfBits-len; i++) {
prefix += bit;
}
return prefix + binaryString;
}
/* Returns the two's complement of 'value' with respect to the specified
* 'mask' that must be an all ones bitmask defining the word size.
*
* - value is an integer
* - mask is an all ones bitmask of word size bits */
function twosComplement(value, mask) {
// 'mask' is defined as a bitmask instead of the word size as a number
// because ((1<<i)-1) will not work if i==32 since (1<<32) is a no-op
return (~value + 1) & mask;
}
/* Returns a string of length 'stringLength' of characters 'character'.
*
* - character is any character
* - stringLength is a non-negative integer */
function generateStringOfCharacters(character, stringLength) {
var retVal = [];
if (typeof stringLength !== 'number' || stringLength < 0) {
throw new Error('stringLength must be a non-negative integer');
}
for (var i=0; i<stringLength; i++) {
retVal.push(character);
}
return retVal.join('');
}
var FRISC = function() {
//
// FRISC memory component
//
var MEM = {
/* Memory has size 256KB, i.e. from 0x00000000 to 0x0003FFFF */
_size: 256*1024,
_memory: [],
/* Read 8-bit byte from a given address */
readb: function(addr) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
return 0xFF & this._memory[addr];
} else {
return 0xFF & ioUnit.readb(addr);
}
},
/* Read 16-bit word from a given address */
readw: function(addr) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
var v1 = (0xFF & this._memory[addr+0]) << 0;
var v2 = (0xFF & this._memory[addr+1]) << 8;
return v1 + v2;
} else {
return 0xFFFF & ioUnit.readw(addr);
}
},
/* Read 32-bit word from a given address */
read: function(addr) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
var v1 = (0xFF & this._memory[addr+0]) << 0;
var v2 = (0xFF & this._memory[addr+1]) << 8;
var v3 = (0xFF & this._memory[addr+2]) << 16;
var v4 = (0xFF & this._memory[addr+3]) << 24;
return v1 + v2 + v3 + v4;
} else {
return 0xFFFFFFFF & ioUnit.read(addr);
}
},
/* Write 8-bit byte to a given address */
writeb: function(addr, val) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
this._memory[addr] = 0xFF & val;
} else {
ioUnit.writeb(addr, 0xFF & val);
}
if (typeof this.onMemoryWrite !== 'undefined') {
this.onMemoryWrite(addr, 0xFF & val, 1);
}
},
/* Write 16-bit word to a given address */
writew: function(addr, val) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
this._memory[addr+0] = 0xFF & (val >> 0);
this._memory[addr+1] = 0xFF & (val >> 8);
} else {
ioUnit.writew(addr, 0xFFFF & val);
}
if (typeof this.onMemoryWrite !== 'undefined') {
this.onMemoryWrite(addr, 0xFFFF & val, 2);
}
},
/* Write 32-bit word to a given address */
write: function(addr, val) {
if (addr < 0) {
addr = convertBinaryToInt(convertIntToBinary(addr, 32), 0);
}
var ioUnit = IO.testMemoryOverlap(addr);
if (ioUnit === null) {
this._memory[addr+0] = 0xFF & (val >> 0);
this._memory[addr+1] = 0xFF & (val >> 8);
this._memory[addr+2] = 0xFF & (val >> 16);
this._memory[addr+3] = 0xFF & (val >> 24);
} else {
ioUnit.write(addr, 0xFFFFFFFF & val);
}
if (typeof this.onMemoryWrite !== 'undefined') {
this.onMemoryWrite(addr, 0xFFFFFFFF & val, 4);
}
},
/* Reset memory to initial state */
reset: function() {
this._memory = [];
for (var i=0; i<this._size; i++) {
this._memory[i] = 0;
}
},
/* Load memory with some program enocoded as string, byte by byte */
loadByteString: function(str) {
this.reset();
if (this._size < str.length) {
throw new Error('Memory too small to fit program.');
}
for (var i=0; i<str.length; i++) {
this._memory[i] = str.charCodeAt(i);
}
},
/* Load memory with some program, byte by byte */
loadBytes: function(bytes) {
this.reset();
if (this._size < bytes.length) {
throw new Error('Memory too small to fit program.');
}
for (var i=0; i<bytes.length; i++) {
this._memory[i] = bytes[i];
}
},
/* Load memory with some program, binary string by binary string */
loadBinaryString: function(binaryStrings) {
this.reset();
if (this._size < binaryStrings.length) {
throw new Error('Memory too small to fit program.');
}
for (var i=0; i<binaryStrings.length; i++) {
this._memory[i] = parseInt(binaryStrings[i], 2);
}
}
};
//
// FRISC CPU component
//
var CPU = {
// Internal state
_r: {r0:0, r1:0, r2:0, r3:0, r4:0, r5:0, r6:0, r7:0, pc:0, sr:0, iif:1},
_regMap: { '000' : 'r0', '001' : 'r1', '010' : 'r2', '011' : 'r3', '100' : 'r4', '101' : 'r5', '110' : 'r6', '111' : 'r7' },
_f: {INT2:1024, INT1:512, INT0:256, GIE:128, EINT2:64, EINT1:32, EINT0:16, Z:8, V:4, C:2, N:1},
_frequency : 1,
// bitmasks
_SIGN_BIT: 0x80000000,
_NONSIGN_BITS: 0x7FFFFFFF,
_WORD_BITS: 0xFFFFFFFF,
_SHIFT_BITS: 0x0000001F,
_setFlag: function(flag, value) {
this._r.sr = value ? (this._r.sr | flag) : (this._r.sr & ~(flag));
},
_getFlag: function(flag) {
return ((this._r.sr & flag) !== 0) + 0;
},
_testCond: function(cond) {
var result = true;
if (cond === '') { // **** Unconditional TRUE
result = true;
} else if (cond === '_N/M') { // ******** N,M N=1
result = !!(this._getFlag(this._f.N));
} else if (cond === '_NN/P') { // ******** NN,P N=0
result = !(this._getFlag(this._f.N));
} else if (cond === '_C/UGE') { // ******** C,UGE C=1
result = !!(this._getFlag(this._f.C));
} else if (cond === '_NC/ULT') { // ******** NC,ULT C=0
result = !(this._getFlag(this._f.C));
} else if (cond === '_V') { // ******** V V=1
result = !!(this._getFlag(this._f.V));
} else if (cond === '_NV') { // ******** NV V=0
result = !(this._getFlag(this._f.V));
} else if (cond === '_Z/EQ') { // ******** Z,EQ Z=1
result = !!(this._getFlag(this._f.Z));
} else if (cond === '_NZ/NE') { // ******** NZ,NE Z=0
result = !(this._getFlag(this._f.Z));
} else if (cond === '_ULE') { // ******** ULE C=0 ili Z=1
result = !(this._getFlag(this._f.C)) ||
!!(this._getFlag(this._f.Z));
} else if (cond === '_UGT') { // ******** UGT C=1 i Z=0
result = !!(this._getFlag(this._f.C)) &&
!(this._getFlag(this._f.Z));
} else if (cond === '_SLT') { // ******** SLT (N xor V)=1
result = !!(this._getFlag(this._f.N)) !==
!!(this._getFlag(this._f.V));
} else if (cond === '_SLE') { // ******** SLE (N xor V)=1 ili Z=1
result = (!!(this._getFlag(this._f.N)) !==
!!(this._getFlag(this._f.V))) ||
!!(this._getFlag(this._f.Z));
} else if (cond === '_SGE') { // ******** SGE (N xor V)=0
result = !!(this._getFlag(this._f.N)) ===
!!(this._getFlag(this._f.V));
} else if (cond === '_SGT') { // ******** SGT (N xor V)=0 i Z=0
result = (!!(this._getFlag(this._f.N)) ===
!!(this._getFlag(this._f.V))) &&
!(this._getFlag(this._f.Z));
} else {
throw new Error('Undefined test condition.');
}
return result;
},
_decode: function(statement) {
var opCode = getBitString(statement, 27, 31);
var op = this._instructionMap[opCode];
var args = [];
if (typeof op === 'undefined') {
return { op : null, args : null };
}
if (op === 'MOVE') {
var src = getBitString(statement, 21, 21);
if (src === '1') {
src = 'sr';
} else {
src = getBitString(statement, 26, 26);
if (src === '0') {
src = getBitString(statement, 17, 19);
src = this._regMap[src];
} else {
src = getBitString(statement, 0, 19);
src = extend(src, 32, 1);
src = convertBinaryToInt(src, 1);
}
}
var dest = getBitString(statement, 20, 20);
if (dest === '1') {
dest = 'sr';
} else {
dest = getBitString(statement, 23, 25);
dest = this._regMap[dest];
}
args.push(src);
args.push(dest);
} else if (op === 'OR' || op === 'AND' || op === 'XOR' || op === 'ADD' || op === 'ADC' || op === 'SUB' || op === 'SBC' || op === 'ROTL' || op === 'ROTR' || op === 'SHL' || op === 'SHR' || op === 'ASHR') {
var source1 = getBitString(statement, 20, 22);
source1 = this._regMap[source1];
var source2 = getBitString(statement, 26, 26);
if (source2 === '0') {
source2 = getBitString(statement, 17, 19); // Rx
source2 = this._regMap[source2];
} else {
source2 = getBitString(statement, 0, 19); // number
source2 = extend(source2, 32, 1);
source2 = convertBinaryToInt(source2, 1);
}
var dest = getBitString(statement, 23, 25); // Rx
dest = this._regMap[dest];
args.push(source1);
args.push(source2);
args.push(dest);
} else if (op === 'CMP') {
var source1 = getBitString(statement, 20, 22);
source1 = this._regMap[source1];
var source2 = getBitString(statement, 26, 26);
if (source2 === '0') {
source2 = getBitString(statement, 17, 19); // Rx
source2 = this._regMap[source2];
} else {
source2 = getBitString(statement, 0, 19); // number
source2 = extend(source2, 32, 1);
source2 = convertBinaryToInt(source2, 1);
}
args.push(source1);
args.push(source2);
} else if (op === 'JP' || op === 'CALL') {
var cond = getBitString(statement, 22, 25);
cond = this._conditionMap[cond];
if (typeof cond === 'undefined') {
args = null;
} else {
var dest = getBitString(statement, 26, 26);
if (dest === '0') {
dest = getBitString(statement, 17, 19); // Rx
dest = this._regMap[dest];
} else {
dest = getBitString(statement, 0, 19); // number
dest = extend(dest, 32, 1);
dest = convertBinaryToInt(dest, 1);
}
args.push(cond);
args.push(dest);
}
} else if (op === 'JR') {
var cond = getBitString(statement, 22, 25);
cond = this._conditionMap[cond];
if (typeof cond === 'undefined') {
args = null;
} else {
var dest = getBitString(statement, 0, 19); // number
dest = extend(dest, 32, 1);
dest = convertBinaryToInt(dest, 1);
args.push(cond);
args.push(dest);
}
} else if (op === 'RET') {
var cond = getBitString(statement, 22, 25);
cond = this._conditionMap[cond];
if (typeof cond === 'undefined') {
args = null;
} else {
var isRETI = getBitString(statement, 0, 0) === '1' && getBitString(statement, 1, 1) === '0';
var isRETN = getBitString(statement, 0, 0) === '1' && getBitString(statement, 1, 1) === '1';
args.push(cond);
args.push(isRETI);
args.push(isRETN);
}
} else if (op === 'LOAD' || op === 'STORE' || op === 'LOADB' || op === 'STOREB' || op === 'LOADH' || op === 'STOREH') {
var addr = getBitString(statement, 26, 26);
var offset = 0;
if (addr === '0') {
addr = 0;
} else {
addr = getBitString(statement, 20, 22);
addr = this._regMap[addr];
}
offset = getBitString(statement, 0, 19);
offset = extend(offset, 32, 1);
offset = convertBinaryToInt(offset, 1);
var reg = getBitString(statement, 23, 25);
reg = this._regMap[reg];
args.push(reg);
args.push(addr);
args.push(offset);
} else if (op === 'POP' || op === 'PUSH') {
var reg = getBitString(statement, 23, 25);
reg = this._regMap[reg];
args.push(reg);
} else if (op === 'HALT') {
var cond = getBitString(statement, 22, 25);
cond = this._conditionMap[cond];
if (typeof cond === 'undefined') {
args = null;
} else {
args.push(cond);
}
}
return { op : op, args : args };
},
// Simulates the addition 'v1'+'v2'+'v3' and stores the result in the
// register specified by 'dest' and updates flags.
// Leave 'dest' undefined to only get the side-effect of changing the flags.
_ADD_three: function(v1, v2, v3, dest) {
// the & just forces ToInt32 from ECMA-262
// v1+v2+v3 can be represented exactly by Number as it is <=2^53
// so there is no loss of precision
var res = (v1+v2+v3) & this._WORD_BITS;
// calculate carry on the next-to-last bit
var t1 = v1 & this._NONSIGN_BITS;
var t2 = v2 & this._NONSIGN_BITS;
var t3 = v3 & this._NONSIGN_BITS;
// (t1+t2+t3) can't overflow 32 bits by construction of t1, t2 and t3
var c_ntl = ((t1+t2+t3)>>31) & 1;
// calculate carry on the last bit
var b1 = (v1>>31) & 1;
var b2 = (v2>>31) & 1;
var b3 = (v3>>31) & 1;
var c_last = b1+b2+b3+c_ntl>1 ? 1 : 0;
this._setFlag(this._f.C, c_last);
this._setFlag(this._f.V, c_ntl ^ c_last);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
if (dest) {
this._r[dest] = res;
}
},
// Leave 'dest' undefined to only get the side-effect of changing the flags.
// dest <- src1 - src2 + carry
_SUB_internal: function(src1, src2, carry, dest) {
var op2 = (typeof src2==='number') ? src2 : this._r[src2];
// Do a three-way add with two's complements of src2 and with the carry bit.
this._ADD_three(this._r[src1],
twosComplement(op2, this._WORD_BITS),
carry,
dest);
if (op2 === 0) {
// Doing two's complement on 0 creates a carry on the MSB (the one's
// complement is all 1s). This is particularly important for the
// implementation of CMP when the second operand is 0. For example,
// comparing 1 with 0 must generate a carry so that the _UGE condition
// is recognized.
//
// TODO: Check the exact flag semantics of SBC in FRISC when the input
// carry is 1.
this._setFlag(this._f.C, 1);
}
},
_i: {
POP: function(dest) {
this._r[dest] = MEM.read(this._r.r7 & ~(0x03));
this._r.r7 += 4;
},
PUSH: function(src) {
this._r.r7 -= 4;
MEM.write(this._r.r7 & ~(0x03), this._r[src]);
},
ADD: function(src1, src2, dest) {
this._ADD_three(this._r[src1],
typeof src2==='number' ? src2 : this._r[src2],
0,
dest);
},
ADC: function(src1, src2, dest) {
this._ADD_three(this._r[src1],
typeof src2==='number' ? src2 : this._r[src2],
this._getFlag(this._f.C),
dest);
},
SUB: function(src1, src2, dest) {
this._SUB_internal(src1, src2, 0, dest);
},
SBC: function(src1, src2, dest) {
this._SUB_internal(src1, src2, this._getFlag(this._f.C), dest);
},
CMP: function(src1, src2) {
// We only want the side-effect of setting the flags.
var dest = undefined;
this._SUB_internal(src1, src2, 0, dest);
},
AND: function(src1, src2, dest) {
var res = this._r[src1] & (typeof src2 === 'number' ? src2 : this._r[src2]);
this._setFlag(this._f.C, 0);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
OR: function(src1, src2, dest) {
var res = this._r[src1] | (typeof src2 === 'number' ? src2 : this._r[src2]);
this._setFlag(this._f.C, 0);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
XOR: function(src1, src2, dest) {
var res = this._r[src1] ^ (typeof src2 === 'number' ? src2 : this._r[src2]);
this._setFlag(this._f.C, 0);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
SHL: function(src1, src2, dest) {
src2 = (typeof src2 === 'number' ? src2 : this._r[src2]);
src2 = src2 & this._SHIFT_BITS;
src1 = convertIntToBinary(this._r[src1], 32);
src1 = src1 + generateStringOfCharacters('0', src2);
var carry = src2 === 0 ? 0 : (src1[src2-1] === '1' ? 1 : 0);
var res = convertBinaryToInt(src1.substring(src2));
this._setFlag(this._f.C, carry);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
SHR: function(src1, src2, dest) {
src2 = (typeof src2 === 'number' ? src2 : this._r[src2]);
src2 = src2 & this._SHIFT_BITS;
src1 = convertIntToBinary(this._r[src1], 32);
src1 = generateStringOfCharacters('0', src2) + src1;
var carry = src2 === 0 ? 0 : (src1[32] === '1' ? 1 : 0);
var res = convertBinaryToInt(src1.substring(0, 32));
this._setFlag(this._f.C, carry);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
ASHR: function(src1, src2, dest) {
src2 = (typeof src2 === 'number' ? src2 : this._r[src2]);
src2 = src2 & this._SHIFT_BITS;
src1 = convertIntToBinary(this._r[src1], 32);
src1 = generateStringOfCharacters(src1[0], src2) + src1;
var carry = src2 === 0 ? 0 : (src1[32] === '1' ? 1 : 0);
var res = convertBinaryToInt(src1.substring(0, 32));
this._setFlag(this._f.C, carry);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
ROTL: function(src1, src2, dest) {
src2 = (typeof src2 === 'number' ? src2 : this._r[src2]);
src2 = src2 & this._SHIFT_BITS;
src1 = convertIntToBinary(this._r[src1], 32);
var carry = src2 === 0 ? 0 : (src1[(src2-1)%32] === '1' ? 1 : 0);
src2 = src2 % 32;
var res = convertBinaryToInt(src1.substring(src2) + src1.substring(0, src2));
this._setFlag(this._f.C, carry);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
ROTR: function(src1, src2, dest) {
src2 = (typeof src2 === 'number' ? src2 : this._r[src2]);
src2 = src2 & this._SHIFT_BITS;
src1 = convertIntToBinary(this._r[src1], 32);
var carry = src2 === 0 ? 0 : (src1[32-src2] === '1' ? 1 : 0);
src2 = src2 % 32;
var res = convertBinaryToInt(src1.substring(32-src2) + src1.substring(0, 32-src2));
this._setFlag(this._f.C, carry);
this._setFlag(this._f.V, 0);
this._setFlag(this._f.N, !!(res & this._SIGN_BIT) + 0);
this._setFlag(this._f.Z, !(res & this._WORD_BITS) + 0);
this._r[dest] = res & this._WORD_BITS;
},
MOVE: function(src, dest) {
if (src === 'sr') {
this._r[dest] = this._r[src] & 0xFF;
} else if (dest === 'sr') {
this._r[dest] = (typeof src === 'number' ? src : this._r[src]) & 0xFF;
} else {
this._r[dest] = (typeof src === 'number' ? src : this._r[src]);
}
},
LOAD: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
destAddr &= ~(0x03);
this._r[reg] = MEM.read(destAddr);
},
LOADH: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
destAddr &= ~(0x01);
this._r[reg] = MEM.readw(destAddr);
},
LOADB: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
this._r[reg] = MEM.readb(destAddr);
},
STORE: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
destAddr &= ~(0x03);
MEM.write(destAddr, this._r[reg]);
},
STOREH: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
destAddr &= ~(0x01);
MEM.writew(destAddr, this._r[reg]);
},
STOREB: function(reg, addr, offset) {
var destAddr = (typeof addr === 'string' ? this._r[addr] : 0) + (typeof offset === 'number' ? offset : 0);
MEM.writeb(destAddr, this._r[reg]);
},
JP: function(cond, dest) {
if (this._testCond(cond)) {
this._r.pc = ((typeof dest === 'string' ? this._r[dest] : dest) & ~(0x03)) - 4;
}
},
JR: function(cond, dest) {
if (this._testCond(cond)) {
this._r.pc = ((this._r.pc + dest) & ~(0x03)) - 4;
}
},
CALL: function(cond, dest) {
if (this._testCond(cond)) {
this._r.r7 -= 4;
MEM.write(this._r.r7, this._r.pc & ~(0x03));
this._r.pc = ((typeof dest === 'string' ? this._r[dest] : dest) & ~(0x03)) - 4;
}
},
RET: function(cond, isRETI, isRETN) {
if (this._testCond(cond)) {
this._r.pc = MEM.read(this._r.r7) & ~(0x03);
this._r.r7 += 4;
if (isRETI) {
this._setFlag(this._f.GIE, 1);
} else if (isRETN) {
this._r.iif = 1;
}
}
},
HALT: function(cond) {
if (this._testCond(cond)) {
this.stop();
}
}
},
acceptNonmaskableInterrupt: function() {
IO.sendIack();
this._r.iif = 1;
this._r.r7 -= 4;
MEM.write(this._r.r7 & ~(0x03), this._r.pc - 4);
this._r.pc = MEM.read(12);
},
acceptMaskableInterrupt: function() {
this._setFlag(this._f.GIE, 0);
this._r.r7 -= 4;
MEM.write(this._r.r7 & ~(0x03), this._r.pc - 4);
this._r.pc = MEM.read(8);
},
acceptInterrupt: function() {
this._setFlag(this._f.INT2, IO.testInterrupt(2));
this._setFlag(this._f.INT1, IO.testInterrupt(1));
this._setFlag(this._f.INT0, IO.testInterrupt(0));
if (this._r.iif === 0) {
return;
} else {
if (IO.testInterrupt(3)) {
this.acceptNonmaskableInterrupt();
} else {
if (this._getFlag(this._f.GIE) === 0) {
return;
} else {
if ((this._getFlag(this._f.INT2) && this._getFlag(this._f.EINT2)) ||
(this._getFlag(this._f.INT1) && this._getFlag(this._f.EINT1)) ||
(this._getFlag(this._f.INT0) && this._getFlag(this._f.EINT0))) {
this.acceptMaskableInterrupt();
}
}
}
}
},
run: function(fastest) {
if (typeof this.onBeforeRun !== 'undefined') {
this.onBeforeRun();
}
if (!fastest) {
this._runTimer = setInterval(this.performCycle.bind(this), (1 / this._frequency) * 1000);
} else {
while (1) {
try {
this.performCycle();
} catch (e) {
if (e.name === "Halting") {
break;
} else {
throw e;
}
}
}
}
},
pause: function() {
if (typeof this._runTimer !== 'undefined') {
clearInterval(this._runTimer);
}
},
stop: function() {
if (typeof this._runTimer !== 'undefined') {
clearInterval(this._runTimer);
}
if (typeof this.onStop !== 'undefined') {
this.onStop();
}
throw {
name: "Halting",
message: "Stop the CPU",
};
},
performCycle: function() {
if (typeof this.onBeforeCycle !== 'undefined') {
var val = this.onBeforeCycle();
if (typeof val !== 'undefined' && val === false) {
return;
}
}
var instruction = MEM.read(this._r.pc);
var decodedInstruction = this._decode(instruction);
if (decodedInstruction.op !== null && decodedInstruction.args !== null) {
if (typeof this.onBeforeExecute !== 'undefined') {
this.onBeforeExecute(decodedInstruction);
}
this._i[decodedInstruction.op].apply(this, decodedInstruction.args);
this._r.pc += 4;
this.acceptInterrupt();
if (typeof this.onAfterCycle !== 'undefined') {
this.onAfterCycle();
}
} else {
this.stop();
throw new Error('undefined operation code or wrongly defined arguments');
}
},
reset: function() {
this._r = {r0:0, r1:0, r2:0, r3:0, r4:0, r5:0, r6:0, r7:0, pc:0, sr:0, iif:1};
},
_instructionMap: {
'00000' : 'MOVE',
'00001' : 'OR',
'00010' : 'AND',
'00011' : 'XOR',
'00100' : 'ADD',
'00101' : 'ADC',
'00110' : 'SUB',
'00111' : 'SBC',
'01000' : 'ROTL',
'01001' : 'ROTR',
'01010' : 'SHL',
'01011' : 'SHR',
'01100' : 'ASHR',
'01101' : 'CMP',
// 01110 Not used
// 01111 Not used
'11000' : 'JP',
'11001' : 'CALL',
'11010' : 'JR',
'11011' : 'RET',
'10110' : 'LOAD',
'10111' : 'STORE',
'10010' : 'LOADB',
'10011' : 'STOREB',
'10100' : 'LOADH',
'10101' : 'STOREH',
'10000' : 'POP',
'10001' : 'PUSH',
'11111' : 'HALT'
},
_conditionMap : {
'0000' : '',
'0001' : '_N/M',
'0010' : '_NN/P',
'0011' : '_C/UGE',
'0100' : '_NC/ULT',
'0101' : '_V',
'0110' : '_NV',
'0111' : '_Z/EQ',
'1000' : '_NZ/NE',
'1001' : '_ULE',
'1010' : '_UGT',
'1011' : '_SLT',
'1100' : '_SLE',
'1101' : '_SGE',
'1110' : '_SGT'
}
};
//
// FRISC IO components
//
var IO = {
// units stored in arrays by interrupt level
_units : {
interrupt : [[], [], [], []],
noninterrupt : []
},