vm.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "vm.h"
  2. /* Load program tape into Memory */
  3. void load_program(struct lilith* vm, char **argv)
  4. {
  5. FILE* program;
  6. program = fopen(argv[1], "r");
  7. /* Figure out how much we need to load */
  8. fseek(program, 0, SEEK_END);
  9. size_t end = ftell(program);
  10. rewind(program);
  11. /* Load the entire tape into memory */
  12. fread(vm->memory, 1, end, program);
  13. fclose(program);
  14. }
  15. void execute_vm(struct lilith* vm)
  16. {
  17. struct Instruction* current;
  18. current = calloc(1, sizeof(struct Instruction));
  19. while(!vm->halted)
  20. {
  21. read_instruction(vm, current);
  22. eval_instruction(vm, current);
  23. }
  24. free(current);
  25. return;
  26. }
  27. /* Standard C main program */
  28. int main(int argc, char **argv)
  29. {
  30. /* Make sure we have a program tape to run */
  31. if (argc < 2)
  32. {
  33. fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of the paper tape of the program being run\n", argv[0]);
  34. return EXIT_FAILURE;
  35. }
  36. /* Perform all the essential stages in order */
  37. struct lilith* vm;
  38. vm = create_vm(1 << 20);
  39. load_program(vm, argv);
  40. execute_vm(vm);
  41. destroy_vm(vm);
  42. return EXIT_SUCCESS;
  43. }