-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisassembler.cpp
1013 lines (814 loc) · 24.7 KB
/
disassembler.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
#define _XOPEN_SOURCE
#include "disassembler.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
static constexpr const char opcodes[] =
"brkoracoporatsboraaslora"
"phporaaslphdtsboraaslora"
"bploraoraoratrboraaslora"
"clcorainctcstrboraaslora"
"jsrandjslandbitandroland"
"plpandrolpldbitandroland"
"bmiandandandbitandroland"
"secanddectscbitandroland"
"rtieorwdmeormvpeorlsreor"
"phaeorlsrphkjmpeorlsreor"
"bvceoreoreormvneorlsreor"
"clieorphytcdjmleorlsreor"
"rtsadcperadcstzadcroradc"
"plaadcrorrtljmpadcroradc"
"bvsadcadcadcstzadcroradc"
"seiadcplytdcjmpadcroradc"
"brastabrlstastystastxsta"
"deybittxaphbstystastxsta"
"bccstastastastystastxsta"
"tyastatxstxystzstastzsta"
"ldyldaldxldaldyldaldxlda"
"tayldataxplbldyldaldxlda"
"bcsldaldaldaldyldaldxlda"
"clvldatsxtyxldyldaldxlda"
"cpycmprepcmpcpycmpdeccmp"
"inycmpdexwaicpycmpdeccmp"
"bnecmpcmpcmppeicmpdeccmp"
"cldcmpphxstpjmlcmpdeccmp"
"cpxsbcsepsbccpxsbcincsbc"
"inxsbcnopxbacpxsbcincsbc"
"beqsbcsbcsbcpeasbcincsbc"
"sedsbcplxxcejsrsbcincsbc"
;
static constexpr const int mImplied = 0x0000;
static constexpr const int mImmediate = 0x1000;
static constexpr const int mAbsolute = 0x2000;
static constexpr const int mAbsoluteI = 0x3000;
static constexpr const int mAbsoluteIL = 0x4000;
static constexpr const int mAbsoluteLong = 0x5000;
static constexpr const int mDP = 0x6000;
static constexpr const int mDPI = 0x7000;
static constexpr const int mDPIL = 0x8000;
static constexpr const int mRelative = 0x9000;
static constexpr const int mBlockMove = 0xa000;
static constexpr const int mImpliedA = 0xb000; // inc a, dec a, etc.
static constexpr const int m_S = 0x0100;
static constexpr const int m_X = 0x0200;
static constexpr const int m_Y = 0x0400;
static constexpr const int m_M = 0x0020;
static constexpr const int m_I = 0x0010;
static constexpr const int modes[] =
{
1 | mAbsolute, // 00 brk #imm
1 | mDPI | m_X, // 01 ora (dp,x)
1 | mAbsolute, // 02 cop #imm
1 | mDP | m_S, // 03 ora ,s
1 | mDP, // 04 tsb <dp
1 | mDP, // 05 ora <dp
1 | mDP, // 06 asl <dp
1 | mDPIL, // 07 ora [dp]
0 | mImplied, // 08 php
1 | mImmediate | m_M, // 09 ora #imm
0 | mImpliedA, // 0a asl a
0 | mImplied, // 0b phd
2 | mAbsolute, // 0c tsb |abs
2 | mAbsolute, // 0d ora |abs
2 | mAbsolute, // 0e asl |abs
3 | mAbsoluteLong, // 0f ora >abs
1 | mRelative, // 10 bpl
1 | mDPI | m_Y, // 11 ora (dp),y
1 | mDPI, // 12 ora (dp)
1 | mDPI | m_S | m_Y, // 13 ora ,s,y
1 | mDP, // 14 trb <dp
1 | mDP | m_X, // 15 ora <dp,x
1 | mDP | m_X, // 16 asl <dp,x
1 | mDPIL | m_Y, // 17 ora [dp],y
0 | mImplied, // 18 clc
2 | mAbsolute | m_Y, // 19 ora |abs,y
0 | mImpliedA, // 1a inc a
0 | mImplied, // 1b tcs
2 | mAbsolute, // 1c trb |abs
2 | mAbsolute | m_X, // 1d ora |abs,x
2 | mAbsolute | m_X, // 1e asl |abs,x
3 | mAbsoluteLong | m_X, // 1f ora >abs,x
2 | mAbsolute, // 20 jsr |abs
1 | mDPI | m_X, // 21 and (dp,x)
3 | mAbsoluteLong, // 22 jsl >abs
1 | mDP | m_S, // 23 and ,s
1 | mDP, // 24 bit <dp
1 | mDP, // 25 and <dp
1 | mDP, // 26 rol <dp
1 | mDPIL, // 27 and [dp]
0 | mImplied, // 28 plp
1 | mImmediate | m_M, // 29 and #imm
0 | mImpliedA, // 2a rol a
0 | mImplied, // 2b pld
2 | mAbsolute, // 2c bit |abs
2 | mAbsolute, // 2d and |abs
2 | mAbsolute, // 2e rol |abs
3 | mAbsoluteLong, // 2f and >abs
1 | mRelative, // 30 bmi
1 | mDPI | m_Y, // 31 and (dp),y
1 | mDPI, // 32 and (dp)
1 | mDPI | m_S | m_Y, // 33 and ,s,y
1 | mDP | m_X, // 34 bit dp,x
1 | mDP | m_X, // 35 and dp,x
1 | mDP | m_X, // 36 rol <dp,x
1 | mDPIL | m_Y, // 37 and [dp],y
0 | mImplied, // 38 sec
2 | mAbsolute | m_Y, // 39 and |abs,y
0 | mImpliedA, // 3a dec a
0 | mImplied, // 3b tsc
2 | mAbsolute | m_X, // 3c bits |abs,x
2 | mAbsolute | m_X, // 3d and |abs,x
2 | mAbsolute | m_X, // 3e rol |abs,x
3 | mAbsoluteLong | m_X, // 3f and >abs,x
0 | mImplied, // 40 rti
1 | mDPI | m_X, // 41 eor (dp,x)
1 | mAbsolute, // 42 wdm #imm
1 | mDP | m_S, // 43 eor ,s
2 | mBlockMove, // 44 mvp x,x
1 | mDP, // 45 eor dp
1 | mDP, // 46 lsr dp
1 | mDPIL, // 47 eor [dp]
0 | mImplied, // 48 pha
1 | mImmediate | m_M, // 49 eor #imm
0 | mImpliedA, // 4a lsr a
0 | mImplied, // 4b phk
2 | mAbsolute, // 4c jmp |abs
2 | mAbsolute, // 4d eor |abs
2 | mAbsolute, // 4e lsr |abs
3 | mAbsoluteLong, // 4f eor >abs
1 | mRelative, // 50 bvc
1 | mDPI | m_Y, // 51 eor (dp),y
1 | mDPI, // 52 eor (dp)
1 | mDPI | m_S | m_Y, // 53 eor ,s,y
2 | mBlockMove, // 54 mvn x,x
1 | mDP | m_X, // 55 eor dp,x
1 | mDP | m_X, // 56 lsr dp,x
1 | mDPIL | m_Y, // 57 eor [dp],y
0 | mImplied, // 58 cli
2 | mAbsolute | m_Y, // 59 eor |abs,y
0 | mImplied, // 5a phy
0 | mImplied, // 5b tcd
3 | mAbsoluteLong, // 5c jml >abs
2 | mAbsolute | m_X, // 5d eor |abs,x
2 | mAbsolute | m_X, // 5e lsr |abs,x
3 | mAbsoluteLong | m_X, // 5f eor >abs,x
0 | mImplied, // 60 rts
1 | mDPI | m_X, // 61 adc (dp,x)
2 | mRelative, // 62 per |abs
1 | mDP | m_S, // 63 adc ,s
1 | mDP, // 64 stz <dp
1 | mDP, // 65 adc <dp
1 | mDP, // 66 ror <dp
1 | mDPIL, // 67 adc [dp]
0 | mImplied, // 68 pla
1 | mImmediate | m_M, // 69 adc #imm
0 | mImplied, // 6a ror a
0 | mImplied, // 6b rtl
2 | mAbsoluteI, // 6c jmp (abs)
2 | mAbsolute, // 6d adc |abs
2 | mAbsolute, // 6e ror |abs
3 | mAbsoluteLong, // 6f adc >abs
1 | mRelative, // 70 bvs
1 | mDPI | m_Y, // 71 adc (dp),y
1 | mDPI, // 72 adc (dp)
1 | mDPI | m_S | m_Y, // 73 adc ,s,y
1 | mDP | m_X, // 74 stz dp,x
1 | mDP | m_X, // 75 adc dp,x
1 | mDP | m_X, // 76 ror dp,x
1 | mDPIL | m_Y, // 77 adc [dp],y
0 | mImplied, // 78 sei
2 | mAbsolute | m_Y, // 79 adc |abs,y
0 | mImplied, // 7a ply
0 | mImplied, // 7b tdc
2 | mAbsoluteI | m_X, // 7c jmp (abs,x)
2 | mAbsolute | m_X, // 7d adc |abs,x
2 | mAbsolute | m_X, // 7e ror |abs,x
3 | mAbsoluteLong | m_X, // 7f adc >abs,x
1 | mRelative, // 80 bra
1 | mDPI | m_X, // 81 sta (dp,x)
2 | mRelative, // 82 brl |abs
1 | mDP | m_S, // 83 sta ,s
1 | mDP, // 84 sty <dp
1 | mDP, // 85 sta <dp
1 | mDP, // 86 stx <dp
1 | mDPIL, // 87 sta [dp]
0 | mImplied, // 88 dey
1 | mImmediate | m_M, // 89 bit #imm
0 | mImplied, // 8a txa
0 | mImplied, // 8b phb
2 | mAbsolute, // 8c sty |abs
2 | mAbsolute, // 8d sta |abs
2 | mAbsolute, // 8e stx |abs
3 | mAbsoluteLong, // 8f sta >abs
1 | mRelative, // 90 bcc
1 | mDPI | m_Y, // 91 sta (dp),y
1 | mDPI, // 92 sta (dp)
1 | mDPI | m_S | m_Y, // 93 sta ,s,y
1 | mDP | m_X, // 94 sty dp,x
1 | mDP | m_X, // 95 sta dp,x
1 | mDP | m_Y, // 96 stx dp,y
1 | mDPIL | m_Y, // 97 sta [dp],y
0 | mImplied, // 98 tya
2 | mAbsolute | m_Y, // 99 sta |abs,y
0 | mImplied, // 9a txs
0 | mImplied, // 9b txy
2 | mAbsolute, // 9c stz |abs
2 | mAbsolute | m_X, // 9d sta |abs,x
2 | mAbsolute | m_X, // 9e stz |abs,x
3 | mAbsoluteLong | m_X, // 9f sta >abs,x
1 | mImmediate | m_I, // a0 ldy #imm
1 | mDPI | m_X, // a1 lda (dp,x)
1 | mImmediate | m_I, // a2 ldx #imm
1 | mDP | m_S, // a3 lda ,s
1 | mDP, // a4 ldy <dp
1 | mDP, // a5 lda <dp
1 | mDP, // a6 ldx <dp
1 | mDPIL, // a7 lda [dp]
0 | mImplied, // a8 tay
1 | mImmediate | m_M, // a9 lda #imm
0 | mImplied, // aa tax
0 | mImplied, // ab plb
2 | mAbsolute, // ac ldy |abs
2 | mAbsolute, // ad lda |abs
2 | mAbsolute, // ae ldx |abs
3 | mAbsoluteLong, // af lda >abs
1 | mRelative, // b0 bcs
1 | mDPI | m_Y, // b1 lda (dp),y
1 | mDPI, // b2 lda (dp)
1 | mDPI | m_S | m_Y, // b3 lda ,s,y
1 | mDP | m_X, // b4 ldy <dp,x
1 | mDP | m_X, // b5 lda <dp,x
1 | mDP | m_Y, // b6 ldx <dp,y
1 | mDPIL | m_Y, // b7 lda [dp],y
0 | mImplied, // b8 clv
2 | mAbsolute | m_Y, // b9 lda |abs,y
0 | mImplied, // ba tsx
0 | mImplied, // bb tyx
2 | mAbsolute | m_X, // bc ldy |abs,x
2 | mAbsolute | m_X, // bd lda |abs,x
2 | mAbsolute | m_Y, // be ldx |abs,y
3 | mAbsoluteLong | m_X, // bf lda >abs,x
1 | mImmediate | m_I, // c0 cpy #imm
1 | mDPI | m_X, // c1 cmp (dp,x)
1 | mImmediate, // c2 rep #
1 | mDP | m_S, // c3 cmp ,s
1 | mDP, // c4 cpy <dp
1 | mDP, // c5 cmp <dp
1 | mDP, // c6 dec <dp
1 | mDPIL, // c7 cmp [dp]
0 | mImplied, // c8 iny
1 | mImmediate | m_M, // c9 cmp #imm
0 | mImplied, // ca dex
0 | mImplied, // cb WAI
2 | mAbsolute, // cc cpy |abs
2 | mAbsolute, // cd cmp |abs
2 | mAbsolute, // ce dec |abs
3 | mAbsoluteLong, // cf cmp >abs
1 | mRelative, // d0 bne
1 | mDPI | m_Y, // d1 cmp (dp),y
1 | mDPI, // d2 cmp (dp)
1 | mDPI | m_S | m_Y, // d3 cmp ,s,y
1 | mDP, // d4 pei (dp) --> pei <dp
1 | mDP | m_X, // d5 cmp dp,x
1 | mDP | m_X, // d6 dec dp,x
1 | mDPIL | m_Y, // d7 cmp [dp],y
0 | mImplied, // d8 cld
2 | mAbsolute | m_Y, // d9 cmp |abs,y
0 | mImplied, // da phx
0 | mImplied, // db stp
2 | mAbsoluteIL, // dc jml [abs]
2 | mAbsolute | m_X, // dd cmp |abs,x
2 | mAbsolute | m_X, // de dec |abs,x
3 | mAbsoluteLong | m_X, // df cmp >abs,x
1 | mImmediate | m_I, // e0 cpx #imm
1 | mDPI | m_X, // e1 sbc (dp,x)
1 | mImmediate, // e2 sep #imm
1 | mDP | m_S, // e3 sbc ,s
1 | mDP, // e4 cpx <dp
1 | mDP, // e5 sbc <dp
1 | mDP, // e6 inc <dp
1 | mDPIL, // e7 sbc [dp]
0 | mImplied, // e8 inx
1 | mImmediate| m_M, // e9 sbc #imm
0 | mImplied, // ea nop
0 | mImplied, // eb xba
2 | mAbsolute, // ec cpx |abs
2 | mAbsolute, // ed abc |abs
2 | mAbsolute, // ee inc |abs
3 | mAbsoluteLong, // ef sbc >abs
1 | mRelative, // f0 beq
1 | mDPI | m_Y, // f1 sbc (dp),y
1 | mDPI, // f2 sbc (dp)
1 | mDPI | m_S | m_Y, // f3 sbc ,s,y
2 | mAbsolute, // f4 pea |abs --> pea #imm
1 | mDP | m_X, // f5 sbc dp,x
1 | mDP | m_X, // f6 inc dp,x
1 | mDPIL | m_Y, // f7 sbc [dp],y
0 | mImplied, // f8 sed
2 | mAbsolute | m_Y, // f9 sbc |abs,y
0 | mImplied, // fa plx
0 | mImplied, // fb xce
2 | mAbsoluteI | m_X, // fc jsr (abs,x)
2 | mAbsolute | m_X, // fd sbc |abs,x
2 | mAbsolute | m_X, // fe inc |abs,x
3 | mAbsoluteLong | m_X, // ff sbc >abs,x
};
static bool branchlike(uint8_t op) {
switch(op) {
case 0x10: // bpl
//case 0x20: // jsr
//case 0x22: // jsl
case 0x30: // bmi
case 0x40: // rti
case 0x4c: // jmp
case 0x50: // bvc
case 0x5c: // jml
case 0x60: // rts
case 0x6b: // rtl
case 0x6c: // jmp
case 0x70: // bvs
case 0x7c: // jmp
case 0x80: // bra
case 0x82: // brl
case 0x90: // bcc
case 0xb0: // bcs
case 0xd0: // bne
case 0xdc: // jml
case 0xf0: // beq
//case 0xfc: // jsr
return true;
default:
return false;
}
}
constexpr const int kOpcodeTab = 20;
constexpr const int kOperandTab = 30;
constexpr const int kCommentTab = 80;
void indent_to(std::string &line, unsigned position) {
if (line.length() < position)
line.resize(position, ' ');
}
disassembler::~disassembler() {
}
std::string disassembler::to_x(uint32_t x, unsigned bytes, char prefix) {
std::string s;
char buffer[16];
if (prefix) s.push_back(prefix);
if (x > 0xff && bytes < 4) bytes = 4;
if (x > 0xffff && bytes < 6) bytes = 6;
if (x > 0xffffff && bytes < 8) bytes = 8;
memset(buffer, '0', sizeof(buffer));
int i = 16;
while (x) {
buffer[--i] = "0123456789abcdef"[x & 0x0f];
x >>= 4;
}
s.append(buffer + 16 - bytes, buffer + 16);
return s;
}
int disassembler::operand_size(uint8_t op, bool m, bool x) {
unsigned mode = modes[op];
unsigned size = mode & 0x0f;
if ((mode & m_I) && x) size++;
if ((mode & m_M) && m) size++;
return size;
}
void disassembler::emit(const std::string &label) {
fputs(label.c_str(), stdout);
fputc('\n', stdout);
}
void disassembler::emit(const std::string &label, const std::string &opcode) {
std::string tmp;
tmp = label;
if (!opcode.empty()) {
indent_to(tmp, kOpcodeTab);
tmp += opcode;
}
tmp.push_back('\n');
fputs(tmp.c_str(), stdout);
}
void disassembler::emit(const std::string &label, const std::string &opcode, const std::string &operand) {
std::string tmp;
tmp = label;
if (!opcode.empty()) {
indent_to(tmp, kOpcodeTab);
tmp += opcode;
}
if (!operand.empty()) {
indent_to(tmp, kOperandTab);
tmp += operand;
}
tmp.push_back('\n');
fputs(tmp.c_str(), stdout);
}
void disassembler::emit(const std::string &label, const std::string &opcode, const std::string &operand, const std::string &comment) {
std::string tmp;
tmp = label;
if (!opcode.empty()) {
indent_to(tmp, kOpcodeTab);
tmp += opcode;
}
if (!operand.empty()) {
indent_to(tmp, kOperandTab);
tmp += operand;
}
if (!comment.empty()) {
indent_to(tmp, kCommentTab);
tmp += "; ";
tmp += comment;
}
tmp.push_back('\n');
fputs(tmp.c_str(), stdout);
}
void disassembler::reset() {
_arg = 0;
_st = 0;
}
std::pair<std::string, std::string>
disassembler::format_data(unsigned size, const uint8_t *data) {
std::string tmp;
for (unsigned i = 0; i < size; ++i) {
if (i > 0) tmp += ", ";
tmp += to_x(data[i], 2, '$');
}
return std::make_pair("db", tmp);
}
std::pair<std::string, std::string>
disassembler::format_data(unsigned size, const std::string &data) {
switch(size) {
case 1: return std::make_pair("db", data);
case 2: return std::make_pair("dw", data);
case 3: return std::make_pair("da", data);
case 4: return std::make_pair("dl", data);
default: {
std::string tmp;
tmp = std::to_string(size) + " bytes";
return std::make_pair(tmp, data);
}
}
}
void disassembler::dump() {
std::string line;
if (!_st) return;
auto p = format_data(_st, _bytes);
indent_to(line, kOpcodeTab);
line += p.first;
indent_to(line, kOperandTab);
line += p.second;
hexdump(line);
line.push_back('\n');
fputs(line.c_str(), stdout);
_pc += _st;
reset();
}
void disassembler::dump(const std::string &expr, unsigned size, uint32_t value) {
std::string line;
if (_st) dump();
for (_st = 0; _st < size; ++_st) {
_bytes[_st] = value & 0xff;
value >>= 8;
}
auto p = format_data(size, expr);
indent_to(line, kOpcodeTab);
line += p.first;
indent_to(line, kOperandTab);
line += p.second;
hexdump(line);
line.push_back('\n');
fputs(line.c_str(), stdout);
_pc += _st;
reset();
}
void disassembler::flush() {
if (_st) dump();
check_labels();
}
void disassembler::check_labels() {
if ( _next_label >= 0 && _pc + _st >= _next_label) {
//flush(); // -- too recursive. see above.
if (_st) dump();
_next_label = next_label(_pc);
}
}
void disassembler::space(unsigned size) {
flush();
std::string line;
indent_to(line, kOpcodeTab);
line += ds();
indent_to(line, kOperandTab);
while (size) {
uint32_t chunk;
if (_next_label == -1) chunk = size;
else {
chunk = _next_label - _pc;
chunk = std::min(chunk, size);
}
line.resize(kOperandTab);
line += std::to_string(chunk);
indent_to(line, kCommentTab);
line += "; ";
line += to_x(_pc, 4);
line.push_back(':');
line.push_back('\n');
fputs(line.c_str(), stdout);
_pc += chunk;
size -= chunk;
if (_next_label == _pc) _next_label = next_label(_pc);
}
}
void disassembler::operator()(const std::string &expr, unsigned size, uint32_t value) {
if (_st == 0 || !_code)
check_labels();
if (!_code) {
dump(expr, size, value);
if (_inline_data) {
_inline_data -= size;
if (_inline_data <= 0) _code = true;
}
return;
}
if (_st != 1 || size != _size) {
dump(expr, size, value);
return;
}
for (int i = 0; i < size; ++i) {
_bytes[_st++] = value & 0xff;
value >>= 8;
}
print(expr);
}
void disassembler::operator()(uint8_t byte) {
if (_st == 0 || !_code)
check_labels();
if (!_code) {
_bytes[_st++] = byte;
if (_inline_data && --_inline_data <= 0) {
dump();
_code = true;
return;
}
if (_st == 4) dump();
return;
}
_bytes[_st++] = byte;
if (_st == 1) {
_op = byte;
_mode = modes[_op];
// bit hack
if (_traits & bit_hacks && _op == 0x2c) {
if (_next_label == _pc + 1) {
dump();
return;
}
}
if (_traits & pea_immediate && _op == 0xf4) _mode = 2 | mImmediate;
_size = _mode & 0x0f;
if (_mode & _flags & m_I) _size++;
if (_mode & _flags & m_M) _size++;
if (!_size) {
print();
if (branchlike(byte)) fputs("\n", stdout);
}
return;
}
unsigned shift = (_st - 2) * 8;
_arg = _arg + (byte << shift);
if (_st <= _size) return;
uint8_t op = _op;
uint32_t arg = _arg;
if (_traits & track_rep_sep) {
switch(_op) {
case 0xc2: // REP
_flags |= (_arg & 0x30);
break;
case 0xe2: // SEP
_flags &= ~(_arg & 0x30);
break;
}
}
// all done... now print it.
print();
if (branchlike(op)) fputs("\n", stdout);
// todo -- subscribe to before/after events...
switch(op) {
case 0xc2:
case 0xe2:
case 0x22:
case 0x5c:
case 0xdc:
event(op, arg);
break;
}
}
std::string disassembler::prefix() {
std::string tmp;
switch(_mode & 0xf000) {
case mImmediate: tmp = "#"; break;
case mDP: tmp = "<"; break;
case mDPI: tmp = "(<"; break;
case mDPIL: tmp = "[<"; break;
case mAbsoluteIL: tmp = "["; break;
case mRelative:
case mBlockMove:
break;
// cop, brk are treated as absolute.
case mAbsolute:
//if (_size == 1) printf("\t");
if (_size > 1) tmp = "|";
break;
case mAbsoluteLong: tmp = ">"; break;
case mAbsoluteI: tmp = "("; break;
}
return tmp;
}
std::string disassembler::suffix() {
std::string tmp;
switch(_mode & 0x0f00) {
case m_X: tmp = ",x"; break;
case m_Y: if (!(_mode & (mDPI|mDPIL))) tmp = ",y"; break;
case m_S:
case m_S | m_Y:
tmp = ",s"; break;
}
switch(_mode & 0xf000) {
case mAbsoluteI:
case mDPI:
tmp += ")"; break;
case mAbsoluteIL:
case mDPIL:
tmp += "]"; break;
}
// (xxx,s),y
// (xxx),y
// [xxx],y
switch(_mode & 0x0f00) {
case m_Y:
if (_mode & (mDPI|mDPIL)) tmp += ",y"; break;
case m_S | m_Y:
tmp += ",y"; break;
}
return tmp;
}
void disassembler::hexdump(std::string &line) {
// print pc and hexdump...
indent_to(line, kCommentTab);
line += "; ";
line += to_x(_pc, 4);
line.push_back(':');
int i;
for (i = 0; i < _st; ++i) {
line.push_back(' ');
line += to_x(_bytes[i], 2);
}
for ( ; i < 4; ++i) {
line += " ";
}
line += " ";
for (i = 0; i < _st; ++i) {
uint8_t c = _bytes[i];
// msb flag?
if (_traits & msb_hexdump) c &= 0x7f;
if (isprint(c) && isascii(c)) line += c;
else line += '.';
}
}
std::string disassembler::label_for_address(uint32_t address) { return ""; }
std::string disassembler::label_for_zp(uint32_t address) { return ""; }
void disassembler::print() {
if (_size) {
std::string tmp;
switch(_mode & 0xf000) {
case mRelative: {
uint32_t pc = _pc + 1 + _size + _arg;
if ((_size == 1) && (_arg & 0x80))
pc += 0xff00;
pc &= 0xffff;
// it would be really fancy if it checked for a label name @pc...
tmp = label_for_address(pc);
if (tmp.empty()) tmp = to_x(pc, 4, '$');
break;
}
case mBlockMove: {
// todo -- verify order.
tmp = to_x((_arg >> 8) & 0xff, 2, '$')
+ ","
+ to_x((_arg >> 0) & 0xff, 2, '$');
break;
}
case mDP:
case mDPI:
case mDPIL:
tmp = label_for_zp(_arg);
if (tmp.empty()) tmp = to_x(_arg, _size * 2, '$');
break;
//case mImmediate:
case mAbsolute:
case mAbsoluteI:
case mAbsoluteIL:
case mAbsoluteLong:
tmp = label_for_address(_arg);
if (tmp.empty()) tmp = to_x(_arg, _size * 2, '$');
break;
default:
tmp = to_x(_arg, _size * 2, '$');
break;
}
print(tmp);
return;
}
std::string line;
indent_to(line, kOpcodeTab);
line.append(&opcodes[_op * 3], 3);
if (_mode == mImpliedA && (_traits & explicit_implied_a)) {
indent_to(line, kOperandTab);
line.append("a");
}
hexdump(line);
line.push_back('\n');
fputs(line.c_str(), stdout);
_pc += _size + 1;
reset();
}
void disassembler::print(const std::string &expr) {
std::string line;
indent_to(line, kOpcodeTab);
line.append(&opcodes[_op * 3], 3);
if (_size) {
indent_to(line, kOperandTab);
line += prefix();
line += expr;
line += suffix();
}
hexdump(line);
line.push_back('\n');
fputs(line.c_str(), stdout);
_pc += _size + 1;
reset();
}
#pragma mark -
const std::vector<uint32_t> &analyzer::finish() {
std::sort(_labels.begin(), _labels.end());
auto end = std::unique(_labels.begin(), _labels.end());
_labels.erase(end, _labels.end());
return _labels;
}
void analyzer::operator()(uint8_t byte) {
if (!_code) {
if (_inline_data && --_inline_data <= 0) {
_code = true;
reset();
return;
}
return;
}
_st++;
if (_st == 1) {
_op = byte;
_mode = modes[_op];
_size = _mode & 0x0f;
if (_mode & _flags & m_I) _size++;
if (_mode & _flags & m_M) _size++;
if (!_size) { reset(); }
return;
}
unsigned shift = (_st - 2) * 8;
_arg = _arg + (byte << shift);
if (_st <= _size) return;
// all done... now process it
process();
}
void analyzer::reset() {
_pc += _st;
_arg = 0;
_st = 0;
}
void analyzer::process() {
if (_traits & disassembler::track_rep_sep) {
switch(_op) {
case 0xc2: // REP
_flags |= (_arg & 0x30);
break;
case 0xe2: // SEP
_flags &= ~(_arg & 0x30);
break;
}
}
switch (_mode & 0xf000) {
case mRelative: {
uint32_t pc = _pc + 1 + _size + _arg;
if ((_size == 1) && (_arg & 0x80))
pc += 0xff00;
pc &= 0xffff;