vm_instructions.c 45 KB

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