-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqhex.cpp
289 lines (245 loc) · 11.4 KB
/
qhex.cpp
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
#include <QApplication>
#include <QCheckBox>
#include <QColor>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QGridLayout>
#include <QGroupBox>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTableWidget>
#include <QTimer>
#include <QVBoxLayout>
#include "QHexView/document/buffer/qmemorybuffer.h"
#include "QHexView/qhexview.h"
#include "include/bus.h"
#include "include/instruction.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setLayout(new QHBoxLayout);
QHexView hexview(&w);
QGroupBox cpu_group(&w);
w.layout()->addWidget(&hexview);
w.layout()->addWidget(&cpu_group);
cpu_group.setLayout(new QVBoxLayout);
QGroupBox instruction_group("Instruction", &cpu_group);
cpu_group.layout()->addWidget(&instruction_group);
instruction_group.setLayout(new QHBoxLayout);
QLineEdit instruction(&instruction_group);
instruction.setReadOnly(true);
QLineEdit operand(&instruction_group);
operand.setReadOnly(true);
QLabel instruction_label("OP", &instruction_group);
instruction_group.layout()->addWidget(&instruction_label);
instruction_group.layout()->addWidget(&instruction);
instruction_group.layout()->addWidget(&operand);
QGroupBox reg_group("Registers", &cpu_group);
cpu_group.layout()->addWidget(®_group);
QLineEdit reg_A(®_group);
QLineEdit reg_X(®_group);
QLineEdit reg_Y(®_group);
QLineEdit reg_PC(®_group);
QLineEdit reg_SP(®_group);
reg_A.setInputMask("hh");
reg_X.setInputMask("hh");
reg_Y.setInputMask("hh");
reg_PC.setInputMask("hhhh");
reg_SP.setInputMask("hhhh");
QLabel reg_A_label("A", ®_group);
QLabel reg_X_label("X", ®_group);
QLabel reg_Y_label("Y", ®_group);
QLabel reg_PC_label("PC", ®_group);
QLabel reg_SP_label("SP", ®_group);
reg_A_label.setBuddy(®_A);
reg_X_label.setBuddy(®_X);
reg_Y_label.setBuddy(®_Y);
reg_PC_label.setBuddy(®_PC);
reg_SP_label.setBuddy(®_SP);
QGridLayout reg_layout;
reg_group.setLayout(®_layout);
reg_layout.addWidget(®_A, 0, 1);
reg_layout.addWidget(®_X, 1, 1);
reg_layout.addWidget(®_Y, 2, 1);
reg_layout.addWidget(®_PC, 3, 1);
reg_layout.addWidget(®_SP, 4, 1);
reg_layout.addWidget(®_A_label, 0, 0);
reg_layout.addWidget(®_X_label, 1, 0);
reg_layout.addWidget(®_Y_label, 2, 0);
reg_layout.addWidget(®_PC_label, 3, 0);
reg_layout.addWidget(®_SP_label, 4, 0);
QGroupBox flag_group("Flags", &cpu_group);
cpu_group.layout()->addWidget(&flag_group);
QCheckBox flag_C("C", &flag_group);
QCheckBox flag_Z("Z", &flag_group);
QCheckBox flag_I("I", &flag_group);
QCheckBox flag_D("D", &flag_group);
QCheckBox flag_B("B", &flag_group);
QCheckBox flag_U("U", &flag_group);
QCheckBox flag_V("V", &flag_group);
QCheckBox flag_N("N", &flag_group);
flag_group.setLayout(new QHBoxLayout);
flag_group.layout()->addWidget(&flag_N);
flag_group.layout()->addWidget(&flag_V);
flag_group.layout()->addWidget(&flag_U);
flag_group.layout()->addWidget(&flag_B);
flag_group.layout()->addWidget(&flag_D);
flag_group.layout()->addWidget(&flag_I);
flag_group.layout()->addWidget(&flag_Z);
flag_group.layout()->addWidget(&flag_C);
QGroupBox control_group("Controls", &cpu_group);
cpu_group.layout()->addWidget(&control_group);
control_group.setLayout(new QHBoxLayout);
QPushButton btn_load("Load File", &control_group);
QPushButton btn_go("Go", &control_group);
QPushButton btn_stop("Stop", &control_group);
QPushButton btn_next("Next", &control_group);
QPushButton btn_reset("Reset", &control_group);
QCheckBox trace("Trace", &control_group);
control_group.layout()->addWidget(&btn_load);
control_group.layout()->addWidget(&btn_go);
control_group.layout()->addWidget(&btn_stop);
control_group.layout()->addWidget(&btn_next);
control_group.layout()->addWidget(&btn_reset);
control_group.layout()->addWidget(&btn_reset);
control_group.layout()->addWidget(&trace);
QTableWidget tableWidget(256, 2, &cpu_group);
cpu_group.layout()->addWidget(&tableWidget);
/**************************/
/**************************/
Bus console;
QObject::connect(&btn_load, &QPushButton::clicked, [&w, &hexview, &console]() {
QString fileName = QFileDialog::getOpenFileName(&w, "Open File", QDir::homePath());
if (fileName.isEmpty()) {
return;
}
bool ok;
uint16_t addr = QInputDialog::getInt(&w, "Target address", "#", 0, 0, 64 * 1024, 1, &ok);
if (!ok) {
return;
}
QFile file(fileName);
file.open(QIODevice::ReadOnly);
file.read(reinterpret_cast<char*>(console.ram.memory.data()) + addr, console.ram.memory.size() - addr);
QHexDocument* document = QHexDocument::fromMemory<QMemoryBuffer>(
reinterpret_cast<char*>(console.ram.memory.data()), console.ram.memory.size());
hexview.setDocument(document); // Associate QHexEditData with this QHexEdit
document->metadata()->background(CPU::STACK_BASE_ADDR / document->hexLineWidth(), CPU::STACK_BASE_ADDR % document->hexLineWidth(),
1, Qt::green);
/*
// Document editing
QByteArray data = document->read(24, 78); // Read 78 bytes starting to offset 24
document->insert(4, "Hello QHexEdit"); // Insert a string to offset 4
document->remove(6, 10); // Delete bytes from offset 6 to offset 10
document->replace(30, "New Data"); // Replace bytes from offset 30 with the string "New Data"
// Metatadata management
QHexMetadata *hexmetadata = document->metadata();
hexmetadata->background(6, 0, 10, Qt::red); // Highlight background to line 6, from 0 to 10
hexmetadata->foreground(8, 0, 15, Qt::darkBlue); // Highlight foreground to line 8, from 0 to 15
hexmetadata->comment(16, 3, 1, "I'm a comment!"); // Add a comment to line 16
//hexmetadata->clear();
qDebug() << buffer;
*/
console.cpu.reset();
});
QObject::connect(&btn_next, &QPushButton::clicked, [&] {
while (!console.cpu.clock(true)) {
;
}
});
/*
QObject::connect(&btn_stop, &QPushButton::clicked, [&] {
while (true) {
#if 0
QList<QTableWidgetItem *> items = tableWidget.findItems(QString::number(console.cpu.registers.PC, 16), Qt::MatchFixedString);
if (items.isEmpty()) {
break;
}
#endif
auto previous_pc = console.cpu.registers.PC;
auto executed = console.cpu.clock(false);
if (executed && (previous_pc == console.cpu.registers.PC)) {
std::cout << "TRAP " << std::hex << previous_pc << std::endl;
console.cpu.signal_update();
break;
}
}
});
*/
auto update_gui = [&]() {
reg_A.setText(QString::number(console.cpu.registers.A, 16));
reg_X.setText(QString::number(console.cpu.registers.X, 16));
reg_Y.setText(QString::number(console.cpu.registers.Y, 16));
reg_PC.setText(QString::number(console.cpu.registers.PC, 16));
reg_SP.setText(QString::number(console.cpu.registers.SP, 16));
flag_I.setChecked(console.cpu.get_flag(CPU::FLAGS::I));
flag_B.setChecked(console.cpu.get_flag(CPU::FLAGS::B));
flag_C.setChecked(console.cpu.get_flag(CPU::FLAGS::C));
flag_D.setChecked(console.cpu.get_flag(CPU::FLAGS::D));
flag_N.setChecked(console.cpu.get_flag(CPU::FLAGS::N));
flag_U.setChecked(console.cpu.get_flag(CPU::FLAGS::U));
flag_V.setChecked(console.cpu.get_flag(CPU::FLAGS::V));
flag_Z.setChecked(console.cpu.get_flag(CPU::FLAGS::Z));
instruction.setText(
QString::fromStdString(InstructionSet::Table[console.cpu.read(console.cpu.registers.PC)].name));
//operand.setText(QString::number(console.cpu.fetched_operand, 16));
QHexDocument* document =
QHexDocument::fromMemory<QMemoryBuffer>((char*)console.ram.memory.data(), console.ram.memory.size());
hexview.setDocument(document);
document->metadata()->background(CPU::STACK_BASE_ADDR / document->hexLineWidth(), CPU::STACK_BASE_ADDR % document->hexLineWidth(),
1, Qt::green);
document->metadata()->background((CPU::STACK_BASE_ADDR + console.cpu.registers.SP) / document->hexLineWidth(),
(CPU::STACK_BASE_ADDR + console.cpu.registers.SP) % document->hexLineWidth(), 1,
Qt::blue);
document->metadata()->background(console.cpu.registers.PC / document->hexLineWidth(),
console.cpu.registers.PC % document->hexLineWidth(), 1, Qt::red);
document->cursor()->moveTo(console.cpu.registers.PC);
/*
for (int i = 0; i <= 0xff; i++) {
QTableWidgetItem *newItem = new QTableWidgetItem(QString::number(console.cpu.read(CPU::STACK_BASE_ADDR + 0xff - i), 16));
if (console.cpu.registers.SP == 0xff - i) {
newItem->setBackgroundColor(Qt::red);
}
newItem->setFlags(newItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
tableWidget.setItem(i, 1, newItem);
}*/
};
console.cpu.register_update_signal_callback(update_gui);
QObject::connect(&flag_C, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::C, flag_C.isChecked()); });
QObject::connect(&flag_Z, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::Z, flag_Z.isChecked()); });
QObject::connect(&flag_I, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::I, flag_I.isChecked()); });
QObject::connect(&flag_D, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::D, flag_D.isChecked()); });
QObject::connect(&flag_B, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::B, flag_B.isChecked()); });
QObject::connect(&flag_U, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::U, flag_U.isChecked()); });
QObject::connect(&flag_V, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::V, flag_V.isChecked()); });
QObject::connect(&flag_N, &QCheckBox::clicked, [&]() { console.cpu.set_flag(CPU::FLAGS::N, flag_N.isChecked()); });
QTimer tick;
auto run = [&] {
auto previous_pc = console.cpu.registers.PC;
auto executed = console.cpu.clock(trace.isChecked());
if (executed && (previous_pc == console.cpu.registers.PC)) {
tick.stop();
update_gui();
std::cout << "TRAP " <<std::hex << (unsigned) previous_pc << std::endl;
}
/*
QList<QTableWidgetItem *> items = tableWidget.findItems(QString::number(console.cpu.registers.PC, 16), Qt::MatchFixedString);
if (!items.isEmpty()) {
tick.stop();
}
*/
};
QObject::connect(&tick, &QTimer::timeout, run);
QObject::connect(&btn_stop, &QPushButton::clicked, [&tick] { tick.stop(); });
QObject::connect(&btn_go, &QPushButton::clicked, [&tick] { tick.start(0); });
QObject::connect(&btn_reset, &QPushButton::clicked, [&console, &tick] {
tick.stop();
console.cpu.reset();
});
w.show();
return a.exec();
}