-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
46 lines (37 loc) · 993 Bytes
/
main.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
#include <stdio.h>
#include "toyvm.h"
static size_t getFileSize(FILE* file)
{
long int original_cursor = ftell(file);
fseek(file, 0L, SEEK_END);
size_t size = ftell(file);
fseek(file, original_cursor, SEEK_SET);
return size;
}
int main(int argc, const char * argv[]) {
if (argc != 2)
{
puts("Usage: toy FILE.brick\n");
return 0;
}
FILE* file = fopen(argv[1], "r");
if (!file)
{
printf("ERROR: cannot read file \"%s\".", argv[1]);
return (EXIT_FAILURE);
}
size_t file_size = getFileSize(file);
TOYVM vm;
InitializeVM(&vm, 2 * file_size, file_size);
fread(vm.memory, 1, file_size, file);
fclose(file);
RunVM(&vm);
if (vm.cpu.status.BAD_ACCESS
|| vm.cpu.status.BAD_INSTRUCTION
|| vm.cpu.status.INVALID_REGISTER_INDEX
|| vm.cpu.status.STACK_OVERFLOW
|| vm.cpu.status.STACK_UNDERFLOW)
{
PrintStatus(&vm);
}
}