vm_instructions.c 45 KB

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