An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

409 lines
11 KiB

  1. /* Copyright (C) 2014-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include "disassembler.h"
  8. #include "disassembler/arguments.h"
  9. #include "disassembler/mnemonics.h"
  10. #include "disassembler/sizes.h"
  11. #include "mmu.h"
  12. #include "rom.h"
  13. #include "util.h"
  14. #include "version.h"
  15. #define HRULE \
  16. "----------------------------------------------------------------------------"
  17. #define NUM_BANKS(rom) \
  18. (((rom)->size + MMU_ROM_BANK_SIZE - 1) / MMU_ROM_BANK_SIZE)
  19. #define MAX_BYTES_PER_LINE 16
  20. /* Structs and things */
  21. typedef struct {
  22. size_t cap, len;
  23. char **lines;
  24. } Disassembly;
  25. typedef enum {
  26. DT_BINARY = 0,
  27. DT_CODE,
  28. DT_HEADER
  29. } DataType;
  30. typedef struct {
  31. size_t index;
  32. size_t size;
  33. int8_t slot;
  34. const uint8_t *data;
  35. DataType *types;
  36. } ROMBank;
  37. /*
  38. Format a sequence of bytes of a certain length as a pretty string.
  39. The result must be freed by the caller.
  40. */
  41. static char* format_bytestring(const uint8_t *bytes, size_t size)
  42. {
  43. // TODO: smarter alignment; pad to full len (then remove pad from TRACE())
  44. if (!size)
  45. return NULL;
  46. char *str = cr_malloc(sizeof(char) * (3 * size));
  47. size_t i;
  48. for (i = 0; i < size; i++) {
  49. snprintf(&str[3 * i], 3, "%02X", bytes[i]);
  50. str[3 * i + 2] = ' ';
  51. }
  52. str[3 * size - 1] = '\0';
  53. return str;
  54. }
  55. /*
  56. Free the given DisasInstr struct.
  57. */
  58. void disas_instr_free(DisasInstr *instr)
  59. {
  60. free(instr->bytestr);
  61. free(instr->line);
  62. free(instr);
  63. }
  64. /*
  65. Disassemble a single instruction starting at the given address.
  66. Return a dynamically allocated structure containing various interesting
  67. fields. This must be freed by the user with disas_instr_free().
  68. */
  69. DisasInstr* disassemble_instruction(const uint8_t *bytes)
  70. {
  71. size_t size = get_instr_size(bytes);
  72. char *bytestr = format_bytestring(bytes, size);
  73. char *mnemonic = decode_mnemonic(bytes);
  74. char *args = decode_arguments(bytes);
  75. char *line;
  76. if (args) {
  77. line = cr_malloc(strlen(mnemonic) + strlen(args) + 2);
  78. sprintf(line, "%s\t%s", mnemonic, args);
  79. free(args);
  80. } else {
  81. line = cr_strdup(mnemonic);
  82. }
  83. DisasInstr *instr = cr_malloc(sizeof(DisasInstr));
  84. instr->size = size;
  85. instr->bytestr = bytestr;
  86. instr->line = line;
  87. return instr;
  88. }
  89. /*
  90. Append a line to the end of a disassembly.
  91. */
  92. static void write_line(Disassembly *dis, char *line)
  93. {
  94. dis->lines[dis->len++] = line;
  95. if (dis->len >= dis->cap) {
  96. dis->cap *= 2;
  97. dis->lines = cr_realloc(dis->lines, sizeof(char*) * dis->cap);
  98. }
  99. }
  100. /*
  101. Macro that wraps write_line() in a printf-like interface.
  102. */
  103. #define WRITE_LINE_(dis, fmt, ...) \
  104. do { \
  105. char *tmp_buffer_; \
  106. if (asprintf(&tmp_buffer_, fmt "\n", __VA_ARGS__) < 0) \
  107. OUT_OF_MEMORY() \
  108. write_line(dis, tmp_buffer_); \
  109. } while(0);
  110. #define WRITE_LINE(dis, ...) WRITE_LINE_(dis, __VA_ARGS__, NULL)
  111. /*
  112. Write some metadata comments to the top of the disassembly.
  113. */
  114. static void write_metadata(Disassembly *dis, const ROM *rom)
  115. {
  116. time_t t;
  117. struct tm *tm_info;
  118. char buf[64];
  119. time(&t);
  120. tm_info = localtime(&t);
  121. strftime(buf, sizeof buf, "on %a %b %d, %Y at %H:%M:%S", tm_info);
  122. WRITE_LINE(dis, ";; GAME GEAR ROM DISASSEMBLY")
  123. WRITE_LINE(dis, ";; File: %s", rom->name)
  124. WRITE_LINE(dis, ";; Generated %s by crater %s", buf, CRATER_VERSION)
  125. WRITE_LINE(dis, ";; " HRULE)
  126. WRITE_LINE(dis, "")
  127. }
  128. /*
  129. Given a size, fill 'output' with a pretty string. Modified from rom.c.
  130. */
  131. static char* size_to_string(char *output, size_t size)
  132. {
  133. if (size >= (1 << 20))
  134. sprintf(output, "%zu MB", size >> 20);
  135. else
  136. sprintf(output, "%zu KB", size >> 10);
  137. return output;
  138. }
  139. /*
  140. Extract appropriate assembler directives from a ROM's header.
  141. */
  142. static void disassemble_header(Disassembly *dis, const ROM *rom)
  143. {
  144. char buf[64];
  145. const char *size, *product, *region, *declsize;
  146. DEBUG("Disassembling header")
  147. size = size_to_string(buf, rom->size);
  148. product = rom_product(rom);
  149. region = rom_region(rom);
  150. declsize = size_to_string(buf, size_code_to_bytes(rom->declared_size));
  151. WRITE_LINE(dis, ".rom_size\t\"%s\"%s\t; $%zX bytes in %zu banks",
  152. size, strlen(size) < 6 ? "\t" : "", rom->size, NUM_BANKS(rom))
  153. WRITE_LINE(dis, ".rom_header\t$%04X",
  154. rom->header_location)
  155. WRITE_LINE(dis, ".rom_checksum\t%s",
  156. (rom->reported_checksum == rom->expected_checksum) ? "on" : "off")
  157. WRITE_LINE(dis, ".rom_product\t%u\t\t; %s",
  158. rom->product_code, product ? product : "(unknown)")
  159. WRITE_LINE(dis, ".rom_version\t%u",
  160. rom->version)
  161. WRITE_LINE(dis, ".rom_region\t%u\t\t; %s",
  162. rom->region_code, region ? region : "(unknown)")
  163. WRITE_LINE(dis, ".rom_declsize\t$%X\t\t; %s",
  164. rom->declared_size, declsize)
  165. }
  166. /*
  167. Initialize and return an array of ROMBank objects for the given ROM.
  168. */
  169. static ROMBank* init_banks(const ROM *rom)
  170. {
  171. size_t nbanks = NUM_BANKS(rom), i;
  172. ROMBank *banks = cr_malloc(sizeof(ROMBank) * (nbanks + 1));
  173. DataType *types = cr_calloc(sizeof(DataType), rom->size);
  174. for (i = 0; i < nbanks; i++) {
  175. banks[i].index = i;
  176. if (i == nbanks - 1 && rom->size % MMU_ROM_BANK_SIZE)
  177. banks[i].size = rom->size % MMU_ROM_BANK_SIZE;
  178. else
  179. banks[i].size = MMU_ROM_BANK_SIZE;
  180. banks[i].slot = -1;
  181. banks[i].data = rom->data + (i * MMU_ROM_BANK_SIZE);
  182. banks[i].types = types + (i * MMU_ROM_BANK_SIZE);
  183. }
  184. banks[nbanks].data = NULL; // Sentinel
  185. return banks;
  186. }
  187. /*
  188. Deallocate the given array of ROM banks.
  189. */
  190. static void free_banks(ROMBank *banks)
  191. {
  192. free(banks[0].types);
  193. free(banks);
  194. }
  195. /*
  196. Return the offset in bytes of the first address in the given bank.
  197. */
  198. static size_t get_bank_offset(const ROMBank *bank)
  199. {
  200. return MMU_ROM_BANK_SIZE * ((bank->slot >= 0) ? bank->slot :
  201. (bank->index > 2) ? 2 : bank->index);
  202. }
  203. /*
  204. Mark the ROM's header as non-binary/non-code inside of the relevant bank.
  205. */
  206. static void mark_header(const ROM *rom, ROMBank *banks)
  207. {
  208. size_t i;
  209. for (i = 0; i < HEADER_SIZE; i++)
  210. banks[0].types[rom->header_location + i] = DT_HEADER;
  211. }
  212. /*
  213. Render a line of binary data within a block.
  214. */
  215. static void render_binary(Disassembly *dis, size_t *idx, const ROMBank *bank)
  216. {
  217. size_t span = 1, i;
  218. while (span < MAX_BYTES_PER_LINE && bank->types[*idx + span] == DT_BINARY)
  219. span++;
  220. char buf[4 * MAX_BYTES_PER_LINE + 1];
  221. for (i = 0; i < span; i++)
  222. sprintf(buf + 4 * i, "$%02X ", bank->data[*idx + i]);
  223. buf[4 * span - 1] = '\0';
  224. WRITE_LINE(dis, ".byte %s", buf)
  225. (*idx) += span;
  226. }
  227. /*
  228. Render a single instruction within a block.
  229. */
  230. static void render_code(Disassembly *dis, size_t *idx, const ROMBank *bank)
  231. {
  232. DisasInstr *instr = disassemble_instruction(bank->data + *idx);
  233. char padding[16], *split;
  234. if ((split = strchr(instr->line, '\t'))) {
  235. size_t tabs = (40 - (instr->line + strlen(instr->line) - split)) / 8;
  236. padding[tabs] = '\0';
  237. while (tabs-- > 0)
  238. padding[tabs] = '\t';
  239. } else {
  240. strcpy(padding, "\t\t\t\t\t");
  241. }
  242. WRITE_LINE(dis, "\t%s%s\t; $%04zX: %s",
  243. instr->line, padding, get_bank_offset(bank) + *idx, instr->bytestr)
  244. (*idx) += instr->size;
  245. disas_instr_free(instr);
  246. }
  247. /*
  248. Render fully analyzed banks into lines of disassembly.
  249. */
  250. static void render_banks(Disassembly *dis, const ROMBank *banks)
  251. {
  252. size_t bn = 0, idx;
  253. DEBUG("Rendering lines")
  254. while (banks[bn].data) {
  255. TRACE("Rendering bank 0x%02zX (0x%06zX-0x%06zX)", bn,
  256. bn * MMU_ROM_BANK_SIZE, bn * MMU_ROM_BANK_SIZE + banks[bn].size)
  257. WRITE_LINE(dis, "")
  258. WRITE_LINE(dis, ";; " HRULE)
  259. WRITE_LINE(dis, "")
  260. WRITE_LINE(dis, ".block $%02zX", bn)
  261. idx = 0;
  262. while (idx < banks[bn].size) {
  263. switch (banks[bn].types[idx]) {
  264. case DT_BINARY:
  265. render_binary(dis, &idx, &banks[bn]);
  266. break;
  267. case DT_CODE:
  268. render_code(dis, &idx, &banks[bn]);
  269. break;
  270. case DT_HEADER:
  271. idx += HEADER_SIZE;
  272. break;
  273. default:
  274. FATAL("invalid data type %d at addr 0x%06zX",
  275. banks[bn].types[idx], bn * MMU_ROM_BANK_SIZE + idx)
  276. }
  277. }
  278. bn++;
  279. }
  280. }
  281. /*
  282. Disassemble a ROM into an array of strings, each storing one source line.
  283. Each line is newline-terminated. The array itself is terminated with a NULL
  284. element. Each line, and the overall array, must be free()d by the caller.
  285. */
  286. char** disassemble(const ROM *rom)
  287. {
  288. Disassembly dis = {.cap = 16, .len = 0};
  289. dis.lines = cr_malloc(sizeof(char*) * dis.cap);
  290. write_metadata(&dis, rom);
  291. disassemble_header(&dis, rom);
  292. ROMBank *banks = init_banks(rom);
  293. mark_header(rom, banks);
  294. // TODO: analyze(): set DT_CODE (future: make labels, slots) where appropriate
  295. for (size_t i = 0; i < 0x1000; i++)
  296. banks[0].types[i] = DT_CODE;
  297. render_banks(&dis, banks);
  298. free_banks(banks);
  299. write_line(&dis, NULL);
  300. return dis.lines;
  301. }
  302. /*
  303. Write a disassembly created by disassemble() to the given output file.
  304. Return whether the file was written successfully. This function frees the
  305. disassembly along the way.
  306. */
  307. static bool write_disassembly(const char *path, char **lines)
  308. {
  309. FILE *fp;
  310. char **itr = lines;
  311. if (!(fp = fopen(path, "w"))) {
  312. ERROR_ERRNO("couldn't open destination file")
  313. return false;
  314. }
  315. while (*itr) {
  316. if (!fwrite(*itr, strlen(*itr), 1, fp)) {
  317. fclose(fp);
  318. do free(*itr); while (*(++itr));
  319. ERROR_ERRNO("couldn't write to destination file")
  320. return false;
  321. }
  322. free(*itr);
  323. itr++;
  324. }
  325. fclose(fp);
  326. free(lines);
  327. return true;
  328. }
  329. /*
  330. Disassemble the binary file at the input path into z80 source code.
  331. Return true if the operation was a success and false if it was a failure.
  332. Errors are printed to STDOUT; if the operation was successful then nothing
  333. is printed.
  334. */
  335. bool disassemble_file(const char *src_path, const char *dst_path)
  336. {
  337. ROM *rom;
  338. const char *errmsg;
  339. char **lines;
  340. DEBUG("Disassembling: %s -> %s", src_path, dst_path)
  341. if ((errmsg = rom_open(&rom, src_path))) {
  342. ERROR("couldn't load ROM image '%s': %s", src_path, errmsg)
  343. return false;
  344. }
  345. lines = disassemble(rom);
  346. rom_close(rom);
  347. DEBUG("Writing output file")
  348. return write_disassembly(dst_path, lines);
  349. }