The address provided in the memory window is now verified to prevent crash
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / memory1browser.cpp
1 //
2 // memory1browser.cpp - Jaguar memory window 1 browser
3 //
4 // by Jean-Paul Mari
5 //
6 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
7 //
8 // Who When What
9 // --- ---------- -----------------------------------------------------------
10 // JPM 08/07/2017 Created this file
11 //
12
13 // STILL TO DO:
14 //
15
16 #include "memory1browser.h"
17 #include "memory.h"
18 #include "debugger/DBGManager.h"
19 #include "settings.h"
20
21
22 //
23 Memory1BrowserWindow::Memory1BrowserWindow(QWidget * parent/*= 0*/): QWidget(parent, Qt::Dialog),
24 // layout(new QVBoxLayout), text(new QTextBrowser),
25 layout(new QVBoxLayout), text(new QLabel),
26 refresh(new QPushButton(tr("Refresh"))),
27 address(new QLineEdit),
28 go(new QPushButton(tr("Go"))),
29 memBase(0), memOrigin(0), NumWinOrigin(0)
30 {
31 //setWindowTitle(tr("Memory 1 Browser"));
32
33 //address->setInputMask("hhhhhh");
34 address->setPlaceholderText("0x<value>, decimal value or symbol name");
35
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(RefreshContentsWindow()));
56 connect(go, SIGNAL(clicked()), this, SLOT(GoToAddress()));
57 }
58
59
60 // Refresh / Display the window contents
61 void Memory1BrowserWindow::RefreshContents(size_t NumWin)
62 {
63 char string[100];
64
65 if (isVisible())
66 {
67 sprintf(string, "Memory %i - 0x%06X", (unsigned int)((NumWinOrigin = NumWin) + 1), (unsigned int)memOrigin);
68 setWindowTitle(tr(string));
69 RefreshContentsWindow();
70 }
71 }
72
73
74 // Refresh / Display the window contents
75 void Memory1BrowserWindow::RefreshContentsWindow(void)
76 {
77 char string[1024], buf[64];
78 QString memDump;
79 size_t i, j;
80 uint8_t c;
81
82 for (i = 0; i < 480; i += 16)
83 {
84 sprintf(string, "%s%06X: ", (i ? "<br>" : ""), (unsigned int)(memBase + i));
85
86 for (j = 0; j < 16; j++)
87 {
88 sprintf(buf, "%02X ", jaguarMainRAM[memBase + i + j]);
89 strcat(string, buf);
90 }
91
92 sprintf(buf, "| ");
93 strcat(string, buf);
94
95 for (j = 0; j < 16; j++)
96 {
97 c = jaguarMainRAM[memBase + i + j];
98 sprintf(buf, "&#%i;", c);
99
100 if (c == 0x20)
101 sprintf(buf, "&nbsp;");
102
103 if ((c < 0x20) || (c > 0x7E))
104 sprintf(buf, ".");
105
106 strcat(string, buf);
107 }
108
109 memDump += QString(string);
110 }
111
112 text->clear();
113 text->setText(memDump);
114 }
115
116
117 //
118 void Memory1BrowserWindow::keyPressEvent(QKeyEvent * e)
119 {
120 if (e->key() == Qt::Key_Escape)
121 {
122 hide();
123 }
124 else
125 {
126 if (e->key() == Qt::Key_PageUp)
127 {
128 memBase -= 480;
129
130 if (memBase < 0)
131 {
132 memBase = 0;
133 }
134
135 RefreshContentsWindow();
136 }
137 else
138 {
139 if (e->key() == Qt::Key_PageDown)
140 {
141 memBase += 480;
142
143 if (memBase > (vjs.DRAM_size - 480))
144 {
145 memBase = vjs.DRAM_size - 480;
146 }
147
148 RefreshContentsWindow();
149 }
150 else
151 {
152 if (e->key() == Qt::Key_Up || e->key() == Qt::Key_Minus)
153 {
154 memBase -= 16;
155
156 if (memBase < 0)
157 {
158 memBase = 0;
159 }
160
161 RefreshContentsWindow();
162 }
163 else
164 {
165 if (e->key() == Qt::Key_Down || e->key() == Qt::Key_Equal)
166 {
167 memBase += 16;
168
169 if (memBase > (vjs.DRAM_size - 480))
170 {
171 memBase = vjs.DRAM_size - 480;
172 }
173
174 RefreshContentsWindow();
175 }
176 else
177 {
178 if (e->key() == Qt::Key_Return)
179 {
180 GoToAddress();
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188
189
190 // Go to the requested address
191 // Address can be an hexa, decimal or a symbol name
192 void Memory1BrowserWindow::GoToAddress(void)
193 {
194 bool ok;
195 QString newAddress;
196 size_t newmemBase;
197
198 QPalette p = address->palette();
199 newAddress = address->text();
200
201 if (newAddress.size())
202 {
203 if ((newAddress.at(0) == QChar('0')) && (newAddress.at(1) == QChar('x')))
204 {
205 newmemBase = newAddress.toUInt(&ok, 16);
206 }
207 else
208 {
209 if (!(newmemBase = DBGManager_GetAdrFromSymbolName(newAddress.toLatin1().data())))
210 {
211 newmemBase = newAddress.toUInt(&ok, 10);
212 }
213 else
214 {
215 ok = true;
216 }
217 }
218
219 if (!ok || (newmemBase < 0) || (newmemBase > vjs.DRAM_size))
220 {
221 p.setColor(QPalette::Text, Qt::red);
222 }
223 else
224 {
225 p.setColor(QPalette::Text, Qt::black);
226 memOrigin = (memBase = newmemBase);
227 RefreshContents(NumWinOrigin);
228 }
229 address->setPalette(p);
230 }
231 }