forked from regarm/u8085
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.cpp
278 lines (275 loc) · 9.58 KB
/
assemble.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
#include "mainwindow.h"
#include "utils.h"
#include <QStatusBar>
#include <QThread>
#include <QApplication>
bool MainWindow::assemble(){
if(documentWidget->document()->isModified() || currentFileName.isEmpty()){
if(!saveDoc())
return false;
}
assembleFreeze();
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 4000);
labelExists.clear();
labelAddressMap.clear();
QString text = documentWidget->document()->toPlainText();
if(!labelVerify(text)){
assembleUnfreeze();
return false;
}
if(!synCheck(text)){
assembleUnfreeze();
return false;
}
genOpcodes(text);
assembleUnfreeze();
return true;
}
bool MainWindow::assembleAndRun(){
if(!assemble()){
return false;
}
return runAt2000();
}
bool MainWindow::labelVerify(QString& text){
QTextStream stream(&text);
QString line;
QString store;
int lineNum = 0;
log->appendLog(tr("Starting Label Verifying ..."), logDock::Init);
qDebug() << tr("Starting Label Verifying ...");
//Label Verifying
while(!(store = stream.readLine().toUpper()).isNull()){
line = store.simplified();
++lineNum; // increase line number
int x = line.indexOf(tr("//")); // get first index of "//"
if(x != -1){
line = line.left(x);
}
if(line.count(tr(":"))>1){
log->appendLog(tr("Line %1: \"%2\"\nOnly one label per line is allowed.")
.arg(lineNum)
.arg(store), logDock::Error);
log->appendLog(tr("Stopping here."));
return false;
}
x = line.indexOf(tr(":"));
if(x == -1){ // no labels
continue;
}
QString label = line.left(x).simplified();
if(label.isEmpty()){
log->appendLog(tr("Line %1: \"%2\"\nEmpty label is not allowed.")
.arg(lineNum).arg(store), logDock::Error);
log->appendLog(tr("Stopping here."));
return false;
}
if(!isValidLabel(label)){
log->appendLog(tr("Line %1: \"%2\"\nOnly alphanumerics characters are allowed in label.")
.arg(lineNum).arg(store), logDock::Error);
log->appendLog(tr("Stopping here."));
return false;
}
if(labelExists.find(label) != labelExists.end()){
log->appendLog(tr("Line %1: \"%2\"\n\"%3\" is also defined elsewhere.")
.arg(lineNum).arg(store).arg(label), logDock::Error);
log->appendLog(tr("Stopping here."));
return false;
}
labelExists[label] = true;
++lineNum;
}
log->appendLog(tr("All labels are ok."));
return true;
}
bool MainWindow::synCheck(QString& text){
log->appendLog(tr("Syntax checking"), logDock::Init);
qDebug() << tr("Syntax checking");
QTextStream stream(&text);
QString line;
QString store;
int address = 0x2000;
int lineNum = 0;
//Syntax Checking
while(!(store = stream.readLine()).isNull()){
line = store.simplified().toUpper();
++lineNum;
int x = line.indexOf(tr("//"));
if(x != -1){
line = line.left(x).simplified();
}
x = line.indexOf(tr(":"));
if(x != -1){
labelAddressMap[line.left(x).simplified()] = address;
line = line.right(line.size() - x - 1).simplified();
}
if(line.isEmpty()){
continue;
}
QString mnem;
QString rem;
x = line.indexOf(tr(" "));
if(x == -1){
mnem = line;
rem = tr("");
} else{
mnem = line.left(x).simplified();
rem = line.right(line.size() - x).simplified();
}
bool error = false;
QString errorVal = tr("");
QString suggestion = tr("");
log->appendLog(tr("Line %1: \"%2\"").arg(lineNum).arg(store));
switch(__8085.instructionType[mnem]){
case 1:
{
//no operand
if(!rem.isEmpty()){
error = true;
errorVal = tr("No operands are required here.");
}
break;
}
case 2:
{
//8-bit data operand
if(!isData(rem)){
error = true;
errorVal = tr("8-bit data operand is required in hexadecimal.");
}
break;
}
case 3:
{
//single register operand [A, B, C, D, E, H, L, M]
if(!isRegister(rem)){
errorVal = tr("Single register operand is required [A, B, C, D, E, H, L, M] .");
error = true;
}
break;
}
case 4:
{
//CALL, JMP Require Label
if(mnem.startsWith(tr("C")) || mnem.startsWith(tr("J"))){
if(!isValidLabel(rem)){
errorVal = tr("%3 is not a valid label.").arg(rem);
error = true;
}
if(labelExists.find(rem) == labelExists.end()){
errorVal = tr("Label \"%3\" is not defined.").arg(rem);
error = true;
}
} else{//STA, LDA, LHLD, SHLD require an address
if(!isValidAddress(rem)){
error = true;
errorVal = tr("%1 requires a 16-bit address in hexadecimal.\ni.e. %1 2000").arg(mnem);
}
}
break;
}
case 5:
{
//Rpair4 {BC, DE, HL, SP}
if(!isRpair4(rem)){
error = true;
errorVal = tr("%1 requires an register pair [B, D, H, SP]").arg(mnem);
}
break;
}
case 6:
{
//Rp2 {BC, DE}
if(!isRp2(rem)){
error = true;
errorVal = tr("%1 requires an register pair [B, D]").arg(mnem);
}
break;
}
case 7:
{
//Rpw4 {BC, DE, HL, PSW}
if(!isRpw4(rem)){
error = true;
errorVal = tr("%1 requires an register pair [B, D, H, PSW]").arg(mnem);
}
break;
}
case 8:
{
x = rem.count(tr(","));
if(x != 1){
error = true;
errorVal = tr("%1 requires two comma separated oprands. i.e. MOV A, B").arg(mnem);
} else{
x = rem.indexOf(tr(","));
QString left = rem.left(x).simplified();
QString right = rem.right(rem.size() - x - 1).simplified();
if(!(isRegister(left)&&isRegister(right))){
error = true;
errorVal = tr("%1 requires two valid register operands.i.e. MOV A, B").arg(mnem);
} else if(left == tr("M") && right == tr("M")){
error = true;
errorVal = tr("\"Mov M, M\" is not an instruction in 8085.");
}
}
break;
}
case 9:
{
x = rem.count(tr(","));
if(x != 1){
error = true;
errorVal = tr("%1 requires two comma separated oprands.\ni.e. MVI A, FE").arg(mnem);
} else{
x = rem.indexOf(tr(","));
QString left = rem.left(x).simplified();
QString right = rem.right(rem.size() - x - 1).simplified();
if(!(isRegister(left)&&isData(right))){
error = true;
errorVal = tr("%1 requires two comma separated oprands.i.e. MVI A, FE").arg(mnem);
}
}
break;
}
case 10:
{
x = rem.count(tr(","));
if(x != 1){
error = true;
errorVal = tr("%1 requires two comma separated oprands.i.e. LXI H, 2000").arg(mnem);
} else{
x = rem.indexOf(tr(","));
QString left = rem.left(x).simplified();
QString right = rem.right(rem.size() - x - 1).simplified();
if(!(isRpair4(left)&&isValidAddress(right))){
error = true;
errorVal = tr("%1 requires two comma separated oprands.i.e. LXI H, 2000").arg(mnem);
}
}
break;
}
case 11:
{
error = true;
errorVal = tr("%1 is not currently supported by this software").arg(mnem);
break;
}
default:
{
error = true;
errorVal = tr("%1 is not a recognized MNEMONIC.").arg(mnem);
}
}
if(error){
log->appendLog(errorVal, logDock::Error);
log->appendLog(tr("Error occured, stopping here."));
return false;
} else{
log->appendLog(tr("Success"), logDock::Success);
}
address += __8085.byteCnt[mnem];
}
log->appendLog(tr("Successfull syntax check. :)"));
return true;
}