-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbas.c
94 lines (76 loc) · 1.59 KB
/
bas.c
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
#include "include/vm.h"
#include <stdlib.h>
#include <stdarg.h>
void usage() {
printf("Usage: bas <filename with *.sb format>.\n");
exit(1);
}
void errorif(bool cond, const char *format, ...) {
va_list arglist;
if (cond) {
va_start(arglist, format);
vprintf(format, arglist);
va_end(arglist);
exit(1);
}
}
int32_t *getcode(char *filename) {
int32_t *code, *tmp;
size_t size;
uint8_t offset = 0;
FILE *f = fopen(filename, "rb");
errorif(f == NULL, "Unable to open file: %s\n", filename);
errorif(fseek(f, 0, SEEK_END) == -1, "Unable to read file: %s\n", filename);
size = ftell(f);
errorif(fseek(f, 0, SEEK_SET) == -1, "Unable to read file: %s\n", filename);
code = malloc(sizeof(int32_t) * size);
tmp = code;
while(fread(tmp++, sizeof(uint8_t), 1, f) > 0) {
fread(&offset, sizeof(uint8_t), 1, f);
if (offset > 0) {
errorif(fread(tmp++, sizeof(int8_t) * offset, 1, f) <= 0, "Unable to decode bytecode from file: %s.\n", filename);
}
}
*tmp = HALT;
fclose(f);
return code;
}
void cpu(VM vm) {
vm.running = true;
while(vm.running) {
Opcode opcode = fetch(vm);
operation op = decode(opcode);
vm.ip = execute(&vm, op);
}
}
int main(int argc, char **argv) {
if (argc != 2) {
usage();
}
char *filename = argv[1];
// int32_t code[] = {
// 2, 0,
// 2, 1,
// 1, 1,
// 3, 1,
// 2, 1,
// 3, 0,
// 2, 0,
// 4,
// 255
// };
int32_t *code = getcode(filename);
int32_t stack[1024];
int32_t data[1024];
VM vm = {
.code = code,
.stack = stack,
.data = data,
.ip = 0,
.sp = -1,
.fp = 0,
.running = false
};
cpu(vm);
return 0;
}