vm_instructions.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  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. }