vm_minimal.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "vm.h"
  18. /* Load program tape into Memory */
  19. void load_program(struct lilith* vm, char **argv)
  20. {
  21. FILE* program;
  22. program = fopen(argv[1], "r");
  23. /* Figure out how much we need to load */
  24. fseek(program, 0, SEEK_END);
  25. size_t end = ftell(program);
  26. rewind(program);
  27. /* Load the entire tape into memory */
  28. fread(vm->memory, 1, end, program);
  29. fclose(program);
  30. }
  31. void execute_vm(struct lilith* vm)
  32. {
  33. struct Instruction* current;
  34. current = calloc(1, sizeof(struct Instruction));
  35. while(!vm->halted)
  36. {
  37. read_instruction(vm, current);
  38. eval_instruction(vm, current);
  39. }
  40. free(current);
  41. return;
  42. }
  43. /* Standard C main program */
  44. int main(int argc, char **argv)
  45. {
  46. /* Make sure we have a program tape to run */
  47. if (argc < 2)
  48. {
  49. fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of the paper tape of the program being run\n", argv[0]);
  50. return EXIT_FAILURE;
  51. }
  52. /* Perform all the essential stages in order */
  53. struct lilith* vm;
  54. tape_01_name = "tape_01";
  55. tape_02_name = "tape_02";
  56. vm = create_vm(1 << 21);
  57. load_program(vm, argv);
  58. execute_vm(vm);
  59. destroy_vm(vm);
  60. return EXIT_SUCCESS;
  61. }