-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharm_isa.cpp
2799 lines (2327 loc) · 86.8 KB
/
arm_isa.cpp
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
/**
* @file arm_isa.cpp
* @author Danilo Marcolin Caravana
* Rafael Auler
*
* The ArchC Team
* http://www.archc.org/
*
* Computer Systems Laboratory (LSC)
* IC-UNICAMP
* http://www.lsc.ic.unicamp.br/
*
* @version 1.0
* @date Apr 2012
*
* @brief The ArchC ARMv5e functional model.
*
* @attention Copyright (C) 2002-2012 --- The ArchC Team
*
*/
#include "arm_isa.H"
#include "arm_isa_init.cpp"
#include "arm_bhv_macros.H"
#include <stdint.h> // define types uint32_t, etc
using namespace arm_parms;
#define DEFAULT_STACK_SIZE (512 * 1024)
static int processors_started = 0;
//If you want debug information for this model, uncomment next line
//#define DEBUG_MODEL
//If you want the processor to operate in system model instead
//of user model, uncomment next line
//#define SYSTEM_MODEL
//This is a switch to turn unpredictable behavior for some
//instructions to be silently ignored. This is necessary for
//some gcc generated ARM user level code.
#define FORGIVE_UNPREDICTABLE
// If FORGIVE_UNPREDITABLE is turned on, necessarily turns off
// SYSTEM_MODEL, since the system model cannot work with this flag.
#ifdef FORGIVE_UNPREDITABLE
#undef SYSTEM_MODEL
#endif
#ifdef DEBUG_MODEL
#include <stdarg.h>
static inline int dprintf(const char *format, ...) {
int ret;
va_list args;
va_start(args, format);
ret = vfprintf(ac_err, format, args);
va_end(args);
return ret;
}
#else
inline void dprintf(const char *format, ...) {}
#endif
//! User defined macros to reference registers
#define LR 14 // link return
#define PC 15 // program counter
#ifdef SYSTEM_MODEL
#define RB_write bypass_write
#define RB_read bypass_read
#else
#define RB_write RB.write
#define RB_read RB.read
#endif
#ifdef SLEEP_AWAKE_MODE
/*********************************************************************************/
/* SLEEP / AWAKE mode control */
/* INTR_REG may store 1 (AWAKE MODE) or 0 (SLEEP MODE) */
/* if intr_reg == 0, the simulator will be suspended until it receives a */
/* interruption 1 */
/*********************************************************************************/
#define test_sleep() { if (intr_reg.read() == 0) ac_wait(); }
#else
#define test_sleep() {}
#endif
void ac_behavior( begin ) {
#ifdef SYSTEM_MODEL
arm_proc_mode.mode = processor_mode::SUPERVISOR_MODE;
arm_proc_mode.thumb = false;
arm_proc_mode.fiq = false;
arm_proc_mode.irq = false;
ac_pc = 0;
#endif
// Initializing flags and model variables
flags.Z = false;
flags.C = false;
flags.N = false;
flags.V = false;
flags.Q = false;
flags.T = false;
execute = false;
dpi_shiftop.entire = 0;
dpi_shiftopcarry = false;
ls_address.entire = 0;
lsm_startaddress.entire = 0;
lsm_endaddress.entire = 0;
OP1.entire = 0;
OP2.entire = 0;
RB.write(13, AC_RAM_END - 1024 - processors_started++ * DEFAULT_STACK_SIZE);
}
//!Generic instruction behavior method.
void ac_behavior( instruction ) {
test_sleep();
dprintf("-------------------- PC=%#x -------------------- %lld\n", (uint32_t)ac_pc, ac_instr_counter);
// Conditionally executes instruction based on COND field, common to all ARM instructions.
execute = false;
switch(cond) {
case 0: if (flags.Z == true) execute = true; break;
case 1: if (flags.Z == false) execute = true; break;
case 2: if (flags.C == true) execute = true; break;
case 3: if (flags.C == false) execute = true; break;
case 4: if (flags.N == true) execute = true; break;
case 5: if (flags.N == false) execute = true; break;
case 6: if (flags.V == true) execute = true; break;
case 7: if (flags.V == false) execute = true; break;
case 8: if ((flags.C == true)&&(flags.Z == false)) execute = true; break;
case 9: if ((flags.C == false)||(flags.Z == true)) execute = true; break;
case 10: if (flags.N == flags.V) execute = true; break;
case 11: if (flags.N != flags.V) execute = true; break;
case 12: if ((flags.Z == false)&&(flags.N == flags.V)) execute = true; break;
case 13: if ((flags.Z == true)||(flags.N != flags.V)) execute = true; break;
case 14: execute = true; break;
default: execute = false;
}
// PC increment
ac_pc += 4;
RB_write(PC, ac_pc);
if(!execute) {
dprintf("cond=0x%X\n", cond);
dprintf("Instruction will not be executed due to condition flags.\n");
ac_annul();
}
}
// Instruction Format behavior methods.
//!DPI1 - Second operand is register with imm shift
void ac_behavior( Type_DPI1 ) {
arm_isa::reg_t RM2;
// Special case: rm = 15
if (rm == 15) {
// PC is already incremented by four, so only add 4 again (not 8)
RM2.entire = RB_read(rm) + 4;
}
else RM2.entire = RB_read(rm);
switch(shift) {
case 0: // Logical shift left
if ((shiftamount >= 0) && (shiftamount <= 31)) {
if (shiftamount == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = flags.C;
} else {
dpi_shiftop.entire = RM2.entire << shiftamount;
dpi_shiftopcarry = getBit(RM2.entire, 32 - shiftamount);
}
}
break;
case 1: // Logical shift right
if ((shiftamount >= 0) && (shiftamount <= 31)) {
if (shiftamount == 0) {
dpi_shiftop.entire = 0;
dpi_shiftopcarry = getBit(RM2.entire, 31);
} else {
dpi_shiftop.entire = ((uint32_t) RM2.entire) >> shiftamount;
dpi_shiftopcarry = getBit(RM2.entire, shiftamount - 1);
}
}
break;
case 2: // Arithmetic shift right
if ((shiftamount >= 0) && (shiftamount <= 31)) {
if (shiftamount == 0) {
if (!isBitSet(RM2.entire, 31)) {
dpi_shiftop.entire = 0;
dpi_shiftopcarry = getBit(RM2.entire, 31);
} else {
dpi_shiftop.entire = 0xFFFFFFFF;
dpi_shiftopcarry = getBit(RM2.entire, 31);
}
} else {
dpi_shiftop.entire = ((int32_t) RM2.entire) >> shiftamount;
dpi_shiftopcarry = getBit(RM2.entire, shiftamount - 1);
}
}
break;
default: // Rotate right
if ((shiftamount >= 0) && (shiftamount <= 31)) {
if (shiftamount == 0) { //Rotate right with extend
dpi_shiftopcarry = getBit(RM2.entire, 0);
dpi_shiftop.entire = (((uint32_t)RM2.entire) >> 1);
if (flags.C) setBit(dpi_shiftop.entire, 31);
} else {
dpi_shiftop.entire = (RotateRight(shiftamount, RM2)).entire;
dpi_shiftopcarry = getBit(RM2.entire, shiftamount - 1);
}
}
}
}
//!DPI2 - Second operand is shifted (shift amount given by third register operand)
void ac_behavior( Type_DPI2 ) {
int rs40;
arm_isa::reg_t RS2, RM2;
// Special case: r* = 15
if ((rd == 15)||(rm == 15)||(rn == 15)||(rs == 15)) {
printf("Register 15 cannot be used in this instruction.\n");
ac_annul();
}
RM2.entire = RB_read(rm);
RS2.entire = RB_read(rs);
rs40 = ((uint32_t)RS2.entire) & 0x0000000F;
switch(shift){
case 0: // Logical shift left
if (RS2.byte[0] == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = flags.C;
}
else if (((uint8_t)RS2.byte[0]) < 32) {
dpi_shiftop.entire = RM2.entire << (uint8_t)RS2.byte[0];
dpi_shiftopcarry = getBit(RM2.entire, 32 - ((uint8_t)RS2.byte[0]));
}
else if (RS2.byte[0] == 32) {
dpi_shiftop.entire = 0;
dpi_shiftopcarry = getBit(RM2.entire, 0);
}
else { // rs > 32
dpi_shiftop.entire = 0;
dpi_shiftopcarry = 0;
}
break;
case 1: // Logical shift right
if (RS2.byte[0] == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = flags.C;
}
else if (((uint8_t)RS2.byte[0]) < 32) {
dpi_shiftop.entire = ((uint32_t) RM2.entire) >> ((uint8_t)RS2.byte[0]);
dpi_shiftopcarry = getBit(RM2.entire, (uint8_t)RS2.byte[0] - 1);
}
else if (RS2.byte[0] == 32) {
dpi_shiftop.entire = 0;
dpi_shiftopcarry = getBit(RM2.entire, 31);
}
else { // rs > 32
dpi_shiftop.entire = 0;
dpi_shiftopcarry = 0;
}
break;
case 2: // Arithmetical shift right
if (RS2.byte[0] == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = flags.C;
}
else if (((uint8_t)RS2.byte[0]) < 32) {
dpi_shiftop.entire = ((int32_t) RM2.entire) >> ((uint8_t)RS2.byte[0]);
dpi_shiftopcarry = getBit(RM2.entire, ((uint8_t)RS2.byte[0]) - 1);
} else { // rs >= 32
if (!isBitSet(RM2.entire, 31)) {
dpi_shiftop.entire = 0;
dpi_shiftopcarry = getBit(RM2.entire, 31);
}
else { // rm_31 == 1
dpi_shiftop.entire = 0xFFFFFFFF;
dpi_shiftopcarry = getBit(RM2.entire, 31);
}
}
break;
default: // Rotate right
if (RS2.byte[0] == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = flags.C;
}
else if (rs40 == 0) {
dpi_shiftop.entire = RM2.entire;
dpi_shiftopcarry = getBit(RM2.entire, 31);
}
else { // rs40 > 0
dpi_shiftop.entire = (RotateRight(rs40, RM2)).entire;
dpi_shiftopcarry = getBit(RM2.entire, rs40 - 1);
}
}
}
//!DPI3 - Second operand is immediate shifted by another imm
void ac_behavior( Type_DPI3 ){
int32_t tmp;
tmp = (uint32_t)imm8;
dpi_shiftop.entire = (((uint32_t)tmp) >> (2 * rotate)) | (((uint32_t)tmp) << (32 - (2 * rotate)));
if (rotate == 0)
dpi_shiftopcarry = flags.C;
else
dpi_shiftopcarry = getBit(dpi_shiftop.entire, 31);
}
void ac_behavior( Type_BBL ) {
// no special actions necessary
}
void ac_behavior( Type_BBLT ) {
// no special actions necessary
}
void ac_behavior( Type_MBXBLX ) {
// no special actions necessary
}
//!MULT1 - 32-bit result multiplication
void ac_behavior( Type_MULT1 ) {
// no special actions necessary
}
//!MULT2 - 64-bit result multiplication
void ac_behavior( Type_MULT2 ) {
// no special actions necessary
}
//!LSI - Load Store Immediate Offset/Index
void ac_behavior( Type_LSI ) {
arm_isa::reg_t RN2;
RN2.entire = RB_read(rn);
ls_address.entire = 0;
if((p == 1)&&(w == 0)) { // immediate pre-indexed without writeback
// Special case: Rn = PC
if (rn == PC)
ls_address.entire = 4;
if(u == 1) {
ls_address.entire += RN2.entire + (uint32_t) imm12;
} else {
ls_address.entire += RN2.entire - (uint32_t) imm12;
}
}
else if((p == 1)&&(w == 1)) { // immediate pre-indexed with writeback
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSI instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case: Rn = Rd
if (rn == rd) {
printf("Unpredictable LSI instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
if(u == 1) {
ls_address.entire = RN2.entire + (uint32_t) imm12;
} else {
ls_address.entire = RN2.entire - (uint32_t) imm12;
}
RB_write(rn,ls_address.entire);
}
else if((p == 0)&&(w == 0)) { // immediate post-indexed (writeback)
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSI instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSI instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
ls_address.entire = RN2.entire;
if(u == 1) {
//checar se imm12 soma direto
RB_write(rn, ls_address.entire + (uint32_t) imm12);
} else {
RB_write(rn, ls_address.entire - (uint32_t) imm12);
}
}
/* FIXME: Check word alignment (Rd = PC) Address[1:0] = 0b00 */
}
//!LSR - Scaled Register Offset/Index
void ac_behavior( Type_LSR ) {
arm_isa::reg_t RM2, RN2, index, tmp;
RM2.entire = RB_read(rm);
RN2.entire = RB_read(rn);
ls_address.entire = 0;
if ((p == 1)&&(w == 0)) { // offset
// Special case: PC
if(rn == PC)
ls_address.entire = 4;
if(rm == PC) {
printf("Unpredictable LSR instruction result (Illegal usage of PC, Rm = PC)\n");
return;
}
switch(shift){
case 0:
if(shiftamount == 0) { // Register
index.entire = RM2.entire;
} else { // Scalled logical shift left
index.entire = RM2.entire << shiftamount;
}
break;
case 1: // logical shift right
if(shiftamount == 0) index.entire = 0;
else index.entire = ((uint32_t) RM2.entire) >> shiftamount;
break;
case 2: // arithmetic shift right
if(shiftamount == 0) {
if (isBitSet(RM2.entire, 31)) index.entire = 0xFFFFFFFF;
else index.entire = 0;
} else index.entire = ((int32_t) RM2.entire) >> shiftamount;
break;
default:
if(shiftamount == 0) { // RRX
tmp.entire = 0;
if(flags.C) setBit(tmp.entire, 31);
index.entire = tmp.entire | (((uint32_t) RM2.entire) >> 1);
} else { // rotate right
index.entire = (RotateRight(shiftamount, RM2)).entire;
}
}
if(u == 1) {
ls_address.entire += (RN2.entire + index.entire);
} else {
ls_address.entire += (RN2.entire - index.entire);
}
}
else if((p == 1)&&(w == 1)) { // pre-indexed
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSR instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSR instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
// Special case Rm = PC
if (rm == PC) {
printf("Unpredictable LSR instruction result (Illegal usage of PC, Rm = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rm
if (rn == rm) {
printf("Unpredictable LSR instruction result (Can't use the same register for Rn and Rm\n");
ac_annul();
return;
}
switch(shift){
case 0:
if(shiftamount == 0) { // Register
index.entire = RM2.entire;
} else { // Scaled logical shift left
index.entire = RM2.entire << shiftamount;
}
break;
case 1: // logical shift right
if(shiftamount == 0) index.entire = 0;
else index.entire = ((uint32_t) RM2.entire) >> shiftamount;
break;
case 2: // arithmetic shift right
if(shiftamount == 0) {
if (isBitSet(RM2.entire,31))
index.entire = 0xFFFFFFFF;
else
index.entire = 0;
} else index.entire = ((int32_t) RM2.entire) >> shiftamount;
break;
default:
if(shiftamount == 0) { // RRX
tmp.entire = 0;
if (flags.C) setBit(tmp.entire,31);
index.entire = tmp.entire | (((uint32_t) RM2.entire) >> 1);
} else { // rotate right
index.entire = (RotateRight(shiftamount, RM2)).entire;
}
}
if(u == 1) {
ls_address.entire = RN2.entire + index.entire;
} else {
ls_address.entire = RN2.entire - index.entire;
}
RB_write(rn, ls_address.entire);
}
else if((p == 0)&&(w == 0)) { // post-indexed
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSR instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSR instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
// Special case Rm = PC
if (rm == PC) {
printf("Unpredictable LSR instruction result (Illegal usage of PC, Rm = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rm
if (rn == rm) {
printf("Unpredictable LSR instruction result (Can't use the same register for Rn and Rm\n");
ac_annul();
return;
}
ls_address.entire = RN2.entire;
switch(shift) {
case 0:
if(shiftamount == 0) { // Register
index.entire = RM2.entire;
} else { // Scaled logical shift left
index.entire = RM2.entire << shiftamount;
}
break;
case 1: // logical shift right
if(shiftamount == 0) index.entire = 0;
else index.entire = ((uint32_t) RM2.entire) >> shiftamount;
break;
case 2: // arithmetic shift right
if(shiftamount == 0) {
if (isBitSet(RM2.entire, 31))
index.entire = 0xFFFFFFFF;
else
index.entire = 0;
} else index.entire = ((int32_t) RM2.entire) >> shiftamount;
break;
default:
if(shiftamount == 0) { // RRX
tmp.entire = 0;
if(flags.C) setBit(tmp.entire, 31);
index.entire = tmp.entire | (((uint32_t) RM2.entire) >> 1);
} else { // rotate right
index.entire = (RotateRight(shiftamount, RM2)).entire;
}
}
if(u == 1) {
RB_write(rn, RN2.entire + index.entire);
} else {
RB_write(rn, RN2.entire - index.entire);
}
}
}
//!LSE - Load Store HalfWord
void ac_behavior( Type_LSE ){
int32_t off8;
arm_isa::reg_t RM2, RN2;
// Special cases handling
if((p == 0)&&(w == 1)) {
printf("Unpredictable LSE instruction result");
ac_annul();
return;
}
if((ss == 0)&&(hh == 0)) {
printf("Decoding error: this is not a LSE instruction");
ac_annul();
return;
}
if((ss == 1)&&(l == 0))
dprintf("Special DSP\n");
// FIXME: Test LDRD and STRD second registers in case of writeback
RN2.entire = RB_read(rn);
// nos LSE's que usam registrador, o campo addr2 armazena Rm
RM2.entire = RB_read(addr2);
off8 = ((uint32_t)(addr1 << 4) | addr2);
ls_address.entire = 0;
if(p == 1) { // offset ou pre-indexed
if((i == 1)&&(w == 0)) { // immediate offset
if(rn == PC)
ls_address.entire = 4;
if(u == 1) {
ls_address.entire += (RN2.entire + off8);
} else {
ls_address.entire += (RN2.entire - off8);
}
}
else if((i == 0)&&(w == 0)) { // register offset
// Special case Rm = PC
if (addr2 == PC) {
printf("Unpredictable LSE instruction result (Illegal usage of PC, Rm = PC)\n");
ac_annul();
return;
}
if(rn == PC)
ls_address.entire = 4;
if(u == 1) {
ls_address.entire += (RN2.entire + RM2.entire);
} else {
ls_address.entire += (RN2.entire - RM2.entire);
}
}
else if ((i == 1)&&(w == 1)) { // immediate pre-indexed
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSE instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSE instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
if(u == 1) {
ls_address.entire = (RN2.entire + off8);
} else {
ls_address.entire = (RN2.entire - off8);
}
RB_write(rn, ls_address.entire);
}
else { // i == 0 && w == 1: register pre-indexed
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSE instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSE instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
// Special case Rm = PC
if (addr2 == PC) {
printf("Unpredictable LSE instruction result (Illegal usage of PC, Rm = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rm
if (rn == addr2) {
printf("Unpredictable LSE instruction result (Can't use the same register for Rn and Rm\n");
ac_annul();
return;
}
if(u == 1) {
ls_address.entire = (RN2.entire + RM2.entire);
} else {
ls_address.entire = (RN2.entire - RM2.entire);
}
RB_write(rn, ls_address.entire);
}
} else { // p == 0: post-indexed
if((i == 1)&&(w == 0)) { // immediate post-indexed
if(rn == PC) {
printf("Unpredictable LSE instruction result");
ac_annul();
return;
}
ls_address.entire = RN2.entire;
if(u == 1) {
RB_write(rn, RN2.entire + off8);
} else {
RB_write(rn, RN2.entire - off8);
}
}
else if((i == 0)&&(w == 0)) { // register post-indexed
// Special case: Rn = PC
if (rn == PC) {
printf("Unpredictable LSE instruction result (Can't writeback to PC, Rn = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rd
if (rn == rd) {
printf("Unpredictable LSE instruction result (Can't writeback to loaded register, Rn = Rd)\n");
ac_annul();
return;
}
// Special case Rm = PC
if (addr2 == PC) {
printf("Unpredictable LSE instruction result (Illegal usage of PC, Rm = PC)\n");
ac_annul();
return;
}
// Special case Rn = Rm
if (rn == addr2) {
printf("Unpredictable LSE instruction result (Can't use the same register for Rn and Rm\n");
ac_annul();
return;
}
ls_address.entire = RN2.entire;
if(u == 1) {
RB_write(rn, RN2.entire + RM2.entire);
} else {
RB_write(rn, RN2.entire - RM2.entire);
}
}
}
}
//!LSM - Load Store Multiple
void ac_behavior( Type_LSM ){
arm_isa::reg_t RN2;
int setbits;
// Put registers list in a variable capable of addressing individual bits
arm_isa::reg_t registerList;
registerList.entire = (uint32_t) rlist;
// Special case - empty list
if (registerList.entire == 0) {
printf("Unpredictable LSM instruction result (No register specified)\n");
ac_annul();
return;
}
RN2.entire = RB_read(rn);
setbits = LSM_CountSetBits(registerList);
// Special case Rn in Rlist
if((w == 1)&&(isBitSet(rlist,rn))) {
lsm_oldrn.entire = RB_read(rn);
}
if((p == 0)&&(u == 1)) { // increment after
lsm_startaddress.entire = RN2.entire;
lsm_endaddress.entire = RN2.entire + (setbits * 4) - 4;
if(w == 1) RN2.entire += (setbits * 4);
}
else if((p == 1)&&(u == 1)) { // increment before
lsm_startaddress.entire = RN2.entire + 4;
lsm_endaddress.entire = RN2.entire + (setbits * 4);
if(w == 1) RN2.entire += (setbits * 4);
}
else if((p == 0)&&(u == 0)) { // decrement after
lsm_startaddress.entire = RN2.entire - (setbits * 4) + 4;
lsm_endaddress.entire = RN2.entire;
if(w == 1) RN2.entire -= (setbits * 4);
}
else { // decrement before
lsm_startaddress.entire = RN2.entire - (setbits * 4);
lsm_endaddress.entire = RN2.entire - 4;
if(w == 1) RN2.entire -= (setbits * 4);
}
RB_write(rn,RN2.entire);
}
void ac_behavior( Type_CDP ){
// no special actions necessary
}
void ac_behavior( Type_CRT ){
// no special actions necessary
}
void ac_behavior( Type_CLS ){
// no special actions necessary
}
void ac_behavior( Type_MBKPT ){
// no special actions necessary
}
void ac_behavior( Type_MSWI ){
// no special actions necessary
}
void ac_behavior( Type_MCLZ ){
// no special actions necessary
}
void ac_behavior( Type_MMSR1 ){
// no special actions necessary
}
void ac_behavior( Type_MMSR2 ){
// no special actions necessary
}
void ac_behavior( Type_DSPSM ){
arm_isa::reg_t RM2, RS2;
RM2.entire = RB_read(rm);
RS2.entire = RB_read(rs);
// Special cases
if((drd == PC)||(drn == PC)||(rm == PC)||(rs == PC)) {
printf("Unpredictable SMLA<y><x> instruction result\n");
return;
}
if(xx == 0)
OP1.entire = SignExtend(RM2.entire, 16);
else
OP1.entire = SignExtend((RM2.entire >> 16), 16);
if(yy == 0)
OP2.entire = SignExtend(RS2.entire, 16);
else
OP2.entire = SignExtend((RS2.entire >> 16), 16);
}
//! Behavior Methods
//------------------------------------------------------
inline void ADC(arm_isa* ref, int rd, int rn, bool s,
ac_regbank<16, arm_parms::ac_word, arm_parms::ac_Dword>& RB,
ac_reg<unsigned>& ac_pc) {
arm_isa::reg_t RD2, RN2;
arm_isa::r64bit_t soma;
dprintf("Instruction: ADC\n");
RN2.entire = RB_read(rn);
if(rn == PC) RN2.entire += 4;
dprintf("Operands:\n A = 0x%lX\n B = 0x%lX\n Carry=%d\n", RN2.entire,ref->dpi_shiftop.entire,ref->flags.C);
soma.hilo = (uint64_t)(uint32_t)RN2.entire + (uint64_t)(uint32_t)ref->dpi_shiftop.entire;
if (ref->flags.C) soma.hilo++;
RD2.entire = soma.reg[0];
RB_write(rd, RD2.entire);
if ((s == 1)&&(rd == PC)) {
#ifndef FORGIVE_UNPREDICTABLE
printf("Unpredictable ADC instruction result\n");
return;
#endif
} else {
if (s == 1) {
ref->flags.N = getBit(RD2.entire,31);
ref->flags.Z = ((RD2.entire == 0) ? true : false);
ref->flags.C = ((soma.reg[1] != 0) ? true : false);
ref->flags.V = (((getBit(RN2.entire,31) && getBit(ref->dpi_shiftop.entire,31) && (!getBit(RD2.entire,31))) ||
((!getBit(RN2.entire,31)) && (!getBit(ref->dpi_shiftop.entire,31)) && getBit(RD2.entire,31))) ? true : false);
}
}
dprintf(" * R%d <= 0x%08X (%d)\n", rd, RD2.entire, RD2.entire);
dprintf(" * Flags <= N=0x%X, Z=0x%X, C=0x%X, V=0x%X\n", ref->flags.N,ref->flags.Z,ref->flags.C,ref->flags.V);
ac_pc = RB_read(PC);
}
//------------------------------------------------------
inline void ADD(arm_isa* ref, int rd, int rn, bool s,
ac_regbank<16, arm_parms::ac_word, arm_parms::ac_Dword>& RB,
ac_reg<unsigned>& ac_pc) {
arm_isa::reg_t RD2, RN2;
arm_isa::r64bit_t soma;
dprintf("Instruction: ADD\n");
RN2.entire = RB_read(rn);
if(rn == PC) RN2.entire += 4;
dprintf("Operands:\n A = 0x%lX\n B = 0x%lX\n", RN2.entire,ref->dpi_shiftop.entire);
soma.hilo = (uint64_t)(uint32_t)RN2.entire + (uint64_t)(uint32_t)ref->dpi_shiftop.entire;
RD2.entire = soma.reg[0];
RB_write(rd, RD2.entire);
if ((s == 1)&&(rd == PC)) {
#ifndef FORGIVE_UNPREDICTABLE
if (ref->arm_proc_mode.mode == arm_isa::processor_mode::USER_MODE ||
ref->arm_proc_mode.mode == arm_isa::processor_mode::SYSTEM_MODE) {
printf("Unpredictable ADD instruction result\n");
return;
}
ref->SPSRtoCPSR();
#endif
} else {
if (s == 1) {
ref->flags.N = getBit(RD2.entire,31);
ref->flags.Z = ((RD2.entire == 0) ? true : false);
ref->flags.C = ((soma.reg[1] != 0) ? true : false);
ref->flags.V = (((getBit(RN2.entire,31) && getBit(ref->dpi_shiftop.entire,31) && (!getBit(RD2.entire,31))) ||
((!getBit(RN2.entire,31)) && (!getBit(ref->dpi_shiftop.entire,31)) && getBit(RD2.entire,31))) ? true : false);
}
}
dprintf(" * R%d <= 0x%08X (%d)\n", rd, RD2.entire, RD2.entire);
dprintf(" * Flags <= N=0x%X, Z=0x%X, C=0x%X, V=0x%X\n", ref->flags.N,ref->flags.Z,ref->flags.C,ref->flags.V);
ac_pc = RB_read(PC);
}
//------------------------------------------------------
inline void AND(arm_isa* ref, int rd, int rn, bool s,
ac_regbank<16, arm_parms::ac_word, arm_parms::ac_Dword>& RB,
ac_reg<unsigned>& ac_pc) {
arm_isa::reg_t RD2, RN2;
dprintf("Instruction: AND\n");
RN2.entire = RB_read(rn);
dprintf("Operands:\n A = 0x%lX\n B = 0x%lX\n", RN2.entire,ref->dpi_shiftop.entire);
RD2.entire = RN2.entire & ref->dpi_shiftop.entire;
RB_write(rd, RD2.entire);
if ((s == 1)&&(rd == PC)) {
#ifndef FORGIVE_UNPREDICTABLE
if (ref->arm_proc_mode.mode == arm_isa::processor_mode::USER_MODE ||
ref->arm_proc_mode.mode == arm_isa::processor_mode::SYSTEM_MODE) {
printf("Unpredictable AND instruction result\n");
return;
}
ref->SPSRtoCPSR();
#endif
} else {
if (s == 1) {
ref->flags.N = getBit(RD2.entire, 31);
ref->flags.Z = ((RD2.entire == 0) ? true : false);
ref->flags.C = ref->dpi_shiftopcarry;
// nothing happens with ref->flags.V
}
}
dprintf(" * R%d <= 0x%08X (%d)\n", rd, RD2.entire, RD2.entire);
dprintf(" * Flags <= N=0x%X, Z=0x%X, C=0x%X, V=0x%X\n", ref->flags.N,ref->flags.Z,ref->flags.C,ref->flags.V);
ac_pc = RB_read(PC);
}
//------------------------------------------------------
inline void B(arm_isa* ref, int h, int offset,
ac_regbank<16, arm_parms::ac_word, arm_parms::ac_Dword>& RB,
ac_reg<unsigned>& ac_pc) {
uint32_t mem_pos, s_extend;
// Note that PC is already incremented by 4, i.e., pointing to the next instruction
if(h == 1)
{ // h? it is really "l"
dprintf("Instruction: BL\n");
RB_write(LR, RB_read(PC));
dprintf("Branch return address: 0x%lX\n", RB_read(LR));
} else {
dprintf("Instruction: B\n");
}
s_extend = arm_isa::SignExtend((int32_t)(offset << 2), 26);
mem_pos = (uint32_t)RB_read(PC) + 4 + s_extend;
dprintf("Calculated branch destination: 0x%X\n", mem_pos);
if((mem_pos < 0)) {
fprintf(stderr, "Branch destination out of bounds\n");
exit(EXIT_FAILURE);
return;
} else RB_write(PC, mem_pos);
//fprintf(stderr, "0x%X\n", (unsigned int)mem_pos);
ac_pc = RB_read(PC);