wrapper.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #define DEBUG true
  19. extern uint32_t performance_counter;
  20. static struct lilith* Globalvm;
  21. void unpack_byte(uint8_t a, char* c);
  22. /* Load program tape into Memory */
  23. size_t load_program(struct lilith* vm, char* name)
  24. {
  25. FILE* program;
  26. program = fopen(name, "r");
  27. /* Figure out how much we need to load */
  28. fseek(program, 0, SEEK_END);
  29. size_t end = ftell(program);
  30. rewind(program);
  31. /* Load the entire tape into memory */
  32. fread(vm->memory, 1, end, program);
  33. fclose(program);
  34. return end;
  35. }
  36. void execute_vm(struct lilith* vm)
  37. {
  38. struct Instruction* current;
  39. current = calloc(1, sizeof(struct Instruction));
  40. read_instruction(vm, current);
  41. eval_instruction(vm, current);
  42. free(current);
  43. return;
  44. }
  45. void initialize_lilith(unsigned int size, unsigned int posix)
  46. {
  47. POSIX_MODE = posix;
  48. tape_01_name = "tape_01";
  49. tape_02_name = "tape_02";
  50. struct lilith* vm;
  51. vm = create_vm(size);
  52. Globalvm = vm;
  53. }
  54. void load_lilith(char* name)
  55. {
  56. load_program(Globalvm, name);
  57. }
  58. unsigned int step_lilith()
  59. {
  60. if(!Globalvm->halted)
  61. {
  62. execute_vm(Globalvm);
  63. }
  64. return Globalvm->ip;
  65. }
  66. unsigned int get_register(unsigned int reg)
  67. {
  68. unsigned int i;
  69. switch(reg)
  70. {
  71. case 0 ... 15:
  72. {
  73. i = Globalvm->reg[reg];
  74. break;
  75. }
  76. case 16: /* Get PC */
  77. {
  78. i = Globalvm->ip;
  79. break;
  80. }
  81. case 17: /* Instruction count */
  82. {
  83. i = performance_counter;
  84. break;
  85. }
  86. default:
  87. {
  88. fprintf(stderr, "What have you done????\n");
  89. exit(EXIT_FAILURE);
  90. }
  91. }
  92. return i;
  93. }
  94. void set_register(unsigned int reg, unsigned int value)
  95. {
  96. switch(reg)
  97. {
  98. case 0 ... 15:
  99. {
  100. Globalvm->reg[reg] = value;
  101. break;
  102. }
  103. case 16: /* Set PC */
  104. {
  105. Globalvm->ip = value;
  106. break;
  107. }
  108. case 17: /* No one mess with the Instruction count */
  109. {
  110. fprintf(stderr, "Don't be a moron!!!!\n");
  111. break;
  112. }
  113. default:
  114. {
  115. fprintf(stderr, "What are you trying to do????\n");
  116. exit(EXIT_FAILURE);
  117. }
  118. }
  119. }
  120. void set_memory(unsigned int address, unsigned char value)
  121. {
  122. outside_of_world(Globalvm, address, "Address outside of World");
  123. Globalvm->memory[address] = value;
  124. }
  125. unsigned char get_byte(unsigned int add)
  126. {
  127. outside_of_world(Globalvm, add, "Address outside of World");
  128. return Globalvm->memory[add];
  129. }
  130. void insert_address(char* p, uint32_t value)
  131. {
  132. char* segment;
  133. segment = p;
  134. unpack_byte((value >> 24), segment);
  135. segment = segment + 2;
  136. unpack_byte((value >> 16)%256, segment);
  137. segment = segment + 2;
  138. unpack_byte((value >> 8)%256, segment);
  139. segment = segment + 2;
  140. unpack_byte((value%256), segment);
  141. }
  142. void process_Memory_Row(char* p, uint32_t addr)
  143. {
  144. char* segment = p;
  145. strncpy(segment, "<tr>\n<td>", 9);
  146. segment = segment + 9;
  147. insert_address(segment, addr);
  148. segment = segment + 8;
  149. strncpy(segment, "</td>", 5);
  150. segment = segment + 5;
  151. int i;
  152. for(i = 0; i < 16; i = i + 1)
  153. {
  154. strncpy(segment, "<td>", 4);
  155. segment = segment + 4;
  156. unpack_byte(Globalvm->memory[i + addr], segment);
  157. segment = segment + 2;
  158. strncpy(segment, "</td>", 5);
  159. segment = segment + 5;
  160. }
  161. strncpy(segment, "\n</tr>\n", 7);
  162. }
  163. char* get_memory(unsigned int start)
  164. {
  165. char* result = calloc(4096 * 205 + 1, sizeof(char));
  166. int i, point;
  167. point = 0;
  168. for(i = 0; i < 4096; i = i + 16)
  169. {
  170. process_Memory_Row(result + point, start + i);
  171. point = point + 205;
  172. }
  173. return result;
  174. }