vm_decode.c 45 KB

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