forked from kcozens/dis68k
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dis68k.c
1435 lines (1289 loc) · 57.5 KB
/
dis68k.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
/* DIS68K by wrm
Submitted to public domain 10/08/93 (V1.2)
Current version 1.22, adds "raw" disasm output, ie ready to re-assemble
1999-11-04: Add 68030 instructions,
Add labels.
2019-03-10: Remove conio dependency, fix modern C build errors.
onward: see Git history. */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
// Enable the #define below to print diagnostics.
//#define PRINT_DIAGNOSTICS
#ifdef PRINT_DIAGNOSTICS
#define diagnostic_printf printf
#else
#define diagnostic_printf(...) while(false);
#endif
struct OpcodeDetails {
uint16_t and;
uint16_t xor;
};
const struct OpcodeDetails optab[88] = {
{0x0000,0x0000}, {0xF1F0,0xC100}, {0xF000,0xD000}, {0xF0C0,0xD0C0},
{0xFF00,0x0600}, {0xF100,0x5000}, {0xF130,0xD100}, {0xF000,0xC000},
{0xFF00,0x0200}, {0xF118,0xE100}, {0xFFC0,0xE1C0}, {0xF118,0xE000},
{0xFFC0,0xE0C0}, {0xF000,0x6000}, {0xF1C0,0x0140}, {0xFFC0,0x0840},
{0xF1C0,0x0180}, {0xFFC0,0x0880}, {0xF1C0,0x01C0}, {0xFFC0,0x08C0},
{0xF1C0,0x0100}, {0xFFC0,0x0800}, {0xF1C0,0x4180}, {0xFF00,0x4200},
{0xF100,0xB000}, {0xF0C0,0xB0C0}, {0xFF00,0x0C00}, {0xF138,0xB108},
{0xF0F8,0x50C8}, {0xF1C0,0x81C0}, {0xF1C0,0x80C0}, {0xF100,0xB100},
{0xFF00,0x0A00}, {0xF100,0xC100}, {0xFFB8,0x4880}, {0xFFC0,0x4EC0},
{0xFFC0,0x4E80}, {0xF1C0,0x41C0}, {0xFFF8,0x4E50}, {0xF118,0xE108},
{0xFFC0,0xE3C0}, {0xF118,0xE008}, {0xFFC0,0xE2C0}, {0xC000,0x0000},
{0xFFC0,0x44C0}, {0xFFC0,0x46C0}, {0xFFC0,0x40C0}, {0xFFF0,0x4E60},
{0xC1C0,0x0040}, {0xFB80,0x4880}, {0xF138,0x0108}, {0xF100,0x7000},
{0xF1C0,0xC1C0}, {0xF1C0,0xC0C0}, {0xFFC0,0x4800}, {0xFF00,0x4400},
{0xFF00,0x4000}, {0xFFFF,0x4E71}, {0xFF00,0x4600}, {0xF000,0x8000},
{0xFF00,0x0000}, {0xFFC0,0x4840}, {0xFFFF,0x4E70}, {0xF118,0xE118},
{0xFFC0,0xE7C0}, {0xF118,0xE018}, {0xFFC0,0xE6C0}, {0xF118,0xE110},
{0xFFC0,0xE5C0}, {0xF118,0xE010}, {0xFFC0,0xE4C0}, {0xFFFF,0x4E73},
{0xFFFF,0x4E77}, {0xFFFF,0x4E75}, {0xF1F0,0x8100}, {0xF0C0,0x50C0},
{0xFFFF,0x4E72}, {0xF000,0x9000}, {0xF0C0,0x90C0}, {0xFF00,0x0400},
{0xF100,0x5100}, {0xF130,0x9100}, {0xFFF8,0x4840}, {0xFFC0,0x4AC0},
{0xFFF0,0x4E40}, {0xFFFF,0x4E76}, {0xFF00,0x4A00}, {0xFFF8,0x4E58}
};
uint32_t address;
uint32_t romstart;
bool rawmode = false;
struct MapEntry {
uint32_t start;
enum Type {
Unknown,
End,
Byte,
Word,
Long,
Text,
Rsvd,
Code
} type;
} *map = NULL;
const char bra_tab[][4] = {
"BRA", "BSR", "BHI", "BLS",
"BCC", "BCS", "BNE", "BEQ",
"BVC", "BVS", "BPL", "BMI",
"BGE", "BLT", "BGT", "BLE"
};
const char scc_tab[][4] = {
"ST", "SF", "SHI", "SLS",
"SCC", "SCS", "SNE", "SEQ",
"SVC", "SVS", "SPL", "SMI",
"SGE", "SLT", "SGT", "SLE"
};
const char size_arr[3] = {'B','W','L'};
/*!
Reads from @c filename and populates the global variable @c map.
If @c filename is @c NULL, populates @c map with the empty map.
@returns @c false if a filename is specified but could not be opened
or else could not properly be parsed. @c true otherwise.
*/
bool readmap(const char *filename) {
FILE *fmap;
// Create a sixteen-item map, to be getting on with.
size_t allocated_map_size = 16;
map = (struct MapEntry *)malloc(sizeof(struct MapEntry) * allocated_map_size);
fmap = fopen(filename, "rt");
if (fmap == NULL) {
romstart = 0;
map[0].start = 0L;
map[0].type = Code;
map[1].type = End;
return true;
}
if (!fscanf(fmap,"romstart = %X", &romstart)) {
fprintf(stderr, "Error in romstart = line!\n");
return false;
}
size_t index = 0;
while (true) {
uint32_t start;
char type[10];
const int items_read = fscanf(fmap, "%x,%9s", &start, type);
if (items_read == 2) {
if (index+1 >= allocated_map_size) {
// Default to 16 entries when first creating a map, double each time the existing
// estimate isn't enough for both this entry and the terminator yet to come.
const size_t new_map_size = allocated_map_size ? (allocated_map_size * 2) : 16;
map = realloc(map, sizeof(struct MapEntry) * new_map_size);
allocated_map_size = new_map_size;
if (!map) {
fprintf(stderr, "Couldn't allocate enough space for map\n");
return false;
}
}
map[index].start = start;
map[index].type = Unknown;
if (strcmp(type,"byte")==0) map[index].type = Byte;
if (strcmp(type,"word")==0) map[index].type = Word;
if (strcmp(type,"long")==0) map[index].type = Long;
if (strcmp(type,"text")==0) map[index].type = Text;
if (strcmp(type,"rsvd")==0) map[index].type = Rsvd;
if (strcmp(type,"code")==0) map[index].type = Code;
if (strcmp(type,"end")==0) map[index].type = End;
if (map[index].type == Unknown) {
fprintf(stderr, "Couldn't parse type '%s' in map file at line %lu\n", type, index+2);
return false;
}
if ((map[index].type == Word || map[index].type == Long) &&
map[index].start & 1)
{
fprintf(stderr, "Address block %06x must be word aligned in line %ld\n",
map[index].start, index);
return false;
}
++ index;
} else {
if (items_read > 0) {
fprintf(stderr, "Syntax error on line %lu (%d)\n", index+2, items_read);
return false;
}
break; // i.e. feof.
}
}
map[index].start = 0xffffffff;
map[index].type = End;
fclose(fmap);
return true;
}
void print_address(uint32_t address) {
if (!rawmode) {
printf("%08x : ", address);
} else {
printf(" ");
}
}
/*!
Gets and echoes the next byte from stdin and increments the global @c address;
if stdin is exhausted, prints an error and causes the program to exit with
code EXIT_FAILURE.
*/
unsigned int getbyte() {
const int byte = fgetc(stdin);
if (feof(stdin)) {
fprintf(stderr, "Unexpected end of input\n");
exit(EXIT_FAILURE);
}
++ address;
return byte;
}
/*!
Gets the next word from stdin and increments the global @c address twice;
if stdin is exhausted, prints an error and causes the program to exit with
code EXIT_FAILURE.
*/
int getword_noprint() {
int word = fgetc(stdin) << 8;
word |= fgetc(stdin);
if (feof(stdin)) {
fprintf(stderr, "Unexpected end of input\n");
exit(EXIT_FAILURE);
}
address += 2;
return word;
}
/*!
Gets and echoes the next word from stdin and increments the global @c address twice;
if stdin is exhausted, prints an error and causes the program to exit with
code EXIT_FAILURE.
*/
int getword() {
int word = getword_noprint();
if (!rawmode) {
printf("%04x ", word);
}
return word;
}
/*!
Prints the addressing mode @c mode, using @c reg and @c size, to @c out_s.
@param mode 0 to 12, indicating addressing mode.
@param size 0 = byte, 1 = word, 2 = long.
*/
void sprintmode(unsigned int mode, unsigned int reg, unsigned int size, char *out_s) {
const char ir[2] = {'W','L'}; /* for mode 6 */
switch(mode) {
case 0 : sprintf(out_s, "D%i", reg); break;
case 1 : sprintf(out_s, "A%i", reg); break;
case 2 : sprintf(out_s, "(A%i)", reg); break;
case 3 : sprintf(out_s, "(A%i)+", reg); break;
case 4 : sprintf(out_s, "-(A%i)", reg); break;
case 5 : /* reg + disp */
case 9 : { /* pcr + disp */
int32_t displacement = (int32_t) getword();
if (displacement >= 32768) displacement -= 65536;
if (mode == 5) {
sprintf(out_s, "%+i(A%i)", displacement, reg);
} else {
const uint32_t ldata = address - 2 + displacement;
if (!rawmode) {
sprintf(out_s, "%+i(PC) {$%08u}", displacement, ldata);
} else {
sprintf(out_s, "%+i(PC)", displacement);
}
}
} break;
case 6 : /* Areg with index + disp */
case 10 : {/* PC with index + disp */
const int data = getword(); /* index and displacement data */
int displacement = (data & 0x00FF);
if (displacement >= 128) displacement -= 256;
const int ireg = (data & 0x7000) >> 12;
const int itype = (data & 0x8000); /* == 0 is Dreg */
const int isize = (data & 0x0800) >> 11; /* == 0 is .W else .L */
if (mode == 6) {
if (itype == 0) {
sprintf(out_s, "%+i(A%i,D%i.%c)", displacement, reg, ireg, ir[isize]);
} else {
sprintf(out_s, "%+i(A%i,A%i.%c)", displacement, reg, ireg, ir[isize]);
}
} else { /* PC */
if (itype == 0) {
sprintf(out_s, "%+i(PC,D%i.%c)", displacement, ireg, ir[isize]);
} else {
sprintf(out_s, "%+i(PC,A%i.%c)", displacement, ireg, ir[isize]);
}
}
} break;
case 7 :
sprintf(out_s, "$0000%04x", getword());
break;
case 8 : {
const int data1 = getword();
const int data2 = getword();
sprintf(out_s, "$%04x%04x", data1, data2);
} break;
case 11 : {
const int data1 = getword();
switch(size) {
case 0 : sprintf(out_s, "#$%02x", (data1 & 0x00FF));
break;
case 1 : sprintf(out_s, "#$%04x", data1);
break;
case 2 : {
const int data2 = getword();
sprintf(out_s, "#$%04x%04x", data1, data2);
} break;
}
} break;
default : fprintf(stderr, "Mode out of range in sprintmode = %i\n", mode);
break;
}
}
/*!
Decodes the addressing mode from @c instruction.
@returns A mode in the range 0 to 11, if a valid addressing mode could be
determined; 12 otherwise.
*/
int getmode(int instruction) {
const int mode = (instruction & 0x0038) >> 3;
const int reg = instruction & 0x0007;
if (mode == 7) {
if (reg >= 5)
return 12; /* i.e. invalid */
return 7 + reg;
}
return mode;
}
void disasm(unsigned long int start, unsigned long int end) {
address = start;
char operand_s[100];
while (!feof(stdin) && (address < end)) {
print_address(address);
const uint32_t start_address = address;
const int word = getword();
bool decoded = false;
char opcode_s[50], operand_s[100];
for (int opnum = 1; opnum <= 87; ++opnum) {
if ((word & optab[opnum].and) == optab[opnum].xor) {
/* Diagnostic code */
diagnostic_printf("(%i) ",opnum);
switch(opnum) { /* opnum = 1..85 */
case 1 :
case 74 : { /* ABCD + SBCD */
const int sreg = word & 0x0007;
const int dreg = (word & 0x0E00) >> 9;
if (opnum == 1) {
sprintf(opcode_s, "ABCD");
} else {
sprintf(opcode_s, "SBCD");
}
if ((word & 0x0008) == 0) {
/* reg-reg */
sprintf(operand_s, "D%i,D%i", sreg, dreg);
} else {
/* mem-mem */
sprintf(operand_s, "-(A%i),-A(%i)", sreg, dreg);
}
decoded = true;
} break;
case 2 :
case 7 :
case 31 :
case 59 : /* ADD, AND, EOR, OR */
case 77 : { /* SUB */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
const int size = (word & 0x00C0) >> 6;
/* Diagnostic code */
diagnostic_printf("dmode = %i, dreg = %i, size = %i",dmode,dreg,size);
if (size == 3) break;
/*
if (dmode == 1) break;
*/
if ((opnum == 2) && (dmode == 1) && (size == 0)) break;
if ((opnum == 77) && (dmode == 1) && (size == 0)) break;
const int dir = (word & 0x0100) >> 8; /* 0 = dreg dest */
if ((opnum == 31) && (dir == 0)) break;
/* dir == 1 : Dreg is source */
if ((dir == 1) && (dmode >= 9)) break;
switch(opnum) {
case 2 : sprintf(opcode_s, "ADD.%c", size_arr[size]);
break;
case 7 : sprintf(opcode_s, "AND.%c", size_arr[size]);
break;
case 31 : sprintf(opcode_s, "EOR.%c", size_arr[size]);
break;
case 59 : sprintf(opcode_s, "OR.%c", size_arr[size]);
break;
case 77 : sprintf(opcode_s, "SUB.%c", size_arr[size]);
break;
}
char dest_s[50];
sprintmode(dmode, dreg, size, dest_s);
const int sreg = (word & 0x0E00) >> 9;
char source_s[50];
sprintf(source_s, "D%i", sreg);
/* reverse source & dest if dir == 0 */
if (dir != 0) {
sprintf(operand_s, "%s,%s", source_s, dest_s);
} else {
sprintf(operand_s, "%s,%s", dest_s, source_s);
}
decoded = true;
} break;
case 3 :
case 78 : { /* ADDA + SUBA */
const int smode = getmode(word);
const int sreg = word & 0x0007;
const int dreg = (word & 0x0E00) >> 9;
const int size = ((word & 0x0100) >> 8) + 1;
switch(opnum) {
case 3 : sprintf(opcode_s, "ADDA.%c", size_arr[size]);
break;
case 78 : sprintf(opcode_s, "SUBA.%c", size_arr[size]);
break;
}
char source_s[50];
sprintmode(smode, sreg, size, source_s);
sprintf(operand_s, "%s,A%i", source_s, sreg);
decoded = true;
} break;
case 4 :
case 8 :
case 26 :
case 32 :
case 60 :
case 79 : { /* ADDI, ANDI, CMPI, EORI, ORI, SUBI */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
const int size = (word & 0x00C0) >> 6;
if (size == 3) break;
if (dmode == 1) break;
if ((dmode == 9) || (dmode == 10)) break; /* Invalid */
if (dmode == 12) break;
if ((dmode == 11) && /* ADDI, CMPI, SUBI */
((opnum == 4) || (opnum == 26) || (opnum == 79))) break;
switch(opnum) {
case 4 : sprintf(opcode_s, "ADDI.%c", size_arr[size]);
break;
case 8 : sprintf(opcode_s, "ANDI.%c", size_arr[size]);
break;
case 26 : sprintf(opcode_s, "CMPI.%c", size_arr[size]);
break;
case 32 : sprintf(opcode_s, "EORI.%c", size_arr[size]);
break;
case 60 : sprintf(opcode_s, "ORI.%c", size_arr[size]);
break;
case 79 : sprintf(opcode_s, "SUBI.%c", size_arr[size]);
break;
}
const int data = getword();
char source_s[50];
switch(size) {
case 0 : sprintf(source_s, "#$%02X", (data & 0x00FF));
break;
case 1 : sprintf(source_s, "#$%04X", data);
break;
case 2 :
sprintf(source_s, "#$%04X%04X", data, getword());
break;
}
char dest_s[50];
if (dmode == 11) {
sprintf(dest_s, "SR");
} else {
sprintmode(dmode, dreg, size, dest_s);
}
sprintf(operand_s, "%s,%s", source_s, dest_s);
decoded = true;
} break;
case 5 :
case 80 : {/* ADDQ + SUBQ */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
const int size = (word & 0x00C0) >> 6;
if (size == 3) break;
if (dmode >= 9) break;
if ((size == 0) && (dmode == 1)) break;
if (opnum == 5) {
sprintf(opcode_s,"ADDQ.%c",size_arr[size]);
} else {
sprintf(opcode_s,"SUBQ.%c",size_arr[size]);
}
char dest_s[50];
sprintmode(dmode, dreg, size, dest_s);
const int count = (word & 0x0E00) >> 9;
sprintf(operand_s, "#%i,%s", count ? count : 8, dest_s);
decoded = true;
} break;
case 6 :
case 81 : /* ADDX + SUBX */
case 27 : { /* CMPM */
const int size = (word & 0x00C0) >> 6;
if (size == 3) break;
const int sreg = word & 0x0007;
const int dreg = (word & 0x0E00) >> 9;
switch(opnum) {
case 6 : sprintf(opcode_s, "ADDX.%c", size_arr[size]);
break;
case 81 : sprintf(opcode_s, "SUBX.%c", size_arr[size]);
break;
case 27 : sprintf(opcode_s, "CMPM.%c", size_arr[size]);
break;
}
if ((opnum != 27) && ((word & 0x0008) == 0)) {
/* reg-reg */
sprintf(operand_s,"D%i,D%i",sreg,dreg);
} else {
/* mem-mem */
sprintf(operand_s,"-(A%i),-(A%i)",sreg,dreg);
}
if (opnum == 27) {
sprintf(operand_s,"(A%i)+,(A%i)+",sreg,dreg);
}
decoded = true;
} break;
case 9 :
case 11 :
case 39 :
case 41 :
case 63 :
case 65 :
case 67 :
case 69 : { /* ASL, ASR, LSL, LSR, ROL, ROR, ROXL, ROXR */
const int dreg = word & 0x0007;
const int size = (word & 0x00C0) >> 6;
if (size == 3) break;
switch(opnum) {
case 9 : sprintf(opcode_s, "ASL.%c", size_arr[size]);
break;
case 11 : sprintf(opcode_s, "ASR.%c", size_arr[size]);
break;
case 39 : sprintf(opcode_s, "LSL.%c", size_arr[size]);
break;
case 41 : sprintf(opcode_s, "LSR.%c", size_arr[size]);
break;
case 63 : sprintf(opcode_s, "ROR.%c", size_arr[size]);
break;
case 65 : sprintf(opcode_s, "ROL.%c", size_arr[size]);
break;
case 67 : sprintf(opcode_s, "ROXL.%c", size_arr[size]);
break;
case 69 : sprintf(opcode_s, "ROXR.%c", size_arr[size]);
break;
}
int count = (word & 0x0E00) >> 9;
if (((word & 0x0020) >> 5) == 0) { /* imm */
if (count == 0) count = 8;
sprintf(operand_s, "#%i,D%i", count, (word & 0x0007));
} else { /* count in dreg */
sprintf(operand_s, "D%i,D%i", count, (word & 0x0007));
}
decoded = true;
} break;
case 10 :
case 12 :
case 40 :
case 42 :
case 64 :
case 66 :
case 68 : /* Memory-to-memory */
case 70 : { /* ASL, ASR, LSL, LSR, ROL, ROR, ROXL, ROXR */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
if ((dmode <= 1) || (dmode >= 9)) break; /* Invalid */
switch(opnum) {
case 10 : sprintf(opcode_s,"ASL");
break;
case 12 : sprintf(opcode_s,"ASR");
break;
case 40 : sprintf(opcode_s,"LSL");
break;
case 42 : sprintf(opcode_s,"LSR");
break;
case 64 : sprintf(opcode_s,"ROR");
break;
case 66 : sprintf(opcode_s,"ROL");
break;
case 68 : sprintf(opcode_s,"ROXL");
break;
case 70 : sprintf(opcode_s,"ROXR");
break;
}
sprintmode(dmode, dreg, 0, operand_s);
decoded = true;
} break;
case 13 : {/* Bcc */
const int cc = (word & 0x0F00) >> 8;
sprintf(opcode_s, "%s", bra_tab[cc]);
int offset = (word & 0x00FF);
if (offset != 0) {
if (offset >= 128) offset -= 256;
if (!rawmode) {
sprintf(operand_s, "$%08x", address + offset);
} else {
sprintf(operand_s, "*%+d", offset);
}
} else {
offset = getword();
if (offset >= 32768l) offset -= 65536l;
if (!rawmode) {
sprintf(operand_s, "$%08x" , address - 2 + offset);
} else {
sprintf(operand_s, "*%+d", offset);
}
}
decoded = true;
} break;
case 14 :
case 15 :
case 16 :
case 17 : /* BCHG + BCLR */
case 18 :
case 19 : /* BSET */
case 20 :
case 21 : {/* BTST */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
if (dmode == 1) break;
if (dmode >= 11) break;
if ((opnum < 20) && (dmode >= 9)) break;
const int sreg = (word & 0x0E00) >> 9;
char source_s[50];
switch(opnum) {
case 14 : /* BCHG_DREG */
sprintf(opcode_s, "BCHG");
sprintf(source_s, "D%i", sreg);
break;
case 15 : {/* BCHG_IMM */
sprintf(opcode_s, "BCHG");
const int data = getword() & 0x002F;
sprintf(source_s, "#%i", data);
} break;
case 16 : /* BCLR_DREG */
sprintf(opcode_s, "BCLR");
sprintf(source_s, "D%i", sreg);
break;
case 17 : {/* BCLR_IMM */
sprintf(opcode_s, "BCLR");
const int data = getword() & 0x002F;
sprintf(source_s, "#%i", data);
} break;
case 18 : /* BSET_DREG */
sprintf(opcode_s, "BSET");
sprintf(source_s, "D%i", sreg);
break;
case 19 : { /* BSET_IMM */
sprintf(opcode_s, "BSET");
const int data = getword() & 0x002F;
sprintf(source_s, "#%i", data);
} break;
case 20 : /* BTST_DREG */
sprintf(opcode_s,"BTST");
sprintf(source_s, "D%i", sreg);
break;
case 21 : {/* BTST_IMM */
sprintf(opcode_s,"BTST");
const int data = getword() & 0x002F;
sprintf(source_s, "#%i", data);
} break;
}
char dest_s[50];
sprintmode(dmode, dreg, 0, dest_s);
sprintf(operand_s, "%s,%s", source_s, dest_s);
decoded = true;
} break;
case 22 : /* CHK */
case 29 :
case 30 :
case 52 :
case 53 : /* DIVS, DIVU, MULS, MULU */
case 24 : {/* CMP */
const int smode = getmode(word);
if ((smode == 1) && (opnum != 24)) break;
if (smode >= 12) break;
const int sreg = word & 0x0007;
const int dreg = (word & 0x0E00) >> 9;
int size;
if (opnum == 24) {
size = (word & 0x00C0) >> 6;
} else {
size = 1; /* WORD */
}
if (size == 3) break;
switch(opnum) {
case 22 : /* CHK */
sprintf(opcode_s, "CHK");
break;
case 24 : /* CMP */
sprintf(opcode_s, "CMP.%c", size_arr[size]);
break;
case 29 : /* DIVS */
sprintf(opcode_s, "DIVS");
break;
case 30 : /* DIVU */
sprintf(opcode_s, "DIVU");
break;
case 52 : /* MULS */
sprintf(opcode_s, "MULS");
break;
case 53 : /* MULU */
sprintf(opcode_s, "MULU");
break;
}
char source_s[50];
sprintmode(smode, sreg, size, source_s);
sprintf(operand_s, "%s,D%i", source_s, dreg);
decoded = true;
} break;
case 23 : {/* CLR */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
if ((dmode == 1) || (dmode >= 9)) break; /* Invalid */
const int size = (word & 0x00C0) >> 6;
if (size == 3) break;
sprintf(opcode_s, "CLR.%c", size_arr[size]);
sprintmode(dmode, dreg, size, operand_s);
decoded = true;
} break;
case 25 : {/* CMPA */
const int smode = getmode(word);
const int sreg = word & 0x0007;
const int areg = (word & 0x0E00) >> 9;
const int size = ((word & 0x0100) >> 8) + 1;
sprintf(opcode_s, "CMPA.%c", size_arr[size]);
char source_s[50];
sprintmode(smode, sreg, size, source_s);
sprintf(operand_s, "%s,A%i", source_s, areg);
decoded = true;
} break;
case 28 : { /* DBcc */
const int cc = (word & 0x0F00) >> 8;
sprintf(opcode_s, "D%s", bra_tab[cc]);
if (cc == 0) sprintf(opcode_s, "DBT");
if (cc == 1) sprintf(opcode_s, "DBF");
int offset = getword();
if (offset >= 32768) offset -= 65536;
const int dreg = word & 0x0007;
sprintf(operand_s, "D%i,$%08x", dreg, address - 2 + offset);
decoded = true;
} break;
case 33 : { /* EXG */
const int dmode = (word & 0x00F8) >> 3;
/* 8 - Both Dreg
9 - Both Areg
17 - Dreg + Areg */
if ((dmode != 8) && (dmode != 9) && (dmode != 17)) break;
const int dreg = word & 0x0007;
const int areg = (word & 0x0E00) >> 9;
sprintf(opcode_s, "EXG");
switch(dmode) {
case 8 : sprintf(operand_s, "D%i,D%i", dreg, areg);
break;
case 9 : sprintf(operand_s, "A%i,A%i", dreg, areg);
break;
case 17 : sprintf(operand_s, "D%i,A%i", dreg, areg);
break;
}
decoded = true;
} break;
case 34 : {/* EXT */
const int dreg = word & 0x0007;
const int size = ((word & 0x0040) >> 6) + 1;
sprintf(opcode_s, "EXT.%c", size_arr[size]);
sprintf(operand_s, "D%i", dreg);
decoded = true;
} break;
case 35 :
case 36 : {/* JMP + JSR */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
if (dmode <= 1) break;
if ((dmode == 3) || (dmode == 4)) break;
if (dmode >= 11) break; /* Invalid */
switch(opnum) {
case 35 : sprintf(opcode_s, "JMP");
break;
case 36 : sprintf(opcode_s, "JSR");
break;
}
sprintmode(dmode, dreg, 0, operand_s);
decoded = true;
} break;
case 37 : {/* LEA */
const int smode = getmode(word);
if ((smode == 0) || (smode == 1)) break;
if ((smode == 3) || (smode == 4)) break;
if (smode >= 11) break;
const int sreg = word & 0x0007;
sprintf(opcode_s, "LEA");
char source_s[50];
sprintmode(smode, sreg, 0, source_s);
const int dreg = (word & 0x0E00) >> 9;
sprintf(operand_s, "%s,A%i", source_s, dreg);
decoded = true;
} break;
case 38 : {/* LINK */
const int areg = word & 0x0007;
int offset = getword();
if (offset >= 32768) offset -= 65536;
sprintf(opcode_s, "LINK");
sprintf(operand_s, "A%i,#%+i", areg, offset);
decoded = true;
} break;
case 43 : {/* MOVE */
const int smode = getmode(word);
const int data = ((word & 0x0E00) >> 9) | ((word & 0x01C0) >> 3);
const int dmode = getmode(data);
const int sreg = word & 0x0007;
const int dreg = data & 0x0007;
int size = (word & 0x3000) >> 12; /* 1=B, 2=L, 3=W */
if (size == 0) break;
switch(size) {
case 1 : size = 0;
break;
case 2 : size = 2;
break;
case 3 : size = 1;
break;
}
/* 0=B, 1=W, 2=L */
/*
printf("smode = %i dmode = %i ",smode,dmode);
printf("sreg = %i dreg = %i \n",sreg,dreg);
*/
/* check for illegal modes */
// smode=1, size=1 is legal; 36 0d
// if ((smode == 1) && (size == 1)) break;
// smode=9 is legal; 2d 40 ff ec
// smode=10 is legal; 30 3b 00 00
// if ((smode == 9) || (smode == 10)) break;
if (smode > 11) break;
if (dmode == 1) break;
if (dmode >= 9) break;
sprintf(opcode_s,"MOVE.%c",size_arr[size]);
char source_s[50], dest_s[50];
sprintmode(smode, sreg, size, source_s);
sprintmode(dmode, dreg, size, dest_s);
sprintf(operand_s, "%s,%s ", source_s, dest_s);
decoded = true;
} break;
case 44 : /* MOVE to CCR */
case 45 : {/* MOVE to SR */
const int smode = getmode(word);
const int sreg = word & 0x0007;
const int size = 1; /* WORD */
if (smode == 1) break;
if (smode >= 12) break;
sprintf(opcode_s, "MOVE.W");
char source_s[50];
sprintmode(smode, sreg, size, source_s);
if (opnum == 44) {
sprintf(operand_s, "%s,CCR", source_s);
} else {
sprintf(operand_s, "%s,SR", source_s);
}
decoded = true;
} break;
case 46 : {/* MOVE from SR */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
const int size = 1; /* WORD */
if (dmode == 1) break;
if (dmode >= 9) break;
sprintf(opcode_s, "MOVE.W");
char dest_s[50];
sprintmode(dmode, dreg, size, dest_s);
sprintf(operand_s, "SR,%s", dest_s);
decoded = true;
} break;
case 47 : { /* MOVE USP */
const int sreg = word & 0x0007;
sprintf(opcode_s, "MOVE");
if ((word & 0x0008) == 0) {
/* to USP */
sprintf(operand_s, "A%i,USP", word & 0x0007);
} else {
/* from USP */
sprintf(operand_s, "USP,A%i", word & 0x0007);
}
decoded = true;
} break;
case 48 : {/* MOVEA */
const int smode = getmode(word);
const int sreg = word & 0x0007;
int size = (word & 0x3000) >> 12;
/* 2 = L, 3 = W */
if (size <= 1) break;
if (size == 3) size = 1;
/* 1 = W, 2 = L */
const int dreg = (word & 0x0e00) >> 9;
sprintf(opcode_s, "MOVEA.%c", size_arr[size]);
char source_s[50];
sprintmode(smode, sreg, size, source_s);
sprintf(operand_s, "%s,A%i", source_s, dreg);
decoded = true;
} break;
case 49 : {/* MOVEM */
const int dmode = getmode(word);
const int dreg = word & 0x0007;
const int size = ((word & 0x0040) >> 6) + 1;
if ((dmode == 0) || (dmode == 1)) break;
if (dmode >= 9) break;
const int dir = (word & 0x0400) >> 10; /* 1 == from mem */
if ((dir == 0) && (dmode == 3)) break;
if ((dir == 1) && (dmode == 4)) break;
int data = getword();
if (dmode == 4) { /* dir == 0 if dmode == 4 !! */
/* reverse bits in data */
int temp = data;
data = 0;
for (int i = 0; i <= 15; ++i) {
data = (data >> 1) | (temp & 0x8000);
temp = temp << 1;
}
}
char source_s[50] = "";
char dest_s[50] = "";
/**** DATA LIST ***/
int rlist[11];
for (int i = 0 ; i <= 7; ++i) {
rlist[i + 1] = (data >> i) & 0x0001;
}
rlist[0] = 0;
rlist[9] = 0;
rlist[10] = 0;
for (int i = 1; i <= 8 ; ++i) {
if ((rlist[i-1] == 0) && (rlist[i] == 1) &&
(rlist[i+1] == 1) && (rlist[i+2] == 1)) {
/* first reg in list */
char temp_s[50];
sprintf(temp_s, "D%i-", i - 1);
strcat(source_s, temp_s);
}
if ((rlist[i] == 1) && (rlist[i+1] == 0)) {
char temp_s[50];
sprintf(temp_s, "D%i/", i-1);
strcat(source_s, temp_s);
}