vm_instructions.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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. #include <unistd.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. FILE* tape_01;
  22. FILE* tape_02;
  23. #ifdef tty_lib
  24. char tty_getchar();
  25. #endif
  26. /* Use first byte of next instruction to identify size */
  27. int next_instruction_size(struct lilith* vm)
  28. {
  29. uint8_t c = (uint8_t)vm->memory[vm->ip];
  30. switch(c)
  31. {
  32. case 0xE0: return 6;
  33. case 0xE1: return 6;
  34. default: return 4;
  35. }
  36. }
  37. /* Correctly write out bytes on little endian hardware */
  38. void writeout_bytes(struct lilith* vm, unsigned_vm_register pointer, unsigned_vm_register value, int count)
  39. {
  40. uint8_t raw0;
  41. outside_of_world(vm, pointer, "Writeout bytes Address_1 is outside of World");
  42. outside_of_world(vm, pointer+count, "Writeout bytes Address_2 is outside of World");
  43. while(0 < count)
  44. {
  45. raw0 = (value >> (8 * (count - 1))) & 0xff;
  46. vm->memory[pointer] = raw0;
  47. pointer = pointer + 1;
  48. count = count - 1;
  49. }
  50. }
  51. void writeout_string(struct lilith* vm, char* s, unsigned_vm_register pointer)
  52. {
  53. while(0 != s[0])
  54. {
  55. vm->memory[pointer] = s[0];
  56. pointer = pointer + 1;
  57. s = s + 1;
  58. }
  59. }
  60. /* Allow the use of native data format for Register operations */
  61. unsigned_vm_register readin_bytes(struct lilith* vm, unsigned_vm_register pointer, bool Signed, int count)
  62. {
  63. outside_of_world(vm, pointer, "READIN bytes Address_1 is outside of World");
  64. outside_of_world(vm, pointer+count, "READIN bytes Address_2 is outside of World");
  65. uint8_t raw0;
  66. if(Signed)
  67. {
  68. signed_vm_register sum = (int8_t) vm->memory[pointer];
  69. while(1 < count)
  70. {
  71. pointer = pointer + 1;
  72. count = count - 1;
  73. raw0 = vm->memory[pointer];
  74. sum = (sum << 8) + raw0;
  75. }
  76. return sum;
  77. }
  78. unsigned_vm_register sum = 0;
  79. while(0 < count)
  80. {
  81. raw0 = vm->memory[pointer];
  82. sum = (sum << 8) + raw0;
  83. pointer = pointer + 1;
  84. count = count - 1;
  85. }
  86. return sum;
  87. }
  88. /* Determine the result of bit shifting */
  89. unsigned_vm_register shift_register(unsigned_vm_register source, unsigned_vm_register amount, bool left, bool zero)
  90. {
  91. unsigned_vm_register tmp = source;
  92. if(left)
  93. {
  94. while( amount > 0 )
  95. {
  96. tmp = tmp * 2;
  97. amount = amount - 1;
  98. if(!zero)
  99. {
  100. tmp = tmp + 1;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. while( amount > 0 )
  107. {
  108. tmp = tmp / 2;
  109. amount = amount - 1;
  110. if(!zero)
  111. {
  112. tmp = tmp | (1 << imax);
  113. }
  114. }
  115. }
  116. return tmp;
  117. }
  118. char* string_copy(struct lilith* vm, signed_vm_register address)
  119. {
  120. int i = 0;
  121. char* r = calloc(4096, sizeof(char));
  122. int c = vm->memory[address];
  123. while(0 != c)
  124. {
  125. r[i] = c;
  126. i = i + 1;
  127. c = vm->memory[address + i];
  128. }
  129. return r;
  130. }
  131. void vm_EXIT(struct lilith* vm, uint64_t performance_counter)
  132. {
  133. vm->halted = true;
  134. fprintf(stderr, "Computer Program has Halted\nAfter Executing %lu instructions\n", performance_counter);
  135. #ifdef TRACE
  136. record_trace("HALT");
  137. print_traces();
  138. #endif
  139. exit(vm->reg[0]);
  140. }
  141. void vm_CHMOD(struct lilith* vm)
  142. {
  143. char* s = string_copy(vm, vm->reg[0]);
  144. chmod(s, vm->reg[1]);
  145. free(s);
  146. }
  147. void vm_UNAME(struct lilith* vm)
  148. {
  149. writeout_string(vm, "sysname", vm->reg[0]);
  150. writeout_string(vm, "nodename", vm->reg[0] + 65);
  151. writeout_string(vm, "release", vm->reg[0] + 130);
  152. writeout_string(vm, "version", vm->reg[0] + 195);
  153. writeout_string(vm, arch_name, vm->reg[0] + 260);
  154. }
  155. void vm_GETCWD(struct lilith* vm)
  156. {
  157. char* s = malloc(vm->reg[1]);
  158. s = getcwd(s, vm->reg[1]);
  159. if(NULL == s)
  160. {
  161. vm->reg[0] = 0;
  162. }
  163. else
  164. {
  165. writeout_string(vm, s, vm->reg[0]);
  166. }
  167. free(s);
  168. }
  169. void vm_CHDIR(struct lilith* vm)
  170. {
  171. char* s = string_copy(vm, vm->reg[0]);
  172. vm->reg[0] = chdir(s);
  173. free(s);
  174. }
  175. void vm_FCHDIR(struct lilith* vm)
  176. {
  177. vm->reg[0] = fchdir(vm->reg[0]);
  178. }
  179. void vm_ACCESS(struct lilith* vm)
  180. {
  181. char* s = string_copy(vm, vm->reg[0]);
  182. vm->reg[0] = access(s, vm->reg[1]);
  183. free(s);
  184. }
  185. void vm_FOPEN(struct lilith* vm)
  186. {
  187. char* s = string_copy(vm, vm->reg[0]);
  188. vm->reg[0] = open(s, vm->reg[1], vm->reg[2]);
  189. free(s);
  190. }
  191. void vm_FOPEN_READ(struct lilith* vm)
  192. {
  193. struct stat sb;
  194. if(POSIX_MODE)
  195. {
  196. char* s = string_copy(vm, vm->reg[0]);
  197. if(-1 == stat(s, &sb))
  198. {
  199. fprintf(stderr, "File named %s does not exist\n", s);
  200. exit(EXIT_FAILURE);
  201. }
  202. vm->reg[0] = open(s, 0);
  203. free(s);
  204. }
  205. else
  206. {
  207. if(0x00001100 == vm->reg[0])
  208. {
  209. if(-1 == stat(tape_01_name, &sb))
  210. {
  211. fprintf(stderr, "File named %s does not exist\n", tape_01_name);
  212. exit(EXIT_FAILURE);
  213. }
  214. tape_01 = fopen(tape_01_name, "r");
  215. }
  216. if (0x00001101 == vm->reg[0])
  217. {
  218. if(-1 == stat(tape_02_name, &sb))
  219. {
  220. fprintf(stderr, "File named %s does not exist\n", tape_02_name);
  221. exit(EXIT_FAILURE);
  222. }
  223. tape_02 = fopen(tape_02_name, "r");
  224. }
  225. }
  226. }
  227. void vm_FOPEN_WRITE(struct lilith* vm)
  228. {
  229. if(POSIX_MODE)
  230. {
  231. char* s = string_copy(vm, vm->reg[0]);
  232. /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  233. vm->reg[0] = open(s, 577, 384);
  234. free(s);
  235. }
  236. else
  237. {
  238. if(0x00001100 == vm->reg[0])
  239. {
  240. tape_01 = fopen(tape_01_name, "w");
  241. }
  242. if (0x00001101 == vm->reg[0])
  243. {
  244. tape_02 = fopen(tape_02_name, "w");
  245. }
  246. }
  247. }
  248. void vm_FCLOSE(struct lilith* vm)
  249. {
  250. if(POSIX_MODE)
  251. {
  252. close(vm->reg[0]);
  253. }
  254. else
  255. {
  256. if(0x00001100 == vm->reg[0])
  257. {
  258. fclose(tape_01);
  259. }
  260. if (0x00001101 == vm->reg[0])
  261. {
  262. fclose(tape_02);
  263. }
  264. }
  265. }
  266. void vm_FSEEK(struct lilith* vm)
  267. {
  268. if(POSIX_MODE)
  269. {
  270. lseek(vm->reg[0], vm->reg[1], SEEK_CUR);
  271. }
  272. else
  273. {
  274. if(0x00001100 == vm->reg[0])
  275. {
  276. fseek(tape_01, vm->reg[1], SEEK_CUR);
  277. }
  278. if (0x00001101 == vm->reg[0])
  279. {
  280. fseek(tape_02, vm->reg[1], SEEK_CUR);
  281. }
  282. }
  283. }
  284. void vm_REWIND(struct lilith* vm)
  285. {
  286. if(POSIX_MODE)
  287. {
  288. lseek(vm->reg[0], 0, SEEK_SET);
  289. }
  290. else
  291. {
  292. if(0x00001100 == vm->reg[0])
  293. {
  294. rewind(tape_01);
  295. }
  296. if (0x00001101 == vm->reg[0])
  297. {
  298. rewind(tape_02);
  299. }
  300. }
  301. }
  302. void vm_FGETC(struct lilith* vm)
  303. {
  304. signed_vm_register byte = -1;
  305. if(POSIX_MODE)
  306. {
  307. read(vm->reg[1], &byte, 1);
  308. if(EOF != byte) byte = byte & 0xFF;
  309. }
  310. else
  311. {
  312. if (0x00000000 == vm->reg[1])
  313. {
  314. #ifdef tty_lib
  315. byte = tty_getchar();
  316. #endif
  317. #ifndef tty_lib
  318. byte = fgetc(stdin);
  319. #endif
  320. }
  321. if(0x00001100 == vm->reg[1])
  322. {
  323. byte = fgetc(tape_01);
  324. }
  325. if (0x00001101 == vm->reg[1])
  326. {
  327. byte = fgetc(tape_02);
  328. }
  329. }
  330. vm->reg[0] = byte;
  331. }
  332. void vm_FPUTC(struct lilith* vm)
  333. {
  334. signed_vm_register byte = vm->reg[0] & 0xFF;
  335. if(POSIX_MODE)
  336. {
  337. write(vm->reg[1], &byte, 1);
  338. }
  339. else
  340. {
  341. if (0x00000000 == vm->reg[1])
  342. {
  343. fputc(byte, stdout);
  344. #ifdef tty_lib
  345. fflush(stdout);
  346. #endif
  347. }
  348. if(0x00001100 == vm->reg[1])
  349. {
  350. fputc(byte, tape_01);
  351. }
  352. if (0x00001101 == vm->reg[1])
  353. {
  354. fputc(byte, tape_02);
  355. }
  356. }
  357. }
  358. void vm_HAL_MEM(struct lilith* vm)
  359. {
  360. vm->reg[0] = vm->amount_of_Ram;
  361. }
  362. /* Condition Codes */
  363. enum condition
  364. {
  365. Carry = (1 << 5),
  366. Borrow = (1 << 4),
  367. Overflow = (1 << 3),
  368. GreaterThan = (1 << 2),
  369. EQual = (1 << 1),
  370. LessThan = 1
  371. };
  372. bool Carry_bit_set(unsigned_vm_register a)
  373. {
  374. return a & Carry;
  375. }
  376. bool Borrow_bit_set(unsigned_vm_register a)
  377. {
  378. return a & Borrow;
  379. }
  380. bool Overflow_bit_set(unsigned_vm_register a)
  381. {
  382. return a & Overflow;
  383. }
  384. bool GreaterThan_bit_set(unsigned_vm_register a)
  385. {
  386. return a & GreaterThan;
  387. }
  388. bool EQual_bit_set(unsigned_vm_register a)
  389. {
  390. return a & EQual;
  391. }
  392. bool LessThan_bit_set(unsigned_vm_register a)
  393. {
  394. return a & LessThan;
  395. }
  396. void ADD_CI(struct lilith* vm, struct Instruction* c)
  397. {
  398. signed_vm_register tmp1, tmp2;
  399. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  400. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  401. /* If carry bit set add in the carry */
  402. if(Carry_bit_set(vm->reg[c->reg3]))
  403. {
  404. vm->reg[c->reg0] = tmp1 + tmp2 + 1;
  405. }
  406. else
  407. {
  408. vm->reg[c->reg0] = tmp1 + tmp2;
  409. }
  410. }
  411. void ADD_CO(struct lilith* vm, struct Instruction* c)
  412. {
  413. signed_vm_register tmp1, tmp2;
  414. signed_wide_register btmp1;
  415. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  416. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  417. btmp1 = ((signed_wide_register)tmp1) + ((signed_wide_register)tmp2);
  418. /* If addition exceeds int32_t MAX, set carry bit */
  419. if(1 == ( btmp1 >> imax ))
  420. {
  421. vm->reg[c->reg3] = vm->reg[c->reg3] | Carry;
  422. }
  423. else
  424. {
  425. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Carry);
  426. }
  427. /* Standard addition */
  428. vm->reg[c->reg0] = (tmp1 + tmp2);
  429. }
  430. void ADD_CIO(struct lilith* vm, struct Instruction* c)
  431. {
  432. signed_vm_register tmp1, tmp2;
  433. signed_wide_register btmp1;
  434. bool C = Carry_bit_set(vm->reg[c->reg3]);
  435. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  436. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  437. btmp1 = ((signed_wide_register)tmp1) + ((signed_wide_register)tmp2);
  438. /* If addition exceeds int32_t MAX, set carry bit */
  439. if(1 == ( btmp1 >> imax ))
  440. {
  441. vm->reg[c->reg3] = vm->reg[c->reg3] | Carry;
  442. }
  443. else
  444. {
  445. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Carry);
  446. }
  447. /* If carry bit set before operation add in the carry */
  448. if(C)
  449. {
  450. vm->reg[c->reg0] = tmp1 + tmp2 + 1;
  451. }
  452. else
  453. {
  454. vm->reg[c->reg0] = tmp1 + tmp2;
  455. }
  456. }
  457. void ADDU_CI(struct lilith* vm, struct Instruction* c)
  458. {
  459. unsigned_vm_register utmp1, utmp2;
  460. utmp1 = vm->reg[c->reg1];
  461. utmp2 = vm->reg[c->reg2];
  462. /* If carry bit set add in the carry */
  463. if(Carry_bit_set(vm->reg[c->reg3]))
  464. {
  465. vm->reg[c->reg0] = utmp1 + utmp2 + 1;
  466. }
  467. else
  468. {
  469. vm->reg[c->reg0] = utmp1 + utmp2;
  470. }
  471. }
  472. void ADDU_CO(struct lilith* vm, struct Instruction* c)
  473. {
  474. unsigned_vm_register utmp1, utmp2;
  475. unsigned_wide_register ubtmp1;
  476. utmp1 = vm->reg[c->reg1];
  477. utmp2 = vm->reg[c->reg2];
  478. ubtmp1 = ((unsigned_wide_register)utmp1) + ((unsigned_wide_register)utmp2);
  479. /* If addition exceeds uint32_t MAX, set carry bit */
  480. if(0 != ( ubtmp1 >> umax ))
  481. {
  482. vm->reg[c->reg3] = vm->reg[c->reg3] | Carry;
  483. }
  484. else
  485. {
  486. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Carry);
  487. }
  488. /* Standard addition */
  489. vm->reg[c->reg0] = (utmp1 + utmp2);
  490. }
  491. void ADDU_CIO(struct lilith* vm, struct Instruction* c)
  492. {
  493. unsigned_vm_register utmp1, utmp2;
  494. unsigned_wide_register ubtmp1;
  495. bool C;
  496. C = Carry_bit_set(vm->reg[c->reg3]);
  497. utmp1 = vm->reg[c->reg1];
  498. utmp2 = vm->reg[c->reg2];
  499. ubtmp1 = ((unsigned_wide_register)utmp1) + ((unsigned_wide_register)utmp2);
  500. /* If addition exceeds uint32_t MAX, set carry bit */
  501. if(0 != ( ubtmp1 >> umax ))
  502. {
  503. vm->reg[c->reg3] = vm->reg[c->reg3] | Carry;
  504. }
  505. else
  506. {
  507. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Carry);
  508. }
  509. /* If carry bit was set before operation add in the carry */
  510. if(C)
  511. {
  512. vm->reg[c->reg0] = utmp1 + utmp2 + 1;
  513. }
  514. else
  515. {
  516. vm->reg[c->reg0] = utmp1 + utmp2;
  517. }
  518. }
  519. void SUB_BI(struct lilith* vm, struct Instruction* c)
  520. {
  521. signed_vm_register tmp1, tmp2;
  522. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  523. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  524. /* If borrow bit set subtract out the borrow */
  525. if(Borrow_bit_set(vm->reg[c->reg3]))
  526. {
  527. vm->reg[c->reg0] = tmp1 - tmp2 - 1;
  528. }
  529. else
  530. {
  531. vm->reg[c->reg0] = tmp1 - tmp2;
  532. }
  533. }
  534. void SUB_BO(struct lilith* vm, struct Instruction* c)
  535. {
  536. signed_vm_register tmp1, tmp2;
  537. signed_wide_register btmp1;
  538. btmp1 = (signed_wide_register)(vm->reg[c->reg1]);
  539. tmp1 = (signed_vm_register)(vm->reg[c->reg2]);
  540. tmp2 = (signed_vm_register)(btmp1 - tmp1);
  541. /* If subtraction goes below int32_t MIN set borrow */
  542. if(btmp1 != (tmp2 + tmp1))
  543. {
  544. vm->reg[c->reg3] = vm->reg[c->reg3] | Borrow;
  545. }
  546. else
  547. {
  548. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Borrow);
  549. }
  550. /* Standard subtraction */
  551. vm->reg[c->reg0] = tmp2;
  552. }
  553. void SUB_BIO(struct lilith* vm, struct Instruction* c)
  554. {
  555. signed_vm_register tmp1, tmp2;
  556. signed_wide_register btmp1;
  557. bool B;
  558. B = Borrow_bit_set(vm->reg[c->reg3]);
  559. btmp1 = (signed_wide_register)(vm->reg[c->reg1]);
  560. tmp1 = (signed_vm_register)(vm->reg[c->reg2]);
  561. tmp2 = (signed_vm_register)(btmp1 - tmp1);
  562. /* If subtraction goes below int32_t MIN set borrow */
  563. if(btmp1 != (tmp2 + tmp1))
  564. {
  565. vm->reg[c->reg3] = vm->reg[c->reg3] | Borrow;
  566. }
  567. else
  568. {
  569. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Borrow);
  570. }
  571. /* If borrow bit was set prior to operation subtract out the borrow */
  572. if(B)
  573. {
  574. vm->reg[c->reg0] = tmp2 - 1;
  575. }
  576. else
  577. {
  578. vm->reg[c->reg0] = tmp2;
  579. }
  580. }
  581. void SUBU_BI(struct lilith* vm, struct Instruction* c)
  582. {
  583. unsigned_vm_register utmp1, utmp2;
  584. utmp1 = vm->reg[c->reg1];
  585. utmp2 = vm->reg[c->reg2];
  586. /* If borrow bit set subtract out the borrow */
  587. if(Borrow_bit_set(vm->reg[c->reg3]))
  588. {
  589. vm->reg[c->reg0] = utmp1 - utmp2 - 1;
  590. }
  591. else
  592. {
  593. vm->reg[c->reg0] = utmp1 - utmp2;
  594. }
  595. }
  596. void SUBU_BO(struct lilith* vm, struct Instruction* c)
  597. {
  598. unsigned_vm_register utmp1, utmp2;
  599. unsigned_wide_register ubtmp1;
  600. utmp1 = vm->reg[c->reg1];
  601. utmp2 = vm->reg[c->reg2];
  602. ubtmp1 = (unsigned_wide_register)(utmp1 - utmp2);
  603. /* If subtraction goes below uint32_t MIN set borrow */
  604. if(utmp1 != (ubtmp1 + utmp2))
  605. {
  606. vm->reg[c->reg3] = vm->reg[c->reg3] | Borrow;
  607. }
  608. else
  609. {
  610. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Borrow);
  611. }
  612. /* Standard subtraction */
  613. vm->reg[c->reg0] = (utmp1 - utmp2);
  614. }
  615. void SUBU_BIO(struct lilith* vm, struct Instruction* c)
  616. {
  617. unsigned_vm_register utmp1, utmp2;
  618. unsigned_wide_register ubtmp1;
  619. bool B;
  620. B = Borrow_bit_set(vm->reg[c->reg3]);
  621. utmp1 = vm->reg[c->reg1];
  622. utmp2 = vm->reg[c->reg2];
  623. ubtmp1 = (unsigned_wide_register)(utmp1 - utmp2);
  624. /* If subtraction goes below uint32_t MIN set borrow */
  625. if(utmp1 != (ubtmp1 + utmp2))
  626. {
  627. vm->reg[c->reg3] = vm->reg[c->reg3] | Borrow;
  628. }
  629. else
  630. {
  631. vm->reg[c->reg3] = vm->reg[c->reg3] & ~(Borrow);
  632. }
  633. /* If borrow bit was set prior to operation subtract out the borrow */
  634. if(B)
  635. {
  636. vm->reg[c->reg0] = utmp1 - utmp2 - 1;
  637. }
  638. else
  639. {
  640. vm->reg[c->reg0] = utmp1 - utmp2;
  641. }
  642. }
  643. void MULTIPLY(struct lilith* vm, struct Instruction* c)
  644. {
  645. signed_vm_register tmp1, tmp2;
  646. signed_wide_register btmp1;
  647. tmp1 = (signed_vm_register)(vm->reg[c->reg2]);
  648. tmp2 = (signed_vm_register)( vm->reg[c->reg3]);
  649. btmp1 = ((signed_wide_register)tmp1) * ((signed_wide_register)tmp2);
  650. vm->reg[c->reg0] = (signed_vm_register)(btmp1 % 0x100000000);
  651. vm->reg[c->reg1] = (signed_vm_register)(btmp1 / 0x100000000);
  652. }
  653. void MULTIPLYU(struct lilith* vm, struct Instruction* c)
  654. {
  655. unsigned_wide_register ubtmp1;
  656. ubtmp1 = (unsigned_wide_register)(vm->reg[c->reg2]) * (unsigned_wide_register)(vm->reg[c->reg3]);
  657. vm->reg[c->reg0] = ubtmp1 % 0x100000000;
  658. vm->reg[c->reg1] = ubtmp1 / 0x100000000;
  659. }
  660. void DIVIDE(struct lilith* vm, struct Instruction* c)
  661. {
  662. signed_vm_register tmp1, tmp2;
  663. tmp1 = (signed_vm_register)(vm->reg[c->reg2]);
  664. tmp2 = (signed_vm_register)(vm->reg[c->reg3]);
  665. vm->reg[c->reg0] = tmp1 / tmp2;
  666. vm->reg[c->reg1] = tmp1 % tmp2;
  667. }
  668. void DIVIDEU(struct lilith* vm, struct Instruction* c)
  669. {
  670. unsigned_vm_register utmp1, utmp2;
  671. utmp1 = vm->reg[c->reg2];
  672. utmp2 = vm->reg[c->reg3];
  673. vm->reg[c->reg0] = utmp1 / utmp2;
  674. vm->reg[c->reg1] = utmp1 % utmp2;
  675. }
  676. void MUX(struct lilith* vm, struct Instruction* c)
  677. {
  678. vm->reg[c->reg0] = ((vm->reg[c->reg2] & ~(vm->reg[c->reg1])) |
  679. (vm->reg[c->reg3] & vm->reg[c->reg1]));
  680. }
  681. void NMUX(struct lilith* vm, struct Instruction* c)
  682. {
  683. vm->reg[c->reg0] = ((vm->reg[c->reg2] & vm->reg[c->reg1]) |
  684. (vm->reg[c->reg3] & ~(vm->reg[c->reg1])));
  685. }
  686. void SORT(struct lilith* vm, struct Instruction* c)
  687. {
  688. signed_vm_register tmp1, tmp2;
  689. tmp1 = (signed_vm_register)(vm->reg[c->reg2]);
  690. tmp2 = (signed_vm_register)(vm->reg[c->reg3]);
  691. if(tmp1 > tmp2)
  692. {
  693. vm->reg[c->reg0] = tmp1;
  694. vm->reg[c->reg1] = tmp2;
  695. }
  696. else
  697. {
  698. vm->reg[c->reg1] = tmp1;
  699. vm->reg[c->reg0] = tmp2;
  700. }
  701. }
  702. void SORTU(struct lilith* vm, struct Instruction* c)
  703. {
  704. unsigned_vm_register utmp1, utmp2;
  705. utmp1 = vm->reg[c->reg2];
  706. utmp2 = vm->reg[c->reg3];
  707. if(utmp1 > utmp2)
  708. {
  709. vm->reg[c->reg0] = utmp1;
  710. vm->reg[c->reg1] = utmp2;
  711. }
  712. else
  713. {
  714. vm->reg[c->reg1] = utmp1;
  715. vm->reg[c->reg0] = utmp2;
  716. }
  717. }
  718. void ADD(struct lilith* vm, struct Instruction* c)
  719. {
  720. signed_vm_register tmp1, tmp2;
  721. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  722. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  723. vm->reg[c->reg0] = (signed_vm_register)(tmp1 + tmp2);
  724. }
  725. void ADDU(struct lilith* vm, struct Instruction* c)
  726. {
  727. vm->reg[c->reg0] = vm->reg[c->reg1] + vm->reg[c->reg2];
  728. }
  729. void SUB(struct lilith* vm, struct Instruction* c)
  730. {
  731. signed_vm_register tmp1, tmp2;
  732. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  733. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  734. vm->reg[c->reg0] = (signed_vm_register)(tmp1 - tmp2);
  735. }
  736. void SUBU(struct lilith* vm, struct Instruction* c)
  737. {
  738. vm->reg[c->reg0] = vm->reg[c->reg1] - vm->reg[c->reg2];
  739. }
  740. void CMP(struct lilith* vm, struct Instruction* c)
  741. {
  742. signed_vm_register tmp1, tmp2;
  743. unsigned_vm_register result = 0;
  744. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  745. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  746. /* Set condition bits accordingly*/
  747. if(tmp1 > tmp2)
  748. {
  749. vm->reg[c->reg0] = result | GreaterThan;
  750. }
  751. else if(tmp1 == tmp2)
  752. {
  753. vm->reg[c->reg0] = result | EQual;
  754. }
  755. else
  756. {
  757. vm->reg[c->reg0] = result | LessThan;
  758. }
  759. }
  760. void CMPU(struct lilith* vm, struct Instruction* c)
  761. {
  762. unsigned_vm_register result = 0;
  763. if(vm->reg[c->reg1] > vm->reg[c->reg2])
  764. {
  765. vm->reg[c->reg0] = result | GreaterThan;
  766. }
  767. else if(vm->reg[c->reg1] == vm->reg[c->reg2])
  768. {
  769. vm->reg[c->reg0] = result | EQual;
  770. }
  771. else
  772. {
  773. vm->reg[c->reg0] = result | LessThan;
  774. }
  775. }
  776. void MUL(struct lilith* vm, struct Instruction* c)
  777. {
  778. signed_vm_register tmp1, tmp2;
  779. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  780. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  781. signed_wide_register sum = tmp1 * tmp2;
  782. /* We only want the bottom 32bits */
  783. vm->reg[c->reg0] = sum % 0x100000000;
  784. }
  785. void MULH(struct lilith* vm, struct Instruction* c)
  786. {
  787. signed_vm_register tmp1, tmp2;
  788. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  789. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  790. signed_wide_register sum = tmp1 * tmp2;
  791. /* We only want the top 32bits */
  792. vm->reg[c->reg0] = sum / 0x100000000;
  793. }
  794. void MULU(struct lilith* vm, struct Instruction* c)
  795. {
  796. unsigned_wide_register tmp1, tmp2, sum;
  797. tmp1 = vm->reg[c->reg1];
  798. tmp2 = vm->reg[c->reg2];
  799. sum = tmp1 * tmp2;
  800. /* We only want the bottom 32bits */
  801. vm->reg[c->reg0] = sum % 0x100000000;
  802. }
  803. void MULUH(struct lilith* vm, struct Instruction* c)
  804. {
  805. unsigned_wide_register tmp1, tmp2, sum;
  806. tmp1 = vm->reg[c->reg1];
  807. tmp2 = vm->reg[c->reg2];
  808. sum = tmp1 * tmp2;
  809. /* We only want the top 32bits */
  810. vm->reg[c->reg0] = sum / 0x100000000;
  811. }
  812. void DIV(struct lilith* vm, struct Instruction* c)
  813. {
  814. signed_vm_register tmp1, tmp2;
  815. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  816. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  817. vm->reg[c->reg0] = tmp1 / tmp2;
  818. }
  819. void MOD(struct lilith* vm, struct Instruction* c)
  820. {
  821. signed_vm_register tmp1, tmp2;
  822. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  823. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  824. vm->reg[c->reg0] = tmp1 % tmp2;
  825. }
  826. void DIVU(struct lilith* vm, struct Instruction* c)
  827. {
  828. vm->reg[c->reg0] = vm->reg[c->reg1] / vm->reg[c->reg2];
  829. }
  830. void MODU(struct lilith* vm, struct Instruction* c)
  831. {
  832. vm->reg[c->reg0] = vm->reg[c->reg1] % vm->reg[c->reg2];
  833. }
  834. void MAX(struct lilith* vm, struct Instruction* c)
  835. {
  836. signed_vm_register tmp1, tmp2;
  837. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  838. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  839. if(tmp1 > tmp2)
  840. {
  841. vm->reg[c->reg0] = tmp1;
  842. }
  843. else
  844. {
  845. vm->reg[c->reg0] = tmp2;
  846. }
  847. }
  848. void MAXU(struct lilith* vm, struct Instruction* c)
  849. {
  850. if(vm->reg[c->reg1] > vm->reg[c->reg2])
  851. {
  852. vm->reg[c->reg0] = vm->reg[c->reg1];
  853. }
  854. else
  855. {
  856. vm->reg[c->reg0] = vm->reg[c->reg2];
  857. }
  858. }
  859. void MIN(struct lilith* vm, struct Instruction* c)
  860. {
  861. signed_vm_register tmp1, tmp2;
  862. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  863. tmp2 = (signed_vm_register)(vm->reg[c->reg2]);
  864. if(tmp1 < tmp2)
  865. {
  866. vm->reg[c->reg0] = tmp1;
  867. }
  868. else
  869. {
  870. vm->reg[c->reg0] = tmp2;
  871. }
  872. }
  873. void MINU(struct lilith* vm, struct Instruction* c)
  874. {
  875. if(vm->reg[c->reg1] < vm->reg[c->reg2])
  876. {
  877. vm->reg[c->reg0] = vm->reg[c->reg1];
  878. }
  879. else
  880. {
  881. vm->reg[c->reg0] = vm->reg[c->reg2];
  882. }
  883. }
  884. void AND(struct lilith* vm, struct Instruction* c)
  885. {
  886. vm->reg[c->reg0] = vm->reg[c->reg1] & vm->reg[c->reg2];
  887. }
  888. void OR(struct lilith* vm, struct Instruction* c)
  889. {
  890. vm->reg[c->reg0] = vm->reg[c->reg1] | vm->reg[c->reg2];
  891. }
  892. void XOR(struct lilith* vm, struct Instruction* c)
  893. {
  894. vm->reg[c->reg0] = vm->reg[c->reg1] ^ vm->reg[c->reg2];
  895. }
  896. void NAND(struct lilith* vm, struct Instruction* c)
  897. {
  898. vm->reg[c->reg0] = ~(vm->reg[c->reg1] & vm->reg[c->reg2]);
  899. }
  900. void NOR(struct lilith* vm, struct Instruction* c)
  901. {
  902. vm->reg[c->reg0] = ~(vm->reg[c->reg1] | vm->reg[c->reg2]);
  903. }
  904. void XNOR(struct lilith* vm, struct Instruction* c)
  905. {
  906. vm->reg[c->reg0] = ~(vm->reg[c->reg1] ^ vm->reg[c->reg2]);
  907. }
  908. void MPQ(struct lilith* vm, struct Instruction* c)
  909. {
  910. vm->reg[c->reg0] = ~(vm->reg[c->reg1]) & vm->reg[c->reg2];
  911. }
  912. void LPQ(struct lilith* vm, struct Instruction* c)
  913. {
  914. vm->reg[c->reg0] = vm->reg[c->reg1] & ~(vm->reg[c->reg2]);
  915. }
  916. void CPQ(struct lilith* vm, struct Instruction* c)
  917. {
  918. vm->reg[c->reg0] = ~(vm->reg[c->reg1]) | vm->reg[c->reg2];
  919. }
  920. void BPQ(struct lilith* vm, struct Instruction* c)
  921. {
  922. vm->reg[c->reg0] = vm->reg[c->reg1] | ~(vm->reg[c->reg2]);
  923. }
  924. void SAL(struct lilith* vm, struct Instruction* c)
  925. {
  926. vm->reg[c->reg0] = vm->reg[c->reg1] << vm->reg[c->reg2];
  927. }
  928. void SAR(struct lilith* vm, struct Instruction* c)
  929. {
  930. vm->reg[c->reg0] = vm->reg[c->reg1] >> vm->reg[c->reg2];
  931. }
  932. void SL0(struct lilith* vm, struct Instruction* c)
  933. {
  934. vm->reg[c->reg0] = shift_register(vm->reg[c->reg1], vm->reg[c->reg2], true, true);
  935. }
  936. void SR0(struct lilith* vm, struct Instruction* c)
  937. {
  938. vm->reg[c->reg0] = shift_register(vm->reg[c->reg1], vm->reg[c->reg2], false, true);
  939. }
  940. void SL1(struct lilith* vm, struct Instruction* c)
  941. {
  942. vm->reg[c->reg0] = shift_register(vm->reg[c->reg1], vm->reg[c->reg2], true, false);
  943. }
  944. void SR1(struct lilith* vm, struct Instruction* c)
  945. {
  946. vm->reg[c->reg0] = shift_register(vm->reg[c->reg1], vm->reg[c->reg2], false, false);
  947. }
  948. void ROL(struct lilith* vm, struct Instruction* c)
  949. {
  950. unsigned_vm_register i, tmp;
  951. bool bit;
  952. tmp = vm->reg[c->reg1];
  953. for(i = vm->reg[c->reg2]; i > 0; i = i - 1)
  954. {
  955. bit = (tmp & 1);
  956. tmp = (tmp / 2) + (bit << imax);
  957. }
  958. vm->reg[c->reg0] = tmp;
  959. }
  960. void ROR(struct lilith* vm, struct Instruction* c)
  961. {
  962. unsigned_vm_register i, tmp;
  963. bool bit;
  964. tmp = vm->reg[c->reg1];
  965. for(i = vm->reg[c->reg2]; i > 0; i = i - 1)
  966. {
  967. bit = ((tmp >> imax) & 1);
  968. tmp = (tmp * 2) + bit;
  969. }
  970. vm->reg[c->reg0] = tmp;
  971. }
  972. void LOADX(struct lilith* vm, struct Instruction* c)
  973. {
  974. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], true, reg_size);
  975. }
  976. void LOADX8(struct lilith* vm, struct Instruction* c)
  977. {
  978. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], true, 1);
  979. }
  980. void LOADXU8(struct lilith* vm, struct Instruction* c)
  981. {
  982. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], false, 1);
  983. }
  984. void LOADX16(struct lilith* vm, struct Instruction* c)
  985. {
  986. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], true, 2);
  987. }
  988. void LOADXU16(struct lilith* vm, struct Instruction* c)
  989. {
  990. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], false, 2);
  991. }
  992. void LOADX32(struct lilith* vm, struct Instruction* c)
  993. {
  994. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], true, 4);
  995. }
  996. void LOADXU32(struct lilith* vm, struct Instruction* c)
  997. {
  998. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2], true, 4);
  999. }
  1000. void STOREX(struct lilith* vm, struct Instruction* c)
  1001. {
  1002. writeout_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2] , vm->reg[c->reg0], reg_size);
  1003. }
  1004. void STOREX8(struct lilith* vm, struct Instruction* c)
  1005. {
  1006. writeout_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2] , vm->reg[c->reg0], 1);
  1007. }
  1008. void STOREX16(struct lilith* vm, struct Instruction* c)
  1009. {
  1010. writeout_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2] , vm->reg[c->reg0], 2);
  1011. }
  1012. void STOREX32(struct lilith* vm, struct Instruction* c)
  1013. {
  1014. writeout_bytes(vm, vm->reg[c->reg1] + vm->reg[c->reg2] , vm->reg[c->reg0], 4);
  1015. }
  1016. void NEG(struct lilith* vm, struct Instruction* c)
  1017. {
  1018. vm->reg[c->reg0] = (signed_vm_register)(vm->reg[c->reg1]) * -1;
  1019. }
  1020. void ABS(struct lilith* vm, struct Instruction* c)
  1021. {
  1022. if(0 <= (signed_vm_register)(vm->reg[c->reg1]))
  1023. {
  1024. vm->reg[c->reg0] = vm->reg[c->reg1];
  1025. }
  1026. else
  1027. {
  1028. vm->reg[c->reg0] = (signed_vm_register)(vm->reg[c->reg1]) * -1;
  1029. }
  1030. }
  1031. void NABS(struct lilith* vm, struct Instruction* c)
  1032. {
  1033. if(0 > (signed_vm_register)(vm->reg[c->reg1]))
  1034. {
  1035. vm->reg[c->reg0] = vm->reg[c->reg1];
  1036. }
  1037. else
  1038. {
  1039. vm->reg[c->reg0] = (signed_vm_register)(vm->reg[c->reg1]) * -1;
  1040. }
  1041. }
  1042. void SWAP(struct lilith* vm, struct Instruction* c)
  1043. {
  1044. unsigned_vm_register utmp1;
  1045. utmp1 = vm->reg[c->reg1];
  1046. vm->reg[c->reg1] = vm->reg[c->reg0];
  1047. vm->reg[c->reg0] = utmp1;
  1048. }
  1049. void COPY(struct lilith* vm, struct Instruction* c)
  1050. {
  1051. vm->reg[c->reg0] = vm->reg[c->reg1];
  1052. }
  1053. void MOVE(struct lilith* vm, struct Instruction* c)
  1054. {
  1055. vm->reg[c->reg0] = vm->reg[c->reg1];
  1056. vm->reg[c->reg1] = 0;
  1057. }
  1058. void BRANCH(struct lilith* vm, struct Instruction* c)
  1059. {
  1060. /* Write out the PC */
  1061. writeout_bytes(vm, vm->reg[c->reg1], vm->ip, reg_size);
  1062. /* Update PC */
  1063. vm->ip = vm->reg[c->reg0];
  1064. }
  1065. void CALL(struct lilith* vm, struct Instruction* c)
  1066. {
  1067. /* Write out the PC */
  1068. writeout_bytes(vm, vm->reg[c->reg1], vm->ip, reg_size);
  1069. /* Update our index */
  1070. vm->reg[c->reg1] = vm->reg[c->reg1] + reg_size;
  1071. /* Update PC */
  1072. vm->ip = vm->reg[c->reg0];
  1073. }
  1074. void READPC(struct lilith* vm, struct Instruction* c)
  1075. {
  1076. vm->reg[c->reg0] = vm->ip;
  1077. }
  1078. void READSCID(struct lilith* vm, struct Instruction* c)
  1079. {
  1080. #ifdef VM256
  1081. /* We only support Base 8, 16, 32, 64, 128 and 256 */
  1082. vm->reg[c->reg0] = 0x00000005;
  1083. #elif VM128
  1084. /* We only support Base 8, 16, 32, 64 and 128 */
  1085. vm->reg[c->reg0] = 0x00000004;
  1086. #elif VM64
  1087. /* We only support Base 8, 16, 32 and 64 */
  1088. vm->reg[c->reg0] = 0x00000003;
  1089. #elif VM32
  1090. /* We only support Base 8, 16 and 32 */
  1091. vm->reg[c->reg0] = 0x00000002;
  1092. #else
  1093. /* We only support Base 8 and 16 */
  1094. vm->reg[c->reg0] = 0x00000001;
  1095. #endif
  1096. }
  1097. void FALSE(struct lilith* vm, struct Instruction* c)
  1098. {
  1099. vm->reg[c->reg0] = 0;
  1100. }
  1101. void TRUE(struct lilith* vm, struct Instruction* c)
  1102. {
  1103. #ifdef VM256
  1104. vm->reg[c->reg0] = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
  1105. #elif VM128
  1106. vm->reg[c->reg0] = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
  1107. #elif VM64
  1108. vm->reg[c->reg0] = 0xFFFFFFFFFFFFFFFF;
  1109. #elif VM32
  1110. vm->reg[c->reg0] = 0xFFFFFFFF;
  1111. #else
  1112. vm->reg[c->reg0] = 0xFFFF;
  1113. #endif
  1114. }
  1115. void JSR_COROUTINE(struct lilith* vm, struct Instruction* c)
  1116. {
  1117. vm->ip = vm->reg[c->reg0];
  1118. }
  1119. void RET(struct lilith* vm, struct Instruction* c)
  1120. {
  1121. /* Update our index */
  1122. vm->reg[c->reg0] = vm->reg[c->reg0] - reg_size;
  1123. /* Read in the new PC */
  1124. vm->ip = readin_bytes(vm, vm->reg[c->reg0], false, reg_size);
  1125. /* Clear Stack Values */
  1126. writeout_bytes(vm, vm->reg[c->reg0], 0, reg_size);
  1127. }
  1128. void PUSHPC(struct lilith* vm, struct Instruction* c)
  1129. {
  1130. /* Write out the PC */
  1131. writeout_bytes(vm, vm->reg[c->reg0], vm->ip, reg_size);
  1132. /* Update our index */
  1133. vm->reg[c->reg0] = vm->reg[c->reg0] + reg_size;
  1134. }
  1135. void POPPC(struct lilith* vm, struct Instruction* c)
  1136. {
  1137. /* Update our index */
  1138. vm->reg[c->reg0] = vm->reg[c->reg0] - reg_size;
  1139. /* Read in the new PC */
  1140. vm->ip = readin_bytes(vm, vm->reg[c->reg0], false, reg_size);
  1141. /* Clear memory where PC was */
  1142. writeout_bytes(vm, vm->reg[c->reg0], 0, reg_size);
  1143. }
  1144. void ADDI(struct lilith* vm, struct Instruction* c)
  1145. {
  1146. signed_vm_register tmp1;
  1147. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  1148. vm->reg[c->reg0] = (signed_vm_register)(tmp1 + c->raw_Immediate);
  1149. }
  1150. void ADDUI(struct lilith* vm, struct Instruction* c)
  1151. {
  1152. vm->reg[c->reg0] = vm->reg[c->reg1] + c->raw_Immediate;
  1153. }
  1154. void SUBI(struct lilith* vm, struct Instruction* c)
  1155. {
  1156. signed_vm_register tmp1;
  1157. tmp1 = (signed_vm_register)(vm->reg[c->reg1]);
  1158. vm->reg[c->reg0] = (signed_vm_register)(tmp1 - c->raw_Immediate);
  1159. }
  1160. void SUBUI(struct lilith* vm, struct Instruction* c)
  1161. {
  1162. vm->reg[c->reg0] = vm->reg[c->reg1] - c->raw_Immediate;
  1163. }
  1164. void CMPI(struct lilith* vm, struct Instruction* c)
  1165. {
  1166. unsigned_vm_register result = 0;
  1167. if((signed_vm_register)(vm->reg[c->reg1]) > c->raw_Immediate)
  1168. {
  1169. vm->reg[c->reg0] = result | GreaterThan;
  1170. }
  1171. else if((signed_vm_register)(vm->reg[c->reg1]) == c->raw_Immediate)
  1172. {
  1173. vm->reg[c->reg0] = result | EQual;
  1174. }
  1175. else
  1176. {
  1177. vm->reg[c->reg0] = result | LessThan;
  1178. }
  1179. }
  1180. void LOAD(struct lilith* vm, struct Instruction* c)
  1181. {
  1182. vm->reg[c->reg0] = readin_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), false,reg_size);
  1183. }
  1184. void LOAD8(struct lilith* vm, struct Instruction* c)
  1185. {
  1186. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + c->raw_Immediate, true, 1);
  1187. }
  1188. void LOADU8(struct lilith* vm, struct Instruction* c)
  1189. {
  1190. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + c->raw_Immediate, false, 1);
  1191. }
  1192. void LOAD16(struct lilith* vm, struct Instruction* c)
  1193. {
  1194. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + c->raw_Immediate, true, 2);
  1195. }
  1196. void LOADU16(struct lilith* vm, struct Instruction* c)
  1197. {
  1198. vm->reg[c->reg0] = readin_bytes(vm, vm->reg[c->reg1] + c->raw_Immediate, false, 2);
  1199. }
  1200. void LOAD32(struct lilith* vm, struct Instruction* c)
  1201. {
  1202. vm->reg[c->reg0] = readin_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), true, 4);
  1203. }
  1204. void LOADU32(struct lilith* vm, struct Instruction* c)
  1205. {
  1206. vm->reg[c->reg0] = readin_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), false, 4);
  1207. }
  1208. void CMPUI(struct lilith* vm, struct Instruction* c)
  1209. {
  1210. unsigned_vm_register result = 0;
  1211. if(vm->reg[c->reg1] > (unsigned_vm_register)c->raw_Immediate)
  1212. {
  1213. vm->reg[c->reg0] = result | GreaterThan;
  1214. }
  1215. else if(vm->reg[c->reg1] == (unsigned_vm_register)c->raw_Immediate)
  1216. {
  1217. vm->reg[c->reg0] = result | EQual;
  1218. }
  1219. else
  1220. {
  1221. vm->reg[c->reg0] = result | LessThan;
  1222. }
  1223. }
  1224. void STORE(struct lilith* vm, struct Instruction* c)
  1225. {
  1226. writeout_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), vm->reg[c->reg0], reg_size);
  1227. }
  1228. void STORE8(struct lilith* vm, struct Instruction* c)
  1229. {
  1230. writeout_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), vm->reg[c->reg0], 1);
  1231. }
  1232. void STORE16(struct lilith* vm, struct Instruction* c)
  1233. {
  1234. writeout_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), vm->reg[c->reg0], 2);
  1235. }
  1236. void STORE32(struct lilith* vm, struct Instruction* c)
  1237. {
  1238. writeout_bytes(vm, (vm->reg[c->reg1] + c->raw_Immediate), vm->reg[c->reg0], 4);
  1239. }
  1240. void JUMP_C(struct lilith* vm, struct Instruction* c)
  1241. {
  1242. if(Carry_bit_set(vm->reg[c->reg0]))
  1243. {
  1244. vm->ip = vm->ip + c->raw_Immediate;
  1245. }
  1246. }
  1247. void JUMP_B(struct lilith* vm, struct Instruction* c)
  1248. {
  1249. if(Borrow_bit_set(vm->reg[c->reg0]))
  1250. {
  1251. vm->ip = vm->ip + c->raw_Immediate;
  1252. }
  1253. }
  1254. void JUMP_O(struct lilith* vm, struct Instruction* c)
  1255. {
  1256. if(Overflow_bit_set(vm->reg[c->reg0]))
  1257. {
  1258. vm->ip = vm->ip + c->raw_Immediate;
  1259. }
  1260. }
  1261. void JUMP_G(struct lilith* vm, struct Instruction* c)
  1262. {
  1263. if(GreaterThan_bit_set(vm->reg[c->reg0]))
  1264. {
  1265. vm->ip = vm->ip + c->raw_Immediate;
  1266. }
  1267. }
  1268. void JUMP_GE(struct lilith* vm, struct Instruction* c)
  1269. {
  1270. if(GreaterThan_bit_set(vm->reg[c->reg0]) || EQual_bit_set(vm->reg[c->reg0]))
  1271. {
  1272. vm->ip = vm->ip + c->raw_Immediate;
  1273. }
  1274. }
  1275. void JUMP_E(struct lilith* vm, struct Instruction* c)
  1276. {
  1277. if(EQual_bit_set(vm->reg[c->reg0]))
  1278. {
  1279. vm->ip = vm->ip + c->raw_Immediate;
  1280. }
  1281. }
  1282. void JUMP_NE(struct lilith* vm, struct Instruction* c)
  1283. {
  1284. if(!EQual_bit_set(vm->reg[c->reg0]))
  1285. {
  1286. vm->ip = vm->ip + c->raw_Immediate;
  1287. }
  1288. }
  1289. void JUMP_LE(struct lilith* vm, struct Instruction* c)
  1290. {
  1291. if(LessThan_bit_set(vm->reg[c->reg0]) || EQual_bit_set(vm->reg[c->reg0]))
  1292. {
  1293. vm->ip = vm->ip + c->raw_Immediate;
  1294. }
  1295. }
  1296. void JUMP_L(struct lilith* vm, struct Instruction* c)
  1297. {
  1298. if(LessThan_bit_set(vm->reg[c->reg0]))
  1299. {
  1300. vm->ip = vm->ip + c->raw_Immediate;
  1301. }
  1302. }
  1303. void JUMP_Z(struct lilith* vm, struct Instruction* c)
  1304. {
  1305. if(0 == vm->reg[c->reg0])
  1306. {
  1307. vm->ip = vm->ip + c->raw_Immediate;
  1308. }
  1309. }
  1310. void JUMP_NZ(struct lilith* vm, struct Instruction* c)
  1311. {
  1312. if(0 != vm->reg[c->reg0])
  1313. {
  1314. vm->ip = vm->ip + c->raw_Immediate;
  1315. }
  1316. }
  1317. void CALLI(struct lilith* vm, struct Instruction* c)
  1318. {
  1319. /* Write out the PC */
  1320. writeout_bytes(vm, vm->reg[c->reg0], vm->ip, reg_size);
  1321. /* Update our index */
  1322. vm->reg[c->reg0] = vm->reg[c->reg0] + reg_size;
  1323. /* Update PC */
  1324. vm->ip = vm->ip + c->raw_Immediate;
  1325. }
  1326. void LOADI(struct lilith* vm, struct Instruction* c)
  1327. {
  1328. vm->reg[c->reg0] = (int16_t)c->raw_Immediate;
  1329. }
  1330. void LOADUI(struct lilith* vm, struct Instruction* c)
  1331. {
  1332. vm->reg[c->reg0] = c->raw_Immediate;
  1333. }
  1334. void SALI(struct lilith* vm, struct Instruction* c)
  1335. {
  1336. vm->reg[c->reg0] = vm->reg[c->reg0] << c->raw_Immediate;
  1337. }
  1338. void SARI(struct lilith* vm, struct Instruction* c)
  1339. {
  1340. vm->reg[c->reg0] = vm->reg[c->reg0] >> c->raw_Immediate;
  1341. }
  1342. void SL0I(struct lilith* vm, struct Instruction* c)
  1343. {
  1344. vm->reg[c->reg0] = shift_register(vm->reg[c->reg0], c->raw_Immediate, true, true);
  1345. }
  1346. void SR0I(struct lilith* vm, struct Instruction* c)
  1347. {
  1348. vm->reg[c->reg0] = shift_register(vm->reg[c->reg0], c->raw_Immediate, false, true);
  1349. }
  1350. void SL1I(struct lilith* vm, struct Instruction* c)
  1351. {
  1352. vm->reg[c->reg0] = shift_register(vm->reg[c->reg0], c->raw_Immediate, true, false);
  1353. }
  1354. void SR1I(struct lilith* vm, struct Instruction* c)
  1355. {
  1356. vm->reg[c->reg0] = shift_register(vm->reg[c->reg0], c->raw_Immediate, false, false);
  1357. }
  1358. void LOADR(struct lilith* vm, struct Instruction* c)
  1359. {
  1360. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), false, reg_size);
  1361. }
  1362. void LOADR8(struct lilith* vm, struct Instruction* c)
  1363. {
  1364. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), true, 1);
  1365. }
  1366. void LOADRU8(struct lilith* vm, struct Instruction* c)
  1367. {
  1368. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), false, 1);
  1369. }
  1370. void LOADR16(struct lilith* vm, struct Instruction* c)
  1371. {
  1372. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), true, 2);
  1373. }
  1374. void LOADRU16(struct lilith* vm, struct Instruction* c)
  1375. {
  1376. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), false, 2);
  1377. }
  1378. void LOADR32(struct lilith* vm, struct Instruction* c)
  1379. {
  1380. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), true, 4);
  1381. }
  1382. void LOADRU32(struct lilith* vm, struct Instruction* c)
  1383. {
  1384. vm->reg[c->reg0] = readin_bytes(vm, (vm->ip + c->raw_Immediate), false, 4);
  1385. }
  1386. void STORER(struct lilith* vm, struct Instruction* c)
  1387. {
  1388. writeout_bytes(vm, (vm->ip + c->raw_Immediate), vm->reg[c->reg0], reg_size);
  1389. }
  1390. void STORER8(struct lilith* vm, struct Instruction* c)
  1391. {
  1392. writeout_bytes(vm, (vm->ip + c->raw_Immediate), vm->reg[c->reg0], 1);
  1393. }
  1394. void STORER16(struct lilith* vm, struct Instruction* c)
  1395. {
  1396. writeout_bytes(vm, (vm->ip + c->raw_Immediate), vm->reg[c->reg0], 2);
  1397. }
  1398. void STORER32(struct lilith* vm, struct Instruction* c)
  1399. {
  1400. writeout_bytes(vm, (vm->ip + c->raw_Immediate), vm->reg[c->reg0], 4);
  1401. }
  1402. void JUMP(struct lilith* vm, struct Instruction* c)
  1403. {
  1404. vm->ip = vm->ip + c->raw_Immediate;
  1405. }
  1406. void JUMP_P(struct lilith* vm, struct Instruction* c)
  1407. {
  1408. signed_vm_register tmp1;
  1409. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1410. if(0 <= tmp1)
  1411. {
  1412. vm->ip = vm->ip + c->raw_Immediate;
  1413. }
  1414. }
  1415. void JUMP_NP(struct lilith* vm, struct Instruction* c)
  1416. {
  1417. signed_vm_register tmp1;
  1418. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1419. if(0 > tmp1)
  1420. {
  1421. vm->ip = vm->ip + c->raw_Immediate;
  1422. }
  1423. }
  1424. void CMPJUMPI_G(struct lilith* vm, struct Instruction* c)
  1425. {
  1426. signed_vm_register tmp1, tmp2;
  1427. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1428. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1429. if(tmp1 > tmp2)
  1430. {
  1431. vm->ip = vm->ip + c->raw_Immediate;
  1432. }
  1433. }
  1434. void CMPJUMPI_GE(struct lilith* vm, struct Instruction* c)
  1435. {
  1436. signed_vm_register tmp1, tmp2;
  1437. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1438. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1439. if(tmp1 >= tmp2)
  1440. {
  1441. vm->ip = vm->ip + c->raw_Immediate;
  1442. }
  1443. }
  1444. void CMPJUMPI_E(struct lilith* vm, struct Instruction* c)
  1445. {
  1446. if((vm->reg[c->reg0]) == (vm->reg[c->reg1]))
  1447. {
  1448. vm->ip = vm->ip + c->raw_Immediate;
  1449. }
  1450. }
  1451. void CMPJUMPI_NE(struct lilith* vm, struct Instruction* c)
  1452. {
  1453. if((vm->reg[c->reg0]) != (vm->reg[c->reg1]))
  1454. {
  1455. vm->ip = vm->ip + c->raw_Immediate;
  1456. }
  1457. }
  1458. void CMPJUMPI_LE(struct lilith* vm, struct Instruction* c)
  1459. {
  1460. signed_vm_register tmp1, tmp2;
  1461. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1462. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1463. if(tmp1 <= tmp2)
  1464. {
  1465. vm->ip = vm->ip + c->raw_Immediate;
  1466. }
  1467. }
  1468. void CMPJUMPI_L(struct lilith* vm, struct Instruction* c)
  1469. {
  1470. signed_vm_register tmp1, tmp2;
  1471. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1472. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1473. if(tmp1 < tmp2)
  1474. {
  1475. vm->ip = vm->ip + c->raw_Immediate;
  1476. }
  1477. }
  1478. void CMPJUMPUI_G(struct lilith* vm, struct Instruction* c)
  1479. {
  1480. if((vm->reg[c->reg0]) > (vm->reg[c->reg1]))
  1481. {
  1482. vm->ip = vm->ip + c->raw_Immediate;
  1483. }
  1484. }
  1485. void CMPJUMPUI_GE(struct lilith* vm, struct Instruction* c)
  1486. {
  1487. if((vm->reg[c->reg0]) >= (vm->reg[c->reg1]))
  1488. {
  1489. vm->ip = vm->ip + c->raw_Immediate;
  1490. }
  1491. }
  1492. void CMPJUMPUI_LE(struct lilith* vm, struct Instruction* c)
  1493. {
  1494. if((vm->reg[c->reg0]) <= (vm->reg[c->reg1]))
  1495. {
  1496. vm->ip = vm->ip + c->raw_Immediate;
  1497. }
  1498. }
  1499. void CMPJUMPUI_L(struct lilith* vm, struct Instruction* c)
  1500. {
  1501. if((vm->reg[c->reg0]) < (vm->reg[c->reg1]))
  1502. {
  1503. vm->ip = vm->ip + c->raw_Immediate;
  1504. }
  1505. }
  1506. void CMPSKIPI_G(struct lilith* vm, struct Instruction* c)
  1507. {
  1508. signed_vm_register tmp1, tmp2;
  1509. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1510. tmp2 = (signed_vm_register)(c->raw_Immediate);
  1511. if(tmp1 > tmp2)
  1512. {
  1513. vm->ip = vm->ip + next_instruction_size(vm);
  1514. }
  1515. }
  1516. void CMPSKIPI_GE(struct lilith* vm, struct Instruction* c)
  1517. {
  1518. signed_vm_register tmp1, tmp2;
  1519. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1520. tmp2 = (signed_vm_register)(c->raw_Immediate);
  1521. if(tmp1 >= tmp2)
  1522. {
  1523. vm->ip = vm->ip + next_instruction_size(vm);
  1524. }
  1525. }
  1526. void CMPSKIPI_E(struct lilith* vm, struct Instruction* c)
  1527. {
  1528. uint16_t utmp1;
  1529. utmp1 = (uint16_t)(c->raw_Immediate);
  1530. if((vm->reg[c->reg0]) == utmp1)
  1531. {
  1532. vm->ip = vm->ip + next_instruction_size(vm);
  1533. }
  1534. }
  1535. void CMPSKIPI_NE(struct lilith* vm, struct Instruction* c)
  1536. {
  1537. uint16_t utmp1;
  1538. utmp1 = (uint16_t)(c->raw_Immediate);
  1539. if((vm->reg[c->reg0]) != utmp1)
  1540. {
  1541. vm->ip = vm->ip + next_instruction_size(vm);
  1542. }
  1543. }
  1544. void CMPSKIPI_LE(struct lilith* vm, struct Instruction* c)
  1545. {
  1546. signed_vm_register tmp1, tmp2;
  1547. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1548. tmp2 = (signed_vm_register)(c->raw_Immediate);
  1549. if(tmp1 <= tmp2)
  1550. {
  1551. vm->ip = vm->ip + next_instruction_size(vm);
  1552. }
  1553. }
  1554. void CMPSKIPI_L(struct lilith* vm, struct Instruction* c)
  1555. {
  1556. signed_vm_register tmp1, tmp2;
  1557. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1558. tmp2 = (signed_vm_register)(c->raw_Immediate);
  1559. if(tmp1 < tmp2)
  1560. {
  1561. vm->ip = vm->ip + next_instruction_size(vm);
  1562. }
  1563. }
  1564. void CMPSKIPUI_G(struct lilith* vm, struct Instruction* c)
  1565. {
  1566. uint16_t utmp1;
  1567. utmp1 = (uint16_t)(c->raw_Immediate);
  1568. if((vm->reg[c->reg0]) > utmp1)
  1569. {
  1570. vm->ip = vm->ip + next_instruction_size(vm);
  1571. }
  1572. }
  1573. void CMPSKIPUI_GE(struct lilith* vm, struct Instruction* c)
  1574. {
  1575. uint16_t utmp1;
  1576. utmp1 = (uint16_t)(c->raw_Immediate);
  1577. if((vm->reg[c->reg0]) >= utmp1)
  1578. {
  1579. vm->ip = vm->ip + next_instruction_size(vm);
  1580. }
  1581. }
  1582. void CMPSKIPUI_LE(struct lilith* vm, struct Instruction* c)
  1583. {
  1584. uint16_t utmp1;
  1585. utmp1 = (uint16_t)(c->raw_Immediate);
  1586. if((vm->reg[c->reg0]) <= utmp1)
  1587. {
  1588. vm->ip = vm->ip + next_instruction_size(vm);
  1589. }
  1590. }
  1591. void CMPSKIPUI_L(struct lilith* vm, struct Instruction* c)
  1592. {
  1593. uint16_t utmp1;
  1594. utmp1 = (uint16_t)(c->raw_Immediate);
  1595. if((vm->reg[c->reg0]) < utmp1)
  1596. {
  1597. vm->ip = vm->ip + next_instruction_size(vm);
  1598. }
  1599. }
  1600. void PUSHR(struct lilith* vm, struct Instruction* c)
  1601. {
  1602. writeout_bytes(vm, vm->reg[c->reg1], vm->reg[c->reg0], reg_size);
  1603. vm->reg[c->reg1] = vm->reg[c->reg1] + reg_size;
  1604. }
  1605. void PUSH8(struct lilith* vm, struct Instruction* c)
  1606. {
  1607. writeout_bytes(vm, vm->reg[c->reg1] , vm->reg[c->reg0], 1);
  1608. vm->reg[c->reg1] = vm->reg[c->reg1] + 1;
  1609. }
  1610. void PUSH16(struct lilith* vm, struct Instruction* c)
  1611. {
  1612. writeout_bytes(vm, vm->reg[c->reg1] , vm->reg[c->reg0], 2);
  1613. vm->reg[c->reg1] = vm->reg[c->reg1] + 2;
  1614. }
  1615. void PUSH32(struct lilith* vm, struct Instruction* c)
  1616. {
  1617. writeout_bytes(vm, vm->reg[c->reg1] , vm->reg[c->reg0], 4);
  1618. vm->reg[c->reg1] = vm->reg[c->reg1] + 4;
  1619. }
  1620. void POPR(struct lilith* vm, struct Instruction* c)
  1621. {
  1622. unsigned_vm_register tmp;
  1623. vm->reg[c->reg1] = vm->reg[c->reg1] - reg_size;
  1624. tmp = readin_bytes(vm, vm->reg[c->reg1], false, reg_size);
  1625. writeout_bytes(vm, vm->reg[c->reg1], 0, reg_size);
  1626. vm->reg[c->reg0] = tmp;
  1627. }
  1628. void POP8(struct lilith* vm, struct Instruction* c)
  1629. {
  1630. int8_t tmp;
  1631. vm->reg[c->reg1] = vm->reg[c->reg1] - 1;
  1632. tmp = readin_bytes(vm, vm->reg[c->reg1], true, 1);
  1633. writeout_bytes(vm, vm->reg[c->reg1], 0, 1);
  1634. vm->reg[c->reg0] = tmp;
  1635. }
  1636. void POPU8(struct lilith* vm, struct Instruction* c)
  1637. {
  1638. uint8_t tmp;
  1639. vm->reg[c->reg1] = vm->reg[c->reg1] - 1;
  1640. tmp = readin_bytes(vm, vm->reg[c->reg1], false, 1);
  1641. writeout_bytes(vm, vm->reg[c->reg1], 0, 1);
  1642. vm->reg[c->reg0] = tmp;
  1643. }
  1644. void POP16(struct lilith* vm, struct Instruction* c)
  1645. {
  1646. int16_t tmp;
  1647. vm->reg[c->reg1] = vm->reg[c->reg1] - 2;
  1648. tmp = readin_bytes(vm, vm->reg[c->reg1], true, 2);
  1649. writeout_bytes(vm, vm->reg[c->reg1], 0, 2);
  1650. vm->reg[c->reg0] = tmp;
  1651. }
  1652. void POPU16(struct lilith* vm, struct Instruction* c)
  1653. {
  1654. uint16_t tmp;
  1655. vm->reg[c->reg1] = vm->reg[c->reg1] - 2;
  1656. tmp = readin_bytes(vm, vm->reg[c->reg1], false, 2);
  1657. writeout_bytes(vm, vm->reg[c->reg1], 0, 2);
  1658. vm->reg[c->reg0] = tmp;
  1659. }
  1660. void POP32(struct lilith* vm, struct Instruction* c)
  1661. {
  1662. signed_vm_register tmp;
  1663. vm->reg[c->reg1] = vm->reg[c->reg1] - 4;
  1664. tmp = readin_bytes(vm, vm->reg[c->reg1], true, 4);
  1665. writeout_bytes(vm, vm->reg[c->reg1], 0, 4);
  1666. vm->reg[c->reg0] = tmp;
  1667. }
  1668. void POPU32(struct lilith* vm, struct Instruction* c)
  1669. {
  1670. unsigned_vm_register tmp;
  1671. vm->reg[c->reg1] = vm->reg[c->reg1] - 4;
  1672. tmp = readin_bytes(vm, vm->reg[c->reg1], false, 4);
  1673. writeout_bytes(vm, vm->reg[c->reg1], 0, 4);
  1674. vm->reg[c->reg0] = tmp;
  1675. }
  1676. void ANDI(struct lilith* vm, struct Instruction* c)
  1677. {
  1678. vm->reg[c->reg0] = vm->reg[c->reg1] & c->raw_Immediate;
  1679. }
  1680. void ORI(struct lilith* vm, struct Instruction* c)
  1681. {
  1682. vm->reg[c->reg0] = vm->reg[c->reg1] | c->raw_Immediate;
  1683. }
  1684. void XORI(struct lilith* vm, struct Instruction* c)
  1685. {
  1686. vm->reg[c->reg0] = vm->reg[c->reg1] ^ c->raw_Immediate;
  1687. }
  1688. void NANDI(struct lilith* vm, struct Instruction* c)
  1689. {
  1690. vm->reg[c->reg0] = ~(vm->reg[c->reg1] & c->raw_Immediate);
  1691. }
  1692. void NORI(struct lilith* vm, struct Instruction* c)
  1693. {
  1694. vm->reg[c->reg0] = ~(vm->reg[c->reg1] | c->raw_Immediate);
  1695. }
  1696. void XNORI(struct lilith* vm, struct Instruction* c)
  1697. {
  1698. vm->reg[c->reg0] = ~(vm->reg[c->reg1] ^ c->raw_Immediate);
  1699. }
  1700. void NOT(struct lilith* vm, struct Instruction* c)
  1701. {
  1702. vm->reg[c->reg0] = ~(vm->reg[c->reg1]);
  1703. }
  1704. void CMPSKIP_G(struct lilith* vm, struct Instruction* c)
  1705. {
  1706. signed_vm_register tmp1, tmp2;
  1707. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1708. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1709. if(tmp1 > tmp2)
  1710. {
  1711. vm->ip = vm->ip + next_instruction_size(vm);
  1712. }
  1713. }
  1714. void CMPSKIP_GE(struct lilith* vm, struct Instruction* c)
  1715. {
  1716. signed_vm_register tmp1, tmp2;
  1717. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1718. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1719. if(tmp1 >= tmp2)
  1720. {
  1721. vm->ip = vm->ip + next_instruction_size(vm);
  1722. }
  1723. }
  1724. void CMPSKIP_E(struct lilith* vm, struct Instruction* c)
  1725. {
  1726. if((vm->reg[c->reg0]) == (vm->reg[c->reg1]))
  1727. {
  1728. vm->ip = vm->ip + next_instruction_size(vm);
  1729. }
  1730. }
  1731. void CMPSKIP_NE(struct lilith* vm, struct Instruction* c)
  1732. {
  1733. if((vm->reg[c->reg0]) != (vm->reg[c->reg1]))
  1734. {
  1735. vm->ip = vm->ip + next_instruction_size(vm);
  1736. }
  1737. }
  1738. void CMPSKIP_LE(struct lilith* vm, struct Instruction* c)
  1739. {
  1740. signed_vm_register tmp1, tmp2;
  1741. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1742. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1743. if(tmp1 <= tmp2)
  1744. {
  1745. vm->ip = vm->ip + next_instruction_size(vm);
  1746. }
  1747. }
  1748. void CMPSKIP_L(struct lilith* vm, struct Instruction* c)
  1749. {
  1750. signed_vm_register tmp1, tmp2;
  1751. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1752. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1753. if(tmp1 < tmp2)
  1754. {
  1755. vm->ip = vm->ip + next_instruction_size(vm);
  1756. }
  1757. }
  1758. void CMPSKIPU_G(struct lilith* vm, struct Instruction* c)
  1759. {
  1760. if((vm->reg[c->reg0]) > (vm->reg[c->reg1]))
  1761. {
  1762. vm->ip = vm->ip + next_instruction_size(vm);
  1763. }
  1764. }
  1765. void CMPSKIPU_GE(struct lilith* vm, struct Instruction* c)
  1766. {
  1767. if((vm->reg[c->reg0]) >= (vm->reg[c->reg1]))
  1768. {
  1769. vm->ip = vm->ip + next_instruction_size(vm);
  1770. }
  1771. }
  1772. void CMPSKIPU_LE(struct lilith* vm, struct Instruction* c)
  1773. {
  1774. if((vm->reg[c->reg0]) <= (vm->reg[c->reg1]))
  1775. {
  1776. vm->ip = vm->ip + next_instruction_size(vm);
  1777. }
  1778. }
  1779. void CMPSKIPU_L(struct lilith* vm, struct Instruction* c)
  1780. {
  1781. if((vm->reg[c->reg0]) < (vm->reg[c->reg1]))
  1782. {
  1783. vm->ip = vm->ip + next_instruction_size(vm);
  1784. }
  1785. }
  1786. void CMPJUMP_G(struct lilith* vm, struct Instruction* c)
  1787. {
  1788. signed_vm_register tmp1, tmp2;
  1789. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1790. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1791. if(tmp1 > tmp2)
  1792. {
  1793. vm->ip = vm->reg[c->reg2];
  1794. }
  1795. }
  1796. void CMPJUMP_GE(struct lilith* vm, struct Instruction* c)
  1797. {
  1798. signed_vm_register tmp1, tmp2;
  1799. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1800. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1801. if(tmp1 >= tmp2)
  1802. {
  1803. vm->ip = vm->reg[c->reg2];
  1804. }
  1805. }
  1806. void CMPJUMP_E(struct lilith* vm, struct Instruction* c)
  1807. {
  1808. if((vm->reg[c->reg0]) == (vm->reg[c->reg1]))
  1809. {
  1810. vm->ip = vm->reg[c->reg2];
  1811. }
  1812. }
  1813. void CMPJUMP_NE(struct lilith* vm, struct Instruction* c)
  1814. {
  1815. if((vm->reg[c->reg0]) != (vm->reg[c->reg1]))
  1816. {
  1817. vm->ip = vm->reg[c->reg2];
  1818. }
  1819. }
  1820. void CMPJUMP_LE(struct lilith* vm, struct Instruction* c)
  1821. {
  1822. signed_vm_register tmp1, tmp2;
  1823. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1824. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1825. if(tmp1 <= tmp2)
  1826. {
  1827. vm->ip = vm->reg[c->reg2];
  1828. }
  1829. }
  1830. void CMPJUMP_L(struct lilith* vm, struct Instruction* c)
  1831. {
  1832. signed_vm_register tmp1, tmp2;
  1833. tmp1 = (signed_vm_register)(vm->reg[c->reg0]);
  1834. tmp2 = (signed_vm_register)(vm->reg[c->reg1]);
  1835. if(tmp1 < tmp2)
  1836. {
  1837. vm->ip = vm->reg[c->reg2];
  1838. }
  1839. }
  1840. void CMPJUMPU_G(struct lilith* vm, struct Instruction* c)
  1841. {
  1842. if((vm->reg[c->reg0]) > (vm->reg[c->reg1]))
  1843. {
  1844. vm->ip = vm->reg[c->reg2];
  1845. }
  1846. }
  1847. void CMPJUMPU_GE(struct lilith* vm, struct Instruction* c)
  1848. {
  1849. if((vm->reg[c->reg0]) >= (vm->reg[c->reg1]))
  1850. {
  1851. vm->ip = vm->reg[c->reg2];
  1852. }
  1853. }
  1854. void CMPJUMPU_LE(struct lilith* vm, struct Instruction* c)
  1855. {
  1856. if((vm->reg[c->reg0]) <= (vm->reg[c->reg1]))
  1857. {
  1858. vm->ip = vm->reg[c->reg2];
  1859. }
  1860. }
  1861. void CMPJUMPU_L(struct lilith* vm, struct Instruction* c)
  1862. {
  1863. if((vm->reg[c->reg0]) < (vm->reg[c->reg1]))
  1864. {
  1865. vm->ip = vm->reg[c->reg2];
  1866. }
  1867. }