vm_instructions.c 43 KB

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