vm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. #ifdef VM256
  23. typedef __int512_t signed_wide_register;
  24. typedef __uint512_t unsigned_wide_register;
  25. typedef __int256_t signed_vm_register;
  26. typedef __uint256_t unsigned_vm_register;
  27. #define umax 256
  28. #define imax 255
  29. #define reg_size 32
  30. #elif VM128
  31. typedef __int256_t signed_wide_register;
  32. typedef __uint256_t unsigned_wide_register;
  33. typedef __int128_t signed_vm_register;
  34. typedef __uint128_t unsigned_vm_register;
  35. #define umax 128
  36. #define imax 127
  37. #define reg_size 16
  38. #elif VM64
  39. typedef __int128_t signed_wide_register;
  40. typedef __uint128_t unsigned_wide_register;
  41. typedef int64_t signed_vm_register;
  42. typedef uint64_t unsigned_vm_register;
  43. #define umax 64
  44. #define imax 63
  45. #define reg_size 8
  46. #elif VM32
  47. typedef int64_t signed_wide_register;
  48. typedef uint64_t unsigned_wide_register;
  49. typedef int32_t signed_vm_register;
  50. typedef uint32_t unsigned_vm_register;
  51. #define umax 32
  52. #define imax 31
  53. #define reg_size 4
  54. #else
  55. typedef int32_t signed_wide_register;
  56. typedef uint32_t unsigned_wide_register;
  57. typedef int16_t signed_vm_register;
  58. typedef uint16_t unsigned_vm_register;
  59. #define umax 16
  60. #define imax 15
  61. #define reg_size 2
  62. #endif
  63. /* Virtual machine state */
  64. struct lilith
  65. {
  66. uint8_t *memory;
  67. size_t amount_of_Ram;
  68. unsigned_vm_register reg[16];
  69. unsigned_vm_register ip;
  70. bool halted;
  71. bool exception;
  72. };
  73. /* Unpacked instruction */
  74. struct Instruction
  75. {
  76. unsigned_vm_register ip;
  77. uint8_t raw0, raw1, raw2, raw3;
  78. char opcode[3];
  79. uint32_t raw_XOP;
  80. char XOP[6];
  81. char operation[9];
  82. int16_t raw_Immediate;
  83. char Immediate[7];
  84. uint32_t HAL_CODE;
  85. uint8_t reg0;
  86. uint8_t reg1;
  87. uint8_t reg2;
  88. uint8_t reg3;
  89. bool invalid;
  90. };
  91. /* Prototypes for functions in vm_instructions.c*/
  92. void vm_FOPEN_READ(struct lilith* vm);
  93. void vm_FOPEN_WRITE(struct lilith* vm);
  94. void vm_FCLOSE(struct lilith* vm);
  95. void vm_FSEEK(struct lilith* vm);
  96. void vm_REWIND(struct lilith* vm);
  97. void vm_FGETC(struct lilith* vm);
  98. void vm_FPUTC(struct lilith* vm);
  99. void vm_HAL_MEM(struct lilith* vm);
  100. void ADD_CI(struct lilith* vm, struct Instruction* c);
  101. void ADD_CO(struct lilith* vm, struct Instruction* c);
  102. void ADD_CIO(struct lilith* vm, struct Instruction* c);
  103. void ADDU_CI(struct lilith* vm, struct Instruction* c);
  104. void ADDU_CO(struct lilith* vm, struct Instruction* c);
  105. void ADDU_CIO(struct lilith* vm, struct Instruction* c);
  106. void SUB_BI(struct lilith* vm, struct Instruction* c);
  107. void SUB_BO(struct lilith* vm, struct Instruction* c);
  108. void SUB_BIO(struct lilith* vm, struct Instruction* c);
  109. void SUBU_BI(struct lilith* vm, struct Instruction* c);
  110. void SUBU_BO(struct lilith* vm, struct Instruction* c);
  111. void SUBU_BIO(struct lilith* vm, struct Instruction* c);
  112. void MULTIPLY(struct lilith* vm, struct Instruction* c);
  113. void MULTIPLYU(struct lilith* vm, struct Instruction* c);
  114. void DIVIDE(struct lilith* vm, struct Instruction* c);
  115. void DIVIDEU(struct lilith* vm, struct Instruction* c);
  116. void MUX(struct lilith* vm, struct Instruction* c);
  117. void NMUX(struct lilith* vm, struct Instruction* c);
  118. void SORT(struct lilith* vm, struct Instruction* c);
  119. void SORTU(struct lilith* vm, struct Instruction* c);
  120. void ADD(struct lilith* vm, struct Instruction* c);
  121. void ADDU(struct lilith* vm, struct Instruction* c);
  122. void SUB(struct lilith* vm, struct Instruction* c);
  123. void SUBU(struct lilith* vm, struct Instruction* c);
  124. void CMP(struct lilith* vm, struct Instruction* c);
  125. void CMPU(struct lilith* vm, struct Instruction* c);
  126. void MUL(struct lilith* vm, struct Instruction* c);
  127. void MULH(struct lilith* vm, struct Instruction* c);
  128. void MULU(struct lilith* vm, struct Instruction* c);
  129. void MULUH(struct lilith* vm, struct Instruction* c);
  130. void DIV(struct lilith* vm, struct Instruction* c);
  131. void MOD(struct lilith* vm, struct Instruction* c);
  132. void DIVU(struct lilith* vm, struct Instruction* c);
  133. void MODU(struct lilith* vm, struct Instruction* c);
  134. void MAX(struct lilith* vm, struct Instruction* c);
  135. void MAXU(struct lilith* vm, struct Instruction* c);
  136. void MIN(struct lilith* vm, struct Instruction* c);
  137. void MINU(struct lilith* vm, struct Instruction* c);
  138. void AND(struct lilith* vm, struct Instruction* c);
  139. void OR(struct lilith* vm, struct Instruction* c);
  140. void XOR(struct lilith* vm, struct Instruction* c);
  141. void NAND(struct lilith* vm, struct Instruction* c);
  142. void NOR(struct lilith* vm, struct Instruction* c);
  143. void XNOR(struct lilith* vm, struct Instruction* c);
  144. void MPQ(struct lilith* vm, struct Instruction* c);
  145. void LPQ(struct lilith* vm, struct Instruction* c);
  146. void CPQ(struct lilith* vm, struct Instruction* c);
  147. void BPQ(struct lilith* vm, struct Instruction* c);
  148. void SAL(struct lilith* vm, struct Instruction* c);
  149. void SAR(struct lilith* vm, struct Instruction* c);
  150. void SL0(struct lilith* vm, struct Instruction* c);
  151. void SR0(struct lilith* vm, struct Instruction* c);
  152. void SL1(struct lilith* vm, struct Instruction* c);
  153. void SR1(struct lilith* vm, struct Instruction* c);
  154. void ROL(struct lilith* vm, struct Instruction* c);
  155. void ROR(struct lilith* vm, struct Instruction* c);
  156. void LOADX(struct lilith* vm, struct Instruction* c);
  157. void LOADX8(struct lilith* vm, struct Instruction* c);
  158. void LOADXU8(struct lilith* vm, struct Instruction* c);
  159. void LOADX16(struct lilith* vm, struct Instruction* c);
  160. void LOADXU16(struct lilith* vm, struct Instruction* c);
  161. void LOADX32(struct lilith* vm, struct Instruction* c);
  162. void LOADXU32(struct lilith* vm, struct Instruction* c);
  163. void STOREX(struct lilith* vm, struct Instruction* c);
  164. void STOREX8(struct lilith* vm, struct Instruction* c);
  165. void STOREX16(struct lilith* vm, struct Instruction* c);
  166. void STOREX32(struct lilith* vm, struct Instruction* c);
  167. void NEG(struct lilith* vm, struct Instruction* c);
  168. void ABS(struct lilith* vm, struct Instruction* c);
  169. void NABS(struct lilith* vm, struct Instruction* c);
  170. void SWAP(struct lilith* vm, struct Instruction* c);
  171. void COPY(struct lilith* vm, struct Instruction* c);
  172. void MOVE(struct lilith* vm, struct Instruction* c);
  173. void BRANCH(struct lilith* vm, struct Instruction* c);
  174. void CALL(struct lilith* vm, struct Instruction* c);
  175. void READPC(struct lilith* vm, struct Instruction* c);
  176. void READSCID(struct lilith* vm, struct Instruction* c);
  177. void FALSE(struct lilith* vm, struct Instruction* c);
  178. void TRUE(struct lilith* vm, struct Instruction* c);
  179. void JSR_COROUTINE(struct lilith* vm, struct Instruction* c);
  180. void RET(struct lilith* vm, struct Instruction* c);
  181. void PUSHPC(struct lilith* vm, struct Instruction* c);
  182. void POPPC(struct lilith* vm, struct Instruction* c);
  183. void ADDI(struct lilith* vm, struct Instruction* c);
  184. void ADDUI(struct lilith* vm, struct Instruction* c);
  185. void SUBI(struct lilith* vm, struct Instruction* c);
  186. void SUBUI(struct lilith* vm, struct Instruction* c);
  187. void CMPI(struct lilith* vm, struct Instruction* c);
  188. void LOAD(struct lilith* vm, struct Instruction* c);
  189. void LOAD8(struct lilith* vm, struct Instruction* c);
  190. void LOADU8(struct lilith* vm, struct Instruction* c);
  191. void LOAD16(struct lilith* vm, struct Instruction* c);
  192. void LOADU16(struct lilith* vm, struct Instruction* c);
  193. void LOAD32(struct lilith* vm, struct Instruction* c);
  194. void LOADU32(struct lilith* vm, struct Instruction* c);
  195. void CMPUI(struct lilith* vm, struct Instruction* c);
  196. void STORE(struct lilith* vm, struct Instruction* c);
  197. void STORE8(struct lilith* vm, struct Instruction* c);
  198. void STORE16(struct lilith* vm, struct Instruction* c);
  199. void STORE32(struct lilith* vm, struct Instruction* c);
  200. void JUMP_C(struct lilith* vm, struct Instruction* c);
  201. void JUMP_B(struct lilith* vm, struct Instruction* c);
  202. void JUMP_O(struct lilith* vm, struct Instruction* c);
  203. void JUMP_G(struct lilith* vm, struct Instruction* c);
  204. void JUMP_GE(struct lilith* vm, struct Instruction* c);
  205. void JUMP_E(struct lilith* vm, struct Instruction* c);
  206. void JUMP_NE(struct lilith* vm, struct Instruction* c);
  207. void JUMP_LE(struct lilith* vm, struct Instruction* c);
  208. void JUMP_L(struct lilith* vm, struct Instruction* c);
  209. void JUMP_Z(struct lilith* vm, struct Instruction* c);
  210. void JUMP_NZ(struct lilith* vm, struct Instruction* c);
  211. void CALLI(struct lilith* vm, struct Instruction* c);
  212. void LOADI(struct lilith* vm, struct Instruction* c);
  213. void LOADUI(struct lilith* vm, struct Instruction* c);
  214. void SALI(struct lilith* vm, struct Instruction* c);
  215. void SARI(struct lilith* vm, struct Instruction* c);
  216. void SL0I(struct lilith* vm, struct Instruction* c);
  217. void SR0I(struct lilith* vm, struct Instruction* c);
  218. void SL1I(struct lilith* vm, struct Instruction* c);
  219. void SR1I(struct lilith* vm, struct Instruction* c);
  220. void LOADR(struct lilith* vm, struct Instruction* c);
  221. void LOADR8(struct lilith* vm, struct Instruction* c);
  222. void LOADRU8(struct lilith* vm, struct Instruction* c);
  223. void LOADR16(struct lilith* vm, struct Instruction* c);
  224. void LOADRU16(struct lilith* vm, struct Instruction* c);
  225. void LOADR32(struct lilith* vm, struct Instruction* c);
  226. void LOADRU32(struct lilith* vm, struct Instruction* c);
  227. void STORER(struct lilith* vm, struct Instruction* c);
  228. void STORER8(struct lilith* vm, struct Instruction* c);
  229. void STORER16(struct lilith* vm, struct Instruction* c);
  230. void STORER32(struct lilith* vm, struct Instruction* c);
  231. void JUMP(struct lilith* vm, struct Instruction* c);
  232. void JUMP_P(struct lilith* vm, struct Instruction* c);
  233. void JUMP_NP(struct lilith* vm, struct Instruction* c);
  234. void CMPJUMPI_G(struct lilith* vm, struct Instruction* c);
  235. void CMPJUMPI_GE(struct lilith* vm, struct Instruction* c);
  236. void CMPJUMPI_E(struct lilith* vm, struct Instruction* c);
  237. void CMPJUMPI_NE(struct lilith* vm, struct Instruction* c);
  238. void CMPJUMPI_LE(struct lilith* vm, struct Instruction* c);
  239. void CMPJUMPI_L(struct lilith* vm, struct Instruction* c);
  240. void CMPJUMPUI_G(struct lilith* vm, struct Instruction* c);
  241. void CMPJUMPUI_GE(struct lilith* vm, struct Instruction* c);
  242. void CMPJUMPUI_LE(struct lilith* vm, struct Instruction* c);
  243. void CMPJUMPUI_L(struct lilith* vm, struct Instruction* c);
  244. void CMPSKIPI_G(struct lilith* vm, struct Instruction* c);
  245. void CMPSKIPI_GE(struct lilith* vm, struct Instruction* c);
  246. void CMPSKIPI_E(struct lilith* vm, struct Instruction* c);
  247. void CMPSKIPI_NE(struct lilith* vm, struct Instruction* c);
  248. void CMPSKIPI_LE(struct lilith* vm, struct Instruction* c);
  249. void CMPSKIPI_L(struct lilith* vm, struct Instruction* c);
  250. void CMPSKIPUI_G(struct lilith* vm, struct Instruction* c);
  251. void CMPSKIPUI_GE(struct lilith* vm, struct Instruction* c);
  252. void CMPSKIPUI_LE(struct lilith* vm, struct Instruction* c);
  253. void CMPSKIPUI_L(struct lilith* vm, struct Instruction* c);
  254. void PUSHR(struct lilith* vm, struct Instruction* c);
  255. void PUSH8(struct lilith* vm, struct Instruction* c);
  256. void PUSH16(struct lilith* vm, struct Instruction* c);
  257. void PUSH32(struct lilith* vm, struct Instruction* c);
  258. void POPR(struct lilith* vm, struct Instruction* c);
  259. void POP8(struct lilith* vm, struct Instruction* c);
  260. void POPU8(struct lilith* vm, struct Instruction* c);
  261. void POP16(struct lilith* vm, struct Instruction* c);
  262. void POPU16(struct lilith* vm, struct Instruction* c);
  263. void POP32(struct lilith* vm, struct Instruction* c);
  264. void POPU32(struct lilith* vm, struct Instruction* c);
  265. void ANDI(struct lilith* vm, struct Instruction* c);
  266. void ORI(struct lilith* vm, struct Instruction* c);
  267. void XORI(struct lilith* vm, struct Instruction* c);
  268. void NANDI(struct lilith* vm, struct Instruction* c);
  269. void NORI(struct lilith* vm, struct Instruction* c);
  270. void XNORI(struct lilith* vm, struct Instruction* c);
  271. void NOT(struct lilith* vm, struct Instruction* c);
  272. void CMPSKIP_G(struct lilith* vm, struct Instruction* c);
  273. void CMPSKIP_GE(struct lilith* vm, struct Instruction* c);
  274. void CMPSKIP_E(struct lilith* vm, struct Instruction* c);
  275. void CMPSKIP_NE(struct lilith* vm, struct Instruction* c);
  276. void CMPSKIP_LE(struct lilith* vm, struct Instruction* c);
  277. void CMPSKIP_L(struct lilith* vm, struct Instruction* c);
  278. void CMPSKIPU_G(struct lilith* vm, struct Instruction* c);
  279. void CMPSKIPU_GE(struct lilith* vm, struct Instruction* c);
  280. void CMPSKIPU_LE(struct lilith* vm, struct Instruction* c);
  281. void CMPSKIPU_L(struct lilith* vm, struct Instruction* c);
  282. void CMPJUMP_G(struct lilith* vm, struct Instruction* c);
  283. void CMPJUMP_GE(struct lilith* vm, struct Instruction* c);
  284. void CMPJUMP_E(struct lilith* vm, struct Instruction* c);
  285. void CMPJUMP_NE(struct lilith* vm, struct Instruction* c);
  286. void CMPJUMP_LE(struct lilith* vm, struct Instruction* c);
  287. void CMPJUMP_L(struct lilith* vm, struct Instruction* c);
  288. void CMPJUMPU_G(struct lilith* vm, struct Instruction* c);
  289. void CMPJUMPU_GE(struct lilith* vm, struct Instruction* c);
  290. void CMPJUMPU_LE(struct lilith* vm, struct Instruction* c);
  291. void CMPJUMPU_L(struct lilith* vm, struct Instruction* c);
  292. /* Prototypes for functions in vm_decode.c*/
  293. struct lilith* create_vm(size_t size);
  294. void destroy_vm(struct lilith* vm);
  295. void read_instruction(struct lilith* vm, struct Instruction *current);
  296. void eval_instruction(struct lilith* vm, struct Instruction* current);
  297. void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* message);
  298. /* Allow tape names to be effectively changed */
  299. char* tape_01_name;
  300. char* tape_02_name;