-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmainmemory.cpp
More file actions
371 lines (300 loc) · 12.8 KB
/
Copy pathmainmemory.cpp
File metadata and controls
371 lines (300 loc) · 12.8 KB
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
// File: mainmemory.cpp
/*
Pep9CPU is a CPU simulator for executing microcode sequences to
implement instructions in the instruction set of the Pep/9 computer.
Copyright (C) 2010 J. Stanley Warford, Pepperdine University
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "mainmemory.h"
#include "ui_mainmemory.h"
#include "pep.h"
#include "sim.h"
#include <QScrollBar>
#include <QResizeEvent>
#include <QDebug>
MainMemory::MainMemory(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainMemory)
{
ui->setupUi(this);
ui->tableWidget->setFont(QFont(Pep::codeFont, Pep::codeFontSize));
ui->tableWidget->setColumnCount(1);
QStringList columns;
columns << "Hex";
ui->tableWidget->setHorizontalHeaderLabels(columns);
ui->tableWidget->setRowCount(1);
oldRowCount = 1;
rows << QString("0000");
ui->tableWidget->setVerticalHeaderLabels(rows);
int address = 0x0000;
ui->tableWidget->setItem(0, 0, new QTableWidgetItem("0x" + QString("%1").arg(Sim::readByte(address), 2, 16).toUpper().trimmed()));
refreshMemory();
ui->tableWidget->resize(ui->tableWidget->size());
populateMemoryItems();
connect(ui->verticalScrollBar, SIGNAL(actionTriggered(int)), this, SLOT(sliderMoved(int)));
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(scrollToChanged(QString)));
ui->scrollToLabel->setFont(QFont(ui->scrollToLabel->font().family(), ui->scrollToLabel->font().pointSize()));
ui->lineEdit->setFont(QFont(ui->lineEdit->font().family(), ui->lineEdit->font().pointSize()));
ui->tableWidget->setFont(QFont(Pep::labelFont, Pep::labelFontSize));
ui->tableWidget->viewport()->installEventFilter(this);
}
MainMemory::~MainMemory()
{
delete ui;
}
void MainMemory::populateMemoryItems()
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
rows.clear();
//qDebug() << "scroll value: " << QString("%1").arg(ui->verticalScrollBar->value(), 4, 16, QLatin1Char('0'));
int scrollBarValue = ui->verticalScrollBar->value();
for (int i = scrollBarValue; i < scrollBarValue + ui->tableWidget->rowCount(); i++) {
rows << QString("%1").arg(i, 4, 16, QLatin1Char('0')).toUpper();
}
ui->tableWidget->setVerticalHeaderLabels(rows);
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
refreshMemory();
}
void MainMemory::refreshMemory()
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
bool ok;
int address;
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
address = ui->tableWidget->verticalHeaderItem(i)->text().toInt(&ok, 16);
if (ok) {
ui->tableWidget->item(i, 0)->setText("0x" +
QString("%1").arg(Sim::readByte(address), 2, 16, QLatin1Char('0')).toUpper());
}
}
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
}
void MainMemory::setMemAddress(int memAddress, int value)
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
if (memAddress > 0xffff || memAddress < 0) {
qDebug() << "invalid address: " << memAddress;
}
if (value > 255) {
value = value & 255;
qDebug() << "setMemAddr of num larger than 255: val: " << value << ", addr: " << memAddress;
}
int firstAddress = ui->tableWidget->verticalHeaderItem(0)->text().toInt();
int lastAddress = firstAddress + ui->tableWidget->rowCount();
if (memAddress < firstAddress || lastAddress < memAddress) {
return;
}
int lineAddress;
for (int i = firstAddress; i < lastAddress; i++) {
lineAddress = ui->tableWidget->verticalHeaderItem(i)->text().toInt((bool*)&lineAddress,16);
if (lineAddress == memAddress) {
ui->tableWidget->item(i, 0)->setText("0x" + QString("%1").arg(value, 2, 16, QLatin1Char('0')).toUpper().trimmed());
}
}
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
}
void MainMemory::setMemPrecondition(int memAddress, int value)
{
Sim::writeByte(memAddress, value);
setMemAddress(memAddress, value);
}
bool MainMemory::testMemPostcondition(int memAddress, int value)
{
return Sim::readByte(memAddress) == value;
}
void MainMemory::clearMemory()
{
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
ui->tableWidget->item(i, 0)->setText("0x00");
}
Sim::clearMemory();
}
void MainMemory::showMemEdited(int address)
{
scrollToAddress(address);
populateMemoryItems();
hightlightModifiedBytes();
}
void MainMemory::hightlightModifiedBytes()
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
if (Sim::modifiedBytes.isEmpty()) {
// clear all highlighted cells
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
ui->tableWidget->itemAt(0, i)->setBackgroundColor(Qt::white);
}
return;
}
// for each item in the table:
for (int i = 0; i < ui->tableWidget->rowCount() - 1; i++) {
bool ok;
int j = ui->tableWidget->verticalHeaderItem(i)->text().right(4).toInt(&ok, 16);
if (ok && Sim::modifiedBytes.contains(j)) {
ui->tableWidget->itemAt(0, i)->setBackgroundColor(Qt::green);
}
else {
ui->tableWidget->itemAt(0, i)->setBackgroundColor(Qt::white);
}
}
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
}
void MainMemory::scrollToAddress(int address)
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
if (address >= 0 && address <= 0xffff) { // defensive programming!
if (address > ui->verticalScrollBar->maximum()) { // ensure we only scroll to the bottom, and don't show values larger than 0xffff
ui->verticalScrollBar->setValue(ui->verticalScrollBar->maximum());
}
else { // simple case:
ui->verticalScrollBar->setValue(address);
}
}
// else, ignore, we're getting told to do something out of the correct range.
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
hightlightModifiedBytes();
}
void MainMemory::highlightOnFocus()
{
if (ui->tableWidget->hasFocus()) {
ui->label->setAutoFillBackground(true);
}
else {
ui->label->setAutoFillBackground(false);
}
}
bool MainMemory::hasFocus()
{
return ui->tableWidget->hasFocus();
}
void MainMemory::sliderMoved(int pos)
{
qDebug() << "slider moved: " << pos;
populateMemoryItems();
hightlightModifiedBytes();
}
void MainMemory::cellDataChanged(QTableWidgetItem *item)
{
// disconnect this signal so that modifying the text of the column next to it doesn't fire this signal; reconnect at the end
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
int row = item->row();
QString contents = item->text();
contents = contents.trimmed();
QRegExp rx = item->column() == 0 ? QRegExp("(0x)?[0-9a-fA-F]+") : QRegExp("[0-9a-fA-F]+");
bool addrConvOk;
bool dataOk;
int base = contents.startsWith("0x", Qt::CaseInsensitive) ? 16 : 10;
int address = ui->tableWidget->verticalHeaderItem(row)->text().toInt(&addrConvOk, 16);
int data = item->text().toInt(&dataOk, base);
data = data % 256;
if (contents.contains(rx) && dataOk && addrConvOk) {
Sim::writeByte(address, data);
qDebug() << "Sim::Mem[" << address << "]: " << Sim::readByte(address);
ui->tableWidget->item(row, 0)->setText("0x" + QString("%1").arg(data, 2, 16, QLatin1Char('0')).toUpper().trimmed());
}
else if (addrConvOk && !dataOk) {
qDebug() << "Conversion from text to int failed. data = " << item->text();
data = Sim::readByte(address);
ui->tableWidget->item(row, 0)->setText("0x" + QString("%1").arg(data, 2, 16, QLatin1Char('0')).toUpper().trimmed());
}
else if (addrConvOk) { // we have problems, the labels are incorrectly formatted
populateMemoryItems(); //ui->verticalScrollBar->value());
}
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
}
void MainMemory::scrollToChanged(QString string)
{
bool ok;
int byte;
if (string.startsWith("0x", Qt::CaseInsensitive)) {
byte = string.toInt(&ok, 16);
if (ok) {
if (byte > 65535) {
ui->lineEdit->setText("0xFFFF");
} else {
ui->verticalScrollBar->setValue(byte);
sliderMoved(0);
}
}
else {
ui->lineEdit->setText("0x");
}
}
else {
ui->lineEdit->setText("0x");
}
// make sure the cells are correctly highlighted
hightlightModifiedBytes();
}
void MainMemory::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainMemory::resizeEvent(QResizeEvent *)
{
int newRowCount = ui->tableWidget->height()/ui->tableWidget->rowHeight(0) - 1;
// Set the maximum row count to be 64k - (num visible rows)
ui->verticalScrollBar->setMaximum(ui->verticalScrollBar->maximum() - newRowCount + 1);
// make sure the scroll bar stays at the bottom when resizing
if (ui->verticalScrollBar->value() > ui->verticalScrollBar->maximum() - newRowCount) {
ui->verticalScrollBar->setValue(ui->verticalScrollBar->maximum()); //0xffff - newRowCount);
}
if (newRowCount > oldRowCount) {
ui->tableWidget->setRowCount(newRowCount);
bool addrConvOk;
int address;
for (int i = oldRowCount; i < newRowCount; i++) {
rows << QString("%1").arg(i, 4, 16, QLatin1Char('0')).toUpper();
}
ui->tableWidget->setVerticalHeaderLabels(rows);
disconnect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
for (int row = oldRowCount; row < newRowCount; row++) {
address = ui->tableWidget->verticalHeaderItem(row)->text().toInt(&addrConvOk, 16);
if (addrConvOk) {
ui->tableWidget->setItem(row, 0, new QTableWidgetItem("0x" + QString("%1").arg(Sim::readByte(address), 2, 16).toUpper().trimmed()));
//ui->tableWidget->itemAt(row, 0)->setFont(QFont(Pep::codeFont, Pep::codeFontSize));
}
else { // malformed address labels
}
}
connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(cellDataChanged(QTableWidgetItem*)));
refreshMemory();
}
else if (oldRowCount > newRowCount) {
ui->tableWidget->setRowCount(newRowCount);
for (int i = oldRowCount; i > newRowCount; i--) {
delete ui->tableWidget->item(i, 0);
rows.removeLast();
}
refreshMemory();
}
}
bool MainMemory::eventFilter(QObject *, QEvent *e)
{
if (e->type() == QEvent::Wheel) {
qApp->sendEvent(ui->verticalScrollBar, e);
return true;
}
return false;
}