wrapper.c 3.9 KB

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