-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dasm6800.cpp
1386 lines (1280 loc) · 54.4 KB
/
Dasm6800.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
/***************************************************************************
* dasmfw -- Disassembler Framework *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
***************************************************************************/
/* Copyright (C) Hermann Seib *
* The 6800 disassembler engine is based on dasm09, last found at *
* http://koti.mbnet.fi/~atjs/mc6809/Disassembler/dasm09.TGZ , so *
* Parts Copyright (C) 2000 Arto Salmi *
***************************************************************************/
/*****************************************************************************/
/* Dasm6800.cpp : 6800 disassembler implementation */
/*****************************************************************************/
#include "Dasm6800.h"
/*****************************************************************************/
/* Create6800 : create an 6800 processor handler */
/*****************************************************************************/
static Disassembler *Create6800(Application *pApp)
{
Disassembler *pDasm = new Dasm6800(pApp);
if (pDasm) pDasm->Setup();
return pDasm;
}
/*****************************************************************************/
/* Auto-registration */
/*****************************************************************************/
static bool bRegistered[] =
{
RegisterDisassembler("6800", Create6800),
RegisterDisassembler("6802", Create6800),
RegisterDisassembler("6808", Create6800),
};
/*===========================================================================*/
/* Dasm6800 class members */
/*===========================================================================*/
/*****************************************************************************/
/* m6800_codes : table of all 6800 instruction bytes and types */
/*****************************************************************************/
CMatrixEntry Dasm6800::m6800_codes[256] =
{
{_ill ,_nom}, {_nop ,_imp}, {_ill ,_nom}, {_ill ,_nom}, /* 00..03 */
{_ill ,_nom}, {_ill ,_nom}, {_tap ,_imp}, {_tpa ,_imp}, /* 04..07 */
{_inx ,_imp}, {_dex ,_imp}, {_clv ,_imp}, {_sev ,_imp}, /* 08..0B */
{_clc ,_imp}, {_sec ,_imp}, {_cli ,_imp}, {_sei ,_imp}, /* 0C..0F */
{_sba ,_imp}, {_cba ,_imp}, {_ill ,_nom}, {_ill ,_nom}, /* 10..13 */
{_ill ,_nom}, {_ill ,_nom}, {_tab ,_imp}, {_tba ,_imp}, /* 14..17 */
{_ill ,_nom}, {_daa ,_imp}, {_ill ,_nom}, {_aba ,_imp}, /* 18..1B */
{_ill ,_nom}, {_ill ,_nom}, {_ill ,_nom}, {_ill ,_nom}, /* 1C..1F */
{_bra ,_reb}, {_ill ,_nom}, {_bhi ,_reb}, {_bls ,_reb}, /* 20..23 */
{_bcc ,_reb}, {_bcs ,_reb}, {_bne ,_reb}, {_beq ,_reb}, /* 24..27 */
{_bvc ,_reb}, {_bvs ,_reb}, {_bpl ,_reb}, {_bmi ,_reb}, /* 28..2B */
{_bge ,_reb}, {_blt ,_reb}, {_bgt ,_reb}, {_ble ,_reb}, /* 2C..2F */
{_tsx ,_imp}, {_ins ,_imp}, {_pula ,_imp}, {_pulb ,_imp}, /* 30..33 */
{_des ,_imp}, {_txs ,_imp}, {_psha ,_imp}, {_pshb ,_imp}, /* 34..37 */
{_ill ,_nom}, {_rts ,_imp}, {_ill ,_nom}, {_rti ,_imp}, /* 38..3B */
{_ill ,_nom}, {_ill ,_nom}, {_wai ,_imp}, {_swi ,_imp}, /* 3C..3F */
{_nega ,_imp}, {_ill ,_nom}, {_ill ,_nom}, {_coma ,_imp}, /* 40..43 */
{_lsra ,_imp}, {_ill ,_nom}, {_rora ,_imp}, {_asra ,_imp}, /* 44..47 */
{_asla ,_imp}, {_rola ,_imp}, {_deca ,_imp}, {_ill ,_nom}, /* 48..4B */
{_inca ,_imp}, {_tsta ,_imp}, {_ill ,_nom}, {_clra ,_imp}, /* 4C..4F */
{_negb ,_imp}, {_ill ,_nom}, {_ill ,_nom}, {_comb ,_imp}, /* 50..53 */
{_lsrb ,_imp}, {_ill ,_nom}, {_rorb ,_imp}, {_asrb ,_imp}, /* 54..57 */
{_aslb ,_imp}, {_rolb ,_imp}, {_decb ,_imp}, {_ill ,_nom}, /* 58..5B */
{_incb ,_imp}, {_tstb ,_imp}, {_ill ,_nom}, {_clrb ,_imp}, /* 5C..5F */
{_neg ,_ix8}, {_ill ,_nom}, {_ill ,_nom}, {_com ,_ix8}, /* 60..63 */
{_lsr ,_ix8}, {_ill ,_nom}, {_ror ,_ix8}, {_asr ,_ix8}, /* 64..67 */
{_asl ,_ix8}, {_rol ,_ix8}, {_dec ,_ix8}, {_ill ,_nom}, /* 68..6B */
{_inc ,_ix8}, {_tst ,_ix8}, {_jmp ,_ix8}, {_clr ,_ix8}, /* 6C..6F */
{_neg ,_ext}, {_ill ,_nom}, {_ill ,_nom}, {_com ,_ext}, /* 70..73 */
{_lsr ,_ext}, {_ill ,_nom}, {_ror ,_ext}, {_asr ,_ext}, /* 74..77 */
{_asl ,_ext}, {_rol ,_ext}, {_dec ,_ext}, {_ill ,_nom}, /* 78..7B */
{_inc ,_ext}, {_tst ,_ext}, {_jmp ,_ext}, {_clr ,_ext}, /* 7C..7F */
{_suba ,_imb}, {_cmpa ,_imb}, {_sbca ,_imb}, {_ill ,_nom}, /* 80..83 */
{_anda ,_imb}, {_bita ,_imb}, {_lda ,_imb}, {_ill ,_nom}, /* 84..87 */
{_eora ,_imb}, {_adca ,_imb}, {_ora ,_imb}, {_adda ,_imb}, /* 88..8B */
{_cpx ,_imw}, {_bsr ,_reb}, {_lds ,_imw}, {_ill ,_nom}, /* 8C..8F */
{_suba ,_dir}, {_cmpa ,_dir}, {_sbca ,_dir}, {_ill ,_nom}, /* 90..93 */
{_anda ,_dir}, {_bita ,_dir}, {_lda ,_dir}, {_sta ,_dir}, /* 94..97 */
{_eora ,_dir}, {_adca ,_dir}, {_ora ,_dir}, {_adda ,_dir}, /* 98..9B */
{_cpx ,_dir}, {_ill ,_nom}, {_lds ,_dir}, {_sts ,_dir}, /* 9C..9F */
{_suba ,_ix8}, {_cmpa ,_ix8}, {_sbca ,_ix8}, {_ill ,_nom}, /* A0..A3 */
{_anda ,_ix8}, {_bita ,_ix8}, {_lda ,_ix8}, {_sta ,_ix8}, /* A4..A7 */
{_eora ,_ix8}, {_adca ,_ix8}, {_ora ,_ix8}, {_adda ,_ix8}, /* A8..AB */
{_cpx ,_ix8}, {_jsr ,_ix8}, {_lds ,_ix8}, {_sts ,_ix8}, /* AC..AF */
{_suba ,_ext}, {_cmpa ,_ext}, {_sbca ,_ext}, {_ill ,_nom}, /* B0..B3 */
{_anda ,_ext}, {_bita ,_ext}, {_lda ,_ext}, {_sta ,_ext}, /* B4..B7 */
{_eora ,_ext}, {_adca ,_ext}, {_ora ,_ext}, {_adda ,_ext}, /* B8..BB */
{_cpx ,_ext}, {_jsr ,_ext}, {_lds ,_ext}, {_sts ,_ext}, /* BC..BF */
{_subb ,_imb}, {_cmpb ,_imb}, {_sbcb ,_imb}, {_ill ,_nom}, /* C0..C3 */
{_andb ,_imb}, {_bitb ,_imb}, {_ldb ,_imb}, {_ill ,_nom}, /* C4..C7 */
{_eorb ,_imb}, {_adcb ,_imb}, {_orb ,_imb}, {_addb ,_imb}, /* C8..CB */
{_ill ,_nom}, {_ill ,_nom}, {_ldx ,_imw}, {_ill ,_nom}, /* CC..CF */
{_subb ,_dir}, {_cmpb ,_dir}, {_sbcb ,_dir}, {_ill ,_nom}, /* D0..D3 */
{_andb ,_dir}, {_bitb ,_dir}, {_ldb ,_dir}, {_stb ,_dir}, /* D4..D7 */
{_eorb ,_dir}, {_adcb ,_dir}, {_orb ,_dir}, {_addb ,_dir}, /* D8..DB */
{_ill ,_nom}, {_ill ,_nom}, {_ldx ,_dir}, {_stx ,_dir}, /* DC..DF */
{_subb ,_ix8}, {_cmpb ,_ix8}, {_sbcb ,_ix8}, {_ill ,_nom}, /* E0..E3 */
{_andb ,_ix8}, {_bitb ,_ix8}, {_ldb ,_ix8}, {_stb ,_ix8}, /* E4..E7 */
{_eorb ,_ix8}, {_adcb ,_ix8}, {_orb ,_ix8}, {_addb ,_ix8}, /* E8..EB */
{_ill ,_nom}, {_ill ,_nom}, {_ldx ,_ix8}, {_stx ,_ix8}, /* EC..EF */
{_subb ,_ext}, {_cmpb ,_ext}, {_sbcb ,_ext}, {_ill ,_nom}, /* F0..F3 */
{_andb ,_ext}, {_bitb ,_ext}, {_ldb ,_ext}, {_stb ,_ext}, /* F4..F7 */
{_eorb ,_ext}, {_adcb ,_ext}, {_orb ,_ext}, {_addb ,_ext}, /* F8..FB */
{_ill ,_nom}, {_ill ,_nom}, {_ldx ,_ext}, {_stx ,_ext}, /* FC..FF */
};
/*****************************************************************************/
/* opcodes : 6800 opcodes array for initialization */
/*****************************************************************************/
OpCode Dasm6800::opcodes[mnemo6800_count] =
{
{ "EQU", Data }, /* _equ */
{ "RMB", Data }, /* _rmb */
{ "FCB", Data }, /* _fcb */
{ "FDB", Data }, /* _fcb */
{ "FCC", Data }, /* _fcc */
{ "FCS", Data }, /* _fcs */
{ "ORG", Data }, /* _org */
{ "PHASE", Data }, /* _phase */
{ "DEPHASE", Data }, /* _dephase */
{ "END", Data }, /* _end */
{ "IF", Data }, /* _if */
{ "ELSE", Data }, /* _else */
{ "ENDIF", Data }, /* _endif */
{ "???", Data }, /* _ill */
{ "ABA", Data }, /* _aba */
{ "ADCA", Data }, /* _adca */
{ "ADCB", Data }, /* _adcb */
{ "ADDA", Data }, /* _adda */
{ "ADDB", Data }, /* _addb */
{ "ANDA", Data }, /* _anda */
{ "ANDB", Data }, /* _andb */
{ "ASLA", Data }, /* _asla */
{ "ASLB", Data }, /* _aslb */
{ "ASL", Data }, /* _asl */
{ "ASRA", Data }, /* _asra */
{ "ASRB", Data }, /* _asrb */
{ "ASR", Data }, /* _asr */
{ "BCC", Code }, /* _bcc */
{ "BCS", Code }, /* _bcs */
{ "BEQ", Code }, /* _beq */
{ "BGE", Code }, /* _bge */
{ "BGT", Code }, /* _bgt */
{ "BHI", Code }, /* _bhi */
{ "BITA", Data }, /* _bita */
{ "BITB", Data }, /* _bitb */
{ "BLE", Code }, /* _ble */
{ "BLS", Code }, /* _bls */
{ "BLT", Code }, /* _blt */
{ "BMI", Code }, /* _bmi */
{ "BNE", Code }, /* _bne */
{ "BPL", Code }, /* _bpl */
{ "BRA", Code }, /* _bra */
{ "BSR", Code }, /* _bsr */
{ "BVC", Code }, /* _bvc */
{ "BVS", Code }, /* _bvs */
{ "CBA", Data }, /* _cba */
{ "CLI", Data }, /* _cli */
{ "CLRA", Data }, /* _clra */
{ "CLRB", Data }, /* _clrb */
{ "CLR", Data }, /* _clr */
{ "CLC", Data }, /* _clc */
{ "CLV", Data }, /* _clv */
{ "CMPA", Data }, /* _cmpa */
{ "CMPB", Data }, /* _cmpb */
{ "COMA", Data }, /* _coma */
{ "COMB", Data }, /* _comb */
{ "COM", Data }, /* _com */
{ "CPX", Data }, /* _cpx */
{ "DAA", Data }, /* _daa */
{ "DECA", Data }, /* _deca */
{ "DECB", Data }, /* _decb */
{ "DECD", Data }, /* _decd */
{ "DEC", Data }, /* _dec */
{ "DES", Data }, /* _des */
{ "DEX", Data }, /* _dex */
{ "EORA", Data }, /* _eora */
{ "EORB", Data }, /* _eorb */
{ "INCA", Data }, /* _inca */
{ "INCB", Data }, /* _incb */
{ "INCD", Data }, /* _incd */
{ "INC", Data }, /* _inc */
{ "INS", Data }, /* _ins */
{ "INX", Data }, /* _inx */
{ "JMP", Code }, /* _jmp */
{ "JSR", Code }, /* _jsr */
{ "LDAA", Data }, /* _lda */
{ "LDAB", Data }, /* _ldb */
{ "LDS", Data }, /* _lds */
{ "LDX", Data }, /* _ldx */
{ "LSRA", Data }, /* _lsra */
{ "LSRB", Data }, /* _lsrb */
{ "LSR", Data }, /* _lsr */
{ "NEGA", Data }, /* _nega */
{ "NEGB", Data }, /* _negb */
{ "NEG", Data }, /* _neg */
{ "NOP", Data }, /* _nop */
{ "ORAA", Data }, /* _ora */
{ "ORAB", Data }, /* _orb */
{ "PSHA", Data }, /* _psha */
{ "PSHB", Data }, /* _pshb */
{ "PULA", Data }, /* _pula */
{ "PULB", Data }, /* _pulb */
{ "ROLA", Data }, /* _rola */
{ "ROLB", Data }, /* _rolb */
{ "ROL", Data }, /* _rol */
{ "RORA", Data }, /* _rora */
{ "RORB", Data }, /* _rorb */
{ "ROR", Data }, /* _ror */
{ "RTI", Data }, /* _rti */
{ "RTS", Data }, /* _rts */
{ "SBA", Data }, /* _sba */
{ "SBCA", Data }, /* _sbca */
{ "SBCB", Data }, /* _sbcb */
{ "SEC", Data }, /* _sec */
{ "SEI", Data }, /* _sei */
{ "SEV", Data }, /* _sev */
{ "STAA", Data }, /* _sta */
{ "STAB", Data }, /* _stb */
{ "STS", Data }, /* _sts */
{ "STX", Data }, /* _stx */
{ "SUBA", Data }, /* _suba */
{ "SUBB", Data }, /* _subb */
{ "SWI", Data }, /* _swi */
{ "TAB", Data }, /* _tab */
{ "TAP", Data }, /* _tap */
{ "TBA", Data }, /* _tba */
{ "TPA", Data }, /* _tpa */
{ "TSTA", Data }, /* _tsta */
{ "TSTB", Data }, /* _tstb */
{ "TST", Data }, /* _tst */
{ "TSX", Data }, /* _tsx */
{ "TXS", Data }, /* _txs */
{ "WAI", Data }, /* _wai */
// Convenience mnemonics
{ "ASLD", Data }, /* _asld */
{ "ASRD", Data }, /* _asrd */
{ "CLRD", Data }, /* _clrd */
{ "LSRD", Data }, /* _lsrd */
};
/*****************************************************************************/
/* regnames : 6800 register names array for initialization */
/*****************************************************************************/
const char *Dasm6800::regnames[reg6800_count] =
{
"?", /* _unk */
"A", /* _a */
"B", /* _b */
"X", /* _x */
"S", /* _s */
"PC", /* _pc */
"CC", /* _cc */
};
/*****************************************************************************/
/* Dasm6800 : constructor */
/*****************************************************************************/
Dasm6800::Dasm6800(Application *pApp)
: Disassembler(pApp)
{
codes = m6800_codes;
useConvenience = true;
showIndexedModeZeroOperand = false;
dirpage = 0; // ... and that's all it can be until the 6809
useFCC = true;
#if RB_VARIANT
forceExtendedAddr = false;
forceDirectAddr = false;
closeCC = true;
#else
forceExtendedAddr = true;
forceDirectAddr = true;
closeCC = false;
#endif
useDPLabels = false;
textDirectAddr = "p-<";
textExtendedAddr = "p->";
int i;
mnemo.resize(mnemo6800_count); /* set up mnemonics table */
for (i = 0; i < mnemo6800_count; i++)
mnemo[i] = opcodes[i];
regname.resize(reg6800_count); /* set up register name table */
for (i = 0; i < reg6800_count; i++)
regname[i] = regnames[i];
// set up options table
// class uses one generic option setter/getter pair (not mandatory)
AddOption("conv", "{off|on}\tUse convenience macros",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("showzero", "{off|on}\tdo not omit indexed-mode operands of $00",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("closecc", "{off|on}\tadd closing delimiter to char constants",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("fcc", "{off|on}\tuse FCC to define data",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("dplabel", "{off|on}\tinterpret single-byte data as direct page labels",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("forceaddr", "{off|on}\tuse forced addressing where necessary",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("forcediraddr","{string}\tstring pattern to use for forced direct addressing",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
AddOption("forceextaddr","{string}\tstring pattern to use for forced extended addressing",
static_cast<PSetter>(&Dasm6800::Set6800Option),
static_cast<PGetter>(&Dasm6800::Get6800Option));
}
/*****************************************************************************/
/* ~Dasm6800 : destructor */
/*****************************************************************************/
Dasm6800::~Dasm6800(void)
{
}
/*****************************************************************************/
/* Set6800Option : sets a disassembler option */
/*****************************************************************************/
int Dasm6800::Set6800Option(string lname, string value)
{
bool bnValue = true; /* default to "on" */
bool bIsBool = ParseBool(value, bnValue);
if (lname == "conv")
{
useConvenience = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "showzero")
{
showIndexedModeZeroOperand = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "closecc")
{
closeCC = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "fcc")
{
useFCC = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "dplabel")
{
useDPLabels = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "forceaddr")
{
forceExtendedAddr = forceDirectAddr = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "forcediraddr")
textDirectAddr = value;
else if (lname == "forceextaddr")
textExtendedAddr = value;
else
return 0; /* only name consumed */
// should never happen ... but better safe than sorry
return 1; /* name and value consumed */
}
/*****************************************************************************/
/* Get6800Option : retrieves a disassembler option */
/*****************************************************************************/
string Dasm6800::Get6800Option(string lname)
{
if (lname == "conv")
return useConvenience ? "on" : "off";
else if (lname == "showzero")
return showIndexedModeZeroOperand ? "on" : "off";
else if (lname == "closecc")
return closeCC ? "on" : "off";
else if (lname == "fcc")
return useFCC ? "on" : "off";
else if (lname == "dplabel")
return useDPLabels ? "on" : "off";
else if (lname == "forceaddr")
return (forceExtendedAddr || forceDirectAddr) ? "on" : "off";
else if (lname == "forcediraddr")
return textDirectAddr;
else if (lname == "forceextaddr")
return textExtendedAddr;
return "";
}
/*****************************************************************************/
/* SFlexRecord : a record in a binary FLEX(9) disk file */
/*****************************************************************************/
#pragma pack(1)
struct SFlexRecord
{
uint8_t bSOI; /* start of record indicator */
uint8_t bLoadAddress[2]; /* Hi/Lo byte of load address */
uint8_t bDataLen; /* length of data record */
uint8_t bData[255]; /* data record */
int IsTransferAddress() { return (bSOI == 0x16); }
int IsRecord() { return (bSOI == 0x02); }
int GetSize() { return bDataLen; }
adr_t GetLoadAddress() { return (((adr_t)(bLoadAddress[0])) << 8) | bLoadAddress[1]; }
uint8_t *GetData() { return bData; }
};
#pragma pack()
/*****************************************************************************/
/* ReadFlexRecord : read one record of a FLEX9 binary */
/*****************************************************************************/
static bool ReadFlexRecord(FILE *f, SFlexRecord *pRecord)
{
int nCurPos = ftell(f);
uint8_t bCur = 0;
int i;
while (!bCur) /* read 1st byte, skipping 0 bytes */
if (!fread(&bCur, 1, 1, f))
return false;
switch (bCur) /* OK, so what's that? */
{
case 0x02 : /* Start of Record Indicator ? */
{
pRecord->bSOI = bCur;
if ((!fread(pRecord->bLoadAddress, 2, 1, f)) ||
(!fread(&pRecord->bDataLen, 1, 1, f)))
return false;
for (i = 0; i < pRecord->bDataLen; i++)
if (!fread(&pRecord->bData[i], 1, 1, f))
return false;
}
break;
case 0x16 : /* Transfer Address ? */
pRecord->bSOI = bCur;
if (!fread(pRecord->bLoadAddress, 2, 1, f))
return false;
break;
default :
fseek(f, nCurPos, SEEK_SET); /* seek back */
return false;
}
return true;
}
/*****************************************************************************/
/* LoadFlex : loads a FLEX(9) binary executable */
/*****************************************************************************/
bool Dasm6800::LoadFlex(FILE *f, string &sLoadType)
{
struct SFlexRecord rec;
int nCurPos = ftell(f);
int nRecs = 0;
bool bExecutable = false;
adr_t fbegin = GetHighestCodeAddr(), fend = GetLowestCodeAddr();
adr_t i;
int c = fgetc(f); /* fetch first byte to decide */
if (c == EOF) /* whether it may be a FLEX(9) binary*/
return false;
ungetc(c, f);
if (c != 0 && c != 0x02 && c != 0x16)
return false;
while (ReadFlexRecord(f, &rec))
{
adr_t nStart = rec.GetLoadAddress();
adr_t nEnd = nStart + rec.GetSize() - 1;
nRecs++;
if (nStart < fbegin)
fbegin = nStart;
if (nEnd > fend)
fend = nEnd;
if (rec.IsRecord() && rec.GetSize())
{
AddMemory(nStart, rec.GetSize(), Code, rec.GetData());
for (i = nStart; i <= nEnd; i++) /* mark area as used */
{
bool bUsed = ((begin == NO_ADDRESS || i >= begin) &&
(end == NO_ADDRESS || i <= end));
SetCellUsed(i, bUsed);
SetDisplay(i, defaultDisplay);
}
}
else if (rec.IsTransferAddress())
{
bExecutable = true;
load = rec.GetLoadAddress();
}
}
if (fgetc(f) != EOF) /* if not read through the whole file*/
{
for (i = fbegin; i <= fend; i++) /* mark area as UNused */
SetCellUsed(i, false);
nRecs = 0; /* this ain't no valid FLEX file */
}
fseek(f, nCurPos, SEEK_SET); /* reset position for next filetype */
if (nRecs > 0)
{
offset = fend + 1;
sLoadType = "FLEX";
}
(void)bExecutable; // unused ATM - "load" is enough
return (nRecs > 0);
}
/*****************************************************************************/
/* LoadFile : loads an opened file */
/*****************************************************************************/
bool Dasm6800::LoadFile(string filename, FILE *f, string &sLoadType, int interleave, int bus)
{
return LoadFlex(f, sLoadType) || // FLEX9 files need no interleave nor bus
Disassembler::LoadFile(filename, f, sLoadType, interleave, bus);
}
/*****************************************************************************/
/* String2Number : convert a string to a number in all known formats */
/*****************************************************************************/
bool Dasm6800::String2Number(string s, adr_t &value)
{
/* Standard formats for known 680x assemblers :
- a character has a leading '
and may be followed by a (n unchecked) closing '
- a binary has a leading %
- an octal constant has a leading @
- a hex constant has a leading $
*/
s = ltrim(s);
if (s[0] == '$')
return (sscanf(s.substr(1).c_str(), "%x", &value) == 1);
else if (s[0] == '@')
return (sscanf(s.substr(1).c_str(), "%o", &value) == 1);
else if (s[0] == '\'' && s.size() > 1)
{
value = s[1];
return true;
}
else if (s[0] == '%')
{
for (string::size_type i = 1; i < s.size(); i++)
{
char c = s[i];
if (c >= '0' && c <= '1')
value = (value << 1) + (c - '0');
else
return false;
}
}
// allow base class to check for others
return Disassembler::String2Number(s, value);
}
/*****************************************************************************/
/* Number2String : converts a number to a string in a variety of formats */
/*****************************************************************************/
string Dasm6800::Number2String
(
adr_t value,
int nDigits,
adr_t addr,
int bus
)
{
(void)bus;
string s;
/* Standard formats for known 680x assemblers :
- a character has a leading '
and may be followed by a (n unchecked) closing '
- a binary has a leading %
- an octal constant has a leading @
- a hex constant has a leading $
*/
MemAttribute::Type cellType = GetCellType(addr);
MemAttribute::Display disp;
bool bSigned = false;
if (cellType == MemAttribute::CellUntyped)
disp = MemAttribute::DefaultDisplay;
else
{
disp = GetDisplay(addr);
bSigned = IsSigned(addr);
}
if (disp == MemAttribute::DefaultDisplay)
disp = defaultDisplay;
if ((nDigits == 2) && /* if 2-digit value */
(disp == MemAttribute::Char)) /* and character output requested */
{
value &= 0xff;
if (isprint(value))
s = sformat("'%c%s", value, closeCC ? "'" : "");
else
s = sformat("$%02x", value);
}
else if (disp == MemAttribute::Binary) /* if a binary */
{
int nBit;
nDigits *= 4; /* convert from digits to bits */
s = '%'; /* prepare a binary value */
/* now do for all bits */
for (nBit = nDigits - 1; nBit >= 0; nBit--)
s.push_back('0' + (!!(value & (1 << nBit))));
}
else if (disp == MemAttribute::Hex) /* if hex */
{
adr_t mask = 0;
for (int i = 0; i < nDigits; i++)
mask |= (0x0f << (i * 4));
value &= mask;
s = sformat("$%0*X", nDigits, value); /* prepare a hex value */
}
else if (disp == MemAttribute::Octal) /* if octal display */
s = sformat("@%0*o", (nDigits * 4) + 2 / 3, value);
else /* otherwise */
{
if (bSigned)
{
int32_t sval; // sign extension, simple way
if (nDigits == 2) sval = (int32_t)((int8_t)value);
else if (nDigits == 4) sval = (int32_t)((int16_t)value);
else sval = (int32_t)value;
s = sformat("%d", sval); /* prepare signed decimal value */
}
else
{
adr_t mask = 0;
for (int i = 0; i < nDigits; i++)
mask |= (0x0f << (i * 4));
value &= mask;
s = sformat("%u", value); /* prepare unsigned decimal value */
}
}
return s; /* pass back generated string */
}
/*****************************************************************************/
/* AddForced : add forced direct or extended addressing specifier */
/*****************************************************************************/
void Dasm6800::AddForced(string &smnemo, string &sparm, bool bExtended)
{
string sf = bExtended ? textExtendedAddr : textDirectAddr;
bool bMnemo = false;
bool bAppend = false;
int txtat = 0;
char c = tolower(sf[0]);
if (c == 'm' || c == 'p')
{
bMnemo = (c == 'm');
c = tolower(sf[++txtat]);
if (c == '-' || c == '+')
{
bAppend = (c == '+');
++txtat;
}
}
if (bMnemo)
{
smnemo = (bAppend) ?
smnemo + sf.substr(txtat) :
sf.substr(txtat) + smnemo;
}
else
{
sparm = (bAppend) ?
sparm + sf.substr(txtat) :
sf.substr(txtat) + sparm;
}
}
/*****************************************************************************/
/* InitParse : initialize parsing */
/*****************************************************************************/
bool Dasm6800::InitParse(int bus)
{
if (bus == BusCode)
{
if (bSetSysVec)
{
// set up IRQ-RST system vectors
static const char *vectbl[] =
{
"IRQ", /* fff8 */
"SWI", /* fffa */
"NMI", /* fffc */
"RST" /* fffe */
};
for (adr_t addr = 0xfff8; addr <= GetHighestCodeAddr(); addr+= 2)
{
MemoryType memType = GetMemType(addr);
if (memType != Untyped && /* if system vector loaded */
memType != Const && /* and not defined as constant */
!FindLabel(addr)) /* and no label set in info file */
{
SetMemType(addr, Data); /* that's a data word */
SetCellSize(addr, 2);
/* look whether it points to loaded */
adr_t tgtaddr = GetUWord(addr);
if (GetMemType(tgtaddr) != Untyped)
{ /* if so, */
SetMemType(tgtaddr, Code); /* that's code there */
AddLabel(tgtaddr, Code, /* and it got a predefined label */
sformat("vec_%s", vectbl[(addr - 0xfff8) / 2]),
true);
}
}
}
}
}
return Disassembler::InitParse(bus);
}
/*****************************************************************************/
/* ParseData : parse data at given memory address for labels */
/*****************************************************************************/
adr_t Dasm6800::ParseData
(
adr_t addr,
int bus /* ignored for 6800 and derivates */
)
{
SetLabelUsed(addr, Const); /* mark DefLabels as used */
int csz = GetCellSize(addr);
if (!IsConst(addr) && !IsBss(addr))
{
if (csz == 2) /* if WORD data */
SetLabelUsed(GetUWord(addr), Code, BusCode, addr);
else if (csz == 1 && useDPLabels) /* or BYTE data and using DP labels */
{
adr_t dp = GetDirectPage(addr, bus);
if (dp != NO_ADDRESS)
SetLabelUsed(dp | GetUByte(addr), Code, BusCode, addr);
}
}
return csz;
}
/*****************************************************************************/
/* FetchInstructionDetails : fetch details about current instruction */
/*****************************************************************************/
adr_t Dasm6800::FetchInstructionDetails
(
adr_t PC,
uint8_t &instpg,
uint8_t &instb,
uint8_t &mode,
int &mnemoIndex
)
{
instpg = 0;
instb = GetUByte(PC++);
mnemoIndex = codes[instb].mne;
mode = codes[instb].adrmode;
return PC;
}
/*****************************************************************************/
/* ParseCode : parse instruction at given memory address for labels */
/*****************************************************************************/
adr_t Dasm6800::ParseCode
(
adr_t addr,
int bus /* ignored for 6800 and derivates */
)
{
uint8_t instpg, instb, T, mode;
uint16_t W;
int mnemoIndex;
bool bSetLabel;
adr_t PC = FetchInstructionDetails(addr, instpg, instb, mode, mnemoIndex);
switch (mode) /* which mode is this ? */
{
case _nom: /* no mode */
SetDefLabelUsed(PC, bus);
PC = addr + 1;
break;
case _imp: /* inherent/implied */
break;
case _imb: /* immediate byte */
SetDefLabelUsed(PC, bus);
PC++;
break;
case _imw: /* immediate word */
bSetLabel = !IsConst(PC);
if (!bSetLabel)
SetDefLabelUsed(PC, bus);
SetCellSize(PC, 2);
W = GetUWord(PC);
if (bSetLabel)
{
W = (uint16_t)PhaseInner(W, PC);
AddRelativeLabel(W, PC, mnemo[mnemoIndex].memType, true, BusCode, addr);
}
PC += 2;
break;
case _dir: /* direct */
bSetLabel = !IsConst(PC);
if (!bSetLabel)
SetDefLabelUsed(PC, bus);
if (bSetLabel)
{
// this isn't really necessary for 6800, but for derived classes
adr_t dp = GetDirectPage(PC);
if (dp == DEFAULT_ADDRESS || dp == NO_ADDRESS)
dp = GetDirectPage(addr);
if (dp == DEFAULT_ADDRESS)
dp = 0;
if (dp != NO_ADDRESS)
{
T = GetUByte(PC);
W = (uint16_t)dp | T;
W = (uint16_t)PhaseInner(W, PC);
AddRelativeLabel(W, PC, mnemo[mnemoIndex].memType, true, BusCode, addr);
}
}
PC++;
break;
case _ext: /* extended */
bSetLabel = !IsConst(PC);
if (!bSetLabel)
SetDefLabelUsed(PC, bus);
SetCellSize(PC, 2);
if (bSetLabel)
{
W = GetUWord(PC);
W = (uint16_t)PhaseInner(W, PC);
AddRelativeLabel(W, PC, mnemo[mnemoIndex].memType, true, BusCode, addr);
}
PC += 2;
break;
case _ix8: /* indexed for 6800 (unsigned) */
bSetLabel = !IsConst(PC);
if (!bSetLabel && !GetRelative(PC))
SetDefLabelUsed(PC, bus);
PC++;
break;
case _reb: /* relative byte */
bSetLabel = !IsConst(PC);
// lbl = bSetLabel ? NULL : FindLabel(PC, Const, bus);
T = GetUByte(PC); PC++;
W = (uint16_t)(PC + (signed char)T);
if (bSetLabel)
{
W = (uint16_t)DephaseOuter(W, PC - 1);
// AddLabel(W, mnemo[mnemoIndex].memType, "", true);
AddRelativeLabel(W, PC, mnemo[mnemoIndex].memType, true, BusCode, addr);
}
break;
}
return PC - addr; /* pass back # processed bytes */
}
/*****************************************************************************/
/* DisassembleLabel : disassemble used external labels */
/*****************************************************************************/
bool Dasm6800::DisassembleLabel
(
Label *label,
string &slabel,
string &smnemo,
string &sparm,
int bus
)
{
string lbltxt = label->GetText();
if (lbltxt.find_first_of("+-") == string::npos)
{
adr_t laddr = label->GetAddress();
if (lbltxt.size() && !GetRelative(laddr, bus))
slabel = lbltxt;
else
slabel = Label2String(laddr, 4, true, laddr, bus);
smnemo = mnemo[_equ].mne;
sparm = Address2String(laddr, bus);
Disassembler::DisassembleLabel(label, slabel, smnemo, sparm, bus);
return true;
}
return false;
}
/*****************************************************************************/
/* DisassembleDefLabel : pass back mnemonic and parameters for a DefLabel */
/*****************************************************************************/
bool Dasm6800::DisassembleDefLabel
(
DefLabel *label,
string &slabel,
string &smnemo,
string &sparm,
int bus
)
{
(void)bus;
slabel = label->GetText();
smnemo = mnemo[_equ].mne;
sparm = label->GetDefinition();
Disassembler::DisassembleDefLabel(label, slabel, smnemo, sparm, bus);
return true;
}
/*****************************************************************************/
/* GetDisassemblyFlags : get flags for disassembly of data areas */
/*****************************************************************************/
uint32_t Dasm6800::GetDisassemblyFlags(adr_t addr, int bus)
{
uint32_t flags = Disassembler::GetDisassemblyFlags(addr, bus);
if (flags & SHMF_TXT)
{
if (FindLabel(addr, Const, bus))
flags &= ~SHMF_TXT;
if (useDPLabels)
{
adr_t dp = GetDirectPage(addr, bus);
if (dp != NO_ADDRESS)
{
#if 0
// activate this to match ALL direct page labels
// only makes sense if data DEFINES the label, too
if (!IsConst(addr, bus))
#else
// activate this to only match DEFINED labels
adr_t t = dp | GetUByte(addr);
if (!IsConst(addr, bus) &&
FindLabel(t, Untyped, bus))
#endif
flags &= ~SHMF_TXT;
}
}