vm_decode.c 46 KB

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