-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
361 lines (306 loc) · 8.45 KB
/
main.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
#include "main.h"
#include <fstream>
struct Machine;
void debug_machine_print(Machine &m);
struct Machine {
u64 mem[MACHINE_MEM];
u64 r[NUM_REGISTERS];
u64 instr_ptr;
u64 stack_ptr;
Instr instr; //current instruction being executed
u8 cmp_result; //0 = eq, 1 = less than, 3 = more than, 4 = err
Machine()
: instr_ptr(0), stack_ptr(STACK_START), cmp_result(4)
{
//TODO: can use memset or something to set everything to 0
for (int i = 0; i < 255; i++)
r[i] = 0;
for (uint16_t i = 0; i < 65535; i++)
mem[i] = 0;
}
//template <OpLayout L>
void apply(Op op, std::function<void(void*, void*, u8)> f) {
switch (op.getLayout()) {
case OpLayout::NONE:
f(NULL, NULL, 0);
break;
case OpLayout::R:
f(&r[instr.r], NULL, 8); //TODO: register thingy is 8 but index is 1?
break;
case OpLayout::M:
f(&mem[instr.a], NULL, 8); //TODO: or 8?
//KK TODO: f(&mem[instr.a], NULL, 4); //TODO: or 8?
//f(&instr.a, NULL, 4);
break;
case OpLayout::C:
f((u64*)&instr.c, NULL, 4);
break;
case OpLayout::RR:
f(&r[instr.rr.r1], &r[instr.rr.r2], 8);
break;
case OpLayout::RM:
f(&r[instr.rm.r], &mem[instr.rm.a], 8);
break;
case OpLayout::MR:
f(&r[instr.mr.a], &mem[instr.mr.r], 8);
break;
case OpLayout::RC:
f(&r[instr.rc.r], &instr.rc.c, 4);
break;
case OpLayout::MC:
f(&mem[instr.mc.a], &instr.mc.c, 4);
break;
default:
err("Bad layout");
break;
}
}
void run() {
while (true) {
//usleep(2);
//usleep(50000); //0.05 seconds
/*debug_machine_print(*this);
getch(true);
cout << endl;*/
instr_ptr++;
//cout << instr_ptr << endl;
if (!mem[instr_ptr]) err("Bad instruction");
instr.uint64 = mem[instr_ptr];
Op op = Op(instr.o);
OpLayout layout = op.getLayout();
OpType type = op.getType();
switch (type) {
case OpType::NOP:
continue;
break;
case OpType::HALT:
return;
break;
case OpType::MOV: {
apply(op, [&](void* a, void* b, u8 size) {
memcpy(a, b, size);
//*a = *b;
});
break;
}
case OpType::PUSH: {
apply(op, [&](void* val, void* unused, u8 size) {
memcpy(&mem[stack_ptr++], val, size);
});
break;
}
case OpType::POP: {
apply(op, [&](void* val, void* unused, u8 size) {
assert(layout == OpLayout::R);
//memcpy(&r[*(u16*)val], &mem[stack_ptr--], size);
memcpy(val, &mem[(stack_ptr--)-1], size);
});
break;
}
case OpType::INT: {
assert(layout == OpLayout::C);
u8 cmd = instr.c;
switch (cmd) {
case 0:
usleep(r[0]);
break;
case 1:
cout << (char)r[0];
break;
case 2:
r[0] = getch(false);
break;
default:
err("interrupt not implemented");
cout << cmd;
break;
}
break;
}
case OpType::JMP: {
apply(op, [&](void* val, void* unused, u8 size) {
//KK TODO assert(layout == OpLayout::M);
assert(layout == OpLayout::C || layout == OpLayout::R);
instr_ptr = *((u16*)val);
});
break;
}
case OpType::CMP: {
apply(op, [&](void* a, void* b, u8 size) {
cmp_result = asm_cmp(a, b, size);
});
break;
}
case OpType::JE: {
apply(op, [&](void* val, void* unused, u8 size) {
assert(layout == OpLayout::C);
if (cmp_result == 4)
err("jump without previous cmp");
if (cmp_result == 0)
instr_ptr = *((u16*)val);
});
break;
}
case OpType::SUB: {
assert(layout == OpLayout::RC); //temporary only for fib
apply(op, [&](void* a, void* b, u8 size) {
assert(size == 4);
*(u64*)a =(u64) *(u32*)a - *(u32*)b;
});
break;
}
case OpType::ADD: {
assert(layout == OpLayout::RR); //temporary only for fib
apply(op, [&](void* a, void* b, u8 size) {
assert(size == 8);
*(u64*)a =(u64) *(u64*)a + *(u64*)b;
});
break;
}
default:
op.print();
err("Command not implemented");
break;
}
}
}
};
/*vector<u64> assemble2(string s) {
vector<u64> ret;
auto lines = split(s, '\n');
for (auto line : lines) {
Instr instr;
auto words = split(line, ' ');
//for (auto word : words) {}
if (words.size() == 0)
continue;
if (words.size() == 1) {
if (words[0] == "nop") {
instr.o = Op(OpType::NOP, OpLayout::NONE).op;
ret.push_back(instr.uint64);
}
}
}
return ret;
}*/
void debug_machine_print(Machine &m) {
cout << "registers: ";
for (int i = 0; i < 20; i++)
cout << m.r[i] << " ";
cout << endl << "mem: ";
for (int i = 0; i < 20; i++)
cout << m.mem[i] << " ";
cout << endl << "stack: ";
for (int i = STACK_START; i < STACK_START + 20; i++)
cout << m.mem[i] << " ";
cout << endl;
}
int test_machine();
Machine load(vector<u64> machine_code) {
Machine m;
int i = 1;
for (auto cmd : machine_code)
m.mem[i++] = cmd;
return m;
}
int main(int nargs, char** args) {
string asm_code = read_file(string(args[1]));
vector<u64> machine_code = assemble(asm_code);
//write_machine_code(machine_code, "test.bin");
Machine m = load(machine_code);
/*write_machine_code(machine_code, "test.bin");
vector<u64> test_machine_code = read_machine_code("test.bin");
write_machine_code(test_machine_code, "test.bin");
Machine m = load(test_machine_code);*/
cout << "initial state:\n";
debug_machine_print(m);
cout << endl << "after execution:\n";
m.run();
debug_machine_print(m);
}
int test_machine() {
Machine m;
Instr instr1;
instr1.o = Op(OpType::MOV, OpLayout::MC).op;
instr1.mc.a = 10;
instr1.mc.c = 16;
m.mem[1] = instr1.uint64;
Instr instr2;
instr2.o = Op(OpType::MOV, OpLayout::RC).op;
instr2.rc.r = 1;
instr2.rc.c = 16;
m.mem[2] = instr2.uint64;
Instr instr3;
instr3.o = Op(OpType::INT, OpLayout::C).op;
instr3.c = 2;
m.mem[3] = instr3.uint64;
Instr instr10;
instr10.o = Op(OpType::HALT, OpLayout::NONE).op;
m.mem[4] = instr10.uint64;
m.run();
//cout << m.r[0] << "\n";
//cout << m.mem[5] << "\n";
//char c = getch();
//testOp();
}
/*
struct Machine {
u64* mem; //mem[65536]
u64 r[256];
u64 instr_ptr;
u64 stack_ptr;
Machine(u64* mem_)
: mem(mem_), instr_ptr(0), stack_ptr(30000)
{
for (int i = 0; i < 255; i++)
r[i] = 0;
}
void run() {
while (true) {
//for (int i = 0; i < 1; i++) {
Cmd c;
c.uint64 = mem[instr_ptr++];
if (c.i == NOP)
continue;
else if (c.i == HALT)
return;
else if (c.i == MOVrr) {
r[c.rr.r1] = r[c.rr.r2];
}
else if (c.i == MOVrm) {
r[c.rm.r] = mem[c.rm.a];
}
else if (c.i == MOVmr) {}
else if (c.i == MOVrc) {
r[c.rc.r] = r[c.rc.c];
}
else if (c.i == MOVmc) {
mem[c.mc.a] = c.mc.c;
}
}
}
void exec() {
}
};
*/
int testOp() {
Op x = Op(OpType::POP, OpLayout::NONE); x.print();
Op(OpType::MOV, OpLayout::RR).print();
Op(OpType::MOV, OpLayout::NONE).print();
}
/*Instr makeInstr(OpType t, OpLayout l, u64 a, u64 b) {
Op o = Op(t, l);
Instr s;
s.o = o;
}*/
int main1() {
string code = "mov %0 #15\nmov %1 #20\nmov @10 #42\nhalt";
vector<u64> parsed_asm = assemble(code);
Machine m = load(parsed_asm);
m.run();
debug_machine_print(m);
/*Instr i;
i.uint64 = parsed_asm[0];
Op(i.o).print();*/
test_machine();
}