Debugger sources code clean-up
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / m68kDasmWin.cpp
1 //
2 // m68kDasmWin.cpp - M68K disassembly window
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -------------------------------------------------------------
10 // JPM 06/27/2016 Created this file
11 // JPM 12/04/2016 Suport ELF debug information
12 // JPM Replacing the ELF support by the debugger information manager calls
13 //
14
15 // STILL TO DO:
16 //
17
18 #include <stdlib.h>
19 #include "debugger/m68kDasmWin.h"
20 #include "m68000/m68kinterface.h"
21 #include "dsp.h"
22 #include "gpu.h"
23 #include "DBGManager.h"
24 #include "settings.h"
25
26
27 //
28 m68KDasmWindow::m68KDasmWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Dialog),
29 layout(new QVBoxLayout), text(new QTextBrowser),
30 memBase(0)
31 {
32 QFont fixedFont("Lucida Console", 8, QFont::Normal);
33 fixedFont.setStyleHint(QFont::Monospace); //TypeWriter
34 fixedFont.setLetterSpacing(QFont::PercentageSpacing, 100);
35 text->setFont(fixedFont);
36 setLayout(layout);
37
38 layout->addWidget(text);
39 }
40
41
42 //
43 void m68KDasmWindow::RefreshContents(void)
44 {
45 QString s;
46 char buffer[1024], string[1024], adresse[16];
47 size_t pc = memBase, oldpc;
48 size_t m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
49 size_t m68KPCNbrDisasmLines = 0;
50 char *Symbol = NULL, *LineSrc, *CurrentLineSrc = NULL;
51 bool m68kPCShow = false;
52 bool constant, adr, equal, Error;
53 size_t j, i;
54 size_t nbr = vjs.nbrdisasmlines;
55 char *PtrFullSource, *CurrentPtrFullSource = (char *)calloc(1, 1);
56 size_t NumLine; // , CurrentNumLine = 0;
57 size_t CurrentNumLine;
58 char singleCharString[2] = { 0, 0 };
59
60 for (i = 0; i < nbr; i++)
61 {
62 oldpc = pc;
63 adr = constant = equal = false;
64
65 // Display source filename based on the program address
66 if (vjs.displayFullSourceFilename && (PtrFullSource = DBGManager_GetFullSourceFilenameFromAdr(oldpc, &Error)) && strcmp(CurrentPtrFullSource, PtrFullSource))
67 {
68 #if 0
69 if (strcmp(OldPtrFullSource, PtrFullSource))
70 #endif
71 {
72 if (i)
73 {
74 nbr++;
75 s += QString("<br>");
76 }
77
78 CurrentNumLine = DBGManager_GetNumLineFromAdr(pc, DBG_NO_TAG) - 1;
79 nbr++;
80 CurrentPtrFullSource = (char *)realloc(CurrentPtrFullSource, strlen(PtrFullSource) + 1);
81 strcpy(CurrentPtrFullSource, PtrFullSource);
82 if (!Error)
83 {
84 sprintf(string, "<font color='#ff0000'><b>%s</b></font><br>", PtrFullSource);
85 }
86 else
87 {
88 sprintf(string, "<font color='#00ff00'><b>%s</b></font><br>", PtrFullSource);
89 }
90 s += QString(string);
91 }
92 }
93
94 // Display line number based on the program address
95 if ((NumLine = DBGManager_GetNumLineFromAdr(oldpc, DBG_NO_TAG)) && ((signed)NumLine > (signed)CurrentNumLine))
96 {
97 if ((signed)CurrentNumLine < 0)
98 {
99 CurrentNumLine = NumLine - 1;
100 }
101 sprintf(string, "| <font color='#006400'>%5u</font> | ", (unsigned int)++CurrentNumLine); // (CurrentNumLine = NumLine));
102 }
103 else
104 {
105 sprintf(string, "| | ");
106 }
107 s += QString(string);
108
109 // Display line source based on the program address
110 if (((signed)CurrentNumLine > 0) && (LineSrc = DBGManager_GetLineSrcFromNumLineBaseAdr(oldpc, CurrentNumLine)) && (LineSrc != CurrentLineSrc))
111 {
112 sprintf(string, "<font color='#006400'>%s</font><br>", (CurrentLineSrc = LineSrc));
113 s += QString(string);
114 nbr++;
115 }
116 else
117 {
118 // Display symbol, or line source, based on the program address
119 if (!CurrentLineSrc && !Symbol && (Symbol = DBGManager_GetSymbolNameFromAdr(oldpc)))
120 {
121 sprintf(string, "%s:<br>", Symbol);
122 s += QString(string);
123 nbr++;
124 }
125 // Display the assembly line based on the current PC
126 else
127 {
128 pc += m68k_disassemble(buffer, (unsigned int)pc, 0, vjs.disasmopcodes);
129
130 if (m68kPC == oldpc)
131 {
132 sprintf(string, "-> <u>%06X: %s</u><br>", (unsigned int)oldpc, buffer);
133 m68kPCShow = true;
134 m68KPCNbrDisasmLines = i;
135 }
136 else
137 {
138 sprintf(string, " %06X: %s<br>", (unsigned int)oldpc, buffer);
139 }
140
141 buffer[0] = 0; // Clear string
142
143 for (j = 0; j < strlen(string); j++)
144 {
145 if (string[j] == ' ')
146 {
147 strcat(buffer, "&nbsp;");
148 adr = constant = false;
149 }
150 else
151 {
152 switch (string[j])
153 {
154 case '#':
155 constant = true;
156 break;
157
158 case '$':
159 adr = true;
160 break;
161
162 case ',':
163 constant = adr = equal = false;
164 break;
165
166 case '=':
167 equal = true;
168 break;
169 }
170
171 if (!constant && adr && !equal)
172 {
173 int l = 0;
174 char *p;
175 do
176 {
177 adresse[l++] = string[++j];
178 } while ((string[(j + 1)] >= '0') && (string[(j + 1)] <= '9') || (string[(j + 1)] >= 'A') && (string[(j + 1)] <= 'F'));
179 adresse[l] = 0;
180
181 if (Symbol = DBGManager_GetSymbolNameFromAdr(strtoul(adresse, &p, 16)))
182 {
183 strcat(buffer, Symbol);
184 }
185 else
186 {
187 strcat(buffer, "$");
188 strcat(buffer, adresse);
189 }
190
191 adr = false;
192 }
193 else
194 {
195 singleCharString[0] = string[j];
196 strcat(buffer, singleCharString);
197 }
198 }
199 }
200
201 Symbol = NULL;
202 s += QString(buffer);
203 }
204 }
205 }
206
207 // Display generated text
208 text->clear();
209 if (m68kPCShow)
210 {
211 text->setText(s);
212 }
213 else
214 {
215 Use68KPCAddress();
216 RefreshContents();
217 }
218
219 // Set the scrollbar position in accordance of the M68K PC pointer
220 if (m68KPCNbrDisasmLines > (nbr / 2))
221 {
222 text->verticalScrollBar()->setValue(text->verticalScrollBar()->maximum());
223 }
224 else
225 {
226 text->verticalScrollBar()->setValue(text->verticalScrollBar()->minimum());
227 }
228
229 free(CurrentPtrFullSource);
230 }
231
232
233 // Set mem base PC address using the 68K pc current address
234 void m68KDasmWindow::Use68KPCAddress(void)
235 {
236 memBase = m68k_get_reg(NULL, M68K_REG_PC);
237 }
238
239
240 // Set mem base PC address
241 void m68KDasmWindow::SetAddress(int address)
242 {
243 memBase = address;
244 }