wrapper.c 3.8 KB

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