vm_decode.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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. uint64_t performance_counter;
  19. #ifdef TRACE
  20. void record_trace(char* c);
  21. void print_traces();
  22. #endif
  23. /* Allocate and intialize memory/state */
  24. struct lilith* create_vm(size_t size)
  25. {
  26. performance_counter = 0;
  27. struct lilith* vm;
  28. vm = calloc(1, sizeof(struct lilith));
  29. vm->memory = calloc(size, sizeof(uint8_t));
  30. vm->amount_of_Ram = size;
  31. vm->halted = false;
  32. vm->exception = false;
  33. return vm;
  34. }
  35. /* Free up the memory we previously allocated */
  36. void destroy_vm(struct lilith* vm)
  37. {
  38. free(vm->memory);
  39. free(vm);
  40. }
  41. /* Validate Memory Addresses*/
  42. void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* message)
  43. {
  44. if(vm->amount_of_Ram <= place)
  45. {
  46. fprintf(stderr, "Invalid state reached after: %lu instructions\n", performance_counter);
  47. fprintf(stderr, "%i: %s\n", place, message);
  48. vm->halted = true;
  49. #ifdef TRACE
  50. record_trace("HALT");
  51. print_traces();
  52. #endif
  53. exit(EXIT_FAILURE);
  54. }
  55. }
  56. /* Deal with illegal instructions and their encodings */
  57. void illegal_instruction(struct lilith* vm, struct Instruction* c)
  58. {
  59. fprintf(stderr, "Invalid instruction was recieved at address:%08X\n", c->ip);
  60. fprintf(stderr, "After %lu instructions\n", performance_counter);
  61. fprintf(stderr, "Unable to execute the following instruction:\n\t%s\n", c->operation);
  62. c->invalid = true;
  63. vm->halted = true;
  64. #ifdef DEBUG
  65. fprintf(stderr, "Computer Program has Halted\n");
  66. #endif
  67. #ifdef TRACE
  68. record_trace("HALT");
  69. print_traces();
  70. #endif
  71. exit(EXIT_FAILURE);
  72. }
  73. /* Deal with 4OP */
  74. void decode_4OP(struct Instruction* c)
  75. {
  76. c->raw_XOP = c->raw1;
  77. c->XOP[0] = c->operation[2];
  78. c->XOP[1] = c->operation[3];
  79. c->raw_Immediate = 0;
  80. c->reg0 = c->raw2/16;
  81. c->reg1 = c->raw2%16;
  82. c->reg2 = c->raw3/16;
  83. c->reg3 = c->raw3%16;
  84. }
  85. /* Deal with 3OP */
  86. void decode_3OP(struct Instruction* c)
  87. {
  88. c->raw_XOP = c->raw1*0x10 + c->raw2/16;
  89. c->XOP[0] = c->operation[2];
  90. c->XOP[1] = c->operation[3];
  91. c->XOP[2] = c->operation[4];
  92. c->raw_Immediate = 0;
  93. c->reg0 = c->raw2%16;
  94. c->reg1 = c->raw3/16;
  95. c->reg2 = c->raw3%16;
  96. }
  97. /* Deal with 2OP */
  98. void decode_2OP(struct Instruction* c)
  99. {
  100. c->raw_XOP = c->raw1*0x100 + c->raw2;
  101. c->XOP[0] = c->operation[2];
  102. c->XOP[1] = c->operation[3];
  103. c->XOP[2] = c->operation[4];
  104. c->XOP[3] = c->operation[5];
  105. c->raw_Immediate = 0;
  106. c->reg0 = c->raw3/16;
  107. c->reg1 = c->raw3%16;
  108. }
  109. /* Deal with 1OP */
  110. void decode_1OP(struct Instruction* c)
  111. {
  112. c->raw_XOP = c->raw1*0x1000 + c->raw2*0x10 + c->raw3/16;
  113. c->XOP[0] = c->operation[2];
  114. c->XOP[1] = c->operation[3];
  115. c->XOP[2] = c->operation[4];
  116. c->XOP[3] = c->operation[5];
  117. c->XOP[4] = c->operation[6];
  118. c->raw_Immediate = 0;
  119. c->reg0 = c->raw3%16;
  120. }
  121. /* Deal with 2OPI */
  122. void decode_2OPI(struct lilith* vm, struct Instruction* c)
  123. {
  124. c->raw_Immediate = (uint8_t)vm->memory[vm->ip];
  125. vm->ip = vm->ip + 1;
  126. uint8_t hold = (uint8_t)vm->memory[vm->ip];
  127. vm->ip = vm->ip + 1;
  128. c->raw_Immediate = c->raw_Immediate*0x100 + hold;
  129. c->Immediate[0] = c->operation[4];
  130. c->Immediate[1] = c->operation[5];
  131. c->Immediate[2] = c->operation[6];
  132. c->Immediate[3] = c->operation[7];
  133. c->reg0 = c->raw3/16;
  134. c->reg1 = c->raw3%16;
  135. }
  136. /* Deal with 1OPI */
  137. void decode_1OPI(struct lilith* vm, struct Instruction* c)
  138. {
  139. c->raw_Immediate = (uint8_t)vm->memory[vm->ip];
  140. vm->ip = vm->ip + 1;
  141. uint8_t hold = (uint8_t)vm->memory[vm->ip];
  142. vm->ip = vm->ip + 1;
  143. c->raw_Immediate = c->raw_Immediate*0x100 + hold;
  144. c->Immediate[0] = c->operation[4];
  145. c->Immediate[1] = c->operation[5];
  146. c->Immediate[2] = c->operation[6];
  147. c->Immediate[3] = c->operation[7];
  148. c->HAL_CODE = 0;
  149. c->raw_XOP = c->raw3/16;
  150. c->XOP[0] = c->operation[7];
  151. c->reg0 = c->raw3%16;
  152. }
  153. /* Deal with 0OPI */
  154. void decode_0OPI(struct Instruction* c)
  155. {
  156. c->raw_Immediate = c->raw2*0x100 + c->raw3;
  157. c->Immediate[0] = c->operation[4];
  158. c->Immediate[1] = c->operation[5];
  159. c->Immediate[2] = c->operation[6];
  160. c->Immediate[3] = c->operation[7];
  161. c->HAL_CODE = 0;
  162. c->raw_XOP = c->raw1;
  163. c->XOP[0] = c->operation[2];
  164. c->XOP[1] = c->operation[3];
  165. }
  166. /* Deal with Halcode */
  167. void decode_HALCODE(struct Instruction* c)
  168. {
  169. c->HAL_CODE = c->raw1*0x10000 + c->raw2*0x100 + c->raw3;
  170. }
  171. /* Useful unpacking functions */
  172. void unpack_byte(uint8_t a, char* c)
  173. {
  174. char table[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46};
  175. c[0] = table[a / 16];
  176. c[1] = table[a % 16];
  177. }
  178. /* Unpack the full instruction */
  179. void unpack_instruction(struct Instruction* c)
  180. {
  181. unpack_byte(c->raw0, &(c->operation[0]));
  182. unpack_byte(c->raw1, &(c->operation[2]));
  183. unpack_byte(c->raw2, &(c->operation[4]));
  184. unpack_byte(c->raw3, &(c->operation[6]));
  185. c->opcode[0] = c->operation[0];
  186. c->opcode[1] = c->operation[1];
  187. }
  188. /* Load instruction addressed at IP */
  189. void read_instruction(struct lilith* vm, struct Instruction *current)
  190. {
  191. memset(current, 0, sizeof(struct Instruction));
  192. /* Store IP for debugging */
  193. current->ip = vm->ip;
  194. outside_of_world(vm, current->ip, "READ Instruction outside of World");
  195. /* Read the actual bytes and increment the IP */
  196. current->raw0 = (uint8_t)vm->memory[vm->ip];
  197. vm->ip = vm->ip + 1;
  198. current->raw1 = (uint8_t)vm->memory[vm->ip];
  199. vm->ip = vm->ip + 1;
  200. current->raw2 = (uint8_t)vm->memory[vm->ip];
  201. vm->ip = vm->ip + 1;
  202. current->raw3 = (uint8_t)vm->memory[vm->ip];
  203. vm->ip = vm->ip + 1;
  204. unpack_instruction(current);
  205. }
  206. /* Process HALCODE instructions */
  207. bool eval_HALCODE(struct lilith* vm, struct Instruction* c)
  208. {
  209. #ifdef DEBUG
  210. char Name[20] = "ILLEGAL_HALCODE";
  211. #endif
  212. switch(c->HAL_CODE)
  213. {
  214. case 0x100000: /* fopen_read */
  215. {
  216. #ifdef DEBUG
  217. strncpy(Name, "FOPEN_READ", 19);
  218. #elif TRACE
  219. record_trace("FOPEN_READ");
  220. #endif
  221. vm_FOPEN_READ(vm);
  222. break;
  223. }
  224. case 0x100001: /* fopen_write */
  225. {
  226. #ifdef DEBUG
  227. strncpy(Name, "FOPEN_WRITE", 19);
  228. #elif TRACE
  229. record_trace("FOPEN_WRITE");
  230. #endif
  231. vm_FOPEN_WRITE(vm);
  232. break;
  233. }
  234. case 0x100002: /* fclose */
  235. {
  236. #ifdef DEBUG
  237. strncpy(Name, "FCLOSE", 19);
  238. #elif TRACE
  239. record_trace("FCLOSE");
  240. #endif
  241. vm_FCLOSE(vm);
  242. break;
  243. }
  244. case 0x100003: /* rewind */
  245. {
  246. #ifdef DEBUG
  247. strncpy(Name, "REWIND", 19);
  248. #elif TRACE
  249. record_trace("REWIND");
  250. #endif
  251. vm_REWIND(vm);
  252. break;
  253. }
  254. case 0x100004: /* fseek */
  255. {
  256. #ifdef DEBUG
  257. strncpy(Name, "FSEEK", 19);
  258. #elif TRACE
  259. record_trace("FSEEK");
  260. #endif
  261. vm_FSEEK(vm);
  262. break;
  263. }
  264. case 0x100100: /* fgetc */
  265. {
  266. #ifdef DEBUG
  267. strncpy(Name, "FGETC", 19);
  268. #elif TRACE
  269. record_trace("FGETC");
  270. #endif
  271. vm_FGETC(vm);
  272. break;
  273. }
  274. case 0x100200: /* fputc */
  275. {
  276. #ifdef DEBUG
  277. strncpy(Name, "FPUTC", 19);
  278. #elif TRACE
  279. record_trace("FPUTC");
  280. #endif
  281. vm_FPUTC(vm);
  282. break;
  283. }
  284. case 0x110000: /* HAL_MEM */
  285. {
  286. #ifdef DEBUG
  287. strncpy(Name, "HAL_MEM", 19);
  288. #elif TRACE
  289. record_trace("HAL_MEM");
  290. #endif
  291. vm_HAL_MEM(vm);
  292. break;
  293. }
  294. default:
  295. {
  296. fprintf(stderr, "Invalid HALCODE\nComputer Program has Halted\n");
  297. illegal_instruction(vm, c);
  298. break;
  299. }
  300. }
  301. #ifdef DEBUG
  302. fprintf(stdout, "# %s\n", Name);
  303. #endif
  304. return false;
  305. }
  306. /* Process 4OP Integer instructions */
  307. bool eval_4OP_Int(struct lilith* vm, struct Instruction* c)
  308. {
  309. #ifdef DEBUG
  310. char Name[20] = "ILLEGAL_4OP";
  311. #endif
  312. switch(c->raw_XOP)
  313. {
  314. case 0x00: /* ADD.CI */
  315. {
  316. #ifdef DEBUG
  317. strncpy(Name, "ADD.CI", 19);
  318. #elif TRACE
  319. record_trace("ADD.CI");
  320. #endif
  321. ADD_CI(vm, c);
  322. break;
  323. }
  324. case 0x01: /* ADD.CO */
  325. {
  326. #ifdef DEBUG
  327. strncpy(Name, "ADD.CO", 19);
  328. #elif TRACE
  329. record_trace("ADD.CO");
  330. #endif
  331. ADD_CO(vm, c);
  332. break;
  333. }
  334. case 0x02: /* ADD.CIO */
  335. {
  336. #ifdef DEBUG
  337. strncpy(Name, "ADD.CIO", 19);
  338. #elif TRACE
  339. record_trace("ADD.CIO");
  340. #endif
  341. ADD_CIO(vm, c);
  342. break;
  343. }
  344. case 0x03: /* ADDU.CI */
  345. {
  346. #ifdef DEBUG
  347. strncpy(Name, "ADDU.CI", 19);
  348. #elif TRACE
  349. record_trace("ADDU.CI");
  350. #endif
  351. ADDU_CI(vm, c);
  352. break;
  353. }
  354. case 0x04: /* ADDU.CO */
  355. {
  356. #ifdef DEBUG
  357. strncpy(Name, "ADDU.CO", 19);
  358. #elif TRACE
  359. record_trace("ADDU.CO");
  360. #endif
  361. ADDU_CO(vm, c);
  362. break;
  363. }
  364. case 0x05: /* ADDU.CIO */
  365. {
  366. #ifdef DEBUG
  367. strncpy(Name, "ADDU.CIO", 19);
  368. #elif TRACE
  369. record_trace("ADDU.CIO");
  370. #endif
  371. ADDU_CIO(vm, c);
  372. break;
  373. }
  374. case 0x06: /* SUB.BI */
  375. {
  376. #ifdef DEBUG
  377. strncpy(Name, "SUB.BI", 19);
  378. #elif TRACE
  379. record_trace("SUB.BI");
  380. #endif
  381. SUB_BI(vm, c);
  382. break;
  383. }
  384. case 0x07: /* SUB.BO */
  385. {
  386. #ifdef DEBUG
  387. strncpy(Name, "SUB.BO", 19);
  388. #elif TRACE
  389. record_trace("SUB.BO");
  390. #endif
  391. SUB_BO(vm, c);
  392. break;
  393. }
  394. case 0x08: /* SUB.BIO */
  395. {
  396. #ifdef DEBUG
  397. strncpy(Name, "SUB.BIO", 19);
  398. #elif TRACE
  399. record_trace("SUB.BIO");
  400. #endif
  401. SUB_BIO(vm, c);
  402. break;
  403. }
  404. case 0x09: /* SUBU.BI */
  405. {
  406. #ifdef DEBUG
  407. strncpy(Name, "SUBU.BI", 19);
  408. #elif TRACE
  409. record_trace("SUBU.BI");
  410. #endif
  411. SUBU_BI(vm, c);
  412. break;
  413. }
  414. case 0x0A: /* SUBU.BO */
  415. {
  416. #ifdef DEBUG
  417. strncpy(Name, "SUBU.BO", 19);
  418. #elif TRACE
  419. record_trace("SUBU.BO");
  420. #endif
  421. SUBU_BO(vm, c);
  422. break;
  423. }
  424. case 0x0B: /* SUBU.BIO */
  425. {
  426. #ifdef DEBUG
  427. strncpy(Name, "SUBU.BIO", 19);
  428. #elif TRACE
  429. record_trace("SUBU.BIO");
  430. #endif
  431. SUBU_BIO(vm, c);
  432. break;
  433. }
  434. case 0x0C: /* MULTIPLY */
  435. {
  436. #ifdef DEBUG
  437. strncpy(Name, "MULTIPLY", 19);
  438. #elif TRACE
  439. record_trace("MULTIPLY");
  440. #endif
  441. MULTIPLY(vm, c);
  442. break;
  443. }
  444. case 0x0D: /* MULTIPLYU */
  445. {
  446. #ifdef DEBUG
  447. strncpy(Name, "MULTIPLYU", 19);
  448. #elif TRACE
  449. record_trace("MULTIPLYU");
  450. #endif
  451. MULTIPLYU(vm, c);
  452. break;
  453. }
  454. case 0x0E: /* DIVIDE */
  455. {
  456. #ifdef DEBUG
  457. strncpy(Name, "DIVIDE", 19);
  458. #elif TRACE
  459. record_trace("DIVIDE");
  460. #endif
  461. DIVIDE(vm, c);
  462. break;
  463. }
  464. case 0x0F: /* DIVIDEU */
  465. {
  466. #ifdef DEBUG
  467. strncpy(Name, "DIVIDEU", 19);
  468. #elif TRACE
  469. record_trace("DIVIDEU");
  470. #endif
  471. DIVIDEU(vm, c);
  472. break;
  473. }
  474. case 0x10: /* MUX */
  475. {
  476. #ifdef DEBUG
  477. strncpy(Name, "MUX", 19);
  478. #elif TRACE
  479. record_trace("MUX");
  480. #endif
  481. MUX(vm, c);
  482. break;
  483. }
  484. case 0x11: /* NMUX */
  485. {
  486. #ifdef DEBUG
  487. strncpy(Name, "NMUX", 19);
  488. #elif TRACE
  489. record_trace("NMUX");
  490. #endif
  491. NMUX(vm, c);
  492. break;
  493. }
  494. case 0x12: /* SORT */
  495. {
  496. #ifdef DEBUG
  497. strncpy(Name, "SORT", 19);
  498. #elif TRACE
  499. record_trace("SORT");
  500. #endif
  501. SORT(vm, c);
  502. break;
  503. }
  504. case 0x13: /* SORTU */
  505. {
  506. #ifdef DEBUG
  507. strncpy(Name, "SORTU", 19);
  508. #elif TRACE
  509. record_trace("SORTU");
  510. #endif
  511. SORTU(vm, c);
  512. break;
  513. }
  514. default:
  515. {
  516. illegal_instruction(vm, c);
  517. break;
  518. }
  519. }
  520. #ifdef DEBUG
  521. fprintf(stdout, "# %s reg%u reg%u reg%u reg%u\n", Name, c->reg0, c->reg1, c->reg2, c->reg3);
  522. #endif
  523. return false;
  524. }
  525. /* Process 3OP Integer instructions */
  526. bool eval_3OP_Int(struct lilith* vm, struct Instruction* c)
  527. {
  528. #ifdef DEBUG
  529. char Name[20] = "ILLEGAL_3OP";
  530. #endif
  531. switch(c->raw_XOP)
  532. {
  533. case 0x000: /* ADD */
  534. {
  535. #ifdef DEBUG
  536. strncpy(Name, "ADD", 19);
  537. #elif TRACE
  538. record_trace("ADD");
  539. #endif
  540. ADD(vm, c);
  541. break;
  542. }
  543. case 0x001: /* ADDU */
  544. {
  545. #ifdef DEBUG
  546. strncpy(Name, "ADDU", 19);
  547. #elif TRACE
  548. record_trace("ADDU");
  549. #endif
  550. ADDU(vm, c);
  551. break;
  552. }
  553. case 0x002: /* SUB */
  554. {
  555. #ifdef DEBUG
  556. strncpy(Name, "SUB", 19);
  557. #elif TRACE
  558. record_trace("SUB");
  559. #endif
  560. SUB(vm, c);
  561. break;
  562. }
  563. case 0x003: /* SUBU */
  564. {
  565. #ifdef DEBUG
  566. strncpy(Name, "SUBU", 19);
  567. #elif TRACE
  568. record_trace("SUBU");
  569. #endif
  570. SUBU(vm, c);
  571. break;
  572. }
  573. case 0x004: /* CMP */
  574. {
  575. #ifdef DEBUG
  576. strncpy(Name, "CMP", 19);
  577. #elif TRACE
  578. record_trace("CMP");
  579. #endif
  580. CMP(vm, c);
  581. break;
  582. }
  583. case 0x005: /* CMPU */
  584. {
  585. #ifdef DEBUG
  586. strncpy(Name, "CMPU", 19);
  587. #elif TRACE
  588. record_trace("CMPU");
  589. #endif
  590. CMPU(vm, c);
  591. break;
  592. }
  593. case 0x006: /* MUL */
  594. {
  595. #ifdef DEBUG
  596. strncpy(Name, "MUL", 19);
  597. #elif TRACE
  598. record_trace("MUL");
  599. #endif
  600. MUL(vm, c);
  601. break;
  602. }
  603. case 0x007: /* MULH */
  604. {
  605. #ifdef DEBUG
  606. strncpy(Name, "MULH", 19);
  607. #elif TRACE
  608. record_trace("MULH");
  609. #endif
  610. MULH(vm, c);
  611. break;
  612. }
  613. case 0x008: /* MULU */
  614. {
  615. #ifdef DEBUG
  616. strncpy(Name, "MULU", 19);
  617. #elif TRACE
  618. record_trace("MULU");
  619. #endif
  620. MULU(vm, c);
  621. break;
  622. }
  623. case 0x009: /* MULUH */
  624. {
  625. #ifdef DEBUG
  626. strncpy(Name, "MULUH", 19);
  627. #elif TRACE
  628. record_trace("MULUH");
  629. #endif
  630. MULUH(vm, c);
  631. break;
  632. }
  633. case 0x00A: /* DIV */
  634. {
  635. #ifdef DEBUG
  636. strncpy(Name, "DIV", 19);
  637. #elif TRACE
  638. record_trace("DIV");
  639. #endif
  640. DIV(vm, c);
  641. break;
  642. }
  643. case 0x00B: /* MOD */
  644. {
  645. #ifdef DEBUG
  646. strncpy(Name, "MOD", 19);
  647. #elif TRACE
  648. record_trace("MOD");
  649. #endif
  650. MOD(vm, c);
  651. break;
  652. }
  653. case 0x00C: /* DIVU */
  654. {
  655. #ifdef DEBUG
  656. strncpy(Name, "DIVU", 19);
  657. #elif TRACE
  658. record_trace("DIVU");
  659. #endif
  660. DIVU(vm, c);
  661. break;
  662. }
  663. case 0x00D: /* MODU */
  664. {
  665. #ifdef DEBUG
  666. strncpy(Name, "MODU", 19);
  667. #elif TRACE
  668. record_trace("MODU");
  669. #endif
  670. MODU(vm, c);
  671. break;
  672. }
  673. case 0x010: /* MAX */
  674. {
  675. #ifdef DEBUG
  676. strncpy(Name, "MAX", 19);
  677. #elif TRACE
  678. record_trace("MAX");
  679. #endif
  680. MAX(vm, c);
  681. break;
  682. }
  683. case 0x011: /* MAXU */
  684. {
  685. #ifdef DEBUG
  686. strncpy(Name, "MAXU", 19);
  687. #elif TRACE
  688. record_trace("MAXU");
  689. #endif
  690. MAXU(vm, c);
  691. break;
  692. }
  693. case 0x012: /* MIN */
  694. {
  695. #ifdef DEBUG
  696. strncpy(Name, "MIN", 19);
  697. #elif TRACE
  698. record_trace("MIN");
  699. #endif
  700. MIN(vm, c);
  701. break;
  702. }
  703. case 0x013: /* MINU */
  704. {
  705. #ifdef DEBUG
  706. strncpy(Name, "MINU", 19);
  707. #elif TRACE
  708. record_trace("MINU");
  709. #endif
  710. MINU(vm, c);
  711. break;
  712. }
  713. case 0x014: /* PACK */
  714. case 0x015: /* UNPACK */
  715. case 0x016: /* PACK8.CO */
  716. case 0x017: /* PACK8U.CO */
  717. case 0x018: /* PACK16.CO */
  718. case 0x019: /* PACK16U.CO */
  719. case 0x01A: /* PACK32.CO */
  720. case 0x01B: /* PACK32U.CO */
  721. {
  722. illegal_instruction(vm, c);
  723. break;
  724. }
  725. case 0x020: /* AND */
  726. {
  727. #ifdef DEBUG
  728. strncpy(Name, "AND", 19);
  729. #elif TRACE
  730. record_trace("AND");
  731. #endif
  732. AND(vm, c);
  733. break;
  734. }
  735. case 0x021: /* OR */
  736. {
  737. #ifdef DEBUG
  738. strncpy(Name, "OR", 19);
  739. #elif TRACE
  740. record_trace("OR");
  741. #endif
  742. OR(vm, c);
  743. break;
  744. }
  745. case 0x022: /* XOR */
  746. {
  747. #ifdef DEBUG
  748. strncpy(Name, "XOR", 19);
  749. #elif TRACE
  750. record_trace("XOR");
  751. #endif
  752. XOR(vm, c);
  753. break;
  754. }
  755. case 0x023: /* NAND */
  756. {
  757. #ifdef DEBUG
  758. strncpy(Name, "NAND", 19);
  759. #elif TRACE
  760. record_trace("NAND");
  761. #endif
  762. NAND(vm, c);
  763. break;
  764. }
  765. case 0x024: /* NOR */
  766. {
  767. #ifdef DEBUG
  768. strncpy(Name, "NOR", 19);
  769. #elif TRACE
  770. record_trace("NOR");
  771. #endif
  772. NOR(vm, c);
  773. break;
  774. }
  775. case 0x025: /* XNOR */
  776. {
  777. #ifdef DEBUG
  778. strncpy(Name, "XNOR", 19);
  779. #elif TRACE
  780. record_trace("XNOR");
  781. #endif
  782. XNOR(vm, c);
  783. break;
  784. }
  785. case 0x026: /* MPQ */
  786. {
  787. #ifdef DEBUG
  788. strncpy(Name, "MPQ", 19);
  789. #elif TRACE
  790. record_trace("MPQ");
  791. #endif
  792. MPQ(vm, c);
  793. break;
  794. }
  795. case 0x027: /* LPQ */
  796. {
  797. #ifdef DEBUG
  798. strncpy(Name, "LPQ", 19);
  799. #elif TRACE
  800. record_trace("LPQ");
  801. #endif
  802. LPQ(vm, c);
  803. break;
  804. }
  805. case 0x028: /* CPQ */
  806. {
  807. #ifdef DEBUG
  808. strncpy(Name, "CPQ", 19);
  809. #elif TRACE
  810. record_trace("CPQ");
  811. #endif
  812. CPQ(vm, c);
  813. break;
  814. }
  815. case 0x029: /* BPQ */
  816. {
  817. #ifdef DEBUG
  818. strncpy(Name, "BPQ", 19);
  819. #elif TRACE
  820. record_trace("BPQ");
  821. #endif
  822. BPQ(vm, c);
  823. break;
  824. }
  825. case 0x030: /* SAL */
  826. {
  827. #ifdef DEBUG
  828. strncpy(Name, "SAL", 19);
  829. #elif TRACE
  830. record_trace("SAL");
  831. #endif
  832. SAL(vm, c);
  833. break;
  834. }
  835. case 0x031: /* SAR */
  836. {
  837. #ifdef DEBUG
  838. strncpy(Name, "SAR", 19);
  839. #elif TRACE
  840. record_trace("SAR");
  841. #endif
  842. SAR(vm, c);
  843. break;
  844. }
  845. case 0x032: /* SL0 */
  846. {
  847. #ifdef DEBUG
  848. strncpy(Name, "SL0", 19);
  849. #elif TRACE
  850. record_trace("SL0");
  851. #endif
  852. SL0(vm, c);
  853. break;
  854. }
  855. case 0x033: /* SR0 */
  856. {
  857. #ifdef DEBUG
  858. strncpy(Name, "SR0", 19);
  859. #elif TRACE
  860. record_trace("SR0");
  861. #endif
  862. SR0(vm, c);
  863. break;
  864. }
  865. case 0x034: /* SL1 */
  866. {
  867. #ifdef DEBUG
  868. strncpy(Name, "SL1", 19);
  869. #elif TRACE
  870. record_trace("SL1");
  871. #endif
  872. SL1(vm, c);
  873. break;
  874. }
  875. case 0x035: /* SR1 */
  876. {
  877. #ifdef DEBUG
  878. strncpy(Name, "SR1", 19);
  879. #elif TRACE
  880. record_trace("SR1");
  881. #endif
  882. SR1(vm, c);
  883. break;
  884. }
  885. case 0x036: /* ROL */
  886. {
  887. #ifdef DEBUG
  888. strncpy(Name, "ROL", 19);
  889. #elif TRACE
  890. record_trace("ROL");
  891. #endif
  892. ROL(vm, c);
  893. break;
  894. }
  895. case 0x037: /* ROR */
  896. {
  897. #ifdef DEBUG
  898. strncpy(Name, "ROR", 19);
  899. #elif TRACE
  900. record_trace("ROR");
  901. #endif
  902. ROR(vm, c);
  903. break;
  904. }
  905. case 0x038: /* LOADX */
  906. {
  907. #ifdef DEBUG
  908. strncpy(Name, "LOADX", 19);
  909. #elif TRACE
  910. record_trace("LOADX");
  911. #endif
  912. LOADX(vm, c);
  913. break;
  914. }
  915. case 0x039: /* LOADX8 */
  916. {
  917. #ifdef DEBUG
  918. strncpy(Name, "LOADX8", 19);
  919. #elif TRACE
  920. record_trace("LOADX8");
  921. #endif
  922. LOADX8(vm, c);
  923. break;
  924. }
  925. case 0x03A: /* LOADXU8 */
  926. {
  927. #ifdef DEBUG
  928. strncpy(Name, "LOADXU8", 19);
  929. #elif TRACE
  930. record_trace("LOADXU8");
  931. #endif
  932. LOADXU8(vm, c);
  933. break;
  934. }
  935. case 0x03B: /* LOADX16 */
  936. {
  937. #ifdef DEBUG
  938. strncpy(Name, "LOADX16", 19);
  939. #elif TRACE
  940. record_trace("LOADX16");
  941. #endif
  942. LOADX16(vm, c);
  943. break;
  944. }
  945. case 0x03C: /* LOADXU16 */
  946. {
  947. #ifdef DEBUG
  948. strncpy(Name, "LOADXU16", 19);
  949. #elif TRACE
  950. record_trace("LOADXU16");
  951. #endif
  952. LOADXU16(vm, c);
  953. break;
  954. }
  955. case 0x03D: /* LOADX32 */
  956. {
  957. #ifdef DEBUG
  958. strncpy(Name, "LOADX32", 19);
  959. #elif TRACE
  960. record_trace("LOADX32");
  961. #endif
  962. LOADX32(vm, c);
  963. break;
  964. }
  965. case 0x03E: /* LOADXU32 */
  966. {
  967. #ifdef DEBUG
  968. strncpy(Name, "LOADXU32", 19);
  969. #elif TRACE
  970. record_trace("LOADXU32");
  971. #endif
  972. LOADXU32(vm, c);
  973. break;
  974. }
  975. case 0x048: /* STOREX */
  976. {
  977. #ifdef DEBUG
  978. strncpy(Name, "STOREX", 19);
  979. #elif TRACE
  980. record_trace("STOREX");
  981. #endif
  982. STOREX(vm, c);
  983. break;
  984. }
  985. case 0x049: /* STOREX8 */
  986. {
  987. #ifdef DEBUG
  988. strncpy(Name, "STOREX8", 19);
  989. #elif TRACE
  990. record_trace("STOREX8");
  991. #endif
  992. STOREX8(vm, c);
  993. break;
  994. }
  995. case 0x04A: /* STOREX16 */
  996. {
  997. #ifdef DEBUG
  998. strncpy(Name, "STOREX16", 19);
  999. #elif TRACE
  1000. record_trace("STOREX16");
  1001. #endif
  1002. STOREX16(vm, c);
  1003. break;
  1004. }
  1005. case 0x04B: /* STOREX32 */
  1006. {
  1007. #ifdef DEBUG
  1008. strncpy(Name, "STOREX32", 19);
  1009. #elif TRACE
  1010. record_trace("STOREX32");
  1011. #endif
  1012. STOREX32(vm, c);
  1013. break;
  1014. }
  1015. case 0x050: /* CMPJUMP.G */
  1016. {
  1017. #ifdef DEBUG
  1018. strncpy(Name, "CMPJUMP.G", 19);
  1019. #elif TRACE
  1020. record_trace("CMPJUMP.G");
  1021. #endif
  1022. CMPJUMP_G(vm, c);
  1023. break;
  1024. }
  1025. case 0x051: /* CMPJUMP.GE */
  1026. {
  1027. #ifdef DEBUG
  1028. strncpy(Name, "CMPJUMP.GE", 19);
  1029. #elif TRACE
  1030. record_trace("CMPJUMP.GE");
  1031. #endif
  1032. CMPJUMP_GE(vm, c);
  1033. break;
  1034. }
  1035. case 0x052: /* CMPJUMP.E */
  1036. {
  1037. #ifdef DEBUG
  1038. strncpy(Name, "CMPJUMP.E", 19);
  1039. #elif TRACE
  1040. record_trace("CMPJUMP.E");
  1041. #endif
  1042. CMPJUMP_E(vm, c);
  1043. break;
  1044. }
  1045. case 0x053: /* CMPJUMP.NE */
  1046. {
  1047. #ifdef DEBUG
  1048. strncpy(Name, "CMPJUMP.NE", 19);
  1049. #elif TRACE
  1050. record_trace("CMPJUMP.NE");
  1051. #endif
  1052. CMPJUMP_NE(vm, c);
  1053. break;
  1054. }
  1055. case 0x054: /* CMPJUMP.LE */
  1056. {
  1057. #ifdef DEBUG
  1058. strncpy(Name, "CMPJUMP.LE", 19);
  1059. #elif TRACE
  1060. record_trace("CMPJUMP.LE");
  1061. #endif
  1062. CMPJUMP_LE(vm, c);
  1063. break;
  1064. }
  1065. case 0x055: /* CMPJUMP.L */
  1066. {
  1067. #ifdef DEBUG
  1068. strncpy(Name, "CMPJUMP.L", 19);
  1069. #elif TRACE
  1070. record_trace("CMPJUMP.L");
  1071. #endif
  1072. CMPJUMP_L(vm, c);
  1073. break;
  1074. }
  1075. case 0x060: /* CMPJUMPU.G */
  1076. {
  1077. #ifdef DEBUG
  1078. strncpy(Name, "CMPJUMPU.G", 19);
  1079. #elif TRACE
  1080. record_trace("CMPJUMPU.G");
  1081. #endif
  1082. CMPJUMPU_G(vm, c);
  1083. break;
  1084. }
  1085. case 0x061: /* CMPJUMPU.GE */
  1086. {
  1087. #ifdef DEBUG
  1088. strncpy(Name, "CMPJUMPU.GE", 19);
  1089. #elif TRACE
  1090. record_trace("CMPJUMPU.GE");
  1091. #endif
  1092. CMPJUMPU_GE(vm, c);
  1093. break;
  1094. }
  1095. case 0x064: /* CMPJUMPU.LE */
  1096. {
  1097. #ifdef DEBUG
  1098. strncpy(Name, "CMPJUMPU.LE", 19);
  1099. #elif TRACE
  1100. record_trace("CMPJUMPU.LE");
  1101. #endif
  1102. CMPJUMPU_LE(vm, c);
  1103. break;
  1104. }
  1105. case 0x065: /* CMPJUMPU.L */
  1106. {
  1107. #ifdef DEBUG
  1108. strncpy(Name, "CMPJUMPU.L", 19);
  1109. #elif TRACE
  1110. record_trace("CMPJUMPU.L");
  1111. #endif
  1112. CMPJUMPU_L(vm, c);
  1113. break;
  1114. }
  1115. default:
  1116. {
  1117. illegal_instruction(vm, c);
  1118. break;
  1119. }
  1120. }
  1121. #ifdef DEBUG
  1122. fprintf(stdout, "# %s reg%u reg%u reg%u\n", Name, c->reg0, c->reg1, c->reg2);
  1123. #endif
  1124. return false;
  1125. }
  1126. /* Process 2OP Integer instructions */
  1127. bool eval_2OP_Int(struct lilith* vm, struct Instruction* c)
  1128. {
  1129. #ifdef DEBUG
  1130. char Name[20] = "ILLEGAL_2OP";
  1131. #endif
  1132. switch(c->raw_XOP)
  1133. {
  1134. case 0x0000: /* NEG */
  1135. {
  1136. #ifdef DEBUG
  1137. strncpy(Name, "NEG", 19);
  1138. #elif TRACE
  1139. record_trace("NEG");
  1140. #endif
  1141. NEG(vm, c);
  1142. break;
  1143. }
  1144. case 0x0001: /* ABS */
  1145. {
  1146. #ifdef DEBUG
  1147. strncpy(Name, "ABS", 19);
  1148. #elif TRACE
  1149. record_trace("ABS");
  1150. #endif
  1151. ABS(vm, c);
  1152. break;
  1153. }
  1154. case 0x0002: /* NABS */
  1155. {
  1156. #ifdef DEBUG
  1157. strncpy(Name, "NABS", 19);
  1158. #elif TRACE
  1159. record_trace("NABS");
  1160. #endif
  1161. NABS(vm, c);
  1162. break;
  1163. }
  1164. case 0x0003: /* SWAP */
  1165. {
  1166. #ifdef DEBUG
  1167. strncpy(Name, "SWAP", 19);
  1168. #elif TRACE
  1169. record_trace("SWAP");
  1170. #endif
  1171. SWAP(vm, c);
  1172. break;
  1173. }
  1174. case 0x0004: /* COPY */
  1175. {
  1176. #ifdef DEBUG
  1177. strncpy(Name, "COPY", 19);
  1178. #elif TRACE
  1179. record_trace("COPY");
  1180. #endif
  1181. COPY(vm, c);
  1182. break;
  1183. }
  1184. case 0x0005: /* MOVE */
  1185. {
  1186. #ifdef DEBUG
  1187. strncpy(Name, "MOVE", 19);
  1188. #elif TRACE
  1189. record_trace("MOVE");
  1190. #endif
  1191. MOVE(vm, c);
  1192. break;
  1193. }
  1194. case 0x0006: /* NOT */
  1195. {
  1196. #ifdef DEBUG
  1197. strncpy(Name, "NOT", 19);
  1198. #elif TRACE
  1199. record_trace("NOT");
  1200. #endif
  1201. NOT(vm, c);
  1202. break;
  1203. }
  1204. case 0x0100: /* BRANCH */
  1205. {
  1206. #ifdef DEBUG
  1207. strncpy(Name, "BRANCH", 19);
  1208. #elif TRACE
  1209. record_trace("BRANCH");
  1210. #endif
  1211. BRANCH(vm, c);
  1212. break;
  1213. }
  1214. case 0x0101: /* CALL */
  1215. {
  1216. #ifdef DEBUG
  1217. strncpy(Name, "CALL", 19);
  1218. #elif TRACE
  1219. record_trace("CALL");
  1220. #endif
  1221. CALL(vm, c);
  1222. break;
  1223. }
  1224. case 0x0200: /* PUSHR */
  1225. {
  1226. #ifdef DEBUG
  1227. strncpy(Name, "PUSHR", 19);
  1228. #elif TRACE
  1229. record_trace("PUSHR");
  1230. #endif
  1231. PUSHR(vm, c);
  1232. break;
  1233. }
  1234. case 0x0201: /* PUSH8 */
  1235. {
  1236. #ifdef DEBUG
  1237. strncpy(Name, "PUSH8", 19);
  1238. #elif TRACE
  1239. record_trace("PUSH8");
  1240. #endif
  1241. PUSH8(vm, c);
  1242. break;
  1243. }
  1244. case 0x0202: /* PUSH16 */
  1245. {
  1246. #ifdef DEBUG
  1247. strncpy(Name, "PUSH16", 19);
  1248. #elif TRACE
  1249. record_trace("PUSH16");
  1250. #endif
  1251. PUSH16(vm, c);
  1252. break;
  1253. }
  1254. case 0x0203: /* PUSH32 */
  1255. {
  1256. #ifdef DEBUG
  1257. strncpy(Name, "PUSH32", 19);
  1258. #elif TRACE
  1259. record_trace("PUSH32");
  1260. #endif
  1261. PUSH32(vm, c);
  1262. break;
  1263. }
  1264. case 0x0280: /* POPR */
  1265. {
  1266. #ifdef DEBUG
  1267. strncpy(Name, "POPR", 19);
  1268. #elif TRACE
  1269. record_trace("POPR");
  1270. #endif
  1271. POPR(vm, c);
  1272. break;
  1273. }
  1274. case 0x0281: /* POP8 */
  1275. {
  1276. #ifdef DEBUG
  1277. strncpy(Name, "POP8", 19);
  1278. #elif TRACE
  1279. record_trace("POP8");
  1280. #endif
  1281. POP8(vm, c);
  1282. break;
  1283. }
  1284. case 0x0282: /* POPU8 */
  1285. {
  1286. #ifdef DEBUG
  1287. strncpy(Name, "POPU8", 19);
  1288. #elif TRACE
  1289. record_trace("POPU8");
  1290. #endif
  1291. POPU8(vm, c);
  1292. break;
  1293. }
  1294. case 0x0283: /* POP16 */
  1295. {
  1296. #ifdef DEBUG
  1297. strncpy(Name, "POP16", 19);
  1298. #elif TRACE
  1299. record_trace("POP16");
  1300. #endif
  1301. POP16(vm, c);
  1302. break;
  1303. }
  1304. case 0x0284: /* POPU16 */
  1305. {
  1306. #ifdef DEBUG
  1307. strncpy(Name, "POPU16", 19);
  1308. #elif TRACE
  1309. record_trace("POPU16");
  1310. #endif
  1311. POPU16(vm, c);
  1312. break;
  1313. }
  1314. case 0x0285: /* POP32 */
  1315. {
  1316. #ifdef DEBUG
  1317. strncpy(Name, "POP32", 19);
  1318. #elif TRACE
  1319. record_trace("POP32");
  1320. #endif
  1321. POP32(vm, c);
  1322. break;
  1323. }
  1324. case 0x0286: /* POPU32 */
  1325. {
  1326. #ifdef DEBUG
  1327. strncpy(Name, "POPU32", 19);
  1328. #elif TRACE
  1329. record_trace("POPU32");
  1330. #endif
  1331. POPU32(vm, c);
  1332. break;
  1333. }
  1334. case 0x0300: /* CMPSKIP.G */
  1335. {
  1336. #ifdef DEBUG
  1337. strncpy(Name, "CMPSKIP.G", 19);
  1338. #elif TRACE
  1339. record_trace("CMPSKIP.G");
  1340. #endif
  1341. CMPSKIP_G(vm, c);
  1342. break;
  1343. }
  1344. case 0x0301: /* CMPSKIP.GE */
  1345. {
  1346. #ifdef DEBUG
  1347. strncpy(Name, "CMPSKIP.GE", 19);
  1348. #elif TRACE
  1349. record_trace("CMPSKIP.GE");
  1350. #endif
  1351. CMPSKIP_GE(vm, c);
  1352. break;
  1353. }
  1354. case 0x0302: /* CMPSKIP.E */
  1355. {
  1356. #ifdef DEBUG
  1357. strncpy(Name, "CMPSKIP.E", 19);
  1358. #elif TRACE
  1359. record_trace("CMPSKIP.E");
  1360. #endif
  1361. CMPSKIP_E(vm, c);
  1362. break;
  1363. }
  1364. case 0x0303: /* CMPSKIP.NE */
  1365. {
  1366. #ifdef DEBUG
  1367. strncpy(Name, "CMPSKIP.NE", 19);
  1368. #elif TRACE
  1369. record_trace("CMPSKIP.NE");
  1370. #endif
  1371. CMPSKIP_NE(vm, c);
  1372. break;
  1373. }
  1374. case 0x0304: /* CMPSKIP.LE */
  1375. {
  1376. #ifdef DEBUG
  1377. strncpy(Name, "CMPSKIP.LE", 19);
  1378. #elif TRACE
  1379. record_trace("CMPSKIP.LE");
  1380. #endif
  1381. CMPSKIP_LE(vm, c);
  1382. break;
  1383. }
  1384. case 0x0305: /* CMPSKIP.L */
  1385. {
  1386. #ifdef DEBUG
  1387. strncpy(Name, "CMPSKIP.L", 19);
  1388. #elif TRACE
  1389. record_trace("CMPSKIP.L");
  1390. #endif
  1391. CMPSKIP_L(vm, c);
  1392. break;
  1393. }
  1394. case 0x0380: /* CMPSKIPU.G */
  1395. {
  1396. #ifdef DEBUG
  1397. strncpy(Name, "CMPSKIPU.G", 19);
  1398. #elif TRACE
  1399. record_trace("CMPSKIPU.G");
  1400. #endif
  1401. CMPSKIPU_G(vm, c);
  1402. break;
  1403. }
  1404. case 0x0381: /* CMPSKIPU.GE */
  1405. {
  1406. #ifdef DEBUG
  1407. strncpy(Name, "CMPSKIPU.GE", 19);
  1408. #elif TRACE
  1409. record_trace("CMPSKIPU.GE");
  1410. #endif
  1411. CMPSKIPU_GE(vm, c);
  1412. break;
  1413. }
  1414. case 0x0384: /* CMPSKIPU.LE */
  1415. {
  1416. #ifdef DEBUG
  1417. strncpy(Name, "CMPSKIPU.LE", 19);
  1418. #elif TRACE
  1419. record_trace("CMPSKIPU.LE");
  1420. #endif
  1421. CMPSKIPU_LE(vm, c);
  1422. break;
  1423. }
  1424. case 0x0385: /* CMPSKIPU.L */
  1425. {
  1426. #ifdef DEBUG
  1427. strncpy(Name, "CMPSKIPU.L", 19);
  1428. #elif TRACE
  1429. record_trace("CMPSKIPU.L");
  1430. #endif
  1431. CMPSKIPU_L(vm, c);
  1432. break;
  1433. }
  1434. default:
  1435. {
  1436. illegal_instruction(vm, c);
  1437. break;
  1438. }
  1439. }
  1440. #ifdef DEBUG
  1441. fprintf(stdout, "# %s reg%u reg%u\n", Name, c->reg0, c->reg1);
  1442. #endif
  1443. return false;
  1444. }
  1445. /* Process 1OP Integer instructions */
  1446. bool eval_1OP_Int(struct lilith* vm, struct Instruction* c)
  1447. {
  1448. #ifdef DEBUG
  1449. char Name[20] = "ILLEGAL_1OP";
  1450. #endif
  1451. switch(c->raw_XOP)
  1452. {
  1453. case 0x00000: /* READPC */
  1454. {
  1455. #ifdef DEBUG
  1456. strncpy(Name, "READPC", 19);
  1457. #elif TRACE
  1458. record_trace("READPC");
  1459. #endif
  1460. READPC(vm, c);
  1461. break;
  1462. }
  1463. case 0x00001: /* READSCID */
  1464. {
  1465. #ifdef DEBUG
  1466. strncpy(Name, "READSCID", 19);
  1467. #elif TRACE
  1468. record_trace("READSCID");
  1469. #endif
  1470. READSCID(vm, c);
  1471. break;
  1472. }
  1473. case 0x00002: /* FALSE */
  1474. {
  1475. #ifdef DEBUG
  1476. strncpy(Name, "FALSE", 19);
  1477. #elif TRACE
  1478. record_trace("FALSE");
  1479. #endif
  1480. FALSE(vm, c);
  1481. break;
  1482. }
  1483. case 0x00003: /* TRUE */
  1484. {
  1485. #ifdef DEBUG
  1486. strncpy(Name, "TRUE", 19);
  1487. #elif TRACE
  1488. record_trace("TRUE");
  1489. #endif
  1490. TRUE(vm, c);
  1491. break;
  1492. }
  1493. case 0x01000: /* JSR_COROUTINE */
  1494. {
  1495. #ifdef DEBUG
  1496. strncpy(Name, "JSR_COROUTINE", 19);
  1497. #elif TRACE
  1498. record_trace("JSR_COROUTINE");
  1499. #endif
  1500. JSR_COROUTINE(vm, c);
  1501. break;
  1502. }
  1503. case 0x01001: /* RET */
  1504. {
  1505. #ifdef DEBUG
  1506. strncpy(Name, "RET", 19);
  1507. #elif TRACE
  1508. record_trace("RET");
  1509. #endif
  1510. RET(vm, c);
  1511. break;
  1512. }
  1513. case 0x02000: /* PUSHPC */
  1514. {
  1515. #ifdef DEBUG
  1516. strncpy(Name, "PUSHPC", 19);
  1517. #elif TRACE
  1518. record_trace("PUSHPC");
  1519. #endif
  1520. PUSHPC(vm, c);
  1521. break;
  1522. }
  1523. case 0x02001: /* POPPC */
  1524. {
  1525. #ifdef DEBUG
  1526. strncpy(Name, "POPPC", 19);
  1527. #elif TRACE
  1528. record_trace("POPPC");
  1529. #endif
  1530. POPPC(vm, c);
  1531. break;
  1532. }
  1533. default:
  1534. {
  1535. illegal_instruction(vm, c);
  1536. break;
  1537. }
  1538. }
  1539. #ifdef DEBUG
  1540. fprintf(stdout, "# %s reg%u\n", Name, c->reg0);
  1541. #endif
  1542. return false;
  1543. }
  1544. /* Process 2OPI Integer instructions */
  1545. bool eval_2OPI_Int(struct lilith* vm, struct Instruction* c)
  1546. {
  1547. #ifdef DEBUG
  1548. char Name[20] = "ILLEGAL_2OPI";
  1549. #endif
  1550. /* 0x0E ... 0x2B */
  1551. /* 0xB0 ... 0xDF */
  1552. switch(c->raw2)
  1553. {
  1554. case 0x0E: /* ADDI */
  1555. {
  1556. #ifdef DEBUG
  1557. strncpy(Name, "ADDI", 19);
  1558. #elif TRACE
  1559. record_trace("ADDI");
  1560. #endif
  1561. ADDI(vm, c);
  1562. break;
  1563. }
  1564. case 0x0F: /* ADDUI */
  1565. {
  1566. #ifdef DEBUG
  1567. strncpy(Name, "ADDUI", 19);
  1568. #elif TRACE
  1569. record_trace("ADDUI");
  1570. #endif
  1571. ADDUI(vm, c);
  1572. break;
  1573. }
  1574. case 0x10: /* SUBI */
  1575. {
  1576. #ifdef DEBUG
  1577. strncpy(Name, "SUBI", 19);
  1578. #elif TRACE
  1579. record_trace("SUBI");
  1580. #endif
  1581. SUBI(vm, c);
  1582. break;
  1583. }
  1584. case 0x11: /* SUBUI */
  1585. {
  1586. #ifdef DEBUG
  1587. strncpy(Name, "SUBUI", 19);
  1588. #elif TRACE
  1589. record_trace("SUBUI");
  1590. #endif
  1591. SUBUI(vm, c);
  1592. break;
  1593. }
  1594. case 0x12: /* CMPI */
  1595. {
  1596. #ifdef DEBUG
  1597. strncpy(Name, "CMPI", 19);
  1598. #elif TRACE
  1599. record_trace("CMPI");
  1600. #endif
  1601. CMPI(vm, c);
  1602. break;
  1603. }
  1604. case 0x13: /* LOAD */
  1605. {
  1606. #ifdef DEBUG
  1607. strncpy(Name, "LOAD", 19);
  1608. #elif TRACE
  1609. record_trace("LOAD");
  1610. #endif
  1611. LOAD(vm, c);
  1612. break;
  1613. }
  1614. case 0x14: /* LOAD8 */
  1615. {
  1616. #ifdef DEBUG
  1617. strncpy(Name, "LOAD8", 19);
  1618. #elif TRACE
  1619. record_trace("LOAD8");
  1620. #endif
  1621. LOAD8(vm, c);
  1622. break;
  1623. }
  1624. case 0x15: /* LOADU8 */
  1625. {
  1626. #ifdef DEBUG
  1627. strncpy(Name, "LOADU8", 19);
  1628. #elif TRACE
  1629. record_trace("LOADU8");
  1630. #endif
  1631. LOADU8(vm, c);
  1632. break;
  1633. }
  1634. case 0x16: /* LOAD16 */
  1635. {
  1636. #ifdef DEBUG
  1637. strncpy(Name, "LOAD16", 19);
  1638. #elif TRACE
  1639. record_trace("LOAD16");
  1640. #endif
  1641. LOAD16(vm, c);
  1642. break;
  1643. }
  1644. case 0x17: /* LOADU16 */
  1645. {
  1646. #ifdef DEBUG
  1647. strncpy(Name, "LOADU16", 19);
  1648. #elif TRACE
  1649. record_trace("LOADU16");
  1650. #endif
  1651. LOADU16(vm, c);
  1652. break;
  1653. }
  1654. case 0x18: /* LOAD32 */
  1655. {
  1656. #ifdef DEBUG
  1657. strncpy(Name, "LOAD32", 19);
  1658. #elif TRACE
  1659. record_trace("LOAD32");
  1660. #endif
  1661. LOAD32(vm, c);
  1662. break;
  1663. }
  1664. case 0x19: /* LOADU32 */
  1665. {
  1666. #ifdef DEBUG
  1667. strncpy(Name, "LOADU32", 19);
  1668. #elif TRACE
  1669. record_trace("LOADU32");
  1670. #endif
  1671. LOADU32(vm, c);
  1672. break;
  1673. }
  1674. case 0x1F: /* CMPUI */
  1675. {
  1676. #ifdef DEBUG
  1677. strncpy(Name, "CMPUI", 19);
  1678. #elif TRACE
  1679. record_trace("CMPUI");
  1680. #endif
  1681. CMPUI(vm, c);
  1682. break;
  1683. }
  1684. case 0x20: /* STORE */
  1685. {
  1686. #ifdef DEBUG
  1687. strncpy(Name, "STORE", 19);
  1688. #elif TRACE
  1689. record_trace("STORE");
  1690. #endif
  1691. STORE(vm, c);
  1692. break;
  1693. }
  1694. case 0x21: /* STORE8 */
  1695. {
  1696. #ifdef DEBUG
  1697. strncpy(Name, "STORE8", 19);
  1698. #elif TRACE
  1699. record_trace("STORE8");
  1700. #endif
  1701. STORE8(vm, c);
  1702. break;
  1703. }
  1704. case 0x22: /* STORE16 */
  1705. {
  1706. #ifdef DEBUG
  1707. strncpy(Name, "STORE16", 19);
  1708. #elif TRACE
  1709. record_trace("STORE16");
  1710. #endif
  1711. STORE16(vm, c);
  1712. break;
  1713. }
  1714. case 0x23: /* STORE32 */
  1715. {
  1716. #ifdef DEBUG
  1717. strncpy(Name, "STORE32", 19);
  1718. #elif TRACE
  1719. record_trace("STORE32");
  1720. #endif
  1721. STORE32(vm, c);
  1722. break;
  1723. }
  1724. case 0xB0: /* ANDI */
  1725. {
  1726. #ifdef DEBUG
  1727. strncpy(Name, "ANDI", 19);
  1728. #elif TRACE
  1729. record_trace("ANDI");
  1730. #endif
  1731. ANDI(vm, c);
  1732. break;
  1733. }
  1734. case 0xB1: /* ORI */
  1735. {
  1736. #ifdef DEBUG
  1737. strncpy(Name, "ORI", 19);
  1738. #elif TRACE
  1739. record_trace("ORI");
  1740. #endif
  1741. ORI(vm, c);
  1742. break;
  1743. }
  1744. case 0xB2: /* XORI */
  1745. {
  1746. #ifdef DEBUG
  1747. strncpy(Name, "XORI", 19);
  1748. #elif TRACE
  1749. record_trace("XORI");
  1750. #endif
  1751. XORI(vm, c);
  1752. break;
  1753. }
  1754. case 0xB3: /* NANDI */
  1755. {
  1756. #ifdef DEBUG
  1757. strncpy(Name, "NANDI", 19);
  1758. #elif TRACE
  1759. record_trace("NANDI");
  1760. #endif
  1761. NANDI(vm, c);
  1762. break;
  1763. }
  1764. case 0xB4: /* NORI */
  1765. {
  1766. #ifdef DEBUG
  1767. strncpy(Name, "NORI", 19);
  1768. #elif TRACE
  1769. record_trace("NORI");
  1770. #endif
  1771. NORI(vm, c);
  1772. break;
  1773. }
  1774. case 0xB5: /* XNORI */
  1775. {
  1776. #ifdef DEBUG
  1777. strncpy(Name, "XNORI", 19);
  1778. #elif TRACE
  1779. record_trace("XNORI");
  1780. #endif
  1781. XNORI(vm, c);
  1782. break;
  1783. }
  1784. case 0xC0: /* CMPJUMPI.G */
  1785. {
  1786. #ifdef DEBUG
  1787. strncpy(Name, "CMPJUMPI.G", 19);
  1788. #elif TRACE
  1789. record_trace("CMPJUMPI.G");
  1790. #endif
  1791. CMPJUMPI_G(vm, c);
  1792. break;
  1793. }
  1794. case 0xC1: /* CMPJUMPI.GE */
  1795. {
  1796. #ifdef DEBUG
  1797. strncpy(Name, "CMPJUMPI.GE", 19);
  1798. #elif TRACE
  1799. record_trace("CMPJUMPI.GE");
  1800. #endif
  1801. CMPJUMPI_GE(vm, c);
  1802. break;
  1803. }
  1804. case 0xC2: /* CMPJUMPI.E */
  1805. {
  1806. #ifdef DEBUG
  1807. strncpy(Name, "CMPJUMPI.E", 19);
  1808. #elif TRACE
  1809. record_trace("CMPJUMPI.E");
  1810. #endif
  1811. CMPJUMPI_E(vm, c);
  1812. break;
  1813. }
  1814. case 0xC3: /* CMPJUMPI.NE */
  1815. {
  1816. #ifdef DEBUG
  1817. strncpy(Name, "CMPJUMPI.NE", 19);
  1818. #elif TRACE
  1819. record_trace("CMPJUMPI.NE");
  1820. #endif
  1821. CMPJUMPI_NE(vm, c);
  1822. break;
  1823. }
  1824. case 0xC4: /* CMPJUMPI.LE */
  1825. {
  1826. #ifdef DEBUG
  1827. strncpy(Name, "CMPJUMPI.LE", 19);
  1828. #elif TRACE
  1829. record_trace("CMPJUMPI.LE");
  1830. #endif
  1831. CMPJUMPI_LE(vm, c);
  1832. break;
  1833. }
  1834. case 0xC5: /* CMPJUMPI.L */
  1835. {
  1836. #ifdef DEBUG
  1837. strncpy(Name, "CMPJUMPI.L", 19);
  1838. #elif TRACE
  1839. record_trace("CMPJUMPI.L");
  1840. #endif
  1841. CMPJUMPI_L(vm, c);
  1842. break;
  1843. }
  1844. case 0xD0: /* CMPJUMPUI.G */
  1845. {
  1846. #ifdef DEBUG
  1847. strncpy(Name, "CMPJUMPUI.G", 19);
  1848. #elif TRACE
  1849. record_trace("CMPJUMPUI.G");
  1850. #endif
  1851. CMPJUMPUI_G(vm, c);
  1852. break;
  1853. }
  1854. case 0xD1: /* CMPJUMPUI.GE */
  1855. {
  1856. #ifdef DEBUG
  1857. strncpy(Name, "CMPJUMPUI.GE", 19);
  1858. #elif TRACE
  1859. record_trace("CMPJUMPUI.GE");
  1860. #endif
  1861. CMPJUMPUI_GE(vm, c);
  1862. break;
  1863. }
  1864. case 0xD4: /* CMPJUMPUI.LE */
  1865. {
  1866. #ifdef DEBUG
  1867. strncpy(Name, "CMPJUMPUI.LE", 19);
  1868. #elif TRACE
  1869. record_trace("CMPJUMPUI.LE");
  1870. #endif
  1871. CMPJUMPUI_LE(vm, c);
  1872. break;
  1873. }
  1874. case 0xD5: /* CMPJUMPUI.L */
  1875. {
  1876. #ifdef DEBUG
  1877. strncpy(Name, "CMPJUMPUI.L", 19);
  1878. #elif TRACE
  1879. record_trace("CMPJUMPUI.L");
  1880. #endif
  1881. CMPJUMPUI_L(vm, c);
  1882. break;
  1883. }
  1884. default:
  1885. {
  1886. illegal_instruction(vm, c);
  1887. break;
  1888. }
  1889. }
  1890. #ifdef DEBUG
  1891. fprintf(stdout, "# %s reg%u reg%u %i\n", Name, c->reg0, c->reg1, c->raw_Immediate);
  1892. #endif
  1893. return false;
  1894. }
  1895. /* Process 1OPI Integer instructions */
  1896. bool eval_Integer_1OPI(struct lilith* vm, struct Instruction* c)
  1897. {
  1898. #ifdef DEBUG
  1899. char Name[20] = "ILLEGAL_1OPI";
  1900. #endif
  1901. uint32_t Opcode = (c->raw2 * 16) + c->raw_XOP;
  1902. switch(Opcode)
  1903. {
  1904. case 0x2C0: /* JUMP.C */
  1905. {
  1906. #ifdef DEBUG
  1907. strncpy(Name, "JUMP.C", 19);
  1908. #elif TRACE
  1909. record_trace("JUMP.C");
  1910. #endif
  1911. JUMP_C(vm, c);
  1912. break;
  1913. }
  1914. case 0x2C1: /* JUMP.B */
  1915. {
  1916. #ifdef DEBUG
  1917. strncpy(Name, "JUMP.B", 19);
  1918. #elif TRACE
  1919. record_trace("JUMP.B");
  1920. #endif
  1921. JUMP_B(vm, c);
  1922. break;
  1923. }
  1924. case 0x2C2: /* JUMP.O */
  1925. {
  1926. #ifdef DEBUG
  1927. strncpy(Name, "JUMP.O", 19);
  1928. #elif TRACE
  1929. record_trace("JUMP.O");
  1930. #endif
  1931. JUMP_O(vm, c);
  1932. break;
  1933. }
  1934. case 0x2C3: /* JUMP.G */
  1935. {
  1936. #ifdef DEBUG
  1937. strncpy(Name, "JUMP.G", 19);
  1938. #elif TRACE
  1939. record_trace("JUMP.G");
  1940. #endif
  1941. JUMP_G(vm, c);
  1942. break;
  1943. }
  1944. case 0x2C4: /* JUMP.GE */
  1945. {
  1946. #ifdef DEBUG
  1947. strncpy(Name, "JUMP.GE", 19);
  1948. #elif TRACE
  1949. record_trace("JUMP.GE");
  1950. #endif
  1951. JUMP_GE(vm, c);
  1952. break;
  1953. }
  1954. case 0x2C5: /* JUMP.E */
  1955. {
  1956. #ifdef DEBUG
  1957. strncpy(Name, "JUMP.E", 19);
  1958. #elif TRACE
  1959. record_trace("JUMP.E");
  1960. #endif
  1961. JUMP_E(vm, c);
  1962. break;
  1963. }
  1964. case 0x2C6: /* JUMP.NE */
  1965. {
  1966. #ifdef DEBUG
  1967. strncpy(Name, "JUMP.NE", 19);
  1968. #elif TRACE
  1969. record_trace("JUMP.NE");
  1970. #endif
  1971. JUMP_NE(vm, c);
  1972. break;
  1973. }
  1974. case 0x2C7: /* JUMP.LE */
  1975. {
  1976. #ifdef DEBUG
  1977. strncpy(Name, "JUMP.LE", 19);
  1978. #elif TRACE
  1979. record_trace("JUMP.LE");
  1980. #endif
  1981. JUMP_LE(vm, c);
  1982. break;
  1983. }
  1984. case 0x2C8: /* JUMP.L */
  1985. {
  1986. #ifdef DEBUG
  1987. strncpy(Name, "JUMP.L", 19);
  1988. #elif TRACE
  1989. record_trace("JUMP.L");
  1990. #endif
  1991. JUMP_L(vm, c);
  1992. break;
  1993. }
  1994. case 0x2C9: /* JUMP.Z */
  1995. {
  1996. #ifdef DEBUG
  1997. strncpy(Name, "JUMP.Z", 19);
  1998. #elif TRACE
  1999. record_trace("JUMP.Z");
  2000. #endif
  2001. JUMP_Z(vm, c);
  2002. break;
  2003. }
  2004. case 0x2CA: /* JUMP.NZ */
  2005. {
  2006. #ifdef DEBUG
  2007. strncpy(Name, "JUMP.NZ", 19);
  2008. #elif TRACE
  2009. record_trace("JUMP.NZ");
  2010. #endif
  2011. JUMP_NZ(vm, c);
  2012. break;
  2013. }
  2014. case 0x2CB: /* JUMP.P */
  2015. {
  2016. #ifdef DEBUG
  2017. strncpy(Name, "JUMP.P", 19);
  2018. #elif TRACE
  2019. record_trace("JUMP.P");
  2020. #endif
  2021. JUMP_P(vm, c);
  2022. break;
  2023. }
  2024. case 0x2CC: /* JUMP.NP */
  2025. {
  2026. #ifdef DEBUG
  2027. strncpy(Name, "JUMP.NP", 19);
  2028. #elif TRACE
  2029. record_trace("JUMP.NP");
  2030. #endif
  2031. JUMP_NP(vm, c);
  2032. break;
  2033. }
  2034. case 0x2D0: /* CALLI */
  2035. {
  2036. #ifdef DEBUG
  2037. strncpy(Name, "CALLI", 19);
  2038. #elif TRACE
  2039. record_trace("CALLI");
  2040. #endif
  2041. CALLI(vm, c);
  2042. break;
  2043. }
  2044. case 0x2D1: /* LOADI */
  2045. {
  2046. #ifdef DEBUG
  2047. strncpy(Name, "LOADI", 19);
  2048. #elif TRACE
  2049. record_trace("LOADI");
  2050. #endif
  2051. LOADI(vm, c);
  2052. break;
  2053. }
  2054. case 0x2D2: /* LOADUI*/
  2055. {
  2056. #ifdef DEBUG
  2057. strncpy(Name, "LOADUI", 19);
  2058. #elif TRACE
  2059. record_trace("LOADUI");
  2060. #endif
  2061. LOADUI(vm, c);
  2062. break;
  2063. }
  2064. case 0x2D3: /* SALI */
  2065. {
  2066. #ifdef DEBUG
  2067. strncpy(Name, "SALI", 19);
  2068. #elif TRACE
  2069. record_trace("SALI");
  2070. #endif
  2071. SALI(vm, c);
  2072. break;
  2073. }
  2074. case 0x2D4: /* SARI */
  2075. {
  2076. #ifdef DEBUG
  2077. strncpy(Name, "SARI", 19);
  2078. #elif TRACE
  2079. record_trace("SARI");
  2080. #endif
  2081. SARI(vm, c);
  2082. break;
  2083. }
  2084. case 0x2D5: /* SL0I */
  2085. {
  2086. #ifdef DEBUG
  2087. strncpy(Name, "SL0I", 19);
  2088. #elif TRACE
  2089. record_trace("SL0I");
  2090. #endif
  2091. SL0I(vm, c);
  2092. break;
  2093. }
  2094. case 0x2D6: /* SR0I */
  2095. {
  2096. #ifdef DEBUG
  2097. strncpy(Name, "SR0I", 19);
  2098. #elif TRACE
  2099. record_trace("SR0I");
  2100. #endif
  2101. SR0I(vm, c);
  2102. break;
  2103. }
  2104. case 0x2D7: /* SL1I */
  2105. {
  2106. #ifdef DEBUG
  2107. strncpy(Name, "SL1I", 19);
  2108. #elif TRACE
  2109. record_trace("SL1I");
  2110. #endif
  2111. SL1I(vm, c);
  2112. break;
  2113. }
  2114. case 0x2D8: /* SR1I */
  2115. {
  2116. #ifdef DEBUG
  2117. strncpy(Name, "SR1I", 19);
  2118. #elif TRACE
  2119. record_trace("SR1I");
  2120. #endif
  2121. SR1I(vm, c);
  2122. break;
  2123. }
  2124. case 0x2E0: /* LOADR */
  2125. {
  2126. #ifdef DEBUG
  2127. strncpy(Name, "LOADR", 19);
  2128. #elif TRACE
  2129. record_trace("LOADR");
  2130. #endif
  2131. LOADR(vm, c);
  2132. break;
  2133. }
  2134. case 0x2E1: /* LOADR8 */
  2135. {
  2136. #ifdef DEBUG
  2137. strncpy(Name, "LOADR8", 19);
  2138. #elif TRACE
  2139. record_trace("LOADR8");
  2140. #endif
  2141. LOADR8(vm, c);
  2142. break;
  2143. }
  2144. case 0x2E2: /* LOADRU8 */
  2145. {
  2146. #ifdef DEBUG
  2147. strncpy(Name, "LOADRU8", 19);
  2148. #elif TRACE
  2149. record_trace("LOADRU8");
  2150. #endif
  2151. LOADRU8(vm, c);
  2152. break;
  2153. }
  2154. case 0x2E3: /* LOADR16 */
  2155. {
  2156. #ifdef DEBUG
  2157. strncpy(Name, "LOADR16", 19);
  2158. #elif TRACE
  2159. record_trace("LOADR16");
  2160. #endif
  2161. LOADR16(vm, c);
  2162. break;
  2163. }
  2164. case 0x2E4: /* LOADRU16 */
  2165. {
  2166. #ifdef DEBUG
  2167. strncpy(Name, "LOADRU16", 19);
  2168. #elif TRACE
  2169. record_trace("LOADRU16");
  2170. #endif
  2171. LOADRU16(vm, c);
  2172. break;
  2173. }
  2174. case 0x2E5: /* LOADR32 */
  2175. {
  2176. #ifdef DEBUG
  2177. strncpy(Name, "LOADR32", 19);
  2178. #elif TRACE
  2179. record_trace("LOADR32");
  2180. #endif
  2181. LOADR32(vm, c);
  2182. break;
  2183. }
  2184. case 0x2E6: /* LOADRU32 */
  2185. {
  2186. #ifdef DEBUG
  2187. strncpy(Name, "LOADRU32", 19);
  2188. #elif TRACE
  2189. record_trace("LOADRU32");
  2190. #endif
  2191. LOADRU32(vm, c);
  2192. break;
  2193. }
  2194. case 0x2F0: /* STORER */
  2195. {
  2196. #ifdef DEBUG
  2197. strncpy(Name, "STORER", 19);
  2198. #elif TRACE
  2199. record_trace("STORER");
  2200. #endif
  2201. STORER(vm, c);
  2202. break;
  2203. }
  2204. case 0x2F1: /* STORER8 */
  2205. {
  2206. #ifdef DEBUG
  2207. strncpy(Name, "STORER8", 19);
  2208. #elif TRACE
  2209. record_trace("STORER8");
  2210. #endif
  2211. STORER8(vm, c);
  2212. break;
  2213. }
  2214. case 0x2F2: /* STORER16 */
  2215. {
  2216. #ifdef DEBUG
  2217. strncpy(Name, "STORER16", 19);
  2218. #elif TRACE
  2219. record_trace("STORER16");
  2220. #endif
  2221. STORER16(vm, c);
  2222. break;
  2223. }
  2224. case 0x2F3: /* STORER32 */
  2225. {
  2226. #ifdef DEBUG
  2227. strncpy(Name, "STORER32", 19);
  2228. #elif TRACE
  2229. record_trace("STORER32");
  2230. #endif
  2231. STORER32(vm, c);
  2232. break;
  2233. }
  2234. case 0xA00: /* CMPSKIPI.G */
  2235. {
  2236. #ifdef DEBUG
  2237. strncpy(Name, "CMPSKIPI.G", 19);
  2238. #elif TRACE
  2239. record_trace("CMPSKIPI.G");
  2240. #endif
  2241. CMPSKIPI_G(vm, c);
  2242. break;
  2243. }
  2244. case 0xA01: /* CMPSKIPI.GE */
  2245. {
  2246. #ifdef DEBUG
  2247. strncpy(Name, "CMPSKIPI.GE", 19);
  2248. #elif TRACE
  2249. record_trace("CMPSKIPI.GE");
  2250. #endif
  2251. CMPSKIPI_GE(vm, c);
  2252. break;
  2253. }
  2254. case 0xA02: /* CMPSKIPI.E */
  2255. {
  2256. #ifdef DEBUG
  2257. strncpy(Name, "CMPSKIPI.E", 19);
  2258. #elif TRACE
  2259. record_trace("CMPSKIPI.E");
  2260. #endif
  2261. CMPSKIPI_E(vm, c);
  2262. break;
  2263. }
  2264. case 0xA03: /* CMPSKIPI.NE */
  2265. {
  2266. #ifdef DEBUG
  2267. strncpy(Name, "CMPSKIPI.NE", 19);
  2268. #elif TRACE
  2269. record_trace("CMPSKIPI.NE");
  2270. #endif
  2271. CMPSKIPI_NE(vm, c);
  2272. break;
  2273. }
  2274. case 0xA04: /* CMPSKIPI.LE */
  2275. {
  2276. #ifdef DEBUG
  2277. strncpy(Name, "CMPSKIPI.LE", 19);
  2278. #elif TRACE
  2279. record_trace("CMPSKIPI.LE");
  2280. #endif
  2281. CMPSKIPI_LE(vm, c);
  2282. break;
  2283. }
  2284. case 0xA05: /* CMPSKIPI.L */
  2285. {
  2286. #ifdef DEBUG
  2287. strncpy(Name, "CMPSKIPI.L", 19);
  2288. #elif TRACE
  2289. record_trace("CMPSKIPI.L");
  2290. #endif
  2291. CMPSKIPI_L(vm, c);
  2292. break;
  2293. }
  2294. case 0xA10: /* CMPSKIPUI.G */
  2295. {
  2296. #ifdef DEBUG
  2297. strncpy(Name, "CMPSKIPUI.G", 19);
  2298. #elif TRACE
  2299. record_trace("CMPSKIPUI.G");
  2300. #endif
  2301. CMPSKIPUI_G(vm, c);
  2302. break;
  2303. }
  2304. case 0xA11: /* CMPSKIPUI.GE */
  2305. {
  2306. #ifdef DEBUG
  2307. strncpy(Name, "CMPSKIPUI.GE", 19);
  2308. #elif TRACE
  2309. record_trace("CMPSKIPUI.GE");
  2310. #endif
  2311. CMPSKIPUI_GE(vm, c);
  2312. break;
  2313. }
  2314. case 0xA14: /* CMPSKIPUI.LE */
  2315. {
  2316. #ifdef DEBUG
  2317. strncpy(Name, "CMPSKIPUI.LE", 19);
  2318. #elif TRACE
  2319. record_trace("CMPSKIPUI.LE");
  2320. #endif
  2321. CMPSKIPUI_LE(vm, c);
  2322. break;
  2323. }
  2324. case 0xA15: /* CMPSKIPUI.L */
  2325. {
  2326. #ifdef DEBUG
  2327. strncpy(Name, "CMPSKIPUI.L", 19);
  2328. #elif TRACE
  2329. record_trace("CMPSKIPUI.L");
  2330. #endif
  2331. CMPSKIPUI_L(vm, c);
  2332. break;
  2333. }
  2334. default:
  2335. {
  2336. illegal_instruction(vm, c);
  2337. break;
  2338. }
  2339. }
  2340. #ifdef DEBUG
  2341. fprintf(stdout, "# %s reg%u %d\n", Name, c->reg0, c->raw_Immediate);
  2342. #endif
  2343. return false;
  2344. }
  2345. /* Process 0OPI Integer instructions */
  2346. bool eval_Integer_0OPI(struct lilith* vm, struct Instruction* c)
  2347. {
  2348. #ifdef DEBUG
  2349. char Name[20] = "ILLEGAL_0OPI";
  2350. #endif
  2351. switch(c->raw_XOP)
  2352. {
  2353. case 0x00: /* JUMP */
  2354. {
  2355. #ifdef DEBUG
  2356. strncpy(Name, "JUMP", 19);
  2357. #elif TRACE
  2358. record_trace("JUMP");
  2359. #endif
  2360. JUMP(vm, c);
  2361. break;
  2362. }
  2363. default:
  2364. {
  2365. illegal_instruction(vm, c);
  2366. break;
  2367. }
  2368. }
  2369. #ifdef DEBUG
  2370. fprintf(stdout, "# %s %d\n", Name, c->raw_Immediate);
  2371. #endif
  2372. return false;
  2373. }
  2374. /* Use Opcode to decide what to do and then have it done */
  2375. void eval_instruction(struct lilith* vm, struct Instruction* current)
  2376. {
  2377. performance_counter = performance_counter + 1;
  2378. #ifdef DEBUG
  2379. fprintf(stdout, "Executing: %s\n", current->operation);
  2380. usleep(1000);
  2381. #endif
  2382. switch(current->raw0)
  2383. {
  2384. case 0x00: /* Deal with NOPs */
  2385. {
  2386. if(0 == current->raw0 && 0 == current->raw1 && 0 == current->raw2 && 0 == current->raw3)
  2387. {
  2388. #ifdef TRACE
  2389. record_trace("NOP");
  2390. #endif
  2391. return;
  2392. }
  2393. illegal_instruction(vm, current);
  2394. }
  2395. case 0x01: /* Integer 4OP */
  2396. {
  2397. decode_4OP(current);
  2398. eval_4OP_Int(vm, current);
  2399. break;
  2400. }
  2401. case 0x05: /* Integer 3OP */
  2402. {
  2403. decode_3OP(current);
  2404. eval_3OP_Int(vm, current);
  2405. break;
  2406. }
  2407. case 0x09: /* Integer 2OP */
  2408. {
  2409. decode_2OP(current);
  2410. eval_2OP_Int(vm, current);
  2411. break;
  2412. }
  2413. case 0x0D: /* Integer 1OP */
  2414. {
  2415. decode_1OP(current);
  2416. eval_1OP_Int(vm, current);
  2417. break;
  2418. }
  2419. case 0xE1: /* Integer 2OPI */
  2420. {
  2421. decode_2OPI(vm, current);
  2422. eval_2OPI_Int(vm, current);
  2423. break;
  2424. }
  2425. case 0xE0: /* Integer 1OPI */
  2426. {
  2427. decode_1OPI(vm, current);
  2428. eval_Integer_1OPI(vm, current);
  2429. break;
  2430. }
  2431. case 0x3C: /* Integer 0OPI */
  2432. {
  2433. decode_0OPI(current);
  2434. eval_Integer_0OPI(vm, current);
  2435. break;
  2436. }
  2437. case 0x42: /* HALCODE */
  2438. {
  2439. decode_HALCODE(current);
  2440. eval_HALCODE(vm, current);
  2441. break;
  2442. }
  2443. case 0xFF: /* Deal with HALT */
  2444. {
  2445. vm->halted = true;
  2446. fprintf(stderr, "Computer Program has Halted\nAfter Executing %lu instructions\n", performance_counter);
  2447. #ifdef TRACE
  2448. record_trace("HALT");
  2449. print_traces();
  2450. #endif
  2451. break;
  2452. }
  2453. default: /* Deal with illegal instruction */
  2454. {
  2455. illegal_instruction(vm, current);
  2456. break;
  2457. }
  2458. }
  2459. }