-
Notifications
You must be signed in to change notification settings - Fork 139
/
mac.c
284 lines (245 loc) · 7.5 KB
/
mac.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
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
/*
A more advanced version of the VM
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define STACK_SIZE 256
static int stack[STACK_SIZE];
/** Instructions */
typedef enum {
HLT, // 0 -- hlt :: halts program
PSH, // 1 -- psh val :: pushes <val> to stack
POP, // 2 -- pop :: pops value from stack
ADD, // 3 -- add :: adds top two vals on stack
MUL, // 4 -- mul :: multiplies top two vals on stack
DIV, // 5 -- div :: divides top two vals on stack
SUB, // 6 -- sub :: subtracts top two vals on stack
SLT, // 7 -- slt reg_a, reg_b :: pushes (reg_a < reg_b) to stack
MOV, // 8 -- mov reg_a, reg_b :: movs the value in reg_a to reg_b
SET, // 9 -- set reg, val :: sets the reg to value
LOG, // 10 -- log a :: prints out a
IF, // 11 -- if reg val ip :: if the register == val branch to the ip
IFN, // 12 -- ifn reg val ip :: if the register != val branch to the ip
GLD, // 13 -- gld reg :: loads a register to the stack
GPT, // 14 -- gpt reg :: pushes top of stack to the given register
NOP // 15 -- nop :: nothing
} Instructions;
/** Registers */
typedef enum {
A, B, C, D, E, F, I, J, // GENERAL PURPOSE
EX, // EXCESS
EXA, // MORE EXCESS
IP, // INSTRUCTION POINTER
SP, // STACK POINTER
REGISTER_SIZE
} Registers;
static int registers[REGISTER_SIZE];
// instructions array
int *instructions;
// how many instructions to do
int instruction_count = 0;
// how much space is allocated for the instructions
// 4 instructions by default
int instruction_space = 4;
/** if the program is running */
static bool running = true;
/** if the IP is assigned by jmp instructions(such as IF,IFN),it should not increase 1 any more **/
bool is_jmp = false;
/** quick ways to get SP and IP */
#define SP (registers[SP])
#define IP (registers[IP])
/** fetch current instruction set */
// #define FETCH (test_a[IP])
// #define FETCH (test_b[IP])
#define FETCH (instructions[IP])
/** prints the stack from A to B */
void print_stack() {
for (int i = 0; i < SP; i++) {
printf("0x%04d ", stack[i]);
if ((i + 1) % 4 == 0) { printf("\n"); }
}
if (SP != 0) { printf("\n"); }
}
void print_registers() {
printf("Register Dump:\n");
for (int i = 0; i < REGISTER_SIZE; i++) {
printf("%04d ", registers[i]);
if ((i + 1) % 4 == 0) { printf("\n"); }
}
}
int find_empty_register() {
for (int i = 0; i < REGISTER_SIZE; i++) {
if (i != registers[EX] && i != registers[EXA]) { return i; }
}
return EX;
}
void eval(int instr) {
is_jmp = false;
switch (instr) {
case HLT: {
running = false;
printf("Finished Execution\n");
// print_stack(0, 16);
// print_registers();
break;
}
case PSH: {
SP = SP + 1;
IP = IP + 1;
stack[SP] = instructions[IP];
break;
}
case POP: {
SP = SP - 1;
break;
}
case ADD: {
registers[A] = stack[SP];
SP = SP - 1;
registers[B] = stack[SP];
/* SP = SP - 1; */
registers[C] = registers[B] + registers[A];
/* SP = SP + 1; */
stack[SP] = registers[C];
printf("%d + %d = %d\n", registers[B], registers[A], registers[C]);
break;
}
case MUL: {
registers[A] = stack[SP];
SP = SP - 1;
registers[B] = stack[SP];
/*SP = SP - 1;*/
registers[C] = registers[B] * registers[A];
/*SP = SP + 1;*/
stack[SP] = registers[C];
printf("%d * %d = %d\n", registers[B], registers[A], registers[C]);
break;
}
case DIV: {
registers[A] = stack[SP];
SP = SP - 1;
registers[B] = stack[SP];
/* SP = SP - 1;*/
registers[C] = registers[B] / registers[A];
/* SP = SP + 1; */
stack[SP] = registers[C];
printf("%d / %d = %d\n", registers[B], registers[A], registers[C]);
break;
}
case SUB: {
registers[A] = stack[SP];
SP = SP - 1;
registers[B] = stack[SP];
/* SP = SP - 1; */
registers[C] = registers[B] - registers[A];
/* SP = SP + 1; */
stack[SP] = registers[C];
printf("%d - %d = %d\n", registers[B], registers[A], registers[C]);
break;
}
case SLT: {
SP = SP - 1;
stack[SP] = stack[SP+1] < stack[SP];
break;
}
case MOV: {
registers[instructions[IP + 2]] = registers[instructions[IP + 1]];
IP = IP + 2;
break;
}
case SET: {
registers[instructions[IP + 1]] = instructions[IP + 2];
IP = IP + 2;
break;
}
case LOG: {
printf("%d\n", registers[instructions[IP + 1]]);
IP = IP + 1;
break;
}
case IF: {
if (registers[instructions[IP + 1]] == instructions[IP + 2]) {
IP = instructions[IP + 3];
is_jmp = true;
}
else{
IP = IP + 3;
}
break;
}
case IFN: {
if (registers[instructions[IP + 1]] != instructions[IP + 2]) {
IP = instructions[IP + 3];
is_jmp = true;
}
else {
IP = IP + 3;
}
break;
}
case GLD: {
SP = SP + 1;
IP = IP + 1;
stack[SP] = registers[instructions[IP]];
break;
}
case GPT: {
registers[instructions[IP + 1]] = stack[SP];
IP = IP + 1;
break;
}
case NOP: {
printf("Do Nothing\n");
break;
}
default: {
printf("Unknown Instruction %d\n", instr);
break;
}
}
}
int main(int argc, char** argv) {
if (argc <= 1) {
printf("error: no input files\n");
return -1;
}
// filename
char *filename = argv[1];
FILE *file = fopen(filename, "r");
if (!file) {
printf("error: could not read file `%s`\n", filename);
return -1;
}
// allocate space for instructions
instructions = malloc(sizeof(*instructions) * instruction_space); // 4 instructions
// read the "binary" file
int num;
int i = 0;
while (fscanf(file, "%d", &num) > 0) {
instructions[i] = num;
printf("%d\n", instructions[i]);
i++;
if (i >= instruction_space) {
instruction_space *= 2;
instructions = realloc(instructions, sizeof(*instructions) * instruction_space); // double size
}
}
// set 'instruction_count' to number of instructions read
instruction_count = i;
// close the file
fclose(file);
// initialize stack pointer
SP = -1;
// loop through program, but don't
// go out of the programs bounds
while (running && IP < instruction_count) {
eval(FETCH);
if(!is_jmp){
IP = IP + 1;
}
}
// clean up instructions
free(instructions);
return 0;
}