Debugger sources code clean-up
[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 QLabel),
25 refresh(new QPushButton(tr("Refresh"))),
26 address(new QLineEdit),
27 go(new QPushButton(tr("Go"))),
28 memBase(0), memOrigin(0), NumWinOrigin(0)
29 {
30 address->setPlaceholderText("0x<value>, decimal value or symbol name");
31
32 QHBoxLayout * hbox1 = new QHBoxLayout;
33 hbox1->addWidget(refresh);
34 hbox1->addWidget(address);
35 hbox1->addWidget(go);
36
37 QFont fixedFont("Lucida Console", 8, QFont::Normal);
38 fixedFont.setStyleHint(QFont::TypeWriter);
39 text->setFont(fixedFont);
40 setLayout(layout);
41
42 layout->addWidget(text);
43 layout->addLayout(hbox1);
44
45 connect(refresh, SIGNAL(clicked()), this, SLOT(RefreshContentsWindow()));
46 connect(go, SIGNAL(clicked()), this, SLOT(GoToAddress()));
47 }
48
49
50 // Refresh / Display the window contents
51 void Memory1BrowserWindow::RefreshContents(size_t NumWin)
52 {
53 char string[100];
54
55 if (isVisible())
56 {
57 sprintf(string, "Memory %i - 0x%06X", (unsigned int)((NumWinOrigin = NumWin) + 1), (unsigned int)memOrigin);
58 setWindowTitle(tr(string));
59 RefreshContentsWindow();
60 }
61 }
62
63
64 // Refresh / Display the window contents
65 void Memory1BrowserWindow::RefreshContentsWindow(void)
66 {
67 char string[1024], buf[64];
68 QString memDump;
69 size_t i, j;
70 uint8_t c;
71
72 for (i = 0; i < 480; i += 16)
73 {
74 sprintf(string, "%s%06X: ", (i ? "<br>" : ""), (unsigned int)(memBase + i));
75
76 for (j = 0; j < 16; j++)
77 {
78 sprintf(buf, "%02X ", jaguarMainRAM[memBase + i + j]);
79 strcat(string, buf);
80 }
81
82 sprintf(buf, "| ");
83 strcat(string, buf);
84
85 for (j = 0; j < 16; j++)
86 {
87 c = jaguarMainRAM[memBase + i + j];
88 sprintf(buf, "&#%i;", c);
89
90 if (c == 0x20)
91 sprintf(buf, "&nbsp;");
92
93 if ((c < 0x20) || (c > 0x7E))
94 sprintf(buf, ".");
95
96 strcat(string, buf);
97 }
98
99 memDump += QString(string);
100 }
101
102 text->clear();
103 text->setText(memDump);
104 }
105
106
107 //
108 void Memory1BrowserWindow::keyPressEvent(QKeyEvent * e)
109 {
110 if (e->key() == Qt::Key_Escape)
111 {
112 hide();
113 }
114 else
115 {
116 if (e->key() == Qt::Key_PageUp)
117 {
118 if ((memBase -= 480) < 0)
119 {
120 memBase = 0;
121 }
122
123 RefreshContentsWindow();
124 }
125 else
126 {
127 if (e->key() == Qt::Key_PageDown)
128 {
129 if ((memBase += 480) > (vjs.DRAM_size - 480))
130 {
131 memBase = vjs.DRAM_size - 480;
132 }
133
134 RefreshContentsWindow();
135 }
136 else
137 {
138 if (e->key() == Qt::Key_Up || e->key() == Qt::Key_Minus)
139 {
140 if ((memBase -= 16) < 0)
141 {
142 memBase = 0;
143 }
144
145 RefreshContentsWindow();
146 }
147 else
148 {
149 if (e->key() == Qt::Key_Down || e->key() == Qt::Key_Equal)
150 {
151 if ((memBase += 16) > (vjs.DRAM_size - 480))
152 {
153 memBase = vjs.DRAM_size - 480;
154 }
155
156 RefreshContentsWindow();
157 }
158 else
159 {
160 if (e->key() == Qt::Key_Return)
161 {
162 GoToAddress();
163 }
164 }
165 }
166 }
167 }
168 }
169 }
170
171
172 // Go to the requested address
173 // Address can be an hexa, decimal or a symbol name
174 void Memory1BrowserWindow::GoToAddress(void)
175 {
176 bool ok;
177 size_t len;
178 QString newAddress;
179 size_t newmemBase;
180
181 QPalette p = address->palette();
182 newAddress = address->text();
183
184 if ((len = newAddress.size()))
185 {
186 if ((len > 1) && (newAddress.at(0) == QChar('0')) && (newAddress.at(1) == QChar('x')))
187 {
188 newmemBase = newAddress.toUInt(&ok, 16);
189 }
190 else
191 {
192 if (!(newmemBase = DBGManager_GetAdrFromSymbolName(newAddress.toLatin1().data())))
193 {
194 newmemBase = newAddress.toUInt(&ok, 10);
195 }
196 else
197 {
198 ok = true;
199 }
200 }
201
202 if (!ok || (newmemBase < 0) || (newmemBase > vjs.DRAM_size))
203 {
204 p.setColor(QPalette::Text, Qt::red);
205 }
206 else
207 {
208 p.setColor(QPalette::Text, Qt::black);
209 memOrigin = (memBase = newmemBase);
210 RefreshContents(NumWinOrigin);
211 }
212 address->setPalette(p);
213 }
214 }