-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtinyvm.c
More file actions
163 lines (132 loc) · 3.04 KB
/
tinyvm.c
File metadata and controls
163 lines (132 loc) · 3.04 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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define NUM_REGS 4
#define RAM_SIZE 65535
unsigned pc = 0;
unsigned long long *program = NULL;
int *ram = NULL;
int *r = NULL;
bool running = true;
unsigned long long fetch() {
return program[pc++];
}
void debug_registers() {
for(int i = 0; i < NUM_REGS; i++) printf("%08x ", r[i]);
printf("\n");
}
void debug_ram() {
printf("ram\n");
for(int i = 0; i <= RAM_SIZE; i++) {
printf("%08x ", ram[i]);
}
printf("\n");
}
void cycle() {
char op = 0;
unsigned r0 = 0;
unsigned r1 = 0;
unsigned r2 = 0;
unsigned im = 0;
unsigned long long instr = fetch();
printf("%08x %016llx ", pc, instr);
/*
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
instr. r0 r1 r2 immediate value
*/
op = (instr & 0xFF00000000000000) >> 56;
r0 = (instr & 0x00FF000000000000) >> 48;
r1 = (instr & 0x0000FF0000000000) >> 40;
r2 = (instr & 0x000000FF00000000) >> 32;
im = (instr & 0x00000000FFFFFFFF);
switch(op) {
case 0x0: // halt
running = false;
break;
case 0x1: // nop
break;
case 0x2: // li
r[r0] = im;
break;
case 0x3: // lw
r[r0] = ram[r[r1]];
break;
case 0x4: // sw
ram[r[r1]] = r[r0];
break;
case 0x5: // add
r[r0] = r[r1] + r[r2];
break;
case 0x6: // sub
r[r0] = r[r1] - r[r2];
break;
case 0x7: // mult
r[r0] = r[r1] * r[r2];
break;
case 0x8: // div
r[r0] = r[r1] / r[r2];
break;
case 0x9: // j
pc = im;
break;
case 0xA: // jr
pc = r[r0];
break;
case 0xB: // beq
if(r[r0] == r[r1]) pc = r[r2];
break;
case 0xC: // bne
if(r[r0] != r[r1]) pc = r[r2];
break;
case 0xD: // inc
r[r0]++;
break;
case 0xE: // dec
r[r0]--;
break;
}
debug_registers();
}
bool load_program(const char *filename) {
FILE *fp;
long length;
fp = fopen(filename, "r");
if(fp == NULL) return false;
fseek(fp, 0L, SEEK_END);
length = ftell(fp) / sizeof(int);
fseek(fp, 0L, SEEK_SET);
program = (unsigned long long*)calloc(length, sizeof(unsigned long long));
if(program == NULL) return false;
fread(program, sizeof(unsigned long long), length, fp);
fclose(fp);
return true;
}
bool allocate_ram() {
ram = (int*)calloc(RAM_SIZE, sizeof(int));
return ram == NULL ? false : true;
}
bool allocate_registers() {
r = (int*)calloc(NUM_REGS, sizeof(int));
return r == NULL ? false : true;
}
int main(int argc, const char *argv[]) {
if(!allocate_registers()) {
printf("error allocating registers\n");
return EXIT_FAILURE;
}
if(!allocate_ram()) {
printf("error allocating ram\n");
return EXIT_FAILURE;
}
if(!load_program(argv[1])) {
printf("error loading program\n");
return EXIT_FAILURE;
}
while(running) {
cycle();
}
//debug_ram();
return EXIT_SUCCESS;
}