-
Notifications
You must be signed in to change notification settings - Fork 25
/
arm7dasm.c
1422 lines (1334 loc) · 46.3 KB
/
arm7dasm.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
/*****************************************************************************
*
* arm7dasm.c
* Portable ARM7TDMI Core Emulator - Disassembler
*
* Copyright Steve Ellenoff, all rights reserved.
*
* - This source code is released as freeware for non-commercial purposes.
* - You are free to use and redistribute this code in modified or
* unmodified form, provided you list me in the credits.
* - If you modify this source code, you must add a notice to each modified
* source file that it has been changed. If you're a nice person, you
* will clearly mark each change too. :)
* - If you wish to use this for commercial purposes, please contact me at
* - The author of this copywritten work reserves the right to change the
* terms of its usage and license at any time, including retroactively
* - This entire notice must remain in the source code.
*
* This work is based on:
* #1) 'Atmel Corporation ARM7TDMI (Thumb) Datasheet - January 1999'
* #2) Arm 2/3/6 emulator By Bryan McPhail ([email protected]) and Phil Stroffolino (MAME CORE 0.76)
*
*****************************************************************************/
/******************************************************************************
* Notes:
*
* Because Co-Processor functions are highly specialized to the actual co-proc
* implementation being used, I've setup callback handlers to allow for custom
* dasm display of the co-proc functions so that the implementation specific
* commands/interpretation can be used. If not used, the default handlers which
* implement the ARM7TDMI guideline format is used
******************************************************************************/
#include "emu.h"
#include "arm7core.h"
static char *get_reg_name(int n)
{
static char buf[16][4];
static int i;
n &= 15;
i &= 15;
switch (n)
{
case 13:
return "SP";
case 14:
return "LR";
case 15:
return "PC";
}
sprintf(buf[i], "R%d", n);
return buf[i++];
}
static char *WritePadding( char *pBuf, const char *pBuf0 )
{
pBuf0 += 8;
while( pBuf<pBuf0 )
{
*pBuf++ = ' ';
}
return pBuf;
}
static char *DasmCoProc_RT( char *pBuf, UINT32 opcode, const char *pConditionCode, const char *pBuf0)
{
/* co processor register transfer */
/* xxxx 1110 oooL nnnn dddd cccc ppp1 mmmm */
if( opcode&0x00100000 ) //Bit 20 = Load or Store
{
pBuf += sprintf( pBuf, "MRC" );
}
else
{
pBuf += sprintf( pBuf, "MCR" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf( pBuf, "p%d, %d, %s, c%d, c%d",
(opcode>>8)&0xf, (opcode>>21)&7, get_reg_name((opcode>>12)&0xf), (opcode>>16)&0xf, opcode&0xf );
if((opcode>>5)&7) pBuf += sprintf( pBuf, ", %d",(opcode>>5)&7);
return pBuf;
}
static char *DasmCoProc_DT( char *pBuf, UINT32 opcode, const char *pConditionCode, const char *pBuf0 )
{
/* co processor data transfer */
/* xxxx 111P UNWL nnnn dddd pppp oooooooo */
//todo: test this on valid instructions
pBuf += sprintf(pBuf, "%s%s",(opcode&0x00100000)?"LDC":"STC",pConditionCode); //Bit 20 = 1 for Load, 0 for Store
//Long Operation
if(opcode & 0x400000) pBuf += sprintf(pBuf, "L");
pBuf = WritePadding( pBuf, pBuf0 );
//P# & CD #
pBuf += sprintf(pBuf, "p%d, c%d, ",(opcode>>8)&0x0f,(opcode>>12)&0x0f);
//Base Register (Rn)
pBuf += sprintf(pBuf, "[%s%s",get_reg_name((opcode>>16)&0x0f),(opcode&0x1000000)?"":"]"); //If Bit 24 = 1, Pre-increment, otherwise, Post increment so close brace
//immediate value ( 8 bit value is << 2 according to manual )
if(opcode & 0xff) pBuf += sprintf(pBuf, ",%s#$%x",(opcode&0x800000)?"":"-",(opcode & 0xff)<<2);
//Pre-Inc brace & Write back
pBuf += sprintf(pBuf, "%s%s",(opcode&0x1000000)?"]":"",(opcode&0x200000)?"{!}":"");
return pBuf;
}
static char *DasmCoProc_DO( char *pBuf, UINT32 opcode, const char *pConditionCode, const char *pBuf0 )
{
/* co processor data operation */
/* xxxx 1110 oooo nnnn dddd cccc ppp0 mmmm */
pBuf += sprintf( pBuf, "CDP" );
pBuf += sprintf( pBuf, "%s", pConditionCode );
pBuf = WritePadding( pBuf, pBuf0 );
//p#,CPOpc,cd,cn,cm
pBuf += sprintf( pBuf, "p%d, %d, c%d, c%d, c%d",
(opcode>>8)&0xf, (opcode>>20)&0xf, (opcode>>12)&0xf, (opcode>>16)&0xf, opcode&0xf );
if((opcode>>5)&7) pBuf += sprintf( pBuf, ", %d",(opcode>>5)&7);
return pBuf;
}
static char *WriteImmediateOperand( char *pBuf, UINT32 opcode )
{
/* rrrrbbbbbbbb */
UINT32 imm;
int r;
imm = opcode&0xff;
r = ((opcode>>8)&0xf)*2;
imm = (imm>>r)|(imm<<(32-r));
pBuf += sprintf( pBuf, ", #$%x", imm );
return pBuf;
}
static char *WriteDataProcessingOperand( char *pBuf, UINT32 opcode, int printOp0, int printOp1, int printOp2 )
{
/* ccccctttmmmm */
static const char *const pRegOp[4] = { "LSL","LSR","ASR","ROR" };
if (printOp0)
pBuf += sprintf(pBuf,"%s, ", get_reg_name((opcode>>12)&0xf));
if (printOp1)
pBuf += sprintf(pBuf,"%s, ", get_reg_name((opcode>>16)&0xf));
/* Immediate Op2 */
if( opcode&0x02000000 )
return WriteImmediateOperand(pBuf-2,opcode);
/* Register Op2 */
if (printOp2)
//SJE: pBuf += sprintf(pBuf,"%s, ", get_reg_name((opcode>>0)&0xf));
pBuf += sprintf(pBuf,"%s ", get_reg_name((opcode>>0)&0xf));
/* Immediate Op2 */
//SJE: ignore if LSL#0 for register shift
if( ((opcode&0x2000000) == 0) && (((opcode>>4) & 0xff)==0) )
return pBuf;
pBuf += sprintf(pBuf, ",%s ", pRegOp[(opcode>>5)&3] );
//SJE: pBuf += sprintf(pBuf, "%s ", pRegOp[(opcode>>5)&3] );
if( opcode&0x10 ) /* Shift amount specified in bottom bits of RS */
{
pBuf += sprintf( pBuf, "%s", get_reg_name((opcode>>8)&0xf));
}
else /* Shift amount immediate 5 bit unsigned integer */
{
int c=(opcode>>7)&0x1f;
if( c==0 ) c = 32;
pBuf += sprintf( pBuf, "#%d", c );
}
return pBuf;
}
static char *WriteRegisterOperand1( char *pBuf, UINT32 opcode )
{
/* ccccctttmmmm */
static const char *const pRegOp[4] = { "LSL","LSR","ASR","ROR" };
pBuf += sprintf(
pBuf,
", %s", /* Operand 1 register, Operand 2 register, shift type */
get_reg_name((opcode>> 0)&0xf));
//check for LSL 0
if( (((opcode>>5)&3)==0) && (((opcode>>7)&0xf)==0) )
return pBuf;
else
//Add rotation type
pBuf += sprintf(pBuf," %s ",pRegOp[(opcode>>5)&3]);
if( opcode&0x10 ) /* Shift amount specified in bottom bits of RS */
{
pBuf += sprintf( pBuf, "%s", get_reg_name((opcode>>7)&0xf));
}
else /* Shift amount immediate 5 bit unsigned integer */
{
int c=(opcode>>7)&0x1f;
if( c==0 ) c = 32;
pBuf += sprintf( pBuf, "#%d", c );
}
return pBuf;
} /* WriteRegisterOperand */
static char *WriteBranchAddress( char *pBuf, UINT32 pc, UINT32 opcode )
{
opcode &= 0x00ffffff;
if( opcode&0x00800000 )
{
opcode |= 0xff000000; /* sign-extend */
}
register_coderef(pc, pc + 8 + 4 * opcode);
pc += 8+4*opcode;
strcpy(pBuf, get_symbol_name(pc));
return pBuf;
} /* WriteBranchAddress */
static UINT32 arm7_disasm( char *pBuf, UINT32 pc, UINT32 opcode )
{
const char *pBuf0;
static const char *const pConditionCodeTable[16] =
{
"EQ","NE","CS","CC",
"MI","PL","VS","VC",
"HI","LS","GE","LT",
"GT","LE","","NV"
};
static const char *const pOperation[16] =
{
"AND","EOR","SUB","RSB",
"ADD","ADC","SBC","RSC",
"TST","TEQ","CMP","CMN",
"ORR","MOV","BIC","MVN"
};
const char *pConditionCode;
UINT32 dasmflags = 0;
pConditionCode= pConditionCodeTable[opcode>>28];
pBuf0 = pBuf;
if( (opcode&0x0fffffd0)==0x012fff10 ) { //bits 27-4 == 0001001011111111111100x1
/* Branch and Exchange (BX,BLX) */
if (opcode & 0x00000020) {
pBuf += sprintf( pBuf, "BL");
dasmflags = DASMFLAG_STEP_OUT;
}
else {
pBuf += sprintf( pBuf, "B");
}
pBuf += sprintf( pBuf, "%sX", pConditionCode );
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf( pBuf, "%s",get_reg_name((opcode&0xf)));
if ((opcode & 0x0f) == 14)
dasmflags = DASMFLAG_STEP_OUT;
}
else if ((opcode & 0x0ff000f0) == 0x01600010) // CLZ - v5
{
pBuf += sprintf(pBuf, "CLZ");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s", get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf));
}
else if ((opcode & 0x0ff000f0) == 0x01000050) // QADD - v5
{
pBuf += sprintf(pBuf, "QADD");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>16)&0xf));
}
else if ((opcode & 0x0ff000f0) == 0x01400050) // QDADD - v5
{
pBuf += sprintf(pBuf, "QDADD");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>16)&0xf));
}
else if ((opcode & 0x0ff000f0) == 0x01200050) // QSUB - v5
{
pBuf += sprintf(pBuf, "QSUB");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>16)&0xf));
}
else if ((opcode & 0x0ff000f0) == 0x01600050) // QDSUB - v5
{
pBuf += sprintf(pBuf, "QDSUB");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>16)&0xf));
}
else if ((opcode & 0x0ff00090) == 0x01000080) // SMLAxy - v5
{
pBuf += sprintf(pBuf, "SMLA%c%c", (opcode&0x20) ? 'T' : 'B', (opcode&0x40) ? 'T' : 'B');
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s, %s", get_reg_name((opcode>>16)&0xf), get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>8)&0xf));
}
else if ((opcode & 0x0ff00090) == 0x01400080) // SMLALxy - v5
{
pBuf += sprintf(pBuf, "SMLAL%c%c", (opcode&0x20) ? 'T' : 'B', (opcode&0x40) ? 'T' : 'B');
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s, %s", get_reg_name((opcode>>16)&0xf), get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>8)&0xf));
}
else if ((opcode & 0x0ff00090) == 0x01600080) // SMULxy - v5
{
pBuf += sprintf(pBuf, "SMUL%c%c", (opcode&0x20) ? 'T' : 'B', (opcode&0x40) ? 'T' : 'B');
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>16)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>12)&0xf));
}
else if ((opcode & 0x0ff000b0) == 0x012000a0) // SMULWy - v5
{
pBuf += sprintf(pBuf, "SMULW%c", (opcode&0x40) ? 'T' : 'B');
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s", get_reg_name((opcode>>16)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>8)&0xf));
}
else if ((opcode & 0x0ff000b0) == 0x01200080) // SMLAWy - v5
{
pBuf += sprintf(pBuf, "SMLAW%c", (opcode&0x40) ? 'T' : 'B');
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s, %s, %s, %s", get_reg_name((opcode>>16)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>8)&0xf), get_reg_name((opcode>>12)&0xf));
}
else if( (opcode&0x0e000000)==0 && (opcode&0x80) && (opcode&0x10) ) //bits 27-25 == 000, bit 7=1, bit 4=1
{
/* multiply or swap or half word data transfer */
if(opcode&0x60)
{ //bits = 6-5 != 00
/* half word data transfer */
if (((opcode & 0x60) == 0x40) && !(opcode & 0x100000)) // bit 20 = 0, bits 5&6 = 10 is ARMv5 LDRD
{
pBuf += sprintf(pBuf, "LDRD%s", pConditionCode);
}
else if (((opcode & 0x60) == 0x60) && !(opcode & 0x100000)) // bit 20 = 0, bits 5&6 = 11 is ARMv5 STRD
{
pBuf += sprintf(pBuf, "STRD%s", pConditionCode);
}
else
{
pBuf += sprintf(pBuf, "%s%s",(opcode&0x00100000)?"LDR":"STR",pConditionCode); //Bit 20 = 1 for Load, 0 for Store
//Signed? (if not, always unsigned half word)
if(opcode&0x40)
{
pBuf += sprintf(pBuf, "%s",(opcode&0x20)?"SH":"SB"); //Bit 5 = 1 for Half Word, 0 for Byte
}
else
{
pBuf += sprintf(pBuf, "H");
}
}
pBuf = WritePadding( pBuf, pBuf0 );
//Dest Register
pBuf += sprintf(pBuf, "%s, ", get_reg_name((opcode>>12)&0x0f));
//Base Register
pBuf += sprintf(pBuf, "[%s%s", get_reg_name((opcode>>16)&0x0f),(opcode&0x1000000)?"":"]"); //If Bit 24 = 1, Pre-increment, otherwise, Post increment so close brace
//Immediate or Register Offset?
if(opcode&0x400000) { //Bit 22 - 1 = immediate, 0 = register
//immediate ( imm. value in high nibble (bits 8-11) and lo nibble (bit 0-3) )
pBuf += sprintf(pBuf, ",%s#$%x",(opcode&0x800000)?"":"-",( (((opcode>>8)&0x0f)<<4) | (opcode&0x0f)));
}
else {
//register
pBuf += sprintf(pBuf, ",%s%s",(opcode&0x800000)?"":"-", get_reg_name((opcode & 0x0f)));
}
//Pre-Inc brace & Write back
pBuf += sprintf(pBuf, "%s%s",(opcode&0x1000000)?"]":"",(opcode&0x200000)?"{!}":"");
}
else {
if(opcode&0x01000000) { //bit 24 = 1
/* swap */
//todo: Test on valid instructions
/* xxxx 0001 0B00 nnnn dddd 0000 1001 mmmm */
pBuf += sprintf( pBuf, "SWP" );
pBuf += sprintf( pBuf, "%s%s", pConditionCode, (opcode & 0x400000)?"B":"" ); //Bit 22 = Byte/Word selection
//Rd, Rm, [Rn]
pBuf += sprintf( pBuf, "%s, %s, [%s]",
get_reg_name((opcode>>12)&0xf), get_reg_name(opcode&0xf), get_reg_name((opcode>>16)&0xf));
}
else {
/* multiply or multiply long */
if( opcode&0x800000 ) //Bit 23 = 1 for Multiply Long
{
/* Multiply Long */
/* xxxx0001 UAShhhhllllnnnn1001mmmm */
/* Signed? */
if( opcode&0x00400000 )
pBuf += sprintf( pBuf, "S" );
else
pBuf += sprintf( pBuf, "U" );
/* Multiply & Accumulate? */
if( opcode&0x00200000 )
{
pBuf += sprintf( pBuf, "MLAL" );
}
else
{
pBuf += sprintf( pBuf, "MULL" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
/* Set Status Flags */
if( opcode&0x00100000 )
{
*pBuf++ = 'S';
}
pBuf = WritePadding( pBuf, pBuf0 );
//Format is RLo,RHi,Rm,Rs
pBuf += sprintf( pBuf,
"%s, %s, %s, %s",
get_reg_name((opcode>>12)&0xf),
get_reg_name((opcode>>16)&0xf),
get_reg_name((opcode&0xf)),
get_reg_name((opcode>>8)&0xf));
}
else
{
/* Multiply */
/* xxxx0000 00ASdddd nnnnssss 1001mmmm */
/* Multiply & Accumulate? */
if( opcode&0x00200000 )
{
pBuf += sprintf( pBuf, "MLA" );
}
/* Multiply */
else
{
pBuf += sprintf( pBuf, "MUL" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
if( opcode&0x00100000 )
{
*pBuf++ = 'S';
}
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf( pBuf,
"%s, %s, %s",
get_reg_name((opcode>>16)&0xf),
get_reg_name((opcode&0xf)),
get_reg_name((opcode>>8)&0xf));
if( opcode&0x00200000 )
{
pBuf += sprintf( pBuf, ", %s", get_reg_name((opcode>>12)&0xf));
}
}
}
}
}
else if( (opcode&0x0c000000)==0 ) //bits 27-26 == 00 - This check can only exist properly after Multiplication check above
{
/* Data Processing OR PSR Transfer */
//SJE: check for MRS & MSR ( S bit must be clear, and bit 24,23 = 10 )
if( ((opcode&0x00100000)==0) && ((opcode&0x01800000)==0x01000000) ) {
char strpsr[6];
sprintf(strpsr, "%s",(opcode&0x400000)?"SPSR":"CPSR");
//MSR ( bit 21 set )
if( (opcode&0x00200000) ) {
pBuf += sprintf(pBuf, "MSR%s",pConditionCode );
//Flag Bits Only? (Bit 16 Clear)
if( (opcode&0x10000)==0) pBuf += sprintf(pBuf, "F");
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s,",strpsr);
WriteDataProcessingOperand(pBuf, opcode, (opcode&0x02000000)?1:0, 0, 1);
}
//MRS ( bit 21 clear )
else {
pBuf += sprintf(pBuf, "MRS%s",pConditionCode );
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf(pBuf, "%s,", get_reg_name((opcode>>12)&0x0f));
pBuf += sprintf(pBuf, "%s",strpsr);
}
}
else {
/* Data Processing */
/* xxxx001a aaaSnnnn ddddrrrr bbbbbbbb */
/* xxxx000a aaaSnnnn ddddcccc ctttmmmm */
int op=(opcode>>21)&0xf;
pBuf += sprintf(
pBuf, "%s%s",
pOperation[op],
pConditionCode );
//SJE: corrected S-Bit bug here
//if( (opcode&0x01000000) )
if( (opcode&0x0100000) )
{
*pBuf++ = 'S';
}
pBuf = WritePadding( pBuf, pBuf0 );
switch (op) {
case 0x00:
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
case 0x0c:
case 0x0e:
WriteDataProcessingOperand(pBuf, opcode, 1, 1, 1);
break;
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
WriteDataProcessingOperand(pBuf, opcode, 0, 1, 1);
break;
case 0x0d:
/* look for mov pc,lr */
if (((opcode >> 12) & 0x0f) == 15 && ((opcode >> 0) & 0x0f) == 14 && (opcode & 0x02000000) == 0)
dasmflags = DASMFLAG_STEP_OUT;
case 0x0f:
WriteDataProcessingOperand(pBuf, opcode, 1, 0, 1);
break;
}
}
}
else if( (opcode&0x0c000000)==0x04000000 ) //bits 27-26 == 01
{
UINT32 rn = 0;
UINT32 rnv = 0;
/* Data Transfer */
/* xxxx010P UBWLnnnn ddddoooo oooooooo Immediate form */
/* xxxx011P UBWLnnnn ddddcccc ctt0mmmm Register form */
if( opcode&0x00100000 )
{
pBuf += sprintf( pBuf, "LDR" );
}
else
{
pBuf += sprintf( pBuf, "STR" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
if( opcode&0x00400000 )
{
pBuf += sprintf( pBuf, "B" );
}
if( opcode&0x00200000 )
{
/* writeback addr */
if( opcode&0x01000000 )
{
/* pre-indexed addressing */
pBuf += sprintf( pBuf, "!" );
}
else
{
/* post-indexed addressing */
pBuf += sprintf( pBuf, "T" );
}
}
pBuf = WritePadding( pBuf, pBuf0 );
if (((opcode>>16) & 0xf) == 15 && (opcode & 0x02000000) == 0)
{
pBuf += sprintf( pBuf, "%s, ", get_reg_name((opcode>>12) & 0xf));
rnv = pc + 8;
if (rnv)
{
if (opcode & 0xfff)
{
if( opcode & 0x00800000 )
rnv += opcode & 0xfff;
else
rnv -= opcode & 0xfff;
}
if (rnv >= image_base && rnv < image_base + image_size)
{
UINT32 data = rnv - image_base;
data = (image_data[data + 3] << 24) | (image_data[data + 2] << 16) | (image_data[data + 1] << 8) | image_data[data];
pBuf += sprintf( pBuf, "=%s ", get_symbol_name(data));
check_rnv(rnv);
}
pBuf += sprintf( pBuf, "[$%08x]", rnv);
}
else
pBuf += sprintf( pBuf, "[PC]");
}
else
{
pBuf += sprintf( pBuf, "%s, [%s",
get_reg_name((opcode>>12)&0xf), get_reg_name((opcode>>16)&0xf));
//grab value of pc if used as base register
rn = (opcode>>16)&0xf;
if(rn==15) rnv = pc+8;
if( opcode&0x02000000 )
{
/* register form */
pBuf += sprintf( pBuf, "%s",(opcode&0x01000000)?"":"]" );
pBuf = WriteRegisterOperand1( pBuf, opcode );
pBuf += sprintf( pBuf, "%s",(opcode&0x01000000)?"]":"" );
}
else
{
/* immediate form */
pBuf += sprintf( pBuf, "%s",(opcode&0x01000000)?"":"]" );
//hide zero offsets
if(opcode&0xfff) {
if( opcode&0x00800000 )
{
pBuf += sprintf( pBuf, ", #$%x", opcode&0xfff );
rnv += (rnv)?opcode&0xfff:0;
}
else
{
pBuf += sprintf( pBuf, ", -#$%x", opcode&0xfff );
rnv -= (rnv)?opcode&0xfff:0;
}
}
pBuf += sprintf( pBuf, "%s",(opcode&0x01000000)?"]":"" );
//show where the read will occur if we found a value
if(rnv) pBuf += sprintf( pBuf, " (%x)",rnv);
}
}
}
else if( (opcode&0x0e000000) == 0x08000000 ) //bits 27-25 == 100
{
/* xxxx100P USWLnnnn llllllll llllllll */
/* Block Data Transfer */
if( opcode&0x00100000 )
{
pBuf += sprintf( pBuf, "LDM" );
}
else
{
pBuf += sprintf( pBuf, "STM" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
if( opcode&0x01000000 )
{
pBuf += sprintf( pBuf, "P" );
}
if( opcode&0x00800000 )
{
pBuf += sprintf( pBuf, "U" );
}
if( opcode&0x00400000 )
{
pBuf += sprintf( pBuf, "^" );
}
if( opcode&0x00200000 )
{
pBuf += sprintf( pBuf, "W" );
}
pBuf = WritePadding( pBuf, pBuf0 );
pBuf += sprintf( pBuf, "[%s], {", get_reg_name((opcode>>16)&0xf));
{
int j=0,last=0,found=0;
#if 0
for (j=0; j<16; j++) {
if (opcode&(1<<j) && found==0) {
found=1;
last=j;
}
else if ((opcode&(1<<j))==0 && found) {
if (last==j-1)
pBuf += sprintf( pBuf, " %s,", get_reg_name(last));
else
pBuf += sprintf( pBuf, " %s-%s,", get_reg_name(last), get_reg_name(j-1));
found=0;
}
}
if (found && last==15)
pBuf += sprintf( pBuf, " PC,");
else if (found)
pBuf += sprintf( pBuf, " %s-%s,", get_reg_name(last), get_reg_name(15)); // FIXME
#else
for (j=0; j<=12; j++) {
if (opcode&(1<<j) && found==0) {
found=1;
last=j;
}
else if ((opcode&(1<<j))==0 && found) {
if (last==j-1)
pBuf += sprintf( pBuf, " %s,", get_reg_name(last));
else
pBuf += sprintf( pBuf, " %s-%s,", get_reg_name(last), get_reg_name(j-1));
found=0;
}
}
if (found)
pBuf += sprintf( pBuf, " %s-%s,", get_reg_name(last), get_reg_name(12));
for (j=13; j<16; j++)
if (opcode&(1<<j))
pBuf += sprintf( pBuf, " %s,", get_reg_name(j));
#endif
}
pBuf--;
pBuf += sprintf( pBuf, " }");
}
else if( (opcode&0x0e000000)==0x0a000000 ) //bits 27-25 == 101
{
/* branch instruction */
/* xxxx101L oooooooo oooooooo oooooooo */
if( opcode&0x01000000 )
{
pBuf += sprintf( pBuf, "BL" );
dasmflags = DASMFLAG_STEP_OVER;
}
else
{
pBuf += sprintf( pBuf, "B" );
}
pBuf += sprintf( pBuf, "%s", pConditionCode );
pBuf = WritePadding( pBuf, pBuf0 );
pBuf = WriteBranchAddress( pBuf, pc, opcode );
}
else if( (opcode&0x0e000000)==0x0c000000 ) //bits 27-25 == 110
{
/* co processor data transfer */
DasmCoProc_DT(pBuf,opcode,(char*)pConditionCode,(char*)pBuf0);
}
else if( (opcode&0x0f000000)==0x0e000000 ) //bits 27-24 == 1110
{
/* co processor data operation or register transfer */
//Register Transfer
if(opcode&0x10)
{
DasmCoProc_RT(pBuf,opcode,pConditionCode,pBuf0);
}
//Data Op
else
{
DasmCoProc_DO(pBuf,opcode,pConditionCode,pBuf0);
}
}
else if( (opcode&0x0f000000) == 0x0f000000 ) //bits 27-24 == 1111
{
/* Software Interrupt */
pBuf += sprintf( pBuf, "SWI%s $%x",
pConditionCode,
opcode&0x00ffffff );
dasmflags = DASMFLAG_STEP_OVER;
}
else
{
pBuf += sprintf( pBuf, "Undefined" );
}
return dasmflags | DASMFLAG_SUPPORTED;
}
static UINT32 thumb_disasm( char *pBuf, UINT32 pc, UINT16 opcode )
{
const char *pBuf0;
UINT32 dasmflags = 0;
// UINT32 readword;
UINT32 addr;
UINT32 rm, rn, rs, rd, op2, imm;//, rrs;
INT32 offs;
pBuf0 = pBuf;
pBuf = WritePadding( pBuf, pBuf0 );
switch( ( opcode & THUMB_INSN_TYPE ) >> THUMB_INSN_TYPE_SHIFT )
{
case 0x0: /* Logical shifting */
if( opcode & THUMB_SHIFT_R ) /* Shift right */
{
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
offs = ( opcode & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
pBuf += sprintf( pBuf, "LSR %s, %s, %d", get_reg_name(rd), get_reg_name(rs), offs);
}
else /* Shift left */
{
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
offs = ( opcode & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
pBuf += sprintf( pBuf, "LSL %s, %s, %d", get_reg_name(rd), get_reg_name(rs), offs);
}
break;
case 0x1: /* Arithmetic */
if( opcode & THUMB_INSN_ADDSUB )
{
switch( ( opcode & THUMB_ADDSUB_TYPE ) >> THUMB_ADDSUB_TYPE_SHIFT )
{
case 0x0: /* ADD Rd, Rs, Rn */
rn = ( opcode & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ADD %s, %s, %s", get_reg_name(rd), get_reg_name(rs), get_reg_name(rn));
break;
case 0x1: /* SUB Rd, Rs, Rn */
rn = ( opcode & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "SUB %s, %s, %s", get_reg_name(rd), get_reg_name(rs), get_reg_name(rn));
break;
case 0x2: /* ADD Rd, Rs, #imm */
imm = ( opcode & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ADD %s, %s, #%d", get_reg_name(rd), get_reg_name(rs), imm );
break;
case 0x3: /* SUB Rd, Rs, #imm */
imm = ( opcode & THUMB_ADDSUB_RNIMM ) >> THUMB_ADDSUB_RNIMM_SHIFT;
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "SUB %s, %s, #%d", get_reg_name(rd), get_reg_name(rs), imm );
break;
default:
sprintf( pBuf, "INVALID %04x", opcode);
break;
}
}
else
{
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
offs = ( opcode & THUMB_SHIFT_AMT ) >> THUMB_SHIFT_AMT_SHIFT;
pBuf += sprintf( pBuf, "ASR %s, %s, %d", get_reg_name(rd), get_reg_name(rs), offs);
}
break;
case 0x2: /* CMP / MOV */
if( opcode & THUMB_INSN_CMP )
{
rn = ( opcode & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT;
op2 = ( opcode & THUMB_INSN_IMM );
pBuf += sprintf( pBuf, "CMP %s, %02x", get_reg_name(rn), op2 );
}
else
{
rd = ( opcode & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT;
op2 = ( opcode & THUMB_INSN_IMM );
pBuf += sprintf( pBuf, "MOV %s, %02x", get_reg_name(rd), op2 );
}
break;
case 0x3: /* ADD/SUB immediate */
if( opcode & THUMB_INSN_SUB ) /* SUB Rd, #Offset8 */
{
rn = ( opcode & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT;
op2 = ( opcode & THUMB_INSN_IMM );
pBuf += sprintf( pBuf, "SUB %s, %02x", get_reg_name(rn), op2 ); // fixed, rd -> rn
}
else /* ADD Rd, #Offset8 */
{
rn = ( opcode & THUMB_INSN_IMM_RD ) >> THUMB_INSN_IMM_RD_SHIFT;
op2 = opcode & THUMB_INSN_IMM;
pBuf += sprintf( pBuf, "ADD %s, %02x", get_reg_name(rn), op2 );
}
break;
case 0x4: /* Rd & Rm instructions */
switch( ( opcode & THUMB_GROUP4_TYPE ) >> THUMB_GROUP4_TYPE_SHIFT )
{
case 0x0:
switch( ( opcode & THUMB_ALUOP_TYPE ) >> THUMB_ALUOP_TYPE_SHIFT )
{
case 0x0: /* AND Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "AND %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x1: /* EOR Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "EOR %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x2: /* LSL Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "LSL %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x3: /* LSR Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "LSR %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x4: /* ASR Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ASR %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x5: /* ADC Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ADC %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x6: /* SBC Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "SBC %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x7: /* ROR Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ROR %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x8: /* TST Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "TST %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0x9: /* NEG Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "NEG %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xa: /* CMP Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "CMP %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xb: /* CMN Rd, Rs - check flags, add dasm */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "CMN %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xc: /* ORR Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "ORR %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xd: /* MUL Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "MUL %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xe: /* MUL Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "BIC %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
case 0xf: /* MVN Rd, Rs */
rs = ( opcode & THUMB_ADDSUB_RS ) >> THUMB_ADDSUB_RS_SHIFT;
rd = ( opcode & THUMB_ADDSUB_RD ) >> THUMB_ADDSUB_RD_SHIFT;
pBuf += sprintf( pBuf, "MVN %s, %s", get_reg_name(rd), get_reg_name(rs));
break;
default:
sprintf( pBuf, "INVALID %04x", opcode);
break;
}
break;
case 0x1:
switch( ( opcode & THUMB_HIREG_OP ) >> THUMB_HIREG_OP_SHIFT )
{
case 0x0: /* ADD Rd, Rs */
rs = ( opcode & THUMB_HIREG_RS ) >> THUMB_HIREG_RS_SHIFT;
rd = opcode & THUMB_HIREG_RD;
switch( ( opcode & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
{
case 0x1: /* ADD Rd, HRs */
pBuf += sprintf( pBuf, "ADD %s, %s", get_reg_name(rd), get_reg_name(rs + 8));
break;
case 0x2: /* ADD HRd, Rs */
pBuf += sprintf( pBuf, "ADD %s, %s", get_reg_name(rd + 8), get_reg_name(rs));
break;
case 0x3: /* ADD HRd, HRs */
pBuf += sprintf( pBuf, "ADD %s, %s", get_reg_name(rd + 8), get_reg_name(rs + 8));
break;
default:
sprintf( pBuf, "INVALID %04x", opcode);
break;
}
break;
case 0x1: /* CMP */
switch( ( opcode & THUMB_HIREG_H ) >> THUMB_HIREG_H_SHIFT )
{
case 0x0: /* CMP Rd, Rs */