-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.cpp
275 lines (238 loc) · 8.56 KB
/
processor.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
#include "processor.h"
#include <QDebug>
#include <memory.h>
QDataStream& operator<<(QDataStream& stream, const Processor &proc) {
stream << proc.ip << proc.sp << proc.bp << proc.acc << proc.steps_per_update << proc.ram << proc.stack << QByteArray((char *)proc.out, OUTPUTS);
return stream;
}
QDataStream& operator>>(QDataStream& stream, Processor &proc) {
QByteArray out;
stream >> proc.ip >> proc.sp >> proc.bp >> proc.acc >> proc.steps_per_update >> proc.ram >> proc.stack >> out;
for(int i = 0; i < OUTPUTS; i++)
proc.out[i] = out[i];
return stream;
}
Processor::Processor(): ip(0), sp(0), bp(0), acc(0), steps_per_update(1) {
stack.resize(16);
}
Processor::Processor(int steps, int stack_size): ip(0), sp(0), bp(0), acc(0), steps_per_update(steps) {
//for(int i = 0; i < INPUTS; i++)
// in[i] = 0;
//
//for(int i = 0; i < OUTPUTS; i++)
// out[i] = 0;
::memset(in, 0, sizeof(in));
::memset(out, 0, sizeof(out));
stack.resize(stack_size);
}
void Processor::set_data(const QByteArray &rom) {
ram = rom;
}
void Processor::update() {
for(int i = 0; i < steps_per_update; i++)
step();
}
void Processor::step() {
//qDebug() << "ram size:" << ram.size();
Instruction instruction = (Instruction)((int)(unsigned char)ram[inc_ip()] % MAX_I);
//qDebug() << "inst:" << instruction;
int addr;
switch(instruction) {
case I_PUSH:
//qDebug() << "push";
if(sp < stack.size()) stack[sp++] = (unsigned char)acc;
break;
case I_POP:
//qDebug() << "pop";
if(sp > bp) acc = (unsigned char)stack[--sp];
break;
case I_PUSHA:
//qDebug() << "pusha";
addr = addy();
if(sp < stack.size()) stack[sp++] = ram[addr];
break;
case I_POPA:
//qDebug() << "popa";
addr = addy();
if(sp > bp) ram[addr] = stack[--sp];
break;
case I_SUBROUTINE:
//qDebug() << "subroutine";
addr = addy();
if(sp <= stack.size() - 4) {
stack[sp++] = ((ip >> 8) & 0xff);
stack[sp++] = (ip & 0xff);
stack[sp++] = ((bp >> 8) & 0xff);
stack[sp++] = (bp & 0xff);
bp = sp;
ip = addr;
}
break;
case I_RET:
//qDebug() << "ret";
if(bp >= 4) {
sp = bp;
bp = make_addy(stack[sp - 1], stack[sp - 2]);
ip = make_addy(stack[sp - 3], stack[sp - 4]);
sp -= 4;
}
break;
case I_IN: acc = in[(unsigned char)ram[inc_ip()] % INPUTS]; break;
case I_OUT: out[(unsigned char)ram[inc_ip()] % OUTPUTS] = acc; break;
case I_JMP: ip = addy(); break;
case I_JMPZ:
addr = addy();
if(acc == 0) ip = addr;
break;
case I_JMPNZ:
addr = addy();
if(acc != 0) ip = addr;
break;
case I_LOAD:
acc = (unsigned char)ram[addy()];
break;
case I_STORE:
ram[addy()] = (unsigned char)acc;
break;
case I_INC: acc++; break;
case I_DEC: acc--; break;
case I_NOT: acc = ~acc; break;
case I_AND: acc = (acc & (unsigned char)ram[inc_ip()]); break;
case I_OR: acc = (acc | (unsigned char)ram[inc_ip()]); break;
case I_NAND: acc = ~(acc & (unsigned char)ram[inc_ip()]); break;
case I_NOR: acc = ~(acc | (unsigned char)ram[inc_ip()]); break;
case I_XOR: acc = (acc ^ (unsigned char)ram[inc_ip()]); break;
case I_ADD: acc += (unsigned char)ram[inc_ip()]; break;
case I_SUB: acc -= (unsigned char)ram[inc_ip()]; break;
case I_MUL: acc *= (unsigned char)ram[inc_ip()]; break;
case I_DIV:
{
unsigned char val = (unsigned char)ram[inc_ip()];
if(val != 0) acc /= val;
}
break;
case I_RAND:
#ifdef ENABLE_RAND
acc = qrand() % 0xff;
#endif
break;
default: // I_NOOP;
break;
}
}
#define MAX_DISASSEMBLE_LINES 2000
const char *pneumonics[Processor::MAX_I] = {
"NOOP", "PUSH", "POP", "PUSHA", "POPA", "CALL", "RET", "IN", "OUT", "JMP", "JMPZ", "JMPNZ",
"LOAD", "STORE", "INC", "DEC", "NOT", "AND", "OR", "NAND", "NOR", "XOR",
"ADD", "SUB", "MUL", "DIV", "RAND"
};
int inst_bytes[Processor::MAX_I] = {
1, 1, 1, 3, 3, 3, 1, 2, 2, 3, 3, 3,
3, 3, 1, 1, 1, 2, 2, 2, 2, 2,
2, 2, 2, 2, 1
};
QString Processor::disassemble_from(int address) {
if(address >= ram.size()) return "Out of Range";
QString str = "<html><body><table>";
int ip_save = ip;
ip = address;
int lines = 0;
int v;
bool wrap = false;
while(ip >= address && lines < MAX_DISASSEMBLE_LINES && !wrap) {
wrap = false;
str += QString("<tr><td>%1:</td>").arg(ip, 4, 10, QLatin1Char('0'));
Instruction instruction = (Instruction)((int)(unsigned char)ram[ip] % MAX_I);
if(ip + inst_bytes[(int)instruction] >= ram.size())
wrap = true;
str += "<td>";
for(int i = 0; i < inst_bytes[(int)instruction]; i++) {
v = (int)(unsigned char)ram[(ip + i) % ram.size()];
if(v == 0)
str += QString("<font color='red'>%1</font> ").arg(v, 3, 10, QLatin1Char('0'));
else
str += QString("%1 ").arg(v, 3, 10, QLatin1Char('0'));
}
str += "</td>";
inc_ip();
//qDebug() << "inst:" << instruction;
int addr;
switch(instruction) {
case I_NOOP:
str += "<td>NOOP</td>";
break;
case I_PUSH:
str += "<td>PUSH</td>";
break;
case I_POP:
str += "<td>POP</td>";
break;
case I_PUSHA:
//qDebug() << "pusha";
addr = addy();
str += QString("<td>PUSHA [%1]</td>").arg(addr);
break;
case I_POPA:
//qDebug() << "popa";
addr = addy();
str += QString("<td>POPA [%1]</td>").arg(addr);
break;
case I_SUBROUTINE:
addr = addy();
str += QString("<td>CALL [%1]</td>").arg(addr);
break;
case I_RET:
str += "<td>RET</td>";
break;
case I_IN: str += QString("<td>IN {%1}</td>").arg((int)(unsigned char)ram[inc_ip()] % INPUTS); break;
case I_OUT: str += QString("<td>OUT {%1}</td>").arg((int)(unsigned char)ram[inc_ip()] % OUTPUTS); break;
case I_JMP:
addr = addy();
str += QString("<td>JUMP [%1]</td>").arg(addr);
break;
case I_JMPZ:
addr = addy();
str += QString("<td>JMPZ [%1]</td>").arg(addr);
break;
case I_JMPNZ:
addr = addy();
str += QString("<td>JMPNZ [%1]</td>").arg(addr);
break;
case I_LOAD:
addr = addy();
str += QString("<td>LOAD [%1]</td>").arg(addr);
break;
case I_STORE:
addr = addy();
str += QString("<td>STORE [%1]</td>").arg(addr);
break;
case I_INC: str += "<td>INC</td>"; break;
case I_DEC: str += "<td>DEC</td>"; break;
case I_NOT: str += "<td>NOT</td>"; break;
case I_AND: str += QString("<td>AND %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_OR: str += QString("<td>OR %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_NAND: str += QString("<td>NAND %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_NOR: str += QString("<td>NOR %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_XOR: str += QString("<td>XOR %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_ADD: str += QString("<td>ADD %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_SUB: str += QString("<td>SUB %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_MUL: str += QString("<td>MUL %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_DIV: str += QString("<td>DIV %1</td>").arg((int)(unsigned char)ram[inc_ip()]); break;
case I_RAND:
#ifdef ENABLE_RAND
str += "<td>RAND</td>";
#else
str += "<td>NOOP</td>";
#endif
break;
default: // I_NOOP;
str += "<td>NOOP</td>";
break;
}
str += "</tr>";
lines++;
}
ip = ip_save;
str += "</table></body></html>";
return str;
}