f83120b84b0819d9e91757a027a561afc2084eb3
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / localbrowser.cpp
1 //
2 // localbrowser.cpp - Local variables
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -----------------------------------------------------------
10 // JPM 11/03/2017 Created this file
11 //
12
13
14 #include "debugger/localbrowser.h"
15 #include "memory.h"
16 #include "debugger/DBGManager.h"
17 #include "settings.h"
18 #include "m68000/m68kinterface.h"
19
20
21 //
22 LocalBrowserWindow::LocalBrowserWindow(QWidget * parent/*= 0*/) : QWidget(parent, Qt::Dialog),
23 layout(new QVBoxLayout), text(new QTextBrowser),
24 // layout(new QVBoxLayout), text(new QLabel),
25 // refresh(new QPushButton(tr("Refresh"))),
26 // address(new QLineEdit),
27 // go(new QPushButton(tr("Go"))),
28 // memBase(0),
29 NbLocal(0),
30 FuncName((char *)calloc(1, 1)),
31 LocalInfo(NULL)
32 {
33 setWindowTitle(tr("Local"));
34
35 // address->setInputMask("hhhhhh");
36 // QHBoxLayout * hbox1 = new QHBoxLayout;
37 // hbox1->addWidget(refresh);
38 // hbox1->addWidget(address);
39 // hbox1->addWidget(go);
40
41 // Need to set the size as well...
42 // resize(560, 480);
43
44 QFont fixedFont("Lucida Console", 8, QFont::Normal);
45 // QFont fixedFont("", 8, QFont::Normal);
46 fixedFont.setStyleHint(QFont::TypeWriter);
47 text->setFont(fixedFont);
48 //// layout->setSizeConstraint(QLayout::SetFixedSize);
49 setLayout(layout);
50
51 layout->addWidget(text);
52 // layout->addWidget(refresh);
53 // layout->addLayout(hbox1);
54
55 // connect(refresh, SIGNAL(clicked()), this, SLOT(RefreshContents()));
56 // connect(go, SIGNAL(clicked()), this, SLOT(GoToAddress()));
57 }
58
59
60 //
61 LocalBrowserWindow::~LocalBrowserWindow(void)
62 {
63 free(LocalInfo);
64 free(FuncName);
65 // NbLocal = 0;
66 }
67
68
69 //
70 bool LocalBrowserWindow::UpdateInfos(void)
71 {
72 size_t Adr;
73 char *Ptr;
74
75 if (NbLocal = DBGManager_GetNbLocalVariables(Adr = m68k_get_reg(NULL, M68K_REG_PC)))
76 {
77 if (Ptr = DBGManager_GetFunctionName(Adr))
78 {
79 if (strcmp(FuncName, Ptr))
80 {
81 if (FuncName = (char *)realloc(FuncName, strlen(Ptr) + 1))
82 {
83 strcpy(FuncName, Ptr);
84
85 if (LocalInfo = (WatchInfo *)realloc(LocalInfo, (sizeof(WatchInfo) * NbLocal)))
86 {
87 for (size_t i = 0; i < NbLocal; i++)
88 {
89 // Get local variable name and his information
90 if (LocalInfo[i].PtrVariableName = DBGManager_GetLocalVariableName(Adr, i + 1))
91 {
92 LocalInfo[i].Op = DBGManager_GetLocalVariableOp(Adr, i + 1);
93 LocalInfo[i].Adr = NULL;
94 LocalInfo[i].PtrCPURegisterName = NULL;
95 LocalInfo[i].TypeTag = DBGManager_GetLocalVariableTypeTag(Adr, i + 1);
96 LocalInfo[i].PtrVariableBaseTypeName = DBGManager_GetLocalVariableTypeName(Adr, i + 1);
97 LocalInfo[i].TypeEncoding = DBGManager_GetLocalVariableTypeEncoding(Adr, i + 1);
98 LocalInfo[i].TypeByteSize = DBGManager_GetLocalVariableTypeByteSize(Adr, i + 1);
99 LocalInfo[i].Offset = DBGManager_GetLocalVariableOffset(Adr, i + 1);
100 }
101 }
102 }
103 }
104 }
105
106 return true;
107 }
108 }
109
110 *FuncName = 0;
111
112 return false;
113 }
114
115
116 //
117 void LocalBrowserWindow::RefreshContents(void)
118 {
119 char string[1024];
120 // char buf[64];
121 QString Local;
122 char Value[100];
123 char *PtrValue;
124 // size_t NbWatch, Adr;
125 // WatchInfo PtrLocalInfo;
126
127 const char *CPURegName[] = { "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7" };
128
129 if (isVisible())
130 {
131 if (UpdateInfos())
132 {
133 //#ifdef _MSC_VER
134 //#pragma message("Warning: !!! Need to check the memory desalocation for LocalInfo !!!")
135 //#else
136 //#warning "!!! Need to do the memory desalocation for LocalInfo !!!"
137 //#endif // _MSC_VER
138 //#ifdef _MSC_VER
139 //#pragma message("Warning: !!! Need to check the memory desalocation for FuncName !!!")
140 //#else
141 //#warning "!!! Need to do the memory desalocation for FuncName !!!"
142 //#endif // _MSC_VER
143
144 for (size_t i = 0; i < NbLocal; i++)
145 {
146 if (LocalInfo[i].PtrVariableName)
147 {
148 // Local or parameters variables
149 if (((LocalInfo[i].Op >= DBG_OP_breg0) && (LocalInfo[i].Op <= DBG_OP_breg31)) || (LocalInfo[i].Op == DBG_OP_fbreg))
150 {
151 LocalInfo[i].Adr = m68k_get_reg(NULL, M68K_REG_A6) + LocalInfo[i].Offset;
152
153 if ((LocalInfo[i].Op == DBG_OP_fbreg))
154 {
155 LocalInfo[i].Adr += 8;
156 }
157
158 if ((LocalInfo[i].Adr >= 0) && (LocalInfo[i].Adr < vjs.DRAM_size))
159 {
160 PtrValue = DBGManager_GetVariableValueFromAdr(LocalInfo[i].Adr, LocalInfo[i].TypeEncoding, LocalInfo[i].TypeByteSize);
161 }
162 else
163 {
164 PtrValue = NULL;
165 }
166 }
167 else
168 {
169 // Value from CPU register
170 if ((LocalInfo[i].Op >= DBG_OP_reg0) && (LocalInfo[i].Op <= DBG_OP_reg31))
171 {
172 LocalInfo[i].PtrCPURegisterName = (char *)CPURegName[(LocalInfo[i].Op - DBG_OP_reg0)];
173 PtrValue = itoa(m68k_get_reg(NULL, (m68k_register_t)((size_t)M68K_REG_D0 + (LocalInfo[i].Op - DBG_OP_reg0))), Value, 10);
174 }
175 else
176 {
177 PtrValue = NULL;
178 }
179 }
180
181 if (!LocalInfo[i].Op)
182 {
183 sprintf(string, "<font color='#A52A2A'>%i : %s | %s | [Not used]</font>", (i + 1), (LocalInfo[i].PtrVariableBaseTypeName ? LocalInfo[i].PtrVariableBaseTypeName : (char *)"<font color='#ff0000'>N/A</font>"), LocalInfo[i].PtrVariableName);
184 }
185 else
186 {
187 sprintf(string, "%i : %s | %s | ", (i + 1), (LocalInfo[i].PtrVariableBaseTypeName ? LocalInfo[i].PtrVariableBaseTypeName : (char *)"<font color='#ff0000'>N/A</font>"), LocalInfo[i].PtrVariableName);
188 Local += QString(string);
189 if ((unsigned int)LocalInfo[i].Adr)
190 {
191 sprintf(string, "0x%06X", (unsigned int)LocalInfo[i].Adr);
192 }
193 else
194 {
195 if (LocalInfo[i].PtrCPURegisterName)
196 {
197 sprintf(string, "<font color='#0000FF'>%s</font>", LocalInfo[i].PtrCPURegisterName);
198 }
199 else
200 {
201 sprintf(string, "%s", (char *)"<font color='#ff0000'>N/A</font>");
202 }
203 }
204 Local += QString(string);
205 sprintf(string, " | %s", (!PtrValue ? (char *)"<font color='#ff0000'>N/A</font>" : PtrValue));
206 }
207 Local += QString(string);
208 sprintf(string, "<br>");
209 Local += QString(string);
210 }
211 }
212
213 text->clear();
214 text->setText(Local);
215 }
216 else
217 {
218 text->clear();
219 }
220 }
221 }
222
223
224 //
225 void LocalBrowserWindow::keyPressEvent(QKeyEvent * e)
226 {
227 if (e->key() == Qt::Key_Escape)
228 {
229 hide();
230 }
231 #if 0
232 else if (e->key() == Qt::Key_PageUp)
233 {
234 memBase -= 480;
235
236 if (memBase < 0)
237 memBase = 0;
238
239 RefreshContents();
240 }
241 else if (e->key() == Qt::Key_PageDown)
242 {
243 memBase += 480;
244
245 if (memBase > (0x200000 - 480))
246 memBase = 0x200000 - 480;
247
248 RefreshContents();
249 }
250 else if (e->key() == Qt::Key_Up || e->key() == Qt::Key_Minus)
251 {
252 memBase -= 16;
253
254 if (memBase < 0)
255 memBase = 0;
256
257 RefreshContents();
258 }
259 else if (e->key() == Qt::Key_Down || e->key() == Qt::Key_Equal)
260 {
261 memBase += 16;
262
263 if (memBase > (0x200000 - 480))
264 memBase = 0x200000 - 480;
265
266 RefreshContents();
267 }
268 #endif
269 }
270
271
272 #if 0
273 void LocalBrowserWindow::GoToAddress(void)
274 {
275 bool ok;
276 QString newAddress = address->text();
277 memBase = newAddress.toUInt(&ok, 16);
278 RefreshContents();
279 }
280 #endif
281