Advertisement
PT_

Untitled

PT_
Nov 3rd, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define ARG_NORM 0
  12. #define ARG_JR 1
  13. #define ARG_OFF 2
  14.  
  15. void print_hexadecimal(uint8_t, FILE*);
  16. char *address_string(int);
  17. char *byteToHexString(uint8_t);
  18.  
  19. int address = 0xD1A881;
  20. const char hexadecimals[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  21. const char *cc_table[] = {"nz", "z", "nc", "c", "po", "pe", "p", "m"};
  22. const char *suffixes[] = {".sis", ".lis", ".sil", ".lil"};
  23. char addressString[7];
  24. char hex[3] = {0, 0, 0};
  25.  
  26. typedef struct {
  27. char *opcodeName;
  28. char *opcodeOperand;
  29. uint8_t opcodeLength;
  30. uint8_t argType;
  31. } opcodes_t;
  32.  
  33. extern const opcodes_t opcodes[256];
  34.  
  35. typedef struct {
  36. char opcode[10];
  37. char operand[50];
  38. } disassembly;
  39.  
  40. disassembly disasm;
  41.  
  42. int main(int argc, char **argv) {
  43. uint8_t inputBytes[6];
  44. uint8_t inputWhichByte = 0;
  45. uint8_t whichSuffix = 0;
  46. uint8_t byte;
  47. uint8_t index;
  48. char* spaces = " ";
  49. int offset = 0x4C;
  50. int programSize;
  51. FILE *inPrgm;
  52. FILE *outPrgm;
  53.  
  54. if (argc != 3) {
  55. fprintf(stderr, "Error: invalid arguments\n");
  56. exit(1);
  57. }
  58.  
  59. inPrgm = fopen(argv[1], "r");
  60. outPrgm = fopen(argv[2], "w");
  61.  
  62. if (!inPrgm || !outPrgm) {
  63. fprintf(stderr, "Error: can't open input or output file\n");
  64. }
  65.  
  66. fseek(inPrgm, -2, SEEK_END);
  67. programSize = ftell(inPrgm);
  68. fseek(inPrgm, 0x4C, SEEK_SET);
  69. fprintf(outPrgm, "Disassembly of %s:\n\n", argv[1]);
  70.  
  71. while (offset < programSize) {
  72. fprintf(stderr, "%d - ", ftell(inPrgm));
  73. byte = fgetc(inPrgm);
  74. inputBytes[inputWhichByte++] = byte;
  75.  
  76. // Check suffixes
  77. if (byte == 0x40) {
  78. whichSuffix = 1;
  79. continue;
  80. } else if (byte == 0x49) {
  81. whichSuffix = 2;
  82. continue;
  83. } else if (byte == 0x52) {
  84. whichSuffix = 3;
  85. continue;
  86. } else if (byte == 0x5B) {
  87. whichSuffix = 4;
  88. continue;
  89. } else {
  90. whichSuffix = 0;
  91.  
  92. // Check 2-byte opcode
  93. if (byte == 0xCB) {
  94. inputWhichByte++;
  95. } else if (byte == 0xDD) {
  96. inputWhichByte++;
  97. } else if (byte == 0xED) {
  98. inputWhichByte++;
  99. } else if (byte == 0xFD) {
  100. inputWhichByte++;
  101. } else {
  102. // Single byte opcode
  103. strcpy(disasm.opcode, opcodes[byte].opcodeName);
  104. if (whichSuffix) {
  105. strcat(disasm.opcode, suffixes[whichSuffix - 1]);
  106. }
  107.  
  108. if (opcodes[byte].opcodeLength == 1) {
  109. if (opcodes[byte].argType == ARG_NORM) {
  110. sprintf(disasm.operand, opcodes[byte].opcodeOperand, byteToHexString(inputBytes[inputWhichByte++] = fgetc(inPrgm)));
  111. } else {
  112. char byte2 = fgetc(inPrgm);
  113.  
  114. inputBytes[inputWhichByte++] = (uint8_t)byte2;
  115. sprintf(disasm.operand, opcodes[byte].opcodeOperand, address_string(address + byte2 + 2));
  116. }
  117. } else if (opcodes[byte].opcodeLength == 3) {
  118. uint8_t byte2 = fgetc(inPrgm);
  119. uint8_t byte3 = fgetc(inPrgm);
  120. uint8_t byte4 = fgetc(inPrgm);
  121.  
  122. inputBytes[inputWhichByte++] = byte2;
  123. inputBytes[inputWhichByte++] = byte3;
  124. inputBytes[inputWhichByte++] = byte4;
  125. sprintf(disasm.operand, opcodes[byte].opcodeOperand, address_string(byte2 + (byte3 << 8) + (byte4 << 16)));
  126. } else {
  127. strcpy(disasm.operand, opcodes[byte].opcodeOperand);
  128. }
  129. }
  130. }
  131.  
  132. // Fix spacing of the opcode
  133. strcat(disasm.opcode, spaces + strlen(disasm.opcode));
  134.  
  135. // Print address to disassembly
  136. fprintf(outPrgm, address_string(address));
  137. fprintf(outPrgm, ": ");
  138.  
  139. // Print input hexadecimals to disassembly
  140. for (index = 0; index < inputWhichByte; index++) {
  141. print_hexadecimal(inputBytes[index], outPrgm);
  142. }
  143. for (index = 6; index > inputWhichByte; index--) {
  144. fprintf(outPrgm, " ");
  145. }
  146.  
  147. // Print opcode, suffix and operand to disassembly, and new line
  148. fprintf(outPrgm, "%s %s\n", disasm.opcode, disasm.operand);
  149. address += inputWhichByte;
  150. offset += inputWhichByte;
  151. inputWhichByte = 0;
  152. }
  153.  
  154. return 0;
  155. }
  156.  
  157. void print_hexadecimal(uint8_t byte, FILE *outPrgm) {
  158. fprintf(outPrgm, "%c%c", hexadecimals[byte >> 4], hexadecimals[byte & 0x0F]);
  159. }
  160.  
  161. char *address_string(int address) {
  162. uint8_t index;
  163.  
  164. for (index = 0; index < 6; index++) {
  165. addressString[index] = hexadecimals[(address >> (20 - 4 * index)) & 0x0F];
  166. }
  167.  
  168. return addressString;
  169. }
  170.  
  171. char *byteToHexString(uint8_t byte) {
  172. hex[0] = hexadecimals[byte >> 4];
  173. hex[1] = hexadecimals[byte & 0x0F];
  174.  
  175. return hex;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement