-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
304 lines (262 loc) · 6.65 KB
/
Copy pathmain.c
File metadata and controls
304 lines (262 loc) · 6.65 KB
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
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef dbg
#define dbgdo(task) task
#endif
#ifndef dbg
#define dbgdo(task) ;
#endif
void argumentError(int minCount) {
printf("Argument error: Expected at least %d %s\n", minCount, (minCount > 1) ? "arguments" : "argument");
}
#define MIN_ARGS(minimum) int minargs = minimum; if (line.tokCount < minargs+1) { argumentError(minargs); break; }
typedef enum ARG_TYPE {
ARG_TYPE_IMM_INT,
ARG_TYPE_REGISTER,
ARG_TYPE_RAM_ADDR,
ARG_TYPE_VEC,
ARG_TYPE_UNKNOWN
} ARG_TYPE;
struct arg {
enum ARG_TYPE type;
uint8_t value;
};
struct Vector3 {
float x;
float y;
float z;
};
enum powerStates {
POWER_OFF,
POWER_FROZEN,
POWER_ON
};
typedef union {
char i;
float f;
struct Vector3 v;
} Register;
struct tmachine {
uint8_t bram[1024];
uint8_t dram[1024];
struct Vector3 vram[1024];
Register R[16];
Register RET;//return register
enum powerStates powereState;
uint64_t sp;
};
static uint64_t convertMnemonic(const char *mnemonic) {
int i = 0;
//Convert mnemonic
uint64_t converTedMnemonic = 0;
uint8_t sym = mnemonic[i];
while (mnemonic[i] != '\0') {
sym = mnemonic[i];
//printf("%d: %32lb\n", i, converTedMnemonic);
if (sym > 96) {//Convert lower case to upper
sym -= 32;
//printf(" (Converting to UPPER case) ");
}
if (mnemonic[i] != ' ') converTedMnemonic += sym << 8 * i;
i++;
}
return converTedMnemonic;
}
//TOken stuff
struct token {
char val[4];
ARG_TYPE type;
};
struct tokenizedLine {
unsigned short tokCount;
struct token tokens [4];
};
void tokenizeLine(struct tokenizedLine *tl, char *line) {
uint8_t linePos = 0, tokenPos = 0;
char *targ, *src = &line[linePos];
tl->tokCount = 0;
while ( *src != '\0') {
src = &line[linePos];
targ = &tl->tokens[tl->tokCount].val[tokenPos];
if (*src != '\n') *targ = *src;
else *targ = '\0';
tokenPos++; linePos++;
if (*src == ' ' || *src == '\0') {
tl->tokens[tl->tokCount].val[tokenPos-1] = '\0';
tokenPos = 0;
tl->tokCount++;
}
}
//Determine arg types
for (int i = 0; i < tl->tokCount; i++) {
switch (tl->tokens[i].val[0]) {
case 'r':
tl->tokens[i].type = ARG_TYPE_REGISTER;
break;
case 'i':
tl->tokens[i].type = ARG_TYPE_IMM_INT;
break;
}
}
}
int isNumber(char c) {
return ((c > 47) && (c < 58));
}
int stringToInt(const char *string) {
ushort i = 0;
ushort out = 0;
if (!isNumber(string[i])) { i++; }
while (1) {
char buf = string[i];
if (buf == ' ') break;
if (buf == '\n') break;
if (buf == 0) break;
//printf("i:%x dec:%d char:%c\n", i, buf, buf);
out *= 10;
out += string[i]-48;
i++;
}
return out;
}
void evalStr(struct tmachine *vm, char *str) {
if (*str == 0) return;
struct tokenizedLine line;
tokenizeLine(&line, str);
char *mnemonic;
uint64_t converTedMnemonic;
converTedMnemonic = convertMnemonic(line.tokens[0].val);
int val1 = stringToInt(&line.tokens[1].val[0]);
int val2 = stringToInt(&line.tokens[2].val[0]);
mnemonic = &line.tokens[0].val[0];
char *targ;
char src;
switch (converTedMnemonic) {
case 0x444c: {//LD FROM TO
MIN_ARGS(2);
if (line.tokens[1].type == ARG_TYPE_IMM_INT) { src = val1;}//Set first argument dig 2 as source
else if (line.tokens[1].type == ARG_TYPE_REGISTER) { src = vm->R[val1].i; }//set
if (line.tokens[2].type == ARG_TYPE_REGISTER) { targ = &vm->R[val2].i; }
*targ = src;
dbgdo(printf("Loading %d into R%d\n", src, val2));
break;
}
case 0x54554f:{//OUTPUT
MIN_ARGS(1);
dbgdo(printf("OUTPUT: \n", src, val2, *targ));
if (line.tokens[1].type == ARG_TYPE_REGISTER) printf("%d\n", vm->R[val1].i);
if (line.tokens[1].type == ARG_TYPE_IMM_INT) printf("%d\n", val1);
break;
}
case 0x425553:{//Subtract
MIN_ARGS(2);
if (line.tokens[1].type == ARG_TYPE_IMM_INT) { src = val1;}//Set first argument dig 2 as source
else if (line.tokens[1].type == ARG_TYPE_REGISTER) { src = vm->R[val1].i; }//set register from arg1
if (line.tokens[2].type == ARG_TYPE_REGISTER) { targ = &vm->R[val2].i; }
dbgdo(printf("ADDING %d to R%d (%d)\n", src, val2, *targ));
*targ -= src;
break;
}
case 0x444441:{//ADD
MIN_ARGS(2);
if (line.tokens[1].type == ARG_TYPE_IMM_INT) { src = val1;}//Set first argument dig 2 as source
else if (line.tokens[1].type == ARG_TYPE_REGISTER) { src = vm->R[val1].i; }//set register from arg1
if (line.tokens[2].type == ARG_TYPE_REGISTER) { targ = &vm->R[val2].i; }
dbgdo(printf("ADDING %d to R%d (%d)\n", src, val2, *targ));
*targ += src;
break;
}
case 0x524c43://Clear Console
dbgdo(printf("CLEARING SCREEN\n"));
system("clear");
break;
case 0x504f5453:
dbgdo(printf("STOPPING MACHINE\n"));
vm->powereState = POWER_OFF;
break;
case 0x54554f43: {//COUT
MIN_ARGS(1);
dbgdo(printf("OUTPUT AS CHAR: \n", src, val2, *targ));
if (line.tokens[1].type == ARG_TYPE_REGISTER) putchar(vm->R[val1].i);
if (line.tokens[1].type == ARG_TYPE_IMM_INT) putchar(val1);
break;
}
case 0x464544://DEF (define a function)
break;
default:
printf("unknown mnemonic: %s 0x%x\n", mnemonic, converTedMnemonic);
break;
}
}
static void skipLine(FILE * file) {
char buf;
while (fread(&buf, 1, 1, file)) {
if (buf == '\n') break;
}
}
int evalFile(struct tmachine *vm, FILE *file) {
char buffer[128];
//Read a line
if (!file) return 0;
fseek(file, 0, SEEK_END);
size_t flen = ftell(file);
rewind(file);
int readFail = 0;
while (!readFail) {
for (int i = 0; i < 128; i++) {
readFail = !fread(&buffer[i], 1, 1, file);
//printf("%c", buffer[i]);
if (buffer[i] == '\n' || readFail) {
buffer[i] = '\0';
break;
}
}
//printf("%s\n", buffer);
evalStr(vm, buffer);
}
return 1;
}
int main(int argc, char **argv) {
int stayOn = 0;
int nonFlagArgCount = argc-1;
int flagCount = 0;
char *scriptPath = "main.tscript";
//Check arguments
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
nonFlagArgCount--;
flagCount++;
switch (argv[i][1]) {
case 'i':
stayOn = 1;
break;
default:
printf("Unknown argument %s\n", argv[i]);
break;
}
} else {
scriptPath = argv[i];
}
}
//INteractive mode
struct tmachine machine;
machine.powereState = POWER_ON;//put in init functionl later
//Try to run requested script
FILE *script = fopen(scriptPath, "rb");
if (!evalFile(&machine, script)) {
printf("Failed to open script\n");
return 0;
}
//printf("%d flags, %d args\n", flagCount, nonFlagArgCount);
if ( nonFlagArgCount==0 ) stayOn = 1;
if (!stayOn) return 0;
char input[256];
while (machine.powereState != POWER_OFF) {
printf("\n>>: ");
fgets(input, 256, stdin);
//printf("%s-> ", input);
evalStr(&machine, input);
}
}