-
Notifications
You must be signed in to change notification settings - Fork 20
/
thumbulator.c
2194 lines (2025 loc) · 52.5 KB
/
thumbulator.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int read32 ( unsigned int );
unsigned int read_register ( unsigned int );
#define DBUGFETCH 0
#define DBUGRAM 0
#define DBUGRAMW 0
#define DBUGREG 0
#define DBUG 0
#define DISS 1
#define ROMADDMASK 0xFFFFF
#define RAMADDMASK 0xFFFFF
#define ROMSIZE (ROMADDMASK+1)
#define RAMSIZE (RAMADDMASK+1)
unsigned short rom[ROMSIZE>>1];
unsigned short ram[RAMSIZE>>1];
#define CPSR_N (1<<31)
#define CPSR_Z (1<<30)
#define CPSR_C (1<<29)
#define CPSR_V (1<<28)
#define CPSR_Q (1<<27)
unsigned int vcdcount;
unsigned int output_vcd;
FILE *fpvcd;
unsigned int systick_ctrl;
unsigned int systick_reload;
unsigned int systick_count;
unsigned int systick_calibrate;
unsigned int halfadd;
unsigned int cpsr;
unsigned int handler_mode;
unsigned int reg_norm[16]; //normal execution mode, do not have a thread mode
unsigned long instructions;
unsigned long fetches;
unsigned long reads;
unsigned long writes;
unsigned long systick_ints;
//-------------------------------------------------------------------
void dump_counters ( void )
{
printf("\n\n");
printf("instructions %lu\n",instructions);
printf("fetches %lu\n",fetches);
printf("reads %lu\n",reads);
printf("writes %lu\n",writes);
printf("memcycles %lu\n",fetches+reads+writes);
printf("systick_ints %lu\n",systick_ints);
}
//-------------------------------------------------------------------
unsigned int fetch16 ( unsigned int addr )
{
unsigned int data;
fetches++;
if(DBUGFETCH) fprintf(stderr,"fetch16(0x%08X)=",addr);
if(DBUG) fprintf(stderr,"fetch16(0x%08X)=",addr);
switch(addr&0xF0000000)
{
case 0x00000000: //ROM
addr&=ROMADDMASK;
//if(addr<0x50)
//{
// fprintf(stderr,"fetch16(0x%08X), abort\n",addr);
// exit(1);
//}
addr>>=1;
data=rom[addr];
if(DBUGFETCH) fprintf(stderr,"0x%04X\n",data);
if(DBUG) fprintf(stderr,"0x%04X\n",data);
return(data);
case 0x40000000: //RAM
addr&=RAMADDMASK;
addr>>=1;
data=ram[addr];
if(DBUGFETCH) fprintf(stderr,"0x%04X\n",data);
if(DBUG) fprintf(stderr,"0x%04X\n",data);
return(data);
}
fprintf(stderr,"fetch16(0x%08X), abort pc = 0x%04X\n",addr,read_register(15));
exit(1);
}
//-------------------------------------------------------------------
unsigned int fetch32 ( unsigned int addr )
{
unsigned int data;
if(DBUGFETCH) fprintf(stderr,"fetch32(0x%08X)=",addr);
if(DBUG) fprintf(stderr,"fetch32(0x%08X)=",addr);
switch(addr&0xF0000000)
{
case 0x00000000: //ROM
if(addr<0x50)
{
data=read32(addr);
if(DBUGFETCH) fprintf(stderr,"0x%08X\n",data);
if(DBUG) fprintf(stderr,"0x%08X\n",data);
if(addr==0x00000000) return(data);
if(addr==0x00000004) return(data);
if(addr==0x0000003C) return(data);
fprintf(stderr,"fetch32(0x%08X), abort pc = 0x%04X\n",addr,read_register(15));
exit(1);
}
case 0x40000000: //RAM
//data=fetch16(addr+0);
//data|=((unsigned int)fetch16(addr+2))<<16;
data=read32(addr);
if(DBUGFETCH) fprintf(stderr,"0x%08X\n",data);
if(DBUG) fprintf(stderr,"0x%08X\n",data);
return(data);
}
fprintf(stderr,"fetch32(0x%08X), abort pc 0x%04X\n",addr,read_register(15));
exit(1);
}
//-------------------------------------------------------------------
void write16 ( unsigned int addr, unsigned int data )
{
writes++;
if(DBUG) fprintf(stderr,"write16(0x%08X,0x%04X)\n",addr,data);
switch(addr&0xF0000000)
{
case 0x40000000: //RAM
if(DBUGRAM) fprintf(stderr,"write16(0x%08X,0x%04X)\n",addr,data);
addr&=RAMADDMASK;
addr>>=1;
ram[addr]=data&0xFFFF;
return;
}
fprintf(stderr,"write16(0x%08X,0x%04X), abort pc 0x%04X\n",addr,data,read_register(15));
exit(1);
}
//-------------------------------------------------------------------
void write32 ( unsigned int addr, unsigned int data )
{
if(DBUG) fprintf(stderr,"write32(0x%08X,0x%08X)\n",addr,data);
switch(addr&0xF0000000)
{
case 0xF0000000: //halt
dump_counters();
exit(0);
case 0xE0000000: //periph
switch(addr)
{
case 0xE0000000:
if(DISS) printf("uart: [");
printf("%c",data&0xFF);
if(DISS) printf("]\n");
fflush(stdout);
break;
case 0xE000E010:
{
unsigned int old;
old=systick_ctrl;
systick_ctrl = data&0x00010007;
if(((old&1)==0)&&(systick_ctrl&1))
{
//timer started, load count
systick_count=systick_reload;
}
break;
}
case 0xE000E014:
{
systick_reload=data&0x00FFFFFF;
break;
}
case 0xE000E018:
{
systick_count=data&0x00FFFFFF;
break;
}
case 0xE000E01C:
{
systick_calibrate=data&0x00FFFFFF;
break;
}
}
return;
case 0xD0000000: //debug
switch(addr&0xFF)
{
case 0x00:
{
fprintf(stderr,"[0x%08X][0x%08X] 0x%08X\n",read_register(14),addr,data);
return;
}
case 0x10:
{
printf("0x%08X ",data);
return;
}
case 0x20:
{
printf("0x%08X\n",data);
return;
}
}
case 0x40000000: //RAM
if(DBUGRAMW) fprintf(stderr,"write32(0x%08X,0x%08X)\n",addr,data);
write16(addr+0,(data>> 0)&0xFFFF);
write16(addr+2,(data>>16)&0xFFFF);
return;
}
fprintf(stderr,"write32(0x%08X,0x%08X), abort pc 0x%04X\n",addr,data,read_register(15));
exit(1);
}
//-----------------------------------------------------------------
unsigned int read16 ( unsigned int addr )
{
unsigned int data;
reads++;
if(DBUG) fprintf(stderr,"read16(0x%08X)=",addr);
switch(addr&0xF0000000)
{
case 0x00000000: //ROM
addr&=ROMADDMASK;
addr>>=1;
data=rom[addr];
if(DBUG) fprintf(stderr,"0x%04X\n",data);
return(data);
case 0x40000000: //RAM
if(DBUGRAM) fprintf(stderr,"read16(0x%08X)=",addr);
addr&=RAMADDMASK;
addr>>=1;
data=ram[addr];
if(DBUG) fprintf(stderr,"0x%04X\n",data);
if(DBUGRAM) fprintf(stderr,"0x%04X\n",data);
return(data);
}
fprintf(stderr,"read16(0x%08X), abort pc 0x%04X\n",addr,read_register(15));
exit(1);
}
//-------------------------------------------------------------------
unsigned int read32 ( unsigned int addr )
{
unsigned int data;
if(DBUG) fprintf(stderr,"read32(0x%08X)=",addr);
switch(addr&0xF0000000)
{
case 0x00000000: //ROM
case 0x40000000: //RAM
if(DBUGRAMW) fprintf(stderr,"read32(0x%08X)=",addr);
data =read16(addr+0);
data|=((unsigned int)read16(addr+2))<<16;
if(DBUG) fprintf(stderr,"0x%08X\n",data);
if(DBUGRAMW) fprintf(stderr,"0x%08X\n",data);
return(data);
case 0xE0000000:
{
switch(addr)
{
case 0xE000E010:
{
data = systick_ctrl;
systick_ctrl&=(~0x00010000);
return(data);
}
case 0xE000E014:
{
data=systick_reload;
return(data);
}
case 0xE000E018:
{
data=systick_count;
return(data);
}
case 0xE000E01C:
{
data=systick_calibrate;
return(data);
}
}
}
}
fprintf(stderr,"read32(0x%08X), abort pc 0x%04X\n",addr,read_register(15));
exit(1);
}
//-------------------------------------------------------------------
unsigned int read_register ( unsigned int reg )
{
unsigned int data;
reg&=0xF;
if(DBUG) fprintf(stderr,"read_register(%u)=",reg);
if(DBUGREG) fprintf(stderr,"read_register(%u)=",reg);
data=reg_norm[reg];
if(reg==15)
{
if(data&1)
{
fprintf(stderr,"pc has lsbit set 0x%08X\n",data);
}
data&=~1;
}
if(DBUG) fprintf(stderr,"0x%08X\n",data);
if(DBUGREG) fprintf(stderr,"0x%08X\n",data);
return(data);
}
//-------------------------------------------------------------------
void write_register ( unsigned int reg, unsigned int data )
{
reg&=0xF;
if(DBUG) fprintf(stderr,"write_register(%u,0x%08X)\n",reg,data);
if(DBUGREG) fprintf(stderr,"write_register(%u,0x%08X)\n",reg,data);
if(reg==15) data&=~1;
reg_norm[reg]=data;
if(output_vcd)
{
unsigned int vv;
fprintf(fpvcd,"b");
for(vv=0x80000000;vv;vv>>=1)
{
if(vv&data) fprintf(fpvcd,"1"); else fprintf(fpvcd,"0");
}
fprintf(fpvcd," r%u\n",reg);
}
}
//-------------------------------------------------------------------
void do_zflag ( unsigned int x )
{
if(x==0) cpsr|=CPSR_Z; else cpsr&=~CPSR_Z;
}
//-------------------------------------------------------------------
void do_nflag ( unsigned int x )
{
if(x&0x80000000) cpsr|=CPSR_N; else cpsr&=~CPSR_N;
}
//-------------------------------------------------------------------
void do_cflag ( unsigned int a, unsigned int b, unsigned int c )
{
unsigned int rc;
cpsr&=~CPSR_C;
rc=(a&0x7FFFFFFF)+(b&0x7FFFFFFF)+c; //carry in
rc = (rc>>31)+(a>>31)+(b>>31); //carry out
if(rc&2) cpsr|=CPSR_C;
}
//-------------------------------------------------------------------
void do_vflag ( unsigned int a, unsigned int b, unsigned int c )
{
unsigned int rc;
unsigned int rd;
cpsr&=~CPSR_V;
rc=(a&0x7FFFFFFF)+(b&0x7FFFFFFF)+c; //carry in
rc>>=31; //carry in in lsbit
rd=(rc&1)+((a>>31)&1)+((b>>31)&1); //carry out
rd>>=1; //carry out in lsbit
rc=(rc^rd)&1; //if carry in != carry out then signed overflow
if(rc) cpsr|=CPSR_V;
}
//-------------------------------------------------------------------
void do_cflag_bit ( unsigned int x )
{
if(x) cpsr|=CPSR_C; else cpsr&=~CPSR_C;
}
//-------------------------------------------------------------------
void do_vflag_bit ( unsigned int x )
{
if(x) cpsr|=CPSR_V; else cpsr&=~CPSR_V;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
int execute ( void )
{
unsigned int pc;
unsigned int sp;
unsigned int inst;
unsigned int ra,rb,rc;
unsigned int rm,rd,rn,rs;
unsigned int op;
//if(fetches>400000) return(1);
pc=read_register(15);
if(handler_mode)
{
if((pc&0xF0000000)==0xF0000000)
{
unsigned int sp;
handler_mode = 0;
//fprintf(stderr,"--leaving handler\n");
sp=read_register(13);
write_register(0,read32(sp)); sp+=4;
write_register(1,read32(sp)); sp+=4;
write_register(2,read32(sp)); sp+=4;
write_register(3,read32(sp)); sp+=4;
write_register(12,read32(sp)); sp+=4;
write_register(14,read32(sp)); sp+=4;
pc=read32(sp); sp+=4;
cpsr=read32(sp); sp+=4;
write_register(13,sp);
}
}
if(systick_ctrl&1)
{
if(systick_count)
{
systick_count--;
}
else
{
systick_count=systick_reload;
systick_ctrl|=0x00010000;
}
}
if((systick_ctrl&3)==3)
{
if(systick_ctrl&0x00010000)
{
if(handler_mode==0)
{
unsigned int sp;
systick_ints++;
//fprintf(stderr,"--- enter systick handler\n");
sp=read_register(13);
sp-=4; write32(sp,cpsr);
sp-=4; write32(sp,pc);
sp-=4; write32(sp,read_register(14));
sp-=4; write32(sp,read_register(12));
sp-=4; write32(sp,read_register(3));
sp-=4; write32(sp,read_register(2));
sp-=4; write32(sp,read_register(1));
sp-=4; write32(sp,read_register(0));
write_register(13,sp);
pc=fetch32(0x0000003C); //systick vector
pc+=2;
//write_register(14,0xFFFFFF00);
write_register(14,0xFFFFFFF9);
handler_mode=1;
}
}
}
inst=fetch16(pc-2);
pc+=2;
write_register(15,pc);
if(DISS) fprintf(stderr,"--- 0x%08X: 0x%04X ",(pc-4),inst);
if(output_vcd)
{
unsigned int vv;
fprintf(fpvcd,"b");
for(vv=0x8000;vv;vv>>=1)
{
if(vv&inst) fprintf(fpvcd,"1"); else fprintf(fpvcd,"0");
}
fprintf(fpvcd," inst\n");
}
instructions++;
//ADC
if((inst&0xFFC0)==0x4140)
{
rd=(inst>>0)&0x07;
rm=(inst>>3)&0x07;
if(DISS) fprintf(stderr,"adc r%u,r%u\n",rd,rm);
ra=read_register(rd);
rb=read_register(rm);
rc=ra+rb;
if(cpsr&CPSR_C) rc++;
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
if(cpsr&CPSR_C) { do_cflag(ra,rb,1); do_vflag(ra,rb,1); }
else { do_cflag(ra,rb,0); do_vflag(ra,rb,0); }
return(0);
}
//ADD(1) small immediate two registers
if((inst&0xFE00)==0x1C00)
{
rd=(inst>>0)&0x7;
rn=(inst>>3)&0x7;
rb=(inst>>6)&0x7;
if(rb)
{
if(DISS) fprintf(stderr,"adds r%u,r%u,#0x%X\n",rd,rn,rb);
ra=read_register(rn);
rc=ra+rb;
//fprintf(stderr,"0x%08X = 0x%08X + 0x%08X\n",rc,ra,rb);
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
do_cflag(ra,rb,0);
do_vflag(ra,rb,0);
return(0);
}
else
{
//this is a mov
}
}
//ADD(2) big immediate one register
if((inst&0xF800)==0x3000)
{
rb=(inst>>0)&0xFF;
rd=(inst>>8)&0x7;
if(DISS) fprintf(stderr,"adds r%u,#0x%02X\n",rd,rb);
ra=read_register(rd);
rc=ra+rb;
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
do_cflag(ra,rb,0);
do_vflag(ra,rb,0);
return(0);
}
//ADD(3) three registers
if((inst&0xFE00)==0x1800)
{
rd=(inst>>0)&0x7;
rn=(inst>>3)&0x7;
rm=(inst>>6)&0x7;
if(DISS) fprintf(stderr,"adds r%u,r%u,r%u\n",rd,rn,rm);
ra=read_register(rn);
rb=read_register(rm);
rc=ra+rb;
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
do_cflag(ra,rb,0);
do_vflag(ra,rb,0);
return(0);
}
//ADD(4) two registers one or both high no flags
if((inst&0xFF00)==0x4400)
{
if((inst>>6)&3)
{
//UNPREDICTABLE
}
rd=(inst>>0)&0x7;
rd|=(inst>>4)&0x8;
rm=(inst>>3)&0xF;
if(DISS) fprintf(stderr,"add r%u,r%u\n",rd,rm);
ra=read_register(rd);
rb=read_register(rm);
rc=ra+rb;
if(rd==15)
{
if((rc&1)==0)
{
fprintf(stderr,"add pc,... produced an arm address 0x%08X 0x%08X\n",pc,rc);
exit(1);
}
rc&=~1; //write_register may do this as well
rc+=2; //The program counter is special
}
//fprintf(stderr,"0x%08X = 0x%08X + 0x%08X\n",rc,ra,rb);
write_register(rd,rc);
return(0);
}
//ADD(5) rd = pc plus immediate
if((inst&0xF800)==0xA000)
{
rb=(inst>>0)&0xFF;
rd=(inst>>8)&0x7;
rb<<=2;
if(DISS) fprintf(stderr,"add r%u,PC,#0x%02X\n",rd,rb);
ra=read_register(15);
rc=(ra&(~3))+rb;
write_register(rd,rc);
return(0);
}
//ADD(6) rd = sp plus immediate
if((inst&0xF800)==0xA800)
{
rb=(inst>>0)&0xFF;
rd=(inst>>8)&0x7;
rb<<=2;
if(DISS) fprintf(stderr,"add r%u,SP,#0x%02X\n",rd,rb);
ra=read_register(13);
rc=ra+rb;
write_register(rd,rc);
return(0);
}
//ADD(7) sp plus immediate
if((inst&0xFF80)==0xB000)
{
rb=(inst>>0)&0x7F;
rb<<=2;
if(DISS) fprintf(stderr,"add SP,#0x%02X\n",rb);
ra=read_register(13);
rc=ra+rb;
write_register(13,rc);
return(0);
}
//AND
if((inst&0xFFC0)==0x4000)
{
rd=(inst>>0)&0x7;
rm=(inst>>3)&0x7;
if(DISS) fprintf(stderr,"ands r%u,r%u\n",rd,rm);
ra=read_register(rd);
rb=read_register(rm);
rc=ra&rb;
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
return(0);
}
//ASR(1) two register immediate
if((inst&0xF800)==0x1000)
{
rd=(inst>>0)&0x07;
rm=(inst>>3)&0x07;
rb=(inst>>6)&0x1F;
if(DISS) fprintf(stderr,"asrs r%u,r%u,#0x%X\n",rd,rm,rb);
rc=read_register(rm);
if(rb==0)
{
if(rc&0x80000000)
{
do_cflag_bit(1);
rc=~0;
}
else
{
do_cflag_bit(0);
rc=0;
}
}
else
{
do_cflag_bit(rc&(1<<(rb-1)));
ra=rc&0x80000000;
rc>>=rb;
if(ra) //asr, sign is shifted in
{
rc|=(~0)<<(32-rb);
}
}
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
return(0);
}
//ASR(2) two register
if((inst&0xFFC0)==0x4100)
{
rd=(inst>>0)&0x07;
rs=(inst>>3)&0x07;
if(DISS) fprintf(stderr,"asrs r%u,r%u\n",rd,rs);
rc=read_register(rd);
rb=read_register(rs);
rb&=0xFF;
if(rb==0)
{
}
else if(rb<32)
{
do_cflag_bit(rc&(1<<(rb-1)));
ra=rc&0x80000000;
rc>>=rb;
if(ra) //asr, sign is shifted in
{
rc|=(~0)<<(32-rb);
}
}
else
{
if(rc&0x80000000)
{
do_cflag_bit(1);
rc=(~0);
}
else
{
do_cflag_bit(0);
rc=0;
}
}
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
return(0);
}
//B(1) conditional branch
if((inst&0xF000)==0xD000)
{
rb=(inst>>0)&0xFF;
if(rb&0x80) rb|=(~0)<<8;
op=(inst>>8)&0xF;
rb<<=1;
rb+=pc;
rb+=2;
switch(op)
{
case 0x0: //b eq z set
if(DISS) fprintf(stderr,"beq 0x%08X\n",rb-3);
if(cpsr&CPSR_Z)
{
write_register(15,rb);
}
return(0);
case 0x1: //b ne z clear
if(DISS) fprintf(stderr,"bne 0x%08X\n",rb-3);
if(!(cpsr&CPSR_Z))
{
write_register(15,rb);
}
return(0);
case 0x2: //b cs c set
if(DISS) fprintf(stderr,"bcs 0x%08X\n",rb-3);
if(cpsr&CPSR_C)
{
write_register(15,rb);
}
return(0);
case 0x3: //b cc c clear
if(DISS) fprintf(stderr,"bcc 0x%08X\n",rb-3);
if(!(cpsr&CPSR_C))
{
write_register(15,rb);
}
return(0);
case 0x4: //b mi n set
if(DISS) fprintf(stderr,"bmi 0x%08X\n",rb-3);
if(cpsr&CPSR_N)
{
write_register(15,rb);
}
return(0);
case 0x5: //b pl n clear
if(DISS) fprintf(stderr,"bpl 0x%08X\n",rb-3);
if(!(cpsr&CPSR_N))
{
write_register(15,rb);
}
return(0);
case 0x6: //b vs v set
if(DISS) fprintf(stderr,"bvs 0x%08X\n",rb-3);
if(cpsr&CPSR_V)
{
write_register(15,rb);
}
return(0);
case 0x7: //b vc v clear
if(DISS) fprintf(stderr,"bvc 0x%08X\n",rb-3);
if(!(cpsr&CPSR_V))
{
write_register(15,rb);
}
return(0);
case 0x8: //b hi c set z clear
if(DISS) fprintf(stderr,"bhi 0x%08X\n",rb-3);
if((cpsr&CPSR_C)&&(!(cpsr&CPSR_Z)))
{
write_register(15,rb);
}
return(0);
case 0x9: //b ls c clear or z set
if(DISS) fprintf(stderr,"bls 0x%08X\n",rb-3);
if((cpsr&CPSR_Z)||(!(cpsr&CPSR_C)))
{
write_register(15,rb);
}
return(0);
case 0xA: //b ge N == V
if(DISS) fprintf(stderr,"bge 0x%08X\n",rb-3);
ra=0;
if( (cpsr&CPSR_N) && (cpsr&CPSR_V) ) ra++;
if((!(cpsr&CPSR_N))&&(!(cpsr&CPSR_V))) ra++;
if(ra)
{
write_register(15,rb);
}
return(0);
case 0xB: //b lt N != V
if(DISS) fprintf(stderr,"blt 0x%08X\n",rb-3);
ra=0;
if((!(cpsr&CPSR_N))&&(cpsr&CPSR_V)) ra++;
if((!(cpsr&CPSR_V))&&(cpsr&CPSR_N)) ra++;
if(ra)
{
write_register(15,rb);
}
return(0);
case 0xC: //b gt Z==0 and N == V
if(DISS) fprintf(stderr,"bgt 0x%08X\n",rb-3);
ra=0;
if( (cpsr&CPSR_N) && (cpsr&CPSR_V) ) ra++;
if((!(cpsr&CPSR_N))&&(!(cpsr&CPSR_V))) ra++;
if(cpsr&CPSR_Z) ra=0;
if(ra)
{
write_register(15,rb);
}
return(0);
case 0xD: //b le Z==1 or N != V
if(DISS) fprintf(stderr,"ble 0x%08X\n",rb-3);
ra=0;
if((!(cpsr&CPSR_N))&&(cpsr&CPSR_V)) ra++;
if((!(cpsr&CPSR_V))&&(cpsr&CPSR_N)) ra++;
if(cpsr&CPSR_Z) ra++;
if(ra)
{
write_register(15,rb);
}
return(0);
case 0xE:
//undefined instruction
break;
case 0xF:
//swi
break;
}
}
//B(2) unconditional branch
if((inst&0xF800)==0xE000)
{
rb=(inst>>0)&0x7FF;
if(rb&(1<<10)) rb|=(~0)<<11;
rb<<=1;
rb+=pc;
rb+=2;
if(DISS) fprintf(stderr,"B 0x%08X\n",rb-4);
write_register(15,rb);
return(0);
}
//BIC
if((inst&0xFFC0)==0x4380)
{
rd=(inst>>0)&0x7;
rm=(inst>>3)&0x7;
if(DISS) fprintf(stderr,"bics r%u,r%u\n",rd,rm);
ra=read_register(rd);
rb=read_register(rm);
rc=ra&(~rb);
write_register(rd,rc);
do_nflag(rc);
do_zflag(rc);
return(0);
}
//BKPT
if((inst&0xFF00)==0xBE00)
{
rb=(inst>>0)&0xFF;
fprintf(stderr,"bkpt 0x%02X\n",rb);
return(1);
}
//BL/BLX(1)
if((inst&0xE000)==0xE000) //BL,BLX
{
if((inst&0x1800)==0x1000) //H=b10
{
if(DISS) fprintf(stderr,"\n");
rb=inst&((1<<11)-1);
if(rb&1<<10) rb|=(~((1<<11)-1)); //sign extend
rb<<=12;
rb+=pc;
write_register(14,rb);
return(0);
}
else
if((inst&0x1800)==0x1800) //H=b11
{
//branch to thumb
rb=read_register(14);
rb+=(inst&((1<<11)-1))<<1;;
rb+=2;
if(DISS) fprintf(stderr,"bl 0x%08X\n",rb-2);
write_register(14,(pc-2)|1);
write_register(15,rb);
return(0);
}
else
if((inst&0x1800)==0x0800) //H=b01
{
//fprintf(stderr,"cannot branch to arm 0x%08X 0x%04X\n",pc,inst);
//return(1);
//branch to thumb
rb=read_register(14);
rb+=(inst&((1<<11)-1))<<1;;
rb&=0xFFFFFFFC;
rb+=2;
printf("hello\n");
if(DISS) fprintf(stderr,"bl 0x%08X\n",rb-3);
write_register(14,(pc-2)|1);
write_register(15,rb);
return(0);
}
}
//BLX(2)
if((inst&0xFF87)==0x4780)
{
rm=(inst>>3)&0xF;
if(DISS) fprintf(stderr,"blx r%u\n",rm);
rc=read_register(rm);
//fprintf(stderr,"blx r%u 0x%X 0x%X\n",rm,rc,pc);
rc+=2;
if(rc&1)
{
write_register(14,(pc-2)|1);
rc&=~1;
write_register(15,rc);
return(0);
}
else
{
fprintf(stderr,"cannot branch to arm 0x%08X 0x%04X\n",pc,inst);
return(1);
}
}
//BX
if((inst&0xFF87)==0x4700)
{
rm=(inst>>3)&0xF;
if(DISS) fprintf(stderr,"bx r%u\n",rm);
rc=read_register(rm);
rc+=2;
//fprintf(stderr,"bx r%u 0x%X 0x%X\n",rm,rc,pc);
if(rc&1)
{
rc&=~1;
write_register(15,rc);
return(0);
}
else