-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
449 lines (420 loc) · 14.4 KB
/
mainwindow.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QHBoxLayout>
#include <QtMath>
#include <QTableWidget>
#include <QHeaderView>
#include <QButtonGroup>
#include <QPushButton>
#include <QFile>
#include "box256glwidget.h"
#include "tablecelledit.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, dialogLoad(this)
, playTimer()
{
ui->setupUi(this);
ui->gridLayout->addWidget(new QLabel("Source",this),0,0);
ui->gridLayout->addWidget(new QLabel("Memory",this),0,1);
ui->gridLayout->addWidget(new QLabel("Output",this),0,2);
QTableWidget *srcTable = new QTableWidget(64,4,this);
QTableWidget *memTable = new QTableWidget(64,4,this);
Box256GLWidget* box256Widget = new Box256GLWidget(&machine);
ui->gridLayout->addWidget(srcTable,1,0,Qt::AlignLeft);
ui->gridLayout->addWidget(memTable,1,1,Qt::AlignLeft);
ui->gridLayout->addWidget(box256Widget,1,2,1,2);
QPushButton *stopButton = new QPushButton("Stop",this);
QPushButton *stepButton = new QPushButton("Step",this);
QPushButton *playButton = new QPushButton("Play",this);
ui->gridLayout->addWidget(stopButton, 2,0);
ui->gridLayout->addWidget(stepButton, 2,1);
ui->gridLayout->addWidget(playButton, 2,2);
connect(stopButton, SIGNAL(released()), this, SLOT(stopMachine()));
connect(stepButton, SIGNAL(released()), this, SLOT(stepBtnReleased()));
connect(playButton, SIGNAL(released()), this, SLOT(playMachine()));
//Setup table labels
QStringList srcColLabels = {"Cmd","A","B","C"};
QStringList srcRowLabels = {};
for(int i=0;i<256;i+=4)
{
QString lbl = "";
lbl=getHexNum(i,1);
srcRowLabels<<lbl;
}
srcTable->setVerticalHeaderLabels(srcRowLabels);
srcTable->setHorizontalHeaderLabels(srcColLabels);
QStringList memColLabels = {"","","",""};
QStringList memRowLabels = {};
for(int i=0;i<256;i+=4)
{
QString lbl = "";
lbl=getHexNum(i,1);
memRowLabels<<lbl;
}
memTable->setVerticalHeaderLabels(memRowLabels);
memTable->setHorizontalHeaderLabels(memColLabels);
//Setup table sizes
for(int c=0;c<4;c++)
{
srcTable->setColumnWidth(c,30);
memTable->setColumnWidth(c,10);
}
srcTable->setMaximumSize(190,16777215);
memTable->setMaximumSize(190,16777215);
//Setup source table
for(int r=0;r<64;r++)
{
for(int c=0;c<4;c++)
{
auto cellText = new TableCellEdit(srcTable,r,c);
cellTexts[r][c]=cellText;
cellText->setPlainText(getHexNum(0,0));
srcTable->setCellWidget(r,c,cellText);
connect(cellText,SIGNAL(textChanged()),this,SLOT(on_srcCellChanged()));
}
}
//Setup memory table
for(BOXBYTE r=0;r<64;r++)
{
for(BOXBYTE c=0;c<4;c++)
{
auto data = machine.getValue(AccessMethod::ADDRESS,r*4 + c);
auto cellText = new QLabel(getHexNum(data),memTable);
memLabels[r][c] = cellText;
cellText->setAlignment(Qt::AlignCenter);
memTable->setCellWidget(r,c,cellText);
}
}
memTable->setShowGrid(false);
memTable->horizontalHeader()->hide();
on_actionLoad_Example_Program_triggered();
}
void MainWindow::loadBoxFile(const QString& fileName)
{
QFile boxFile(fileName);
QTextStream boxStream(&boxFile);
if(!boxFile.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this,"Could not load box file.",
"Error: <b>"+ boxFile.errorString()+"</b>");
}
setMachineSource(boxStream);
boxFile.close();
}
void MainWindow::setMachineSource(QTextStream& boxStream)
{
int r=0;
while(!boxStream.atEnd())
{
auto fields = boxStream.readLine().split(" ");
if(r>=64)
{
QMessageBox::warning(this,"Could not load box file.",
"The save file had too many lines to fit in the source table.<br/>"
"Could only load <b>" + QString::number(r)+"</b> lines");
break;
}
cellTexts[r][0]->setPlainText("0");
cellTexts[r][1]->setPlainText("0");
cellTexts[r][2]->setPlainText("0");
cellTexts[r][3]->setPlainText("0");
if(fields.length()>=1){
cellTexts[r][0]->setPlainText(fields[0]);
}
if(fields.length()>=2){
cellTexts[r][1]->setPlainText(fields[1]);
}
if(fields.length()>=3){
cellTexts[r][2]->setPlainText(fields[2]);
}
if(fields.length()>=4){
cellTexts[r][3]->setPlainText(fields[3]);
}
r++;
}
}
void MainWindow::setMachineSource(QString& src)
{
QTextStream boxStream(&src);
setMachineSource(boxStream);
}
QString MainWindow::getMachineSource()
{
QString srcCode ="";
for(BOXBYTE r=0;r<64;r++)
{
auto cmdText = cellTexts[r][0]->toPlainText();
auto pAText = cellTexts[r][1]->toPlainText();
auto pBText = cellTexts[r][2]->toPlainText();
auto pCText = cellTexts[r][3]->toPlainText();
srcCode+=cmdText + " " + pAText+ " " + pBText+ " " + pCText + "\n";
}
return srcCode;
}
void MainWindow::stopMachine()
{
machine.reset();
playTimer.stop();
updateMemoryLabels();
}
AccessMethod getAccessMethodFromSymbol(QChar c)
{
if(c=='0' || c=='-')return AccessMethod::CONSTANT;
if(c=='@')return AccessMethod::ADDRESS;
if(c=='*')return AccessMethod::POINTER;
return AccessMethod::NONE;
}
void MainWindow::stepBtnReleased()
{
playTimer.stop();
stepMachine();
}
void MainWindow::stepMachine()
{
if(machine.curCycle==0)//Convert source to memory code on start.
{
for(BOXBYTE r=0;r<64;r++)
{
auto cmdText = cellTexts[r][0]->toPlainText();
auto pAText = cellTexts[r][1]->toPlainText();
auto pBText = cellTexts[r][2]->toPlainText();
auto pCText = cellTexts[r][3]->toPlainText();
//Add constant addressing in the case of a number that has no zero at the start.
if(pAText[0]!='0' && pAText[0]!='@' && pAText[0]!='*'){pAText.insert(0,'0');}
if(pBText[0]!='0' && pBText[0]!='@' && pBText[0]!='*'){pBText.insert(0,'0');}
if(pCText[0]!='0' && pCText[0]!='@' && pCText[0]!='*'){pCText.insert(0,'0');}
//Commutative expressions
if(cmdText=="ADD" || cmdText == "MUL")
{
//Ensure that the constant parameter(if any) is second
if(getAccessMethodFromSymbol(pAText[0])==AccessMethod::CONSTANT)
{
auto tmp = pBText;
pBText = pAText;
pAText = tmp;
}
//Ensure address is after pointer if needed.
if(getAccessMethodFromSymbol(pAText[0])==AccessMethod::ADDRESS
&& getAccessMethodFromSymbol(pBText[0])==AccessMethod::POINTER)
{
if((cmdText=="ADD" && getAccessMethodFromSymbol(pCText[0])==AccessMethod::POINTER)
|| cmdText=="MUL")
{//Ensure address is second for MUL or if pointer is third for ADD to be true to spec.
auto tmp = pBText;
pBText = pAText;
pAText = tmp;
}
}
}
//Algebraic expressions
if(cmdText=="ADD"||cmdText=="SUB"||cmdText=="MUL"||cmdText=="DIV"||cmdText=="MOD")
{
if(getAccessMethodFromSymbol(pAText[0])==AccessMethod::CONSTANT&&
getAccessMethodFromSymbol(pBText[0])==AccessMethod::CONSTANT)
{//Can be compiled into MOV.
BOXBYTE constA = pAText.toInt(nullptr,16);
BOXBYTE constB = pBText.toInt(nullptr,16);
BOXBYTE result = 0x0;
QString storeAddrStr = pCText;
if(cmdText=="ADD"){
result = constA + constB;
}
if(cmdText=="SUB"){
result = constA - constB;
}
if(cmdText=="MUL"){
result = constA * constB;
}
if(cmdText=="DIV"){
if(constB==0)result=0;
else result = constA / constB;
}
if(cmdText=="MOD"){
if(constB==0)result=0;
else result = constA % constB;
}
cmdText = "MOV";
pAText = getHexNum(result,2);
pBText = storeAddrStr;
pCText = "001";
}
}
if(cmdText=="MOV" || cmdText == "FLP")
{
if(getAccessMethodFromSymbol(pCText[0])==AccessMethod::CONSTANT)
{
BOXBYTE constC = pCText.toInt(nullptr,16);
if(constC==0)
{
pCText = "001";//Source code compiles to at least one MOV / FLP, lest it be dead code.
}
}
}
BOXBYTE op = machine.getOpcodeFromCommand(cmdText,
getAccessMethodFromSymbol(pAText[0]),
getAccessMethodFromSymbol(pBText[0]),
getAccessMethodFromSymbol(pCText[0]));
if(cmdText[0]=='0')//If number stored, do not write as opcode.
{
cmdText.remove(0,1);
if(cmdText=="")cmdText="0";
bool isCmdNum;
BOXBYTE i = cmdText.toInt(&isCmdNum,16);
if(isCmdNum)op=i;
}
//Get rid of addressing symbol
pAText.remove(0,1);
pBText.remove(0,1);
pCText.remove(0,1);
//Get values
bool okA, okB, okC;
BOXBYTE numA = static_cast<BOXBYTE>(pAText.toInt(&okA,16));
BOXBYTE numB = static_cast<BOXBYTE>(pBText.toInt(&okB,16));
BOXBYTE numC = static_cast<BOXBYTE>(pCText.toInt(&okC,16));
if(!okA)numA=0;
if(!okB)numB=0;
if(!okC)numC=0;
machine.writeValue(op,r*0x04 + 0x0);
machine.writeValue(numA,r*0x04 + 0x1);
machine.writeValue(numB,r*0x04 + 0x2);
machine.writeValue(numC,r*0x04 + 0x3);
}
machine.flushNewData();
}
if(machine.curCycle==0)
{
machine.curCycle+=1;
}else
{
machine.step();
}
//Update memory table after step.
updateMemoryLabels();
}
void MainWindow::updateMemoryLabels()
{
for(BOXBYTE r=0;r<64;r++){
for(BOXBYTE c=0;c<4;c++){
auto data = machine.getValue(AccessMethod::ADDRESS,r*4 + c);
auto label = memLabels[r][c];
label->setText(getHexNum(data,1));
label->setStyleSheet("QLabel {background: white}");
QString tooltip = "";
if(data==0)
{
label->setStyleSheet("QLabel {background: darkgrey}");
}
if(machine.dataJustWritten[r*4 + c])
{
label->setStyleSheet("QLabel {background: gold}");
}
for(int t=0;t<machine.getNumThreads();t++)
{
int pc = machine.getValue(AccessMethod::ADDRESS,machine.getPC(t));
int pcLoc = machine.getValue(AccessMethod::CONSTANT,machine.getPC(t));
if((pc >= r*4 + c - 3) && (pc <= r*4 + c ))
{
label->setStyleSheet("QLabel {background: blue}");
tooltip+="Thread"+QString::number(t)+"<br/>";
}
if(pcLoc == r*4 + c)
{
label->setStyleSheet("QLabel {background: limegreen}");
}
}
label->setToolTip(tooltip);
}
}
QLabel* memLabel = dynamic_cast<QLabel*>(ui->gridLayout->itemAtPosition(0,1)->widget());
if(memLabel!=nullptr)
memLabel->setText("Memory, " + QString::number(machine.getNumThreads())+" threads");
}
void MainWindow::timerEvent(QTimerEvent * timer)
{
stepMachine();
}
void MainWindow::playMachine()
{
playTimer.start(1000 / 60,this);
/*for(int i=0;i<100;i++)
{
stepMachine();
}*/
}
void MainWindow::on_srcCellChanged()
{
auto srcTblCell = static_cast<TableCellEdit*>(sender());
QString text = srcTblCell->toPlainText();
text = text.toUpper();
if(text.length()>3)//Limit size of input
{
text = ""+text[0]+text[1]+text[2];
}
QChar c1 = text[0];
QChar c2 = text[1];
QChar c3 = text[2];
if(srcTblCell->getCellColumn()==0)
{
text="";
if(c1.isLetter()||c1.isNumber())text+=c1;
if(c2.isLetter()||c2.isNumber())text+=c2;
if(c3.isLetter()||c3.isNumber())text+=c3;
}
if(srcTblCell->getCellColumn()>0)
{
text="";
if(c1.isNumber()||c1=="-"||c1=="@"||c1=="*"){
text+=c1;
}
if(c2.isNumber() || (c2>='A' && c2<='F')){
text+=c2;
}
if(c3.isNumber() || (c3>='A' && c3<='F')){
text+=c3;
}
if(text=="")text="0";
}
text = text.toUpper();
if(text!=srcTblCell->toPlainText())
{
srcTblCell->setPlainText(text);
srcTblCell->moveCursor(QTextCursor::End);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionAbout_QT_triggered()
{
QMessageBox::aboutQt(this,"About QT");
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this,"About Box256QT","<h3>About Box256QT</h3> A Box256 Emulator made with QT. Made by <a href=\"https://github.com/FinalForEach\">FinalForEach</a>. <br/>"
"The original Box256 reference implementation by <a href=\"http://box-256.com\">Juha Kiili</a>");
}
void MainWindow::on_actionNew_triggered()
{
for(int r=0;r<64;r++)
{
cellTexts[r][0]->setPlainText("0");
cellTexts[r][1]->setPlainText("0");
cellTexts[r][2]->setPlainText("0");
cellTexts[r][3]->setPlainText("0");
}
machine.reset();
updateMemoryLabels();
}
void MainWindow::on_actionLoad_Example_Program_triggered()
{
loadBoxFile(":/machine/default_blue_square.box256");
}
void MainWindow::on_actionLoad_triggered()
{
dialogLoad.setSrcText(getMachineSource());
dialogLoad.show();
}