-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.js
847 lines (791 loc) · 30.5 KB
/
simulate.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
var STACK_POINTER = 13;// stack pointer index
var LR = 14; // link and return index
var PC = 15;
var mem = new Uint8Array(2048);
var regs = new Int32Array(16);
regs[15] = 4; // set program counter 2 since its always 2 instructions ahaed of what we execture
var zeroFlag = 0,negativeFlag = 0,carryFlag = 0, overflowFlag = 0;
// conditons flags for implemented formats 1-4 not implemented
var startTimer;
function start(){
startTimer = setInterval(step,1000);
}
function stop(){
cancelTimedExecution();
}
function cancelTimedExecution(){
if(startTimer != undefined){
clearInterval(startTimer);
startTimer = undefined;
}
}
// executes of statement
function step(){
"use strict";
var instrLoc = regs[PC]-4;
if(instrLoc < mem.length){
var instr = mem[instrLoc] | mem[instrLoc+1]<<8
console.log("instr exec"+instrLoc);
simulate(instr);// load upper and lower part of half word instruction
printRegisterContent(regs);
regs[PC] += 2; // increment by one
}else{ // disable buttons of step and start since execution finished
printProgramOut('program terminated')
cancelTimedExecution();
}
}
// returns binary format of decimal numbers
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
function simulate(instr) {
"use strict";
var fmt = instr>>13; // discard all but last 3 bits used for format identification
console.log('simulate case '+fmt);
console.log('simulate instr'+instr);
switch(fmt){
case 0b000: // for format zero check whether to add/subtract or shift register
((instr >> 11) & 0b11) == 3 ? addSubtract(instr) : moveShiftedRegister(instr);
break;
case 0b001: // arithmetic operations with immediate value
arithmeticImediate(instr);
break;
case 0b010: // format 4 & 5 & 6 & 7
if(instr >> 10 == 0x11){
hiRegisterBranch(instr);
}else if (instr >> 10 == 0x10)
alu(instr); // alu operations format 4
else if ( ((instr >> 11) & 0x9) == 0x9)
pcRelativeLoad(instr); // format 6
else if ( ((instr >> 12) & 0x5) == 0x5)
loadStoreRegisterOffset(instr); // format 7
break;
case 0b011: // format 9
loadStoreWithImmOffset(instr);
break;
case 0b100: // format 10 & 11
if( (instr>>12) == 0x8) // format 10
loadStoreHalfword(instr);
else if( (instr>>12) == 0x9)
SPloadStore(instr); // format 11
case 0b101: // format 12 & 13 & 14
//format 13 and 14 may have clashed since L can be 0 or 1 using previous parse
if((instr>>12) == 0xA)
{
console.log('format 12');
loadAddress(instr);
}
else if ( (instr>>8) == 0xb0 ){
addOffsetStackPointer(instr); // format
}else if(((instr >> 9) & 0b10) == 0b10){
pushPopRegisters(instr);
}else{
console.log('undefined format');
}
break;
case 0b110: // format 15 & 16 & 17
if(instr == 0xdead){
console.log('program terminated');
terminateProgram(0);
}else if(instr>>8 == 0xdf){
softwareInterrupt(instr); // format 17
console.log('format 17');
}else{
conditionalBranch(instr); // format 16
console.log('format 16');
}
break;
case 0b111: // format 18 & 19
if(instr>>11 == 0x1c){
unconditionalBranch(instr); // format 18
}else {
longBranchWithLink(instr);// format 19
}
break;
default:
break;
}
}
//format 2 completed condition codes
function addSubtract(instr){
"use strict";
var offsetNReg = (instr>>6) & 0b111; // register id or immediate value depending
var destinationReg = instr & 0b111; // on op immediate flag
var sourceReg = (instr>>3) & 0b111;
var immediateFlag = (instr>>10) & 0b1;
var opCode = (instr>>9) & 0b1;
var stringInstr;
if(opCode == 0){ // add
stringInstr = "ADD R" + destinationReg + ", R" + sourceReg;
if(immediateFlag == 1){
stringInstr += ", #" + offsetNReg;
regs[destinationReg] = regs[sourceReg] + offsetNReg;
overflowFlag = isAddOverflowing(regs[sourceReg],offsetNReg,regs[destinationReg]);
carryFlag = isAddGenCarry(regs[sourceReg],offsetNReg);
}else{
stringInstr += ", R" + offsetNReg;
regs[destinationReg] = regs[sourceReg] + regs[offsetNReg];
overflowFlag = isAddOverflowing(regs[sourceReg],regs[offsetNReg],regs[destinationReg]);
carryFlag = isAddGenCarry(regs[sourceReg],regs[offsetNReg]);
}
}else{ // subtract
stringInstr = "SUB R" + destinationReg + ", R" + sourceReg;
if(immediateFlag == 1){
stringInstr += ", #" + offsetNReg;
regs[destinationReg] = regs[sourceReg] - offsetNReg;
// adding negative since this is equivalent to addition with negative of second operant
overflowFlag = isAddOverflowing(regs[sourceReg],-offsetNReg,regs[destinationReg]);
carryFlag = isAddGenCarry(regs[sourceReg],-offsetNReg);
}else{
stringInstr += ", R" + offsetNReg;
regs[destinationReg] = regs[sourceReg] - regs[offsetNReg];
overflowFlag = isAddOverflowing(regs[sourceReg],-regs[offsetNReg],regs[destinationReg]);
carryFlag = isAddGenCarry(regs[sourceReg],-regs[offsetNReg]);
}
}
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
printInstruction(stringInstr);
}
// format 1 condition flags done
function moveShiftedRegister(instr){
"use strict";
var offset = (instr>>6) & 0b11111; // extract offset
var destinationReg = instr & 0b111;
var sourceReg = (instr>>3) & 0b111;
var opcode = (instr>>11) & 0b11;
var stringInstr; // string representation of instruction
switch(opcode){
case 0:
if(regs[sourceReg] != 0)
carryFlag = regs[sourceReg] & 1<<(32-offset);
regs[destinationReg] = regs[sourceReg]<<offset; // left arthmetic shift
stringInstr = concatArgs('LSL ','R',destinationReg
,',','R',sourceReg,',','#',offset);
break;
case 1:
if(regs[sourceReg] != 0)
carryFlag = regs[sourceReg]>>(regs[sourceReg]-1) & 1;
regs[destinationReg] = regs[sourceReg]>>offset; // right logical shift
stringInstr = concatArgs('LSR ','R',destinationReg
,',','R',sourceReg,',','#',offset);
break;
case 2:
if(regs[sourceReg] != 0)
carryFlag = regs[destinationReg]>>>(regs[sourceReg]-1) & 1;
regs[destinationReg] = regs[sourceReg]>>>offset; // right arithmetic shift
stringInstr = concatArgs('ASR ','R',destinationReg
,',','R',sourceReg,',','#',offset);
break;
default:
console.log('move shifted register unknown operation');
stringInstr = 'unknown instruction';
break;
}
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
printInstruction(stringInstr);
}
// format 3, condition codes done
function arithmeticImediate(instr){
"use strict";
var offset8 = instr & 0xff;
var destinationReg = (instr>>8) & 0b111;
var opCode = (instr>>11) & 0b11;
var stringInstr;
switch(opCode){
case 0:
regs[destinationReg] = offset8;
stringInstr = concatArgs('MOV ','R',
destinationReg,',#',offset8);
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
break;
case 1:
var tmp = regs[destinationReg] - offset8;
zeroFlag = Number(tmp == 0);
negativeFlag = Number(tmp < 0);
overflowFlag = isAddOverflowing(regs[destinationReg],-offset8,tmp);
carryFlag = isAddGenCarry(regs[destinationReg],-offset8);
break;
case 2:
overflowFlag = isAddOverflowing(regs[destinationReg],
offset8,regs[destinationReg]+offset8);
carryFlag = isAddGenCarry(regs[destinationReg],offset8);
regs[destinationReg] = regs[destinationReg] + offset8;
stringInstr = concatArgs('ADD ','R',
destinationReg,',R',destinationReg,',#',offset8);
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
break;
case 3:
overflowFlag = isAddOverflowing(regs[destinationReg],
offset8,regs[destinationReg]+offset8);
carryFlag = isAddGenCarry(regs[destinationReg],offset8);
regs[destinationReg] = regs[destinationReg] - offset8;
stringInstr = concatArgs('SUB ','R',
destinationReg,',R',destinationReg,',#',offset8);
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
break;
}
printInstruction(stringInstr);
}
//format 4 complete
function alu(instr){
"use strict";
var destinationReg = instr & 0b111;
var sourceReg = (instr>>3) & 0b111;
var opcode = (instr>>6) & 0xf;
var stringInstr;
console.log('alu called');
switch(opcode){
case 0: // flags done at endof switch
regs[destinationReg] = regs[destinationReg] & regs[sourceReg];
stringInstr = 'AND R'+destinationReg+',R'+sourceReg;
break;
case 1:// flags done
regs[destinationReg] ^= regs[sourceReg];
stringInstr = 'EOR R'+destinationReg+',R'+sourceReg;
break;
case 2://flags done
//check carry out
if(regs[sourceReg] != 0)
carryFlag = regs[destinationReg] & 1<<(32-regs[sourceReg]);
regs[destinationReg] = regs[destinationReg]<<regs[sourceReg];
stringInstr = 'LSL R'+destinationReg+',R'+sourceReg;
break;
case 3:// done flags
if(regs[sourceReg] != 0)
carryFlag = regs[destinationReg]>>(regs[sourceReg]-1) & 1;
regs[destinationReg] = regs[destinationReg]>>regs[sourceReg];
stringInstr = 'LSR R'+destinationReg+',R'+sourceReg;
break;
case 4:// done
if(regs[sourceReg] != 0)
carryFlag = regs[destinationReg]>>>(regs[sourceReg]-1) & 1;
regs[destinationReg] = regs[destinationReg]>>>regs[sourceReg];
stringInstr = 'ASR R'+destinationReg+',R'+sourceReg;
break;
case 5:
var tmp = regs[destinationReg];
regs[destinationReg] += regs[sourceReg];
regs[destinationReg] += carryFlag;
stringInstr = 'ADC R'+destinationReg+',R'+sourceReg;
overflowFlag = isAddOverflowing(tmp,reg[sourceReg]);
overflowFlag |= isAddOverflowing(tmp+regs[sourceReg],carryFlag);
var carry = Number(tmp)+Number(regs[sourceReg])+carryFlag;
carryFlag = carry>>32; //extract
break;
case 6:
var tmp = regs[destinationReg];
regs[destinationReg] -= regs[sourceReg];
regs[destinationReg] -= (carryFlag); // and with one since carry flag should be 1 bit amd carryhere is i
stringInstr = 'SBC R' + destinationReg+',R'+sourceReg;
overflowFlag = isAddOverflowing(tmp,-reg[sourceReg]);
overflowFlag |= isAddOverflowing(tmp-regs[sourceReg],-carryFlag);
var carry = Number(tmp)-Number(regs[sourceReg])-carryFlag;
carryFlag = carry>>32; //extract
break;
case 7:// rotate right //not sure of c flag status
var tmp = regs[destinationReg]<<(32-regs[sourceReg]);
regs[destinationReg] = regs[destinationReg]>>regs[sourceReg];
regs[destinationReg] |= tmp;
stringInstr = 'ROR R'+destinationReg+',R'+sourceReg;
break;
case 8:// TST print inside since it doesnt have result in destination reg
var result = regs[destinationReg] & regs[sourceReg];
stringInstr = 'TST R' + destinationReg + ', R' + sourceReg;
zeroFlag = Number(result == 0);
negativeFlag = Number(result < 0);
printInstruction(stringInstr);
return;
break;
case 9:
regs[destinationReg] = -regs[sourceReg];
stringInstr = 'NEG R'+destinationReg+',R'+sourceReg;
break;
case 10://CMP
var result = regs[destinationReg] - regs[sourceReg];
stringInstr = 'CMP R'+destinationReg+',R'+sourceReg;
zeroFlag = Number(result == 0);
negativeFlag = Number(result < 0);
overflowFlag = isAddOverflowing(regs[destinationReg]
,regs[sourceReg],result);
carryFlag = isAddGenCarry(regs[destinationReg],regs[sourceReg]);
printInstruction(stringInstr);
return;
break;
case 11:
var result = regs[destinationReg] + regs[sourceReg];
stringInstr = 'CMN R'+destinationReg+',R'+sourceReg;
zeroFlag = Number(result == 0);
negativeFlag = Number(result < 0);
overflowFlag = isAddOverflowing(regs[destinationReg]
,regs[sourceReg],result);
carryFlag = isAddGenCarry(regs[destinationReg],regs[sourceReg]);
printInstruction(stringInstr);
return;
break;
case 12:
regs[destinationReg] |= regs[sourceReg];
stringInstr = 'ORR R'+destinationReg+',R'+sourceReg;
break;
case 13:// c and v unchanged since he wants corruption
regs[destinationReg] *= regs[sourceReg];
stringInstr = 'MUL R'+destinationReg+',R'+sourceReg;
break;
case 14:
regs[destinationReg] = regs[destinationReg] & (~regs[sourceReg]);
stringInstr = 'BIC R'+destinationReg+',R'+sourceReg;
break;
default: // opcode 1111 // left because unknown
regs[destinationReg] = ~regs[sourceReg];
stringInstr = 'MVN R'+destinationReg+',R'+sourceReg;
break;
}//common case for all
zeroFlag = Number(regs[destinationReg] == 0);
negativeFlag = Number(regs[destinationReg] < 0);
printInstruction(stringInstr);
}
// format 6
function pcRelativeLoad(instr){
'use strict';
var rd = (instr>>8) & 0b111;
var word8 = (instr>>8) & 255; // check doc notes
regs[rd] = mem[word8+regs[PC]];
printInstruction('LDR R'+rd+'[PC,#'+word8+']');
}
/*
// format 7
function loadStoreRegisterOffset(instr){
'use strict';
var offsetRegIndex = instr>>6&0b111;
var baseRegisterIndex = instr>>3&0b111;// array index
var offsetReg = regs[offsetRegIndex];
var baseRegister = regs[baseRegisterIndex]; // regs value
var registerSDNum = instr&0b111; // source/destination register index
var stringInstr;
if(instr>>11 & 1 == 0){ // checking L whether store or load
mem[offsetReg+baseRegister] = registerSDNum & 0xff; // get only first 8 bits
if( (instr>>10) & 1 == 0){ // save the rest of word
mem[offsetReg+baseRegister+1] = (regs[registerSDNum]>>8) & 0xff;
mem[offsetReg+baseRegister+2] = (regs[registerSDNum]>>16) & 0xff;
mem[offsetReg+baseRegister+3] = (regs[registerSDNum]>>24) & 0xff;
stringInstr = 'STR';
}else
stringInstr = 'STRB';
}else{
regs[registerSDNum] = mem[offsetReg+baseRegister];
if( (instr>>10 & 1) == 0){
regs[registerSDNum] |= mem[offsetReg+baseRegister+1]<<8; // load bits intro appropriate positons
regs[registerSDNum] |= mem[offsetReg+baseRegister+2]<<16;
regs[registerSDNum] |= mem[offsetReg+baseRegister+3]<<24;
stringInstr = 'LDR';
}else
stringInstr = 'LDRB';
}
stringInstr += ' R,'+registerSDNum+'[R'+baseRegisterIndex+',R'+offsetRegIndex+']';
printInstruction(stringInstr);
} */
// format 7 remade
function loadStoreRegisterOffset(instr){
'use strict';
var rd = instr & 0x7;
var rb = (instr>>3) & 0x7;
var ro = (instr>>6) & 0x7;
var stringInstr;
if( ( (instr>>11) & 1) == 0){ // checking L whether store or load
mem[regs[rb]+regs[ro]] = registerSDNum & 0xff; // get only first 8 bits
if( ((instr>>10) & 1) == 0){ // save the rest of word
mem[regs[rb]+regs[ro]+1] = (regs[rd]>>8) & 0xff;
mem[regs[rb]+regs[ro]+2] = (regs[rd]>>16) & 0xff;
mem[regs[rb]+regs[ro]+3] = (regs[rd]>>24) & 0xff;
stringInstr = 'STR';
}else
stringInstr = 'STRB';
}else{
regs[registerSDNum] = mem[regs[rb]+regs[ro]];
if( ((instr>>10) & 1) == 0){
regs[rd] |= mem[regs[rb]+regs[ro]+1]<<8; // load bits intro appropriate positons
regs[rd] |= mem[regs[rb]+regs[ro]+2]<<16;
regs[rd] |= mem[regs[rb]+regs[ro]+3]<<24;
stringInstr = 'LDR';
}else
stringInstr = 'LDRB';
}
stringInstr += ' R,'+rd+'[R'+rb+',R'+ro+']';
printInstruction(stringInstr);
}
// format 9
function loadStoreWithImmOffset(instr){
'use strict';
var offset5 = (instr>> 6) & 0x1f;
var baseRegisterIndex = (instr>>3) & 0b111;// array index
var baseRegister = regs[baseRegisterIndex]; // regs value
var registerSDNum = instr & 0b111; // source/destination register index
var stringInstr;
var cond = (instr>>11) & 1;
if(cond == 0){ // checking L whether store or load
mem[baseRegister+offset5] = registerSDNum & 0xff; // get only first 8 bits
if( ((instr>>12) & 1) == 0){ // save the rest of word
mem[offset5+baseRegister+1] = regs[registerSDNum]>>8 & 0xff;
mem[offset5+baseRegister+2] = regs[registerSDNum]>>16 & 0xff;
mem[offset5+baseRegister+3] = regs[registerSDNum]>>24 & 0xff;
stringInstr = 'STR';
}else
stringInstr = 'STRB';
}else{
regs[registerSDNum] = mem[offset5+baseRegister];
if( ((instr>>12) & 1) == 0){
regs[registerSDNum] |= mem[offset5+baseRegister+1]<<8; // load bits intro appropriate positons
regs[registerSDNum] |= mem[offset5+baseRegister+2]<<16;
regs[registerSDNum] |= mem[offset5+baseRegister+3]<<24;
stringInstr = 'LDR';
}else
stringInstr = 'LDRB';
}
stringInstr += ' R'+registerSDNum+' ,[R'+baseRegisterIndex+',#'+offset5+']';
printInstruction(stringInstr);
}
//format 13
function addOffsetStackPointer(instr){
var immediate = (instr & 0x7f)<<2; // offset given in word size
if( (instr>>7) & 1 == 1)
immediate = -immediate;
regs[STACK_POINTER] += immediate;
printInstruction('ADD SP,#'+immediate);
}
//format 18
function unconditionalBranch(instr){
var offset11 = instr & 0x7ff;
//sign extend and mult by 2
offset11 = offset11 << 21;// shifting to sign extend the bits
offset11 = offset11 >>> 20;
var stringInstr = "B " + offset11;
regs[PC] += offset11 ;
printInstruction(stringInstr);
}
// format 19
function longBranchWithLink(instr){
var offset = instr & 0x7ff;
if ((instr >> 11 & 1) == 0) {
offset = offset<<21; // sign extending
offset = offset>>>21;
offset = offset<<12;
regs[LR] = regs[PC] + offset;
}else {
var tmp = regs[PC] ;// address of next instruction = tmp ?
console.log(tmp);
offset = offset<<21;
offset = (offset>>>20);
regs[PC] = regs[LR] + offset + 2;
regs[LR] = tmp;
}
var stringInstr = "BL " + offset; // supposed to be label
printInstruction(stringInstr);
}
//format 16
function conditionalBranch(instr){
"use strict";
var instrString = '';
var cond = (instr>>8) & 0xf;
var offset = instr & 0xff;
//sign extend
offset = offset << 24;
offset = offset >>> 23;
// mult * 2 or shift by one
switch(cond){
case 0:
if(zeroFlag == 1) regs[PC] += (offset)*2;
instrString = 'BEQ';
break;
case 1:
if(zeroFlag == 0) regs[PC] += (offset)*2;
instrString = 'BNE';
break;
case 2:
if(carryFlag == 1) regs[PC] += (offset)*2;
instrString = 'BCS';
break;
case 3:
if(carryFlag == 0) regs[PC] += (offset)*2;
instrString = 'BCC';
break;
case 4:
if(negativeFlag == 1) regs[PC] += (offset)*2;
instrString = 'BMI';
break;
case 5:
if(negativeFlag == 0) regs[PC] += (offset)*2;
instrString = 'BPL';
break;
case 6:
if(overflowFlag == 1) regs[PC] += (offset)*2;
instrString = 'BVS';
break;
case 7:
if(overflowFlag == 0) regs[PC] += (offset)*2;
instrString = 'BCS';
break;
case 8:
if(carryFlag == 1 && zeroFlag == 0) regs[PC] += (offset)*2;
instrString = 'BHI';
break;
case 9:
if(carryFlag == 0 || zeroFlag == 1) regs[PC] += (offset)*2;
instrString = 'BLS';
break;
case 10:
if(negativeFlag == overflowFlag) regs[PC] += (offset)*2;
instrString = 'BGE';
break;
case 11:
if(negativeFlag != overflowFlag) regs[PC] += (offset)*2;
instrString = 'BLT';
break;
case 12:
if(zeroFlag == 0 && (negativeFlag == overflowFlag))
regs[PC] += (offset);
instrString = 'BGT';
break;
case 13:
if(zeroFlag == 1 || negativeFlag == overflowFlag)
regs[PC] += (offset)*2;
instrString = 'BLE';
break;
}
instrString += ' ' + offset;
printInstruction(instrString);
}
// concatinates all strings and integers into a string
function concatArgs(){
var str = '';
for (var i = 0; i < arguments.length; i++) {
str = str + arguments[i];
}
return str;
}
// format 14
function pushPopRegisters(instr){
"use strict";
var instrString = '';
var push = (instr>>11) & 1;
if(push == 0){ // push regs
instrString = 'PUSH{ ';
for(var i = 0; i < 8;i++){
if((instr>>i) & 1 == 1){
regs[STACK_POINTER] -= 4;
mem[regs[STACK_POINTER]] = (regs[i]) & 0xff;//chk
mem[regs[STACK_POINTER]+1] = (regs[i]>>8) & 0xff;
mem[regs[STACK_POINTER]+2] = (regs[i]>>16) & 0xff;
mem[regs[STACK_POINTER]+3] = (regs[i]>>24) & 0xff;
instrString += 'R'+i+',';
}
}
if((instr>>8) & 1 == 1){ // save lr reg
regs[STACK_POINTER] -= 4;
mem[regs[STACK_POINTER]] = (regs[LR]) & 0xff;//chk
mem[regs[STACK_POINTER]+1] = (regs[LR]>>8) & 0xff;
mem[regs[STACK_POINTER]+2] = (regs[LR]>>16) & 0xff;
mem[regs[STACK_POINTER]+3] = (regs[LR]>>24) & 0xff;
instrString += 'LR';
}
}else{ // pop regs
instrString = 'POP{ ';
if(((instr>>8) & 1) == 1){
regs[PC] = mem[regs[STACK_POINTER]];
regs[PC] |= mem[regs[STACK_POINTER]+1]<<8;
regs[PC] |= mem[regs[STACK_POINTER]+2]<<16;
regs[PC] |= mem[regs[STACK_POINTER]+3]<<24;
regs[STACK_POINTER] += 4;
instrString += 'PC,';
}
for(var i = 7; i >= 0;i--){
if(instr>>i & 1 == 1){
regs[i] = mem[regs[STACK_POINTER]];
regs[i] |= mem[regs[STACK_POINTER]+1]<<8;
regs[i] |= mem[regs[STACK_POINTER]+2]<<16;
regs[i] |= mem[regs[STACK_POINTER]+3]<<24;
regs[STACK_POINTER] += 4;
instrString += 'R'+i+',';
}
}
}
if(instrString.length == instrString.lastIndexOf(','))
instrString = instrString.substr(0,instrString-1); // remove last comma
printInstruction(instrString+'}');
}
// disables step and start buttons
function terminateProgram(status){
cancelTimedExecution();
printProgramOut("program terminated ");
printInstruction("0xDEAD");
disableExecutionButtons();
}
// format 17 not implemented yet
function softwareInterrupt(instr){
var value8 = instr & 0xff;
switch (value8){
case 1:
printProgramOut(regs[0]);
printInstruction("SWI 1");
break;
case 2: // reading char
var input = prompt("Enter a character");
mem[regs[0]] = input.charCodeAt(0);
break;
case 3: // reading string assume r0 contains address r1 contains string size
var input = prompt("Enter a string");
for(var i = 0; i < regs[1]-1;i++){
mem[regs[0]+i] = input.charCodeAt(i);
}mem[regs[0]+i] = 0;
break;
case 5: // print char
printProgramOut(String.fromCharCode(mem[regs[0]]));
break;
case 6: // print string
var i = regs[0];
var str = '';
while(mem[i] != 0)
str += String.fromCharCode(mem[i++]);
printProgramOut(str);
break;
}
}
/*
// format 10 not required
function loadStoreHalfword(instr){
'use strict';
var offset5 = instr>>6&0b11111;
var baseRegisterIndex = instr>>3&0b111;// array index
var baseRegister = regs[baseRegisterIndex]; // regs value
var registerSDNum = instr&0b111; // source/destination register index
var stringInstr;
if(instr>>11 & 1 == 0){ // checking L whether store or load
mem[baseRegister+offset5] = registerSDNum&255; // get only first 8 bits
mem[offset5+baseRegister+1] = regs[registerSDNum]>>8 & 255;
stringInstr = 'STRH';// save one byte and return
}
else{
regs[registerSDNum] = mem[offset5+baseRegister];
regs[registerSDNum] |= mem[offset5+baseRegister+1]<<8; // load bits intro appropriate positons
stringInstr = 'LDRH';
}
stringInstr += ' R'+registerSDNum+' ,[R'+baseRegisterIndex+',#'+offset5+']';
printInstruction(stringInstr);
} */
/*
// format 11 not required
function SPloadStore(instr){
var immediate = instr & 0b11111111;
var destinationReg = instr >> 8 & 0b111;
var result = immediate + reg[13];
var stringInstr;
if (instr >> 11 & 1 == 0) {
mem[result] = regs[destinationReg] & 255;
mem[result + 1] = regs[destinationReg] >> 8 & 255;
mem[result + 2] = regs[destinationReg] >> 16 & 255;
mem[result + 3] = regs[destinationReg] >> 32 & 255;
stringInstr = "STR ";
}
else {
regs[destinationReg] = mems[result];
regs[destinationReg] |= mems[result + 1] << 8;
regs[destinationReg] |= mems[result + 2] << 16;
regs[destinationReg] |= mems[result + 3] << 32;
stringInstr = "LDR ";
}
stringInstr += 'R' + destinationReg + ",[R13] #" + immediate + ']';
printInstruction(stringInstr);
} */
// format 12 not required
function loadAddress(instr) {
var immediate = instr & 0xff;
var destinationReg = instr >> 8 & 0b111;
var stringInstr = "ADD R";
var sp = (instr>>11) & 1;
if ( ((instr >> 11) & 1) == 0) {
/*regs[destinationReg] = mem[immediate + regs[PC]];
regs[destinationReg] = mem[immediate + regs[PC] + 1] << 8;
regs[destinationReg] = mem[immediate + regs[PC] + 2] << 16;
regs[destinationReg] = mem[immediate + regs[PC] + 3] << 32; */
regs[destinationReg] = regs[PC] + immediate;
stringInstr += destinationReg + ", R15, #" + immediate;
}
else {/*
regs[destinationReg] = mem[immediate + regs[STACK_POINTER]];
regs[destinationReg] = mem[immediate + regs[STACK_POINTER] + 1] << 8;
regs[destinationReg] = mem[immediate + regs[STACK_POINTER] + 2] << 16;
regs[destinationReg] = mem[immediate + regs[STACK_POINTER] + 3] << 32; */
regs[destinationReg] = regs[STACK_POINTER] + immediate;
stringInstr += destinationReg + ", R13, #" + immediate;
}
printInstruction(stringInstr);
}
// determines if answer will overflow
function isAddOverflowing(x,y,z){
var overflow;
if(x < 0 && y < 0 && z >= 0)
overflow = 1;
else if(x > 0 && y > 0 && z < 0)
overflow = 1;
else
overflow = 0;
return overflow;
}
function isAddGenCarry(x,y){
if((x >= 0 && y < 0) || (x < 0 && y >= 0))
return 0; // no carry if opposite signs
if(x < 0&& y < 0)
return Number((Number(x) + Number(y))>>32 & 1 == 0); // carry for negative if bit zero
return (Number(x) + Number(y))>>32 & 1;
}
// format 5
function hiRegisterBranch(instr){
"use strict";
var rdhs = instr & 0x7;
var rshs = (instr>>3) & 0x7;
var h2 = (instr>>6) & 1;
var h1 = (instr>>7) & 1;
var opcode = (instr>>8) & 0x3;
var stringInstr;
switch(opcode){
case 0:
stringInstr = "ADD R";
if(h1 == 0 && h2 == 1){
regs[rdhs] += regs[rshs+8];
stringInstr += rdhs + ",R"+(rshs+8);
}else if(h1 == 1 && h2 == 0){
regs[rdhs+8] += regs[rshs];
stringInstr += (rdhs+8) + ",R"+rshs;
}else if(h1 == 1 && h2 == 1){
regs[rdhs+8] += regs[rshs+8];
stringInstr += (rdhs+8) + ",R"+(rshs+8);
}
break;
case 1:// cmp instructions
stringInstr = "undefined case 1 format 5";
if(h1 == 0 && h2 == 1){
}else if(h1 == 1 && h2 == 0){
}else if(h1 == 1 && h2 == 1){
}
break;
case 2:
stringInstr = "MOV R";
if(h1 == 0 && h2 == 1){
regs[rdhs] = regs[rshs+8];
stringInstr += rdhs + ",R"+(rshs+8);
}else if(h1 == 1 && h2 == 0){
regs[rdhs+8] = regs[rshs];
stringInstr += (rdhs+8) + ",R"+rshs;
}else if(h1 == 1 && h2 == 1){
regs[rdhs+8] = regs[rshs+8];
stringInstr += (rdhs+8) + ",R"+(rshs+8);
}
break;
default:
stringInstr = "undefined case 1 format 5";
}
printInstruction(stringInstr)
}