vm_decode.c 45 KB

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