An emulator, assembler, and disassembler for the Sega Game Gear
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

disassembler.c 2.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "disassembler.h"
  4. #include "disassembler/mnemonics.h"
  5. #include "disassembler/sizes.h"
  6. #include "util.h"
  7. /*
  8. Format a sequence of bytes of a certain length as a pretty string.
  9. The result must be freed by the caller.
  10. */
  11. static char* format_bytestring(const uint8_t *bytes, size_t size)
  12. {
  13. if (!size)
  14. return NULL;
  15. char *str = cr_malloc(sizeof(char) * (3 * size));
  16. size_t i;
  17. for (i = 0; i < size; i++) {
  18. snprintf(&str[3 * i], 3, "%02X", bytes[i]);
  19. str[3 * i + 2] = ' ';
  20. }
  21. str[3 * size - 1] = '\0';
  22. return str;
  23. }
  24. /*
  25. Extract the arguments for the given instruction.
  26. The return value must be free()d.
  27. */
  28. static char* decode_argument(const uint8_t *bytes)
  29. {
  30. // TODO
  31. (void) bytes;
  32. return NULL;
  33. }
  34. /*
  35. Free the given DisasInstr struct.
  36. */
  37. void disas_instr_free(DisasInstr *instr)
  38. {
  39. free(instr->bytestr);
  40. free(instr->line);
  41. free(instr);
  42. }
  43. /*
  44. Disassemble a single instruction starting at the given address.
  45. Return a dynamically allocated structure containing various interesting
  46. fields. This must be freed by the user with disas_instr_free().
  47. */
  48. DisasInstr* disassemble_instruction(const uint8_t *bytes)
  49. {
  50. size_t size = get_instr_size(bytes);
  51. char *bytestr = format_bytestring(bytes, size);
  52. char *mnemonic = decode_mnemonic(bytes);
  53. char *arg = decode_argument(bytes);
  54. char *line;
  55. if (arg) {
  56. line = cr_malloc(strlen(mnemonic) + strlen(arg) + 2);
  57. sprintf(line, "%s\t%s", mnemonic, arg);
  58. free(arg);
  59. } else {
  60. line = cr_strdup(mnemonic);
  61. }
  62. DisasInstr *instr = cr_malloc(sizeof(DisasInstr));
  63. instr->size = size;
  64. instr->bytestr = bytestr;
  65. instr->line = line;
  66. return instr;
  67. }
  68. /*
  69. Disassemble the binary file at the input path into z80 source code.
  70. Return true if the operation was a success and false if it was a failure.
  71. Errors are printed to STDOUT; if the operation was successful then nothing
  72. is printed.
  73. */
  74. bool disassemble_file(const char *src_path, const char *dst_path)
  75. {
  76. // TODO
  77. (void) src_path;
  78. (void) dst_path;
  79. return true;
  80. }