disasm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. FILE* binary_file;
  23. int32_t address;
  24. /* Unpacked instruction */
  25. struct Instruction
  26. {
  27. uint32_t raw0, raw1, raw2, raw3;
  28. char opcode[3];
  29. uint32_t raw_XOP;
  30. char XOP[6];
  31. char operation[13];
  32. int16_t raw_Immediate;
  33. char Immediate[7];
  34. uint32_t HAL_CODE;
  35. uint8_t reg0;
  36. uint8_t reg1;
  37. uint8_t reg2;
  38. uint8_t reg3;
  39. bool invalid;
  40. };
  41. /* Useful unpacking functions */
  42. void unpack_byte(uint8_t a, char* c)
  43. {
  44. char table[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46};
  45. c[0] = table[a / 16];
  46. c[1] = table[a % 16];
  47. }
  48. void unpack_instruction(struct Instruction* c)
  49. {
  50. unpack_byte(c->raw0, &(c->operation[0]));
  51. unpack_byte(c->raw1, &(c->operation[2]));
  52. unpack_byte(c->raw2, &(c->operation[4]));
  53. unpack_byte(c->raw3, &(c->operation[6]));
  54. c->opcode[0] = c->operation[0];
  55. c->opcode[1] = c->operation[1];
  56. }
  57. /* Load instruction addressed at IP */
  58. void read_instruction(struct Instruction *current)
  59. {
  60. memset(current, 0, sizeof(struct Instruction));
  61. /* Read the actual bytes and increment the IP */
  62. current->raw0 = fgetc(binary_file);
  63. if(-1 == (int32_t)(current->raw0)) goto Broken;
  64. current->raw1 = fgetc(binary_file);
  65. if(-1 == (int32_t)(current->raw1)) goto Broken;
  66. current->raw2 = fgetc(binary_file);
  67. if(-1 == (int32_t)(current->raw2)) goto Broken;
  68. current->raw3 = fgetc(binary_file);
  69. if(-1 == (int32_t)(current->raw3)) goto Broken;
  70. unpack_instruction(current);
  71. return;
  72. /* This disassembler doesn't support non-instructions */
  73. Broken:
  74. fclose(binary_file);
  75. exit(EXIT_FAILURE);
  76. }
  77. void print_non_NULL(uint8_t c)
  78. {
  79. switch(c)
  80. {
  81. case 0: return;
  82. case 32 ... 126:
  83. {
  84. fprintf(stdout, "%c", c);
  85. break;
  86. }
  87. default: fprintf(stdout, "0x%X ", c);
  88. }
  89. }
  90. void string_values(struct Instruction *c, bool first)
  91. {
  92. if(first)
  93. {
  94. fprintf(stdout, "\"");
  95. }
  96. print_non_NULL(c->raw0);
  97. print_non_NULL(c->raw1);
  98. print_non_NULL(c->raw2);
  99. print_non_NULL(c->raw3);
  100. if(0 != c->raw3)
  101. {
  102. read_instruction(c);
  103. string_values(c, false);
  104. address = address + 4;
  105. }
  106. else
  107. {
  108. fprintf(stdout, "\"\t #STRING\n");
  109. }
  110. }
  111. void print_address(struct Instruction* c)
  112. {
  113. read_instruction(c);
  114. address = address + 4;
  115. fprintf(stdout,"%08X\t", address);
  116. fprintf(stdout, "%s\t # M2 Large const\n", c->operation);
  117. }
  118. void decode_Integer_4OP(struct Instruction* c)
  119. {
  120. /* Parse Raw Data */
  121. c->raw_XOP = c->raw1;
  122. c->XOP[0] = c->operation[2];
  123. c->XOP[1] = c->operation[3];
  124. c->raw_Immediate = 0;
  125. c->reg0 = c->raw2/16;
  126. c->reg1 = c->raw2%16;
  127. c->reg2 = c->raw3/16;
  128. c->reg3 = c->raw3%16;
  129. char Name[20] = "ILLEGAL_4OP";
  130. /* Convert to Human readable form */
  131. switch(c->raw_XOP)
  132. {
  133. case 0x00: /* ADD.CI */
  134. {
  135. strncpy(Name, "ADD.CI", 19);
  136. break;
  137. }
  138. case 0x01: /* ADD.CO */
  139. {
  140. strncpy(Name, "ADD.CO", 19);
  141. break;
  142. }
  143. case 0x02: /* ADD.CIO */
  144. {
  145. strncpy(Name, "ADD.CIO", 19);
  146. break;
  147. }
  148. case 0x03: /* ADDU.CI */
  149. {
  150. strncpy(Name, "ADDU.CI", 19);
  151. break;
  152. }
  153. case 0x04: /* ADDU.CO */
  154. {
  155. strncpy(Name, "ADDU.CO", 19);
  156. break;
  157. }
  158. case 0x05: /* ADDU.CIO */
  159. {
  160. strncpy(Name, "ADDU.CIO", 19);
  161. break;
  162. }
  163. case 0x06: /* SUB.BI */
  164. {
  165. strncpy(Name, "SUB.BI", 19);
  166. break;
  167. }
  168. case 0x07: /* SUB.BO */
  169. {
  170. strncpy(Name, "SUB.BO", 19);
  171. break;
  172. }
  173. case 0x08: /* SUB.BIO */
  174. {
  175. strncpy(Name, "SUB.BIO", 19);
  176. break;
  177. }
  178. case 0x09: /* SUBU.BI */
  179. {
  180. strncpy(Name, "SUBU.BI", 19);
  181. break;
  182. }
  183. case 0x0A: /* SUBU.BO */
  184. {
  185. strncpy(Name, "SUBU.BO", 19);
  186. break;
  187. }
  188. case 0x0B: /* SUBU.BIO */
  189. {
  190. strncpy(Name, "SUBU.BIO", 19);
  191. break;
  192. }
  193. case 0x0C: /* MULTIPLY */
  194. {
  195. strncpy(Name, "MULTIPLY", 19);
  196. break;
  197. }
  198. case 0x0D: /* MULTIPLYU */
  199. {
  200. strncpy(Name, "MULTIPLYU", 19);
  201. break;
  202. }
  203. case 0x0E: /* DIVIDE */
  204. {
  205. strncpy(Name, "DIVIDE", 19);
  206. break;
  207. }
  208. case 0x0F: /* DIVIDEU */
  209. {
  210. strncpy(Name, "DIVIDEU", 19);
  211. break;
  212. }
  213. case 0x10: /* MUX */
  214. {
  215. strncpy(Name, "MUX", 19);
  216. break;
  217. }
  218. case 0x11: /* NMUX */
  219. {
  220. strncpy(Name, "NMUX", 19);
  221. break;
  222. }
  223. case 0x12: /* SORT */
  224. {
  225. strncpy(Name, "SORT", 19);
  226. break;
  227. }
  228. case 0x13: /* SORTU */
  229. {
  230. strncpy(Name, "SORTU", 19);
  231. break;
  232. }
  233. default: /* Unknown 4OP */
  234. {
  235. string_values(c, true);
  236. return;
  237. }
  238. }
  239. fprintf(stdout, "%s reg%u reg%u reg%u reg%u\t", Name, c->reg0, c->reg1, c->reg2, c->reg3);
  240. fprintf(stdout, "# %s\n", c->operation);
  241. }
  242. void decode_Integer_3OP(struct Instruction* c)
  243. {
  244. /* Parse raw data */
  245. c->raw_XOP = c->raw1*0x10 + c->raw2/16;
  246. c->XOP[0] = c->operation[2];
  247. c->XOP[1] = c->operation[3];
  248. c->XOP[2] = c->operation[4];
  249. c->raw_Immediate = 0;
  250. c->reg0 = c->raw2%16;
  251. c->reg1 = c->raw3/16;
  252. c->reg2 = c->raw3%16;
  253. char Name[20] = "ILLEGAL_3OP";
  254. /* Convert to Human readable form */
  255. switch(c->raw_XOP)
  256. {
  257. case 0x000: /* ADD */
  258. {
  259. strncpy(Name, "ADD", 19);
  260. break;
  261. }
  262. case 0x001: /* ADDU */
  263. {
  264. strncpy(Name, "ADDU", 19);
  265. break;
  266. }
  267. case 0x002: /* SUB */
  268. {
  269. strncpy(Name, "SUB", 19);
  270. break;
  271. }
  272. case 0x003: /* SUBU */
  273. {
  274. strncpy(Name, "SUBU", 19);
  275. break;
  276. }
  277. case 0x004: /* CMP */
  278. {
  279. strncpy(Name, "CMP", 19);
  280. break;
  281. }
  282. case 0x005: /* CMPU */
  283. {
  284. strncpy(Name, "CMPU", 19);
  285. break;
  286. }
  287. case 0x006: /* MUL */
  288. {
  289. strncpy(Name, "MUL", 19);
  290. break;
  291. }
  292. case 0x007: /* MULH */
  293. {
  294. strncpy(Name, "MULH", 19);
  295. break;
  296. }
  297. case 0x008: /* MULU */
  298. {
  299. strncpy(Name, "MULU", 19);
  300. break;
  301. }
  302. case 0x009: /* MULUH */
  303. {
  304. strncpy(Name, "MULUH", 19);
  305. break;
  306. }
  307. case 0x00A: /* DIV */
  308. {
  309. strncpy(Name, "DIV", 19);
  310. break;
  311. }
  312. case 0x00B: /* MOD */
  313. {
  314. strncpy(Name, "MOD", 19);
  315. break;
  316. }
  317. case 0x00C: /* DIVU */
  318. {
  319. strncpy(Name, "DIVU", 19);
  320. break;
  321. }
  322. case 0x00D: /* MODU */
  323. {
  324. strncpy(Name, "MODU", 19);
  325. break;
  326. }
  327. case 0x010: /* MAX */
  328. {
  329. strncpy(Name, "MAX", 19);
  330. break;
  331. }
  332. case 0x011: /* MAXU */
  333. {
  334. strncpy(Name, "MAXU", 19);
  335. break;
  336. }
  337. case 0x012: /* MIN */
  338. {
  339. strncpy(Name, "MIN", 19);
  340. break;
  341. }
  342. case 0x013: /* MINU */
  343. {
  344. strncpy(Name, "MINU", 19);
  345. break;
  346. }
  347. case 0x014: /* PACK */
  348. case 0x015: /* UNPACK */
  349. case 0x016: /* PACK8.CO */
  350. case 0x017: /* PACK8U.CO */
  351. case 0x018: /* PACK16.CO */
  352. case 0x019: /* PACK16U.CO */
  353. case 0x01A: /* PACK32.CO */
  354. case 0x01B: /* PACK32U.CO */
  355. {
  356. strncpy(Name, "ILLEGAL_INSTRUCTION", 19);
  357. break;
  358. }
  359. case 0x020: /* AND */
  360. {
  361. strncpy(Name, "AND", 19);
  362. break;
  363. }
  364. case 0x021: /* OR */
  365. {
  366. strncpy(Name, "OR", 19);
  367. break;
  368. }
  369. case 0x022: /* XOR */
  370. {
  371. strncpy(Name, "XOR", 19);
  372. break;
  373. }
  374. case 0x023: /* NAND */
  375. {
  376. strncpy(Name, "NAND", 19);
  377. break;
  378. }
  379. case 0x024: /* NOR */
  380. {
  381. strncpy(Name, "NOR", 19);
  382. break;
  383. }
  384. case 0x025: /* XNOR */
  385. {
  386. strncpy(Name, "XNOR", 19);
  387. break;
  388. }
  389. case 0x026: /* MPQ */
  390. {
  391. strncpy(Name, "MPQ", 19);
  392. break;
  393. }
  394. case 0x027: /* LPQ */
  395. {
  396. strncpy(Name, "LPQ", 19);
  397. break;
  398. }
  399. case 0x028: /* CPQ */
  400. {
  401. strncpy(Name, "CPQ", 19);
  402. break;
  403. }
  404. case 0x029: /* BPQ */
  405. {
  406. strncpy(Name, "BPQ", 19);
  407. break;
  408. }
  409. case 0x030: /* SAL */
  410. {
  411. strncpy(Name, "SAL", 19);
  412. break;
  413. }
  414. case 0x031: /* SAR */
  415. {
  416. strncpy(Name, "SAR", 19);
  417. break;
  418. }
  419. case 0x032: /* SL0 */
  420. {
  421. strncpy(Name, "SL0", 19);
  422. break;
  423. }
  424. case 0x033: /* SR0 */
  425. {
  426. strncpy(Name, "SR0", 19);
  427. break;
  428. }
  429. case 0x034: /* SL1 */
  430. {
  431. strncpy(Name, "SL1", 19);
  432. break;
  433. }
  434. case 0x035: /* SR1 */
  435. {
  436. strncpy(Name, "SR1", 19);
  437. break;
  438. }
  439. case 0x036: /* ROL */
  440. {
  441. strncpy(Name, "ROL", 19);
  442. break;
  443. }
  444. case 0x037: /* ROR */
  445. {
  446. strncpy(Name, "ROR", 19);
  447. break;
  448. }
  449. case 0x038: /* LOADX */
  450. {
  451. strncpy(Name, "LOADX", 19);
  452. break;
  453. }
  454. case 0x039: /* LOADX8 */
  455. {
  456. strncpy(Name, "LOADX8", 19);
  457. break;
  458. }
  459. case 0x03A: /* LOADXU8 */
  460. {
  461. strncpy(Name, "LOADXU8", 19);
  462. break;
  463. }
  464. case 0x03B: /* LOADX16 */
  465. {
  466. strncpy(Name, "LOADX16", 19);
  467. break;
  468. }
  469. case 0x03C: /* LOADXU16 */
  470. {
  471. strncpy(Name, "LOADXU16", 19);
  472. break;
  473. }
  474. case 0x03D: /* LOADX32 */
  475. {
  476. strncpy(Name, "LOADX32", 19);
  477. break;
  478. }
  479. case 0x03E: /* LOADXU32 */
  480. {
  481. strncpy(Name, "LOADXU32", 19);
  482. break;
  483. }
  484. case 0x048: /* STOREX */
  485. {
  486. strncpy(Name, "STOREX", 19);
  487. break;
  488. }
  489. case 0x049: /* STOREX8 */
  490. {
  491. strncpy(Name, "STOREX8", 19);
  492. break;
  493. }
  494. case 0x04A: /* STOREX16 */
  495. {
  496. strncpy(Name, "STOREX16", 19);
  497. break;
  498. }
  499. case 0x04B: /* STOREX32 */
  500. {
  501. strncpy(Name, "STOREX32", 19);
  502. break;
  503. }
  504. case 0x050: /* CMPJUMP.G */
  505. {
  506. strncpy(Name, "CMPJUMP.G", 19);
  507. break;
  508. }
  509. case 0x051: /* CMPJUMP.GE */
  510. {
  511. strncpy(Name, "CMPJUMP.GE", 19);
  512. break;
  513. }
  514. case 0x052: /* CMPJUMP.E */
  515. {
  516. strncpy(Name, "CMPJUMP.E", 19);
  517. break;
  518. }
  519. case 0x053: /* CMPJUMP.NE */
  520. {
  521. strncpy(Name, "CMPJUMP.NE", 19);
  522. break;
  523. }
  524. case 0x054: /* CMPJUMP.LE */
  525. {
  526. strncpy(Name, "CMPJUMP.LE", 19);
  527. break;
  528. }
  529. case 0x055: /* CMPJUMP.L */
  530. {
  531. strncpy(Name, "CMPJUMP.L", 19);
  532. break;
  533. }
  534. case 0x060: /* CMPJUMPU.G */
  535. {
  536. strncpy(Name, "CMPJUMPU.G", 19);
  537. break;
  538. }
  539. case 0x061: /* CMPJUMPU.GE */
  540. {
  541. strncpy(Name, "CMPJUMPU.GE", 19);
  542. break;
  543. }
  544. case 0x064: /* CMPJUMPU.LE */
  545. {
  546. strncpy(Name, "CMPJUMPU.LE", 19);
  547. break;
  548. }
  549. case 0x065: /* CMPJUMPU.L */
  550. {
  551. strncpy(Name, "CMPJUMPU.L", 19);
  552. break;
  553. }
  554. default: /* Unknown 3OP*/
  555. {
  556. string_values(c, true);
  557. return;
  558. }
  559. }
  560. fprintf(stdout, "%s reg%u reg%u reg%u\t", Name, c->reg0, c->reg1, c->reg2);
  561. fprintf(stdout, "# %s\n", c->operation);
  562. }
  563. void decode_Integer_2OP(struct Instruction* c)
  564. {
  565. /* Parse Raw Data */
  566. c->raw_XOP = c->raw1*0x100 + c->raw2;
  567. c->XOP[0] = c->operation[2];
  568. c->XOP[1] = c->operation[3];
  569. c->XOP[2] = c->operation[4];
  570. c->XOP[3] = c->operation[5];
  571. c->raw_Immediate = 0;
  572. c->reg0 = c->raw3/16;
  573. c->reg1 = c->raw3%16;
  574. char Name[20] = "ILLEGAL_2OP";
  575. /* Convert to Human readable form */
  576. switch(c->raw_XOP)
  577. {
  578. case 0x0000: /* NEG */
  579. {
  580. strncpy(Name, "NEG", 19);
  581. break;
  582. }
  583. case 0x0001: /* ABS */
  584. {
  585. strncpy(Name, "ABS", 19);
  586. break;
  587. }
  588. case 0x0002: /* NABS */
  589. {
  590. strncpy(Name, "NABS", 19);
  591. break;
  592. }
  593. case 0x0003: /* SWAP */
  594. {
  595. strncpy(Name, "SWAP", 19);
  596. break;
  597. }
  598. case 0x0004: /* COPY */
  599. {
  600. strncpy(Name, "COPY", 19);
  601. break;
  602. }
  603. case 0x0005: /* MOVE */
  604. {
  605. strncpy(Name, "MOVE", 19);
  606. break;
  607. }
  608. case 0x0006: /* NOT */
  609. {
  610. strncpy(Name, "NOT", 19);
  611. break;
  612. }
  613. case 0x0100: /* BRANCH */
  614. {
  615. strncpy(Name, "BRANCH", 19);
  616. break;
  617. }
  618. case 0x0101: /* CALL */
  619. {
  620. strncpy(Name, "CALL", 19);
  621. break;
  622. }
  623. case 0x0200: /* PUSHR */
  624. {
  625. strncpy(Name, "PUSHR", 19);
  626. break;
  627. }
  628. case 0x0201: /* PUSH8 */
  629. {
  630. strncpy(Name, "PUSH8", 19);
  631. break;
  632. }
  633. case 0x0202: /* PUSH16 */
  634. {
  635. strncpy(Name, "PUSH16", 19);
  636. break;
  637. }
  638. case 0x0203: /* PUSH32 */
  639. {
  640. strncpy(Name, "PUSH32", 19);
  641. break;
  642. }
  643. case 0x0280: /* POPR */
  644. {
  645. strncpy(Name, "POPR", 19);
  646. break;
  647. }
  648. case 0x0281: /* POP8 */
  649. {
  650. strncpy(Name, "POP8", 19);
  651. break;
  652. }
  653. case 0x0282: /* POPU8 */
  654. {
  655. strncpy(Name, "POPU8", 19);
  656. break;
  657. }
  658. case 0x0283: /* POP16 */
  659. {
  660. strncpy(Name, "POP16", 19);
  661. break;
  662. }
  663. case 0x0284: /* POPU16 */
  664. {
  665. strncpy(Name, "POPU16", 19);
  666. break;
  667. }
  668. case 0x0285: /* POP32 */
  669. {
  670. strncpy(Name, "POP32", 19);
  671. break;
  672. }
  673. case 0x0286: /* POPU32 */
  674. {
  675. strncpy(Name, "POPU32", 19);
  676. break;
  677. }
  678. case 0x0300: /* CMPSKIP.G */
  679. {
  680. strncpy(Name, "CMPSKIP.G", 19);
  681. break;
  682. }
  683. case 0x0301: /* CMPSKIP.GE */
  684. {
  685. strncpy(Name, "CMPSKIP.GE", 19);
  686. break;
  687. }
  688. case 0x0302: /* CMPSKIP.E */
  689. {
  690. strncpy(Name, "CMPSKIP.E", 19);
  691. break;
  692. }
  693. case 0x0303: /* CMPSKIP.NE */
  694. {
  695. strncpy(Name, "CMPSKIP.NE", 19);
  696. break;
  697. }
  698. case 0x0304: /* CMPSKIP.LE */
  699. {
  700. strncpy(Name, "CMPSKIP.LE", 19);
  701. break;
  702. }
  703. case 0x0305: /* CMPSKIP.L */
  704. {
  705. strncpy(Name, "CMPSKIP.L", 19);
  706. break;
  707. }
  708. case 0x0380: /* CMPSKIPU.G */
  709. {
  710. strncpy(Name, "CMPSKIPU.G", 19);
  711. break;
  712. }
  713. case 0x0381: /* CMPSKIPU.GE */
  714. {
  715. strncpy(Name, "CMPSKIPU.GE", 19);
  716. break;
  717. }
  718. case 0x0384: /* CMPSKIPU.LE */
  719. {
  720. strncpy(Name, "CMPSKIPU.LE", 19);
  721. break;
  722. }
  723. case 0x0385: /* CMPSKIPU.L */
  724. {
  725. strncpy(Name, "CMPSKIPU.L", 19);
  726. break;
  727. }
  728. default: /* Unknown 2OP*/
  729. {
  730. string_values(c, true);
  731. return;
  732. }
  733. }
  734. fprintf(stdout, "%s reg%u reg%u\t", Name, c->reg0, c->reg1);
  735. fprintf(stdout, "# %s\n", c->operation);
  736. }
  737. void decode_1OP(struct Instruction* c)
  738. {
  739. /* Parse Raw Data */
  740. c->raw_XOP = c->raw1*0x1000 + c->raw2*0x10 + c->raw3/16;
  741. c->XOP[0] = c->operation[2];
  742. c->XOP[1] = c->operation[3];
  743. c->XOP[2] = c->operation[4];
  744. c->XOP[3] = c->operation[5];
  745. c->XOP[4] = c->operation[6];
  746. c->raw_Immediate = 0;
  747. c->reg0 = c->raw3%16;
  748. char Name[20] = "ILLEGAL_1OP";
  749. /* Convert to Human readable form */
  750. switch(c->raw_XOP)
  751. {
  752. case 0x00000: /* READPC */
  753. {
  754. strncpy(Name, "READPC", 19);
  755. break;
  756. }
  757. case 0x00001: /* READSCID */
  758. {
  759. strncpy(Name, "READSCID", 19);
  760. break;
  761. }
  762. case 0x00002: /* FALSE */
  763. {
  764. strncpy(Name, "FALSE", 19);
  765. break;
  766. }
  767. case 0x00003: /* TRUE */
  768. {
  769. strncpy(Name, "TRUE", 19);
  770. break;
  771. }
  772. case 0x01000: /* JSR_COROUTINE */
  773. {
  774. strncpy(Name, "JSR_COROUTINE", 19);
  775. break;
  776. }
  777. case 0x01001: /* RET */
  778. {
  779. strncpy(Name, "RET", 19);
  780. break;
  781. }
  782. case 0x02000: /* PUSHPC */
  783. {
  784. strncpy(Name, "PUSHPC", 19);
  785. break;
  786. }
  787. case 0x02001: /* POPPC */
  788. {
  789. strncpy(Name, "POPPC", 19);
  790. break;
  791. }
  792. default: /* Unknown 1OP*/
  793. {
  794. string_values(c, true);
  795. return;
  796. }
  797. }
  798. fprintf(stdout, "%s reg%u\t", Name, c->reg0);
  799. fprintf(stdout, "# %s\n", c->operation);
  800. }
  801. void decode_0OP(struct Instruction* c)
  802. {
  803. /* Parse Raw Data*/
  804. uint32_t FULL_OP;
  805. FULL_OP = c->raw0*0x1000000 + c->raw1*0x10000 + c->raw2*0x100 + c->raw3;
  806. char Name[20] = "ILLEGAL_0OP";
  807. /* Convert to Human readable form */
  808. switch(FULL_OP)
  809. {
  810. case 0x00000000: /* NOP */
  811. {
  812. strncpy(Name, "NOP", 19);
  813. break;
  814. }
  815. case 0x00000001 ... 0x00FFFFFF: /* IMPROPER_NOP */
  816. {
  817. strncpy(Name, "IMPROPER_NOP", 19);
  818. break;
  819. }
  820. case 0xFF000000 ... 0xFFFFFFFE: /* IMPROPER_HALT */
  821. {
  822. strncpy(Name, "IMPROPER_HALT", 19);
  823. break;
  824. }
  825. case 0xFFFFFFFF: /* HALT */
  826. {
  827. strncpy(Name, "HALT", 19);
  828. break;
  829. }
  830. default: /* Unknown 1OP*/
  831. {
  832. string_values(c, true);
  833. return;
  834. }
  835. }
  836. fprintf(stdout, "%s\t", Name);
  837. fprintf(stdout, "# %s\n", c->operation);
  838. }
  839. void decode_Integer_2OPI(struct Instruction* c)
  840. {
  841. /* Get Immediate pieces */
  842. c->raw_Immediate = fgetc(binary_file);
  843. int a = fgetc(binary_file);
  844. /* Unpack immediate */
  845. unpack_byte(c->raw_Immediate, &(c->operation[8]));
  846. unpack_byte(a, &(c->operation[10]));
  847. /* Process registers and immediate */
  848. c->raw_Immediate = c->raw_Immediate * 0x100 + a;
  849. c->reg0 = c->raw3/16;
  850. c->reg1 = c->raw3%16;
  851. char Name[20] = "ILLEGAL_2OPI";
  852. if(c->raw1 != 0) goto broken_2OPI;
  853. /* Convert to Human readable form */
  854. switch(c->raw2)
  855. {
  856. case 0x0E: /* ADDI */
  857. {
  858. strncpy(Name, "ADDI", 19);
  859. break;
  860. }
  861. case 0x0F: /* ADDUI */
  862. {
  863. strncpy(Name, "ADDUI", 19);
  864. break;
  865. }
  866. case 0x10: /* SUBI */
  867. {
  868. strncpy(Name, "SUBI", 19);
  869. break;
  870. }
  871. case 0x11: /* SUBUI */
  872. {
  873. strncpy(Name, "SUBUI", 19);
  874. break;
  875. }
  876. case 0x12: /* CMPI */
  877. {
  878. strncpy(Name, "CMPI", 19);
  879. break;
  880. }
  881. case 0x13: /* LOAD */
  882. {
  883. strncpy(Name, "LOAD", 19);
  884. break;
  885. }
  886. case 0x14: /* LOAD8 */
  887. {
  888. strncpy(Name, "LOAD8", 19);
  889. break;
  890. }
  891. case 0x15: /* LOADU8 */
  892. {
  893. strncpy(Name, "LOADU8", 19);
  894. break;
  895. }
  896. case 0x16: /* LOAD16 */
  897. {
  898. strncpy(Name, "LOAD16", 19);
  899. break;
  900. }
  901. case 0x17: /* LOADU16 */
  902. {
  903. strncpy(Name, "LOADU16", 19);
  904. break;
  905. }
  906. case 0x18: /* LOAD32 */
  907. {
  908. strncpy(Name, "LOAD32", 19);
  909. break;
  910. }
  911. case 0x19: /* LOADU32 */
  912. {
  913. strncpy(Name, "LOADU32", 19);
  914. break;
  915. }
  916. case 0x1F: /* CMPUI */
  917. {
  918. strncpy(Name, "CMPUI", 19);
  919. break;
  920. }
  921. case 0x20: /* STORE */
  922. {
  923. strncpy(Name, "STORE", 19);
  924. break;
  925. }
  926. case 0x21: /* STORE8 */
  927. {
  928. strncpy(Name, "STORE8", 19);
  929. break;
  930. }
  931. case 0x22: /* STORE16 */
  932. {
  933. strncpy(Name, "STORE16", 19);
  934. break;
  935. }
  936. case 0x23: /* STORE32 */
  937. {
  938. strncpy(Name, "STORE32", 19);
  939. break;
  940. }
  941. case 0xB0: /* ANDI */
  942. {
  943. strncpy(Name, "ANDI", 19);
  944. break;
  945. }
  946. case 0xB1: /* ORI */
  947. {
  948. strncpy(Name, "ORI", 19);
  949. break;
  950. }
  951. case 0xB2: /* XORI */
  952. {
  953. strncpy(Name, "XORI", 19);
  954. break;
  955. }
  956. case 0xB3: /* NANDI */
  957. {
  958. strncpy(Name, "NANDI", 19);
  959. break;
  960. }
  961. case 0xB4: /* NORI */
  962. {
  963. strncpy(Name, "NORI", 19);
  964. break;
  965. }
  966. case 0xB5: /* XNORI */
  967. {
  968. strncpy(Name, "XNORI", 19);
  969. break;
  970. }
  971. case 0xC0: /* CMPJUMPI.G */
  972. {
  973. strncpy(Name, "CMPJUMPI.G", 19);
  974. break;
  975. }
  976. case 0xC1: /* CMPJUMPI.GE */
  977. {
  978. strncpy(Name, "CMPJUMPI.GE", 19);
  979. break;
  980. }
  981. case 0xC2: /* CMPJUMPI.E */
  982. {
  983. strncpy(Name, "CMPJUMPI.E", 19);
  984. break;
  985. }
  986. case 0xC3: /* CMPJUMPI.NE */
  987. {
  988. strncpy(Name, "CMPJUMPI.NE", 19);
  989. break;
  990. }
  991. case 0xC4: /* CMPJUMPI.LE */
  992. {
  993. strncpy(Name, "CMPJUMPI.LE", 19);
  994. break;
  995. }
  996. case 0xC5: /* CMPJUMPI.L */
  997. {
  998. strncpy(Name, "CMPJUMPI.L", 19);
  999. break;
  1000. }
  1001. case 0xD0: /* CMPJUMPUI.G */
  1002. {
  1003. strncpy(Name, "CMPJUMPUI.G", 19);
  1004. break;
  1005. }
  1006. case 0xD1: /* CMPJUMPUI.GE */
  1007. {
  1008. strncpy(Name, "CMPJUMPUI.GE", 19);
  1009. break;
  1010. }
  1011. case 0xD4: /* CMPJUMPUI.LE */
  1012. {
  1013. strncpy(Name, "CMPJUMPUI.LE", 19);
  1014. break;
  1015. }
  1016. case 0xD5: /* CMPJUMPUI.L */
  1017. {
  1018. strncpy(Name, "CMPJUMPUI.L", 19);
  1019. break;
  1020. }
  1021. default: /* Unknown 2OPI*/
  1022. {
  1023. broken_2OPI:
  1024. string_values(c, true);
  1025. return;
  1026. }
  1027. }
  1028. fprintf(stdout, "%s reg%u reg%u 0x%x\t", Name, c->reg0, c->reg1, c->raw_Immediate);
  1029. fprintf(stdout, "# %s\n", c->operation);
  1030. }
  1031. void decode_1OPI(struct Instruction* c)
  1032. {
  1033. /* Get Immediate pieces */
  1034. c->raw_Immediate = fgetc(binary_file);
  1035. int a = fgetc(binary_file);
  1036. /* Unpack immediate */
  1037. unpack_byte(c->raw_Immediate, &(c->operation[8]));
  1038. unpack_byte(a, &(c->operation[10]));
  1039. /* Parse Raw Data */
  1040. c->raw_Immediate = c->raw_Immediate*0x100 + a;
  1041. c->raw_XOP = c->raw3/16;
  1042. c->reg0 = c->raw3%16;
  1043. char Name[20] = "ILLEGAL_1OPI";
  1044. uint32_t Opcode = (c->raw2 * 16) + c->raw_XOP;
  1045. if(0 != c->raw1) goto Broken_1OPI;
  1046. /* Convert to Human readable form */
  1047. switch(Opcode)
  1048. {
  1049. case 0x2C0: /* JUMP.C */
  1050. {
  1051. strncpy(Name, "JUMP.C", 19);
  1052. break;
  1053. }
  1054. case 0x2C1: /* JUMP.B */
  1055. {
  1056. strncpy(Name, "JUMP.B", 19);
  1057. break;
  1058. }
  1059. case 0x2C2: /* JUMP.O */
  1060. {
  1061. strncpy(Name, "JUMP.O", 19);
  1062. break;
  1063. }
  1064. case 0x2C3: /* JUMP.G */
  1065. {
  1066. strncpy(Name, "JUMP.G", 19);
  1067. break;
  1068. }
  1069. case 0x2C4: /* JUMP.GE */
  1070. {
  1071. strncpy(Name, "JUMP.GE", 19);
  1072. break;
  1073. }
  1074. case 0x2C5: /* JUMP.E */
  1075. {
  1076. strncpy(Name, "JUMP.E", 19);
  1077. break;
  1078. }
  1079. case 0x2C6: /* JUMP.NE */
  1080. {
  1081. strncpy(Name, "JUMP.NE", 19);
  1082. break;
  1083. }
  1084. case 0x2C7: /* JUMP.LE */
  1085. {
  1086. strncpy(Name, "JUMP.LE", 19);
  1087. break;
  1088. }
  1089. case 0x2C8: /* JUMP.L */
  1090. {
  1091. strncpy(Name, "JUMP.L", 19);
  1092. break;
  1093. }
  1094. case 0x2C9: /* JUMP.Z */
  1095. {
  1096. strncpy(Name, "JUMP.Z", 19);
  1097. break;
  1098. }
  1099. case 0x2CA: /* JUMP.NZ */
  1100. {
  1101. strncpy(Name, "JUMP.NZ", 19);
  1102. break;
  1103. }
  1104. case 0x2CB: /* JUMP.P */
  1105. {
  1106. strncpy(Name, "JUMP.P", 19);
  1107. break;
  1108. }
  1109. case 0x2CC: /* JUMP.NP */
  1110. {
  1111. strncpy(Name, "JUMP.NP", 19);
  1112. break;
  1113. }
  1114. case 0x2D0: /* CALLI */
  1115. {
  1116. strncpy(Name, "CALLI", 19);
  1117. break;
  1118. }
  1119. case 0x2D1: /* LOADI */
  1120. {
  1121. strncpy(Name, "LOADI", 19);
  1122. break;
  1123. }
  1124. case 0x2D2: /* LOADUI*/
  1125. {
  1126. strncpy(Name, "LOADUI", 19);
  1127. break;
  1128. }
  1129. case 0x2D3: /* SALI */
  1130. {
  1131. strncpy(Name, "SALI", 19);
  1132. break;
  1133. }
  1134. case 0x2D4: /* SARI */
  1135. {
  1136. strncpy(Name, "SARI", 19);
  1137. break;
  1138. }
  1139. case 0x2D5: /* SL0I */
  1140. {
  1141. strncpy(Name, "SL0I", 19);
  1142. break;
  1143. }
  1144. case 0x2D6: /* SR0I */
  1145. {
  1146. strncpy(Name, "SR0I", 19);
  1147. break;
  1148. }
  1149. case 0x2D7: /* SL1I */
  1150. {
  1151. strncpy(Name, "SL1I", 19);
  1152. break;
  1153. }
  1154. case 0x2D8: /* SR1I */
  1155. {
  1156. strncpy(Name, "SR1I", 19);
  1157. break;
  1158. }
  1159. case 0x2E0: /* LOADR */
  1160. {
  1161. strncpy(Name, "LOADR", 19);
  1162. break;
  1163. }
  1164. case 0x2E1: /* LOADR8 */
  1165. {
  1166. strncpy(Name, "LOADR8", 19);
  1167. break;
  1168. }
  1169. case 0x2E2: /* LOADRU8 */
  1170. {
  1171. strncpy(Name, "LOADRU8", 19);
  1172. break;
  1173. }
  1174. case 0x2E3: /* LOADR16 */
  1175. {
  1176. strncpy(Name, "LOADR16", 19);
  1177. break;
  1178. }
  1179. case 0x2E4: /* LOADRU16 */
  1180. {
  1181. strncpy(Name, "LOADRU16", 19);
  1182. break;
  1183. }
  1184. case 0x2E5: /* LOADR32 */
  1185. {
  1186. strncpy(Name, "LOADR32", 19);
  1187. break;
  1188. }
  1189. case 0x2E6: /* LOADRU32 */
  1190. {
  1191. strncpy(Name, "LOADRU32", 19);
  1192. break;
  1193. }
  1194. case 0x2F0: /* STORER */
  1195. {
  1196. strncpy(Name, "STORER", 19);
  1197. break;
  1198. }
  1199. case 0x2F1: /* STORER8 */
  1200. {
  1201. strncpy(Name, "STORER8", 19);
  1202. break;
  1203. }
  1204. case 0x2F2: /* STORER16 */
  1205. {
  1206. strncpy(Name, "STORER16", 19);
  1207. break;
  1208. }
  1209. case 0x2F3: /* STORER32 */
  1210. {
  1211. strncpy(Name, "STORER32", 19);
  1212. break;
  1213. }
  1214. case 0xA00: /* CMPSKIPI.G */
  1215. {
  1216. strncpy(Name, "CMPSKIPI.G", 19);
  1217. break;
  1218. }
  1219. case 0xA01: /* CMPSKIPI.GE */
  1220. {
  1221. strncpy(Name, "CMPSKIPI.GE", 19);
  1222. break;
  1223. }
  1224. case 0xA02: /* CMPSKIPI.E */
  1225. {
  1226. strncpy(Name, "CMPSKIPI.E", 19);
  1227. break;
  1228. }
  1229. case 0xA03: /* CMPSKIPI.NE */
  1230. {
  1231. strncpy(Name, "CMPSKIPI.NE", 19);
  1232. break;
  1233. }
  1234. case 0xA04: /* CMPSKIPI.LE */
  1235. {
  1236. strncpy(Name, "CMPSKIPI.LE", 19);
  1237. break;
  1238. }
  1239. case 0xA05: /* CMPSKIPI.L */
  1240. {
  1241. strncpy(Name, "CMPSKIPI.L", 19);
  1242. break;
  1243. }
  1244. case 0xA10: /* CMPSKIPUI.G */
  1245. {
  1246. strncpy(Name, "CMPSKIPUI.G", 19);
  1247. break;
  1248. }
  1249. case 0xA11: /* CMPSKIPUI.GE */
  1250. {
  1251. strncpy(Name, "CMPSKIPUI.GE", 19);
  1252. break;
  1253. }
  1254. case 0xA14: /* CMPSKIPUI.LE */
  1255. {
  1256. strncpy(Name, "CMPSKIPUI.LE", 19);
  1257. break;
  1258. }
  1259. case 0xA15: /* CMPSKIPUI.L */
  1260. {
  1261. strncpy(Name, "CMPSKIPUI.L", 19);
  1262. break;
  1263. }
  1264. default: /* Unknown 1OPI*/
  1265. {
  1266. Broken_1OPI:
  1267. string_values(c, true);
  1268. return;
  1269. }
  1270. }
  1271. fprintf(stdout, "%s reg%u %d\t", Name, c->reg0, c->raw_Immediate);
  1272. fprintf(stdout, "# %s\n", c->operation);
  1273. }
  1274. void decode_0OPI(struct Instruction* c)
  1275. {
  1276. /* Parse Raw Data */
  1277. c->raw_Immediate = c->raw2*0x100 + c->raw3;
  1278. c->Immediate[0] = c->operation[4];
  1279. c->Immediate[1] = c->operation[5];
  1280. c->Immediate[2] = c->operation[6];
  1281. c->Immediate[3] = c->operation[7];
  1282. c->HAL_CODE = 0;
  1283. c->raw_XOP = c->raw1;
  1284. c->XOP[0] = c->operation[2];
  1285. c->XOP[1] = c->operation[3];
  1286. char Name[20] = "ILLEGAL_0OPI";
  1287. /* Convert to Human readable form */
  1288. switch(c->raw_XOP)
  1289. {
  1290. case 0x00: /* JUMP */
  1291. {
  1292. strncpy(Name, "JUMP", 19);
  1293. break;
  1294. }
  1295. default: /* Unknown 1OPI*/
  1296. {
  1297. string_values(c, true);
  1298. return;
  1299. }
  1300. }
  1301. fprintf(stdout, "%s %d\t# %s\n", Name, c->raw_Immediate, c->operation);
  1302. if(4 == c->raw_Immediate) print_address(c);
  1303. }
  1304. void decode_HALCODE(struct Instruction* c)
  1305. {
  1306. /* Parse Raw Data */
  1307. c->HAL_CODE = c->raw1*0x10000 + c->raw2*0x100 + c->raw3;
  1308. char Name[20] = "ILLEGAL_HALCODE";
  1309. /* Convert to Human readable form */
  1310. switch(c->HAL_CODE)
  1311. {
  1312. case 0x000002: /* fopen */
  1313. {
  1314. strncpy(Name, "FOPEN", 19);
  1315. break;
  1316. }
  1317. case 0x000003: /* fclose */
  1318. {
  1319. strncpy(Name, "FCLOSE", 19);
  1320. break;
  1321. }
  1322. case 0x000008: /* fseek */
  1323. {
  1324. strncpy(Name, "FSEEK", 19);
  1325. break;
  1326. }
  1327. case 0x00003C: /* EXIT */
  1328. {
  1329. strncpy(Name, "EXIT", 19);
  1330. break;
  1331. }
  1332. case 0x00003F: /* UNAME */
  1333. {
  1334. strncpy(Name, "UNAME", 19);
  1335. break;
  1336. }
  1337. case 0x00005A: /* CHMOD */
  1338. {
  1339. strncpy(Name, "CHMOD", 19);
  1340. break;
  1341. }
  1342. case 0x100000: /* FOPEN_READ */
  1343. {
  1344. strncpy(Name, "FOPEN_READ", 19);
  1345. break;
  1346. }
  1347. case 0x100001: /* FOPEN_WRITE */
  1348. {
  1349. strncpy(Name, "FOPEN_WRITE", 19);
  1350. break;
  1351. }
  1352. case 0x100002: /* FCLOSE */
  1353. {
  1354. strncpy(Name, "FCLOSE", 19);
  1355. break;
  1356. }
  1357. case 0x100003: /* REWIND */
  1358. {
  1359. strncpy(Name, "REWIND", 19);
  1360. break;
  1361. }
  1362. case 0x100004: /* FSEEK */
  1363. {
  1364. strncpy(Name, "FSEEK", 19);
  1365. break;
  1366. }
  1367. case 0x100100: /* FGETC */
  1368. {
  1369. strncpy(Name, "FGETC", 19);
  1370. break;
  1371. }
  1372. case 0x100200: /* FPUTC */
  1373. {
  1374. strncpy(Name, "FPUTC", 19);
  1375. break;
  1376. }
  1377. case 0x110000: /* HAL_MEM */
  1378. {
  1379. strncpy(Name, "HAL_MEM", 19);
  1380. break;
  1381. }
  1382. default: /* Unknown HALCODE*/
  1383. {
  1384. string_values(c, true);
  1385. return;
  1386. }
  1387. }
  1388. fprintf(stdout, "%s\t", Name);
  1389. fprintf(stdout, "# %s\n", c->operation);
  1390. }
  1391. void eval_instruction(struct Instruction* c)
  1392. {
  1393. fprintf(stdout,"%08X\t", address);
  1394. switch(c->raw0)
  1395. {
  1396. case 0x01: /* Integer 4OP */
  1397. {
  1398. decode_Integer_4OP(c);
  1399. address = address + 4;
  1400. break;
  1401. }
  1402. case 0x05: /* Integer 3OP */
  1403. {
  1404. decode_Integer_3OP(c);
  1405. address = address + 4;
  1406. break;
  1407. }
  1408. case 0x09: /* Integer 2OP */
  1409. {
  1410. decode_Integer_2OP(c);
  1411. address = address + 4;
  1412. break;
  1413. }
  1414. case 0x0D: /* 1OP */
  1415. {
  1416. decode_1OP(c);
  1417. address = address + 4;
  1418. break;
  1419. }
  1420. case 0x3C: /* Core 0OPI */
  1421. {
  1422. decode_0OPI(c);
  1423. address = address + 4;
  1424. break;
  1425. }
  1426. case 0x42: /* HALCODE */
  1427. {
  1428. decode_HALCODE(c);
  1429. address = address + 4;
  1430. break;
  1431. }
  1432. case 0xE1: /* 2OPI */
  1433. {
  1434. decode_Integer_2OPI(c);
  1435. address = address + 6;
  1436. break;
  1437. }
  1438. case 0xE0: /* 1OPI */
  1439. {
  1440. decode_1OPI(c);
  1441. address = address + 6;
  1442. break;
  1443. }
  1444. case 0x00: /* NOP */
  1445. case 0xFF: /* HALT */
  1446. {
  1447. decode_0OP(c);
  1448. address = address + 4;
  1449. break;
  1450. }
  1451. default: /* Not supported by this disassembler */
  1452. {
  1453. string_values(c, true);
  1454. address = address + 4;
  1455. return;
  1456. }
  1457. }
  1458. }
  1459. /* Standard C main program */
  1460. int main(int argc, char **argv)
  1461. {
  1462. /* Make sure we have a program tape to run */
  1463. if (argc < 2)
  1464. {
  1465. fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of program being disassembled\n", argv[0]);
  1466. return EXIT_FAILURE;
  1467. }
  1468. binary_file = fopen(argv[1], "r");
  1469. struct Instruction* current;
  1470. current = calloc(1, sizeof(struct Instruction));
  1471. address = 0;
  1472. int32_t byte;
  1473. byte = fgetc(binary_file);
  1474. ungetc(byte, binary_file);
  1475. while(EOF != byte)
  1476. {
  1477. read_instruction(current);
  1478. eval_instruction(current);
  1479. byte = fgetc(binary_file);
  1480. ungetc(byte, binary_file);
  1481. }
  1482. fclose(binary_file);
  1483. return EXIT_SUCCESS;
  1484. }