2016-06-12 14:02:00 +01:00
|
|
|
#include "vm.h"
|
|
|
|
|
2016-05-29 04:40:31 +01:00
|
|
|
/* 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);
|
2016-05-29 18:13:24 +01:00
|
|
|
eval_instruction(vm, current);
|
2016-05-29 04:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-02-12 22:25:03 +00:00
|
|
|
vm = create_vm(1 << 21);
|
2016-05-29 04:40:31 +01:00
|
|
|
load_program(vm, argv);
|
|
|
|
execute_vm(vm);
|
|
|
|
destroy_vm(vm);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|