-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathvm_minimal.c
72 lines (61 loc) · 1.79 KB
/
vm_minimal.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
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of stage0.
*
* stage0 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stage0 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stage0. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vm.h"
/* Load program tape into Memory */
void load_program(struct lilith* vm, char **argv)
{
FILE* program;
program = fopen(argv[1], "r");
/* Figure out how much we need to load */
fseek(program, 0, SEEK_END);
size_t end = ftell(program);
rewind(program);
/* Load the entire tape into memory */
fread(vm->memory, 1, end, program);
fclose(program);
}
void execute_vm(struct lilith* vm)
{
struct Instruction* current;
current = calloc(1, sizeof(struct Instruction));
while(!vm->halted)
{
read_instruction(vm, current);
eval_instruction(vm, current);
}
free(current);
return;
}
/* Standard C main program */
int main(int argc, char **argv)
{
/* Make sure we have a program tape to run */
if (argc < 2)
{
fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of the paper tape of the program being run\n", argv[0]);
return EXIT_FAILURE;
}
/* Perform all the essential stages in order */
struct lilith* vm;
tape_01_name = "tape_01";
tape_02_name = "tape_02";
vm = create_vm(1 << 21);
load_program(vm, argv);
execute_vm(vm);
destroy_vm(vm);
return EXIT_SUCCESS;
}