vm_instructions.c 42 KB

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