-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathburst.c
190 lines (180 loc) · 3.49 KB
/
burst.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
/* Instructions definitions */
#define NOP 0
#define DROP 1
#define SWAP 2
#define ROT 3
#define DUP 4
#define PEEK 5
#define ADD 6
#define SUB 7
#define MUL 8
#define DIV 9
#define MOD 10
#define NEG 11
#define AND 12
#define OR 13
#define XOR 14
#define NOT 15
#define SHR 16
#define SHL 17
#define LOAD 18
#define STOR 19
#define LOADB 20
#define STORB 21
#define JUMP 22
#define JZ 23
#define CALL 24
#define RET 25
#define HALT 31
/* Constants definition */
#define STACK_SIZE 256
#define MEMORY_SIZE 64 * 1024
bool running = true;
uint8_t memory[MEMORY_SIZE], DP=0, CP=0;
uint16_t PC = 0, IR, data_stack[STACK_SIZE], call_stack[STACK_SIZE];
uint64_t cycles = 0;
void data_push(uint16_t data) {
data_stack[++DP] = data;
}
uint16_t data_pop() {
return data_stack[DP--];
}
void call_push(uint16_t data) {
call_stack[++CP] = data;
}
uint16_t call_pop() {
return call_stack[CP--];
}
void execute(uint8_t instruction) {
uint16_t a, b, c;
switch (instruction) {
case NOP:
break;
case DROP:
data_pop();
break;
case ROT:
c = data_pop();
b = data_pop();
a = data_pop();
data_push(b);
data_push(a);
data_push(c);
break;
case DUP:
a = data_stack[DP];
data_push(a);
break;
case SWAP:
a = data_pop();
b = data_pop();
data_push(a);
data_push(b);
break;
case PEEK:
a = data_pop();
if (a > DP) {
printf("Error: stack underflow.\n");
} else {
data_push(data_stack[DP - a]);
}
break;
case ADD:
b = data_pop();
a = data_pop();
data_push(a + b);
break;
case SUB:
b = data_pop();
a = data_pop();
data_push(a - b);
break;
case MUL:
b = data_pop();
a = data_pop();
data_push(a * b);
break;
case DIV:
b = data_pop();
a = data_pop();
data_push(a / b);
break;
case MOD:
b = data_pop();
a = data_pop();
data_push(a % b);
break;
case NEG:
a = data_pop();
data_push(-a);
break;
case JUMP:
PC = data_pop();
break;
case JZ:
a = data_pop();
b = data_pop();
if (a == 0) {
PC = b;
}
break;
case CALL:
call_push(PC);
PC = data_pop();
break;
case RET:
PC = call_pop();
break;
case HALT:
running = false;
break;
default:
printf("Error: %X unimplemented.\n", instruction);
}
cycles++;
}
void cycle() {
IR = memory[PC++] << 8 | memory[PC++];
//printf("PC: %04X IR: %04X\n", PC, IR);
if (IR >> 15 == 1) {
//printf("P %04X\n--\n", IR & 0x7fff);
data_push(IR & 0x7fff);
} else {
for (int i = 2; i >= 0; --i) {
//printf("%02X \n", (IR >> (5 * i)) & 0x1f);
execute((IR >> (5 * i)) & 0x1f);
}
}
}
int main(int argc, char const *argv[]) {
if (argc < 2) {
printf("Usage: %s INFILE\n", argv[0]);
exit(2);
} else {
FILE *fp;
fp = fopen(argv[1], "rb");
if (fp == NULL) {
fprintf(stderr, "Can't open file %s.\n", argv[1]);
exit(1);
}
int c, mp = 0;
while ((c = fgetc(fp)) != EOF) {
memory[mp++] = c;
}
if (ferror(fp)) {
fprintf(stderr, "I/O error while reading file.\n");
exit(1);
}
fclose(fp);
}
while (running) {
cycle();
}
printf("Cycles: %ld\n", cycles);
printf("Topstack: %d\n", data_stack[DP]);
return 0;
}