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.
 
 
 
 
 

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