-
Notifications
You must be signed in to change notification settings - Fork 4
/
dasmfw.cpp
2840 lines (2650 loc) · 98.3 KB
/
dasmfw.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. *
***************************************************************************/
/*****************************************************************************/
/* dasmfw.cpp : main program for the DisASseMbler FrameWork */
/*****************************************************************************/
#include "dasmfw.h"
#include "Disassembler.h"
#include "Label.h"
#include "Memory.h"
/*****************************************************************************/
/* Global Data */
/*****************************************************************************/
static struct DisassemblerCreators /* structure for dasm creators: */
{
string name; /* code name of the disassembler */
Disassembler *(*Factory)(Application *); /* factory to create it */
} *Disassemblers = NULL;
static int nRegDisassemblers = 0; /* # registered disassemblers */
static int nAllocDisassemblers = 0; /* # allocated disassembler structs */
/*****************************************************************************/
/* RegisterDisassembler : registers a disassembler */
/*****************************************************************************/
bool RegisterDisassembler(string name, Disassembler *(*CreateDisassembler)(Application *))
{
if (nRegDisassemblers + 1 > nAllocDisassemblers)
{
DisassemblerCreators *pNew;
try
{
pNew = new DisassemblerCreators[nRegDisassemblers + 10];
if (!pNew) // deal with very old style allocator
return false;
for (int i = nRegDisassemblers; i < nRegDisassemblers + 10; i++)
pNew[i].Factory = NULL;;
nAllocDisassemblers = nRegDisassemblers + 10;
}
catch(...)
{
return false;
}
for (int i = 0; i < nRegDisassemblers; i++)
{
pNew[i].name = Disassemblers[i].name;
pNew[i].Factory = Disassemblers[i].Factory;
}
if (Disassemblers) delete[] Disassemblers;
Disassemblers = pNew;
}
// simple insertion sort will do
for (int i = 0; i < nRegDisassemblers; i++)
{
if (Disassemblers[i].name > name)
{
for (int j = nRegDisassemblers; j > i; j--)
{
Disassemblers[j].name = Disassemblers[j - 1].name;
Disassemblers[j].Factory = Disassemblers[j - 1].Factory;
}
nRegDisassemblers++;
Disassemblers[i].name = name;
Disassemblers[i].Factory = CreateDisassembler;
return true;
}
}
Disassemblers[nRegDisassemblers].name = name;
Disassemblers[nRegDisassemblers++].Factory = CreateDisassembler;
return true;
}
/*****************************************************************************/
/* CreateDisassembler : creates a disassembler with a given code name */
/*****************************************************************************/
Disassembler *CreateDisassembler(string name, Application *pApp, int *pidx = NULL)
{
for (int da = 0; da < nRegDisassemblers; da++)
if (Disassemblers[da].name == name)
{
if (pidx) *pidx = da;
return Disassemblers[da].Factory(pApp);
}
return NULL;
}
/*****************************************************************************/
/* ListDisassemblers : lists out all registered disassemblers */
/*****************************************************************************/
static void ListDisassemblers(Application *pApp, int indent = 0)
{
printf("%*sRegistered disassemblers:\n"
"%*s%-8s %-16s %s\n"
"%*s--------------------------------------------------------------\n",
indent, "",
indent, "", "Code", "Instruction Set", "Attributes",
indent, "");
for (int da = 0; da < nRegDisassemblers; da++)
{
Disassembler *pDasm = Disassemblers[da].Factory(pApp);
printf("%*s%-8s %-16s (%d/%d bits data/code, %s-endian",
indent, "",
Disassemblers[da].name.c_str(),
pDasm->GetName().c_str(),
pDasm->GetDataBits(),
pDasm->GetCodeBits(),
pDasm->GetEndianness() == Disassembler::BigEndian ? "big" : "little");
if (pDasm->GetArchitecture() == Disassembler::Harvard)
printf(", separate data bus");
printf(")\n");
delete pDasm;
}
}
/*****************************************************************************/
/* sformat : sprintf for string */
/*****************************************************************************/
string sformat(const string fmt_str, ...)
{
int final_n, n = ((int)fmt_str.size()) * 2;
vector<char> formatted;
va_list ap;
while (1)
{
formatted.resize(n);
va_start(ap, fmt_str);
final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
va_end(ap);
if (final_n < 0 || final_n >= n)
n += abs(final_n - n + 1);
else
break;
}
return string(&formatted[0]);
}
/*****************************************************************************/
/* lowercase : create lowercase copy of a string */
/*****************************************************************************/
string lowercase(string s)
{
string sout;
// primitive ASCII implementation
for (string::size_type i = 0; i < s.size(); i++)
sout += tolower(s[i]);
return sout;
}
/*****************************************************************************/
/* uppercase : create uppercase copy of a string */
/*****************************************************************************/
string uppercase(string s)
{
string sout;
// primitive ASCII implementation
for (string::size_type i = 0; i < s.size(); i++)
sout += toupper(s[i]);
return sout;
}
/*****************************************************************************/
/* ltrim : remove leading blanks from string */
/*****************************************************************************/
string ltrim(string s)
{
if (s.empty()) return s;
string::size_type from = s.find_first_not_of(" ");
if (from == s.npos)
return "";
return from ? s.substr(from) : s;
}
/*****************************************************************************/
/* trim : remove leading and trailing blanks from string */
/*****************************************************************************/
string trim(string s)
{
if (s.empty()) return s;
string::size_type from = s.find_first_not_of(" ");
if (from == s.npos)
return "";
return s.substr(from, s.find_last_not_of(" ") - from + 1);
}
/*****************************************************************************/
/* main : main program function */
/*****************************************************************************/
int main(int argc, char* argv[])
{
// let's go object-oriented, shall we? :-)
return Application(argc, argv).Run();
}
/*===========================================================================*/
/* Application class members */
/*===========================================================================*/
/*****************************************************************************/
/* Application : constructor */
/*****************************************************************************/
Application::Application(int argc, char* argv[])
: argc(argc), argv(argv)
{
pDasm = NULL; /* selected disassembler */
iDasm = -1; /* index of selected disassembler */
abortHelp = false; /* abort after help has been given */
infoBus = BusCode; /* start with code bus */
showLogo = true; /* show logo in output file */
showHex = true; /* flag for hex data display */
showAddr = true; /* flag for address display */
showCode = true; /* flag for code display */
showComments = true; /* flag for comment display */
showLComments = true; /* flag for line comment display */
showCref = false; /* flag for cross-reference display */
f9dasmComp = false; /* flag for f9dasm compatibility */
labelEqus = false; /* flag for label equate output */
labelLen = 8; /* minimum label length */
lLabelLen = 8; /* minimum EQU label length */
mnemoLen = 8; /* minimum mnemonics length */
cparmLen = 24; /* min parm len with lcomment */
uparmLen = 52; /* max parm len without lcomment */
dbCount = 5; /* min code bytes for hex/asc dump */
#if RB_VARIANT
showAsc = false; /* flag for ASCII content display */
#else
showAsc = true; /* flag for ASCII content display */
#endif
showUnused = false; /* don't show unused labels */
out = stdout;
// pre-load matching disassembler, if possible
sDasmName = argv[0];
string::size_type idx = sDasmName.find_last_of("\\/");
if (idx != sDasmName.npos)
sDasmName = sDasmName.substr(idx + 1);
idx = sDasmName.rfind('.');
if (idx != sDasmName.npos)
sDasmName = sDasmName.substr(0, idx);
// if the name might be "dasm"+code, try to use this disassembler
if (lowercase(sDasmName.substr(0, 4)) == "dasm")
pDasm = CreateDisassembler(sDasmName.substr(4), this, &iDasm);
}
/*****************************************************************************/
/* ~Application : destructor */
/*****************************************************************************/
Application::~Application()
{
}
/*****************************************************************************/
/* Run : the application's main functionality */
/*****************************************************************************/
int Application::Run()
{
if (argc < 2) /* if no arguments given, give help */
{
printf("%s: Disassembler Framework V%s\n", sDasmName.c_str(), DASMFW_VERSION);
return Help(); /* and exit */
}
string defnfo[2]; /* global / user default info files */
#ifdef _MSC_VER
// Microsoft C - guaranteed Windows environment
char const *pHomePath = getenv("USERPROFILE");
defnfo[0] = pHomePath ?
string(pHomePath) + "\\.dasmfw\\" + sDasmName + ".nfo" :
"";
#else
char const *pHomePath = getenv("HOME");
defnfo[0] = pHomePath ?
string(pHomePath) + "/.dasmfw/" + sDasmName + ".nfo" :
"";
#endif
defnfo[1] = sDasmName + ".nfo";
size_t i;
int bus;
/* first, just search for "dasm" */
for (i = 0; i < _countof(defnfo); i++)
if (!defnfo[i].empty())
ParseOption("info", defnfo[i], true, false);
ParseOptions(argc, argv, true);
if (!pDasm) /* disassembler must be loaded now */
{
printf("%s: Disassembler Framework V%s\n", sDasmName.c_str(), DASMFW_VERSION);
return Help(); /* if not, display help & exit */
}
remaps.resize(pDasm->GetBusCount()); /* set up all arrays */
comments[0].resize(pDasm->GetBusCount());
comments[1].resize(pDasm->GetBusCount());
lcomments.resize(pDasm->GetBusCount());
/* then parse all other options */
for (i = 0; i < _countof(defnfo); i++) /* for data & info files to load */
if (!defnfo[i].empty())
ParseOption("info", defnfo[i], false, false);
ParseOptions(argc, argv, false);
if (showLogo)
printf("%s: Disassembler Framework V%s\n", sDasmName.c_str(), DASMFW_VERSION);
if (abortHelp) /* help has been given, so end it */
return 1;
LoadFiles(); /* load all data files */
LoadInfoFiles(); /* load all info files */
// parse labels in 2 passes
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
if (pDasm->GetMemoryArrayCount(bus)) Parse(0, bus);
}
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
if (pDasm->GetMemoryArrayCount(bus)) Parse(1, bus);
}
// resolve all XXXXXXXX+/-nnnn labels and DefLabels
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
pDasm->ResolveLabels(bus);
}
// set up often used texts and flags
string labelDelim = pDasm->GetOption("ldchar");
string sComDel = pDasm->GetOption("cchar");
string sComBlk = sComDel + " ";
string sComHdr(sComDel);
while (sComHdr.size() < 53)
sComHdr += '*';
// create output file
out = outname.size() ? fopen(outname.c_str(), "w") : stdout;
if (out == NULL)
out = stdout;
if (out != stdout && showLogo) /* if output goes to file */
{ /* write header details */
PrintLine(sComDel +
sformat(" %s: Disassembler Framework V" DASMFW_VERSION,
sDasmName.c_str()));
for (i = 0; /* print loaded files */
i < saFNames.size();
i++)
if (saFNames[i].substr(0, 1) != "-")
PrintLine(sComDel + " " + saFNames[i]);
for (i = 0; /* print loaded info files */
i < saINames.size();
i++)
PrintLine(sComDel + " " + saINames[i]);
PrintLine();
}
//#ifdef _DEBUG
#if 0
PrintLine();
PrintLine(sComBlk + "After Parsing:");
PrintLine(sComBlk +
sformat("Loaded code memory areas: begin=%s, end=%s",
pDasm->GetOption("begin").c_str(),
pDasm->GetOption("end").c_str()));
for (i = 0; i < pDasm->GetBusCount(); i++)
DumpMem(i);
#endif
/* show comments that go in front */
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
DisassembleComments(NO_ADDRESS, false, sComDel, bus);
}
// disassembler-specific initialization
DisassembleChanges(NO_ADDRESS, NO_ADDRESS, 0, false, 0);
// output of (def)labels without data
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
DisassembleLabels(sComDel, sComHdr, bus);
DisassembleDefLabels(sComDel, sComHdr, bus);
}
// disassemble all memory areas for the busses
for (i = 0; i < (size_t)pDasm->GetBusCount(); i++)
{
bus = pDasm->GetBus(i);
if (!pDasm->GetMemoryArrayCount(bus))
continue;
adr_t prevaddr = NO_ADDRESS, prevsz = 0;
adr_t addr = pDasm->GetMemoryArray(0, bus).GetStart();
if (!pDasm->IsCellUsed(addr, bus))
addr = pDasm->GetNextAddr(addr, bus);
bool bBusHdrOut = false;
while (addr != NO_ADDRESS)
{
if (!bBusHdrOut)
{
PrintLine();
PrintLine(sComHdr);
PrintLine(sComBlk +
sformat("Program's %s Areas",
pDasm->GetBusName().c_str()));
PrintLine(sComHdr);
PrintLine();
bBusHdrOut = true;
}
DisassembleChanges(addr, prevaddr, prevsz, false, bus);
adr_t sz = DisassembleLine(addr, sComDel, sComHdr, labelDelim, bus);
DisassembleChanges(addr, prevaddr, prevsz, true, bus);
prevaddr = addr;
prevsz = sz;
addr = pDasm->GetNextAddr(addr + sz - 1, bus);
}
// disassembler-specific end of area handling
DisassembleChanges(addr, prevaddr, prevsz, false, bus);
}
// disassembler-specific closing
DisassembleChanges(NO_ADDRESS, NO_ADDRESS, 0, true, pDasm->GetBusCount());
if (out != stdout)
fclose(out);
if (pDasm) delete pDasm;
return 0;
}
/*****************************************************************************/
/* LoadFiles : load all data files referenced on command line or from info */
/*****************************************************************************/
bool Application::LoadFiles()
{
bool bAllOK = true;
infoBus = BusCode; /* start with code bus */
int nInterleave = 1;
for (int i = 0; /* load file(s) given on commandline */
i < (int)saFNames.size(); /* and parsed from info files */
i++)
{ /* and load-relevant option settings */
if (saFNames[i].substr(0, 7) == "-begin:")
ParseOption("begin", saFNames[i].substr(7));
else if (saFNames[i].substr(0, 5) == "-end:")
ParseOption("end", saFNames[i].substr(5));
else if (saFNames[i].substr(0, 8) == "-offset:")
ParseOption("offset", saFNames[i].substr(8));
else if (saFNames[i].substr(0, 12) == "-interleave:")
nInterleave = atoi(saFNames[i].substr(12).c_str());
else if (saFNames[i].substr(0, 5) == "-bus:")
ParseOption("bus", saFNames[i].substr(5));
else
{
string sLoadType;
bool bOK = pDasm->Load(saFNames[i], sLoadType, nInterleave, infoBus);
saFNames[i] = sformat("%soaded: %s file \"%s\"",
bOK ? "L" : "NOT l",
sLoadType.c_str(),
saFNames[i].c_str());
if (nInterleave != 1)
saFNames[i] += sformat(" (interleave=%d)", nInterleave);
#ifndef _DEBUG
if (showLogo)
printf("%s\n", saFNames[i].c_str());
#endif
bAllOK &= bOK;
}
#ifdef _DEBUG
if (showLogo)
printf("%s\n", saFNames[i].c_str());
#endif
}
return bAllOK;
}
/*****************************************************************************/
/* LoadInfoFiles : fully process all info files */
/*****************************************************************************/
bool Application::LoadInfoFiles()
{
bool bAllOK = true;
infoBus = BusCode; /* start with code bus */
for (int i = 0;
i < (int)saINames.size();
i++)
{
bool bOK = LoadInfo(saINames[i]);
saINames[i] = sformat("%soaded: Info file \"%s\"",
bOK ? "L" : "NOT l",
saINames[i].c_str());
if (showLogo)
printf("%s\n", saINames[i].c_str());
bAllOK &= bOK;
}
return bAllOK;
}
/*****************************************************************************/
/* Parse : go through the loaded memory areas and parse all labels */
/*****************************************************************************/
bool Application::Parse(int nPass, int bus)
{
if (!nPass &&
!pDasm->InitParse(bus))
return false;
if (!pDasm->GetMemoryArrayCount(bus))
return true;
adr_t prevaddr = NO_ADDRESS;
adr_t addr = pDasm->GetMemoryArray(0, bus).GetStart();
if (!pDasm->IsCellUsed(addr, bus))
addr = pDasm->GetNextAddr(addr, bus);
while (addr != NO_ADDRESS)
{
MemAttribute::Type oct = pDasm->GetCellType(addr, bus);
adr_t sz = pDasm->Parse(addr, bus);
MemAttribute::Type nct = pDasm->GetCellType(addr, bus);
// cell type has been changed by parser?
if (oct != nct)
{
// code changed to untyped data ==> illegal instruction
if (nct == MemAttribute::CellUntyped)
{
if (nPass == 0)
AddLComment(addr, "Illegal instruction!", false, bus);
}
}
prevaddr = addr;
addr = pDasm->GetNextAddr(addr + sz - 1, bus);
}
(void)prevaddr; // not used ... yet. Maybe later.
return true;
}
/*****************************************************************************/
/* DisassembleComments : show all comments for this line */
/*****************************************************************************/
bool Application::DisassembleComments
(
adr_t addr,
bool bAfterLine,
string sComDel,
int bus
)
{
CommentArray::iterator it;
sComDel += " ";
Comment *pComment = GetFirstComment(addr, it, bAfterLine, bus);
while (pComment)
{
string sTxt(pComment->GetText());
string sHdr((pComment->IsComment() && sTxt.size()) ? sComDel : "");
if (showComments || !pComment->IsComment())
PrintLine(sHdr + sTxt);
pComment = GetNextComment(addr, it, bAfterLine, bus);
}
return true;
}
/*****************************************************************************/
/* DisassembleCref : show all cross-references for this line */
/*****************************************************************************/
bool Application::DisassembleCref(Label *pLabel, string sComDel, int bus)
{
if (!showCref || !pLabel || !pLabel->RefCount())
return false;
pLabel->SortRefs();
string sLine(sComDel + " Xref: ");
AddrBus addrp(NO_ADDRESS, BusCode);
size_t i, j;
for (i = j = 0; i < pLabel->RefCount(); i++)
{
AddrBus &ref = pLabel->GetRef(i);
if (ref != addrp)
{
int nDigits = (pDasm->BusAddressBits(ref.bus) + 3) / 4;
string sLblText = pDasm->Label2String(ref.addr, nDigits, true, NO_ADDRESS, ref.bus);
if (ref.bus != bus)
sLblText += "(" + pDasm->GetBusName(ref.bus) + ")";
if (j && sLine.size() + 2 + sLblText.size() > 79)
{
PrintLine(sLine);
sLine = sComDel + " ";
j = 0;
}
else if (j)
sLine += ", ";
sLine += sLblText;
j++;
}
addrp = ref;
}
PrintLine(sLine);
return true;
}
/*****************************************************************************/
/* DisassembleChanges : parses pre-/post-changes for a disassembly line */
/*****************************************************************************/
bool Application::DisassembleChanges
(
adr_t addr,
adr_t prevaddr,
adr_t prevsz,
bool bAfterLine,
int bus
)
{
vector<Disassembler::LineChange> changes;
bool bRC = pDasm->DisassembleChanges(addr,
prevaddr, prevsz,
bAfterLine,
changes,
bus);
for (vector<Disassembler::LineChange>::size_type i = 0;
i < changes.size();
i++)
{
PrintLine(changes[i].label, changes[i].oper, changes[i].opnds);
}
return bRC;
}
/*****************************************************************************/
/* DisassembleLabels : disassemble all labels without memory area */
/*****************************************************************************/
bool Application::DisassembleLabels
(
string sComDel,
string sComHdr,
int bus
)
{
string sComBlk(sComDel + " ");
CommentArray::iterator it;
Comment *pComment;
static bool bULHOut = false;
adr_t paddr = NO_ADDRESS;
for (int l = 0; l < pDasm->GetLabelCount(bus); l++)
{
Label *pLbl = pDasm->LabelAt(l, bus);
adr_t laddr = pLbl->GetAddress();
bool bShow = showUnused || pLbl->IsUsed();
// if (!pLbl->IsConst())
MemoryType memType = pDasm->GetMemType(laddr, bus);
bShow &= (memType == Untyped);
if (bShow)
{
string slabel, smnemo, sparm;
if (pDasm->DisassembleLabel(pLbl, slabel, smnemo, sparm, bus))
{
// header, if not yet done
if (!bULHOut)
{
PrintLine();
PrintLine(sComHdr);
PrintLine(sComBlk + "Used Labels");
PrintLine(sComHdr);
PrintLine();
bULHOut = true;
}
// comments before line
if (laddr != paddr)
DisassembleComments(laddr, false, sComDel, bus);
// Cross-references before label line
DisassembleCref(pLbl, sComDel, bus);
// the line itself
pComment = (showComments && laddr != paddr) ?
GetFirstLComment(laddr, it, bus) : NULL;
string scomment = (showLComments && pComment) ? pComment->GetText() : "";
if (scomment.size()) scomment = sComBlk + scomment;
PrintLine(slabel, smnemo, sparm, scomment, lLabelLen);
if (showLComments && pComment)
{
while ((pComment = GetNextLComment(laddr, it, bus)) != NULL)
PrintLine("", "", "", sComBlk + pComment->GetText());
}
// comments after line
if (laddr != paddr)
DisassembleComments(laddr, true, sComDel, bus);
paddr = laddr;
}
}
}
return true;
}
/*****************************************************************************/
/* DisassembleDefLabels : disassemble all used DefLabels */
/*****************************************************************************/
bool Application::DisassembleDefLabels
(
string sComDel,
string sComHdr,
int bus
)
{
string sComBlk(sComDel + " ");
static bool bULHOut = false;
for (int l = 0; l < pDasm->GetDefLabelCount(bus); l++)
{
DefLabel *pLbl = pDasm->DefLabelAt(l, bus);
string slabel, smnemo, sparm;
// DefLabels got no comments ATM
if (pLbl &&
pDasm->DisassembleDefLabel(pLbl, slabel, smnemo, sparm, bus))
{
// header, if not yet done
if (!bULHOut)
{
PrintLine();
PrintLine(sComHdr);
PrintLine(sComBlk + "Used Definitions");
PrintLine(sComHdr);
PrintLine();
bULHOut = true;
}
// and no line comments
PrintLine(slabel, smnemo, sparm, "", lLabelLen);
}
}
return true;
}
/*****************************************************************************/
/* DisassembleLine : create a line of disassembler output (+comments) */
/*****************************************************************************/
adr_t Application::DisassembleLine
(
adr_t addr,
string sComDel,
string sComHdr,
string labelDelim,
int bus
)
{
(void)sComHdr; // unused at the moment
string sLabel, sMnemo, sParms, sComBlk(sComDel + " ");
CommentArray::iterator it;
Comment *pComment;
bool bWithComments = showLComments && (showHex || showAsc || showAddr);
// comments before line
if (!labelEqus || showCode)
DisassembleComments(addr, false, sComDel, bus);
// the line itself
// in case of multiple labels, it's always the last one that's used
// for the "real" code/data line, so prepend all others
LabelArray::iterator lit;
Label *p = NULL, *pNext = pDasm->GetFirstLabel(addr, lit, Untyped, bus);
while (pNext)
{
if (pNext->IsUsed() && !pNext->IsConst())
p = pNext;
pNext = pDasm->GetNextLabel(addr, lit, Untyped, bus);
// multiple labels get their own lines
if (p != pNext && pNext && pNext->IsUsed() && !pNext->IsConst())
{
// Cross-references before label line
DisassembleCref(pNext, sComDel, bus);
string s = p->GetText();
if (s.size())
{
if (labelEqus)
{
PrintLabelEqu(p, s);
s = sComDel + s;
}
if (showCode)
PrintLine(s + labelDelim);
}
}
}
// Cross-references before label line
DisassembleCref(p, sComDel, bus);
if (p && p->IsUsed() && !p->IsConst())
sLabel = pDasm->Label2String(addr, 4, true, addr, bus) +
labelDelim;
pComment = (showComments && showLComments) ? GetFirstLComment(addr, it, bus) : NULL;
int maxparmlen = (bWithComments || pComment) ? (cparmLen - 1) : uparmLen;
adr_t sz = pDasm->Disassemble(addr, sMnemo, sParms, maxparmlen, bus);
string scomment;
if (showLComments)
{
if (showAddr)
scomment += sformat("%0*X%s",
(pDasm->BusAddressBits(bus) + 3) / 4, addr,
(showHex || showAsc) ? ": " : " ");
if ((showHex || showAsc) && !pDasm->IsBss(addr, bus))
{
string sHex, sAsc;
if (showHex || showAsc)
{
pDasm->DisassembleHexAsc(addr, sz, (adr_t)dbCount, sHex, sAsc, bus);
if (!pComment)
sAsc = trim(sAsc);
#if 0
for (adr_t i = sz; i < (adr_t)dbCount; i++)
{
if (showHex && showAsc)
sHex += " ";
if (showAsc && pComment)
sAsc += ' ';
}
#endif
}
if (showHex)
scomment += sHex;
if (showAsc)
scomment += sAsc;
}
if (pComment && pComment->GetText().size())
{
if (scomment.size()) scomment += " ";
scomment += pComment->GetText();
}
}
if (scomment.size()) scomment = sComBlk + scomment;
if (labelEqus)
{
PrintLabelEqu(p, sLabel);
sLabel = sComDel + sLabel;
}
if (showCode)
PrintLine(sLabel, sMnemo, sParms, scomment);
if (pComment && (!labelEqus || showCode))
{
// following line comments
while ((pComment = GetNextLComment(addr, it, bus)) != NULL)
PrintLine("", "", "", sComBlk + pComment->GetText());
}
// comments after line
if (!labelEqus || showCode)
DisassembleComments(addr, true, sComDel, bus);
return sz;
}
/*****************************************************************************/
/* PostprocessLine : post-process line if mnemonic dictates so */
/*****************************************************************************/
bool Application::PostprocessLine
(
string &sLabel,
string &smnemo,
string &sparm,
string &scomment
)
{
size_t pos = smnemo.find("$(");
while (pos != string::npos) /* mnemo containing $( is special, */
{
if (pos > 0 && smnemo[pos - 1] == '\\')
{
smnemo = smnemo.substr(0, pos) + smnemo.substr(pos + 1);
}
else
{
size_t endpos = smnemo.substr(pos).find(")");
if (endpos == string::npos)
break;
endpos++;
string part = lowercase(smnemo.substr(pos, endpos));
if (part == "$(label)")
{
string labelDelim = pDasm->GetOption("ldchar");
if (labelDelim.size() && sLabel.size() &&
sLabel.substr(sLabel.size() - labelDelim.size()) == labelDelim)
sLabel = sLabel.substr(0, sLabel.size() - labelDelim.size());
smnemo = smnemo.substr(0, pos) + sLabel + smnemo.substr(pos + endpos);
sLabel = "";
}
else if (part == "$(parm)")
{
smnemo = smnemo.substr(0, pos) + sparm + smnemo.substr(pos + endpos);
sparm = "";
}
else if (part == "$(<<)")
{
smnemo = smnemo.substr(0, pos) + smnemo.substr(pos + endpos);
sLabel = smnemo;
smnemo = sparm;
sparm = ""; // might be a bad bad bad idea to shift comment left
pos = (size_t)-1;
}
}
pos = smnemo.find("$(", pos + 1);
}
(void)scomment; /* not used yet - keep gcc happy */
return true;
}
/*****************************************************************************/
/* PrintLine : prints a formatted line to the output */
/*****************************************************************************/
bool Application::PrintLine
(
string sLabel,
string smnemo,
string sparm,
string scomment,
int labelLen
)
{
if (labelLen < 0) labelLen = this->labelLen;
int nLen = 0;
int nMinLen = labelLen;
PostprocessLine(sLabel, smnemo, sparm, scomment);
if (sLabel.size())
{
nLen += fprintf(out, "%s", sLabel.c_str());
if (nLen > labelLen && // skip to next line on very long labels
smnemo.size())
nLen = fprintf(out, "\n%*s", labelLen - 1, "") - 1;
}
if (smnemo.size())
{
if (nLen > 0) nLen += fprintf(out, " ");
if (nLen < nMinLen) nLen += fprintf(out, "%*s", nMinLen - nLen, "");
nLen += fprintf(out, "%s", smnemo.c_str());