1
0

vm_instructions.c 43 KB

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