Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #ifndef inc_koei_vmdisasm
  2. #define inc_koei_vmdisasm
  3.  
  4. #include <cstdint>
  5. #include <array>
  6. #include <map>
  7. #include <snes/assert.h>
  8. #include <snes/disasm.h>
  9.  
  10. namespace snes
  11. {
  12. namespace koei
  13. {
  14. using u08 = std::uint8_t;
  15. using i16 = std::int16_t;
  16. using u16 = std::uint16_t;
  17. using u32 = std::uint32_t;
  18.  
  19. enum class mode_t
  20. {
  21. null,
  22.  
  23. quick_frame_relative,
  24. quick_immediate,
  25.  
  26. near_frame_relative,
  27. far_frame_relative,
  28.  
  29. immediate,
  30. immediate_byte,
  31.  
  32. absolute,
  33. absolute_goto,
  34.  
  35. branch_forward,
  36. branch_back,
  37.  
  38. call,
  39. call_absolute,
  40. call_stack,
  41.  
  42. switch_c,
  43. switch_nc,
  44.  
  45. // 32-bit
  46. extended,
  47. };
  48.  
  49. struct opcode_data_t
  50. {
  51. opcode_data_t() : short_name(nullptr), long_name(nullptr), mode(mode_t::null) {}
  52. opcode_data_t(const char *sn, const char *ln, mode_t _mode = mode_t::null) : short_name(sn), long_name(ln), mode(_mode) {}
  53. const char *short_name;
  54. const char *long_name;
  55. mode_t mode;
  56. };
  57.  
  58. template <int N>
  59. struct opcode_database_t : std::array<opcode_data_t, N>
  60. {
  61. using db_populator_t = void(*)(opcode_database_t &db);
  62.  
  63. opcode_database_t(db_populator_t populator)
  64. {
  65. if (populator) {
  66. populator(*this);
  67. }
  68. }
  69.  
  70. void add_opcode_data(int index, const opcode_data_t &data)
  71. {
  72. ASSERT((*this)[index].short_name == nullptr);
  73. (*this)[index] = data;
  74. }
  75.  
  76. void add_opcode_data(int start, int end, const opcode_data_t &data)
  77. {
  78. for (int i = start; i <= end; i++) {
  79. ASSERT((*this)[i].short_name == nullptr);
  80. (*this)[i] = data;
  81. }
  82. }
  83. };
  84.  
  85. void vm_opcode_database_populate(koei::opcode_database_t<256> &db);
  86. void vm_opcode_database_long_populate(koei::opcode_database_t<48> &db);
  87.  
  88. extern opcode_database_t<256> opcode_db;
  89. extern opcode_database_t<48> opcode_db_long;
  90.  
  91. struct disasm_t
  92. {
  93. disasm_t() : zero(nullptr), opt(local_options), func_map(local_func_map), data_map(local_data_map)
  94. {
  95. }
  96.  
  97. disasm_t(const disasm_options_t &_opt, const addr_map_t &_func_map, const addr_map_t &_data_map) :
  98. zero(nullptr), opt(_opt), func_map(_func_map), data_map(_data_map)
  99. {
  100. }
  101.  
  102. void set_zero(const void *zero_ptr) { zero = (const u08 *)zero_ptr; }
  103. int disassemble(const u08 *mem, char *result);
  104. int disassemble_long(const u08 *mem, char *result);
  105.  
  106. struct vm_registers_t
  107. {
  108. vm_registers_t()
  109. {
  110. clear();
  111. }
  112. void clear()
  113. {
  114. SPl = 0;
  115. FPl = 0;
  116. PCl = 0;
  117. ll = 0;
  118. rl = 0;
  119. }
  120. union
  121. {
  122. // stack pointer
  123. u32 SPl;
  124. u16 SP;
  125. };
  126. union
  127. {
  128. // frame pointer
  129. u32 FPl;
  130. u16 FP;
  131. };
  132. union
  133. {
  134. // program counter
  135. u32 PCl;
  136. u16 PC;
  137. };
  138. // 32 bit (low / high)
  139. union
  140. {
  141. u32 ll;
  142. u16 l;
  143. };
  144. union
  145. {
  146. u32 rl;
  147. u16 r;
  148. };
  149. };
  150.  
  151. disasm_options_t local_options;
  152. const disasm_options_t &opt;
  153.  
  154. addr_map_t local_func_map;
  155. const addr_map_t &func_map;
  156.  
  157. addr_map_t local_data_map;
  158. const addr_map_t &data_map;
  159.  
  160. addr_map_t line_label;
  161.  
  162. vm_registers_t reg;
  163.  
  164. // pointer to 0x000000 rom address
  165. const u08 *zero;
  166.  
  167. int sval;
  168. int data;
  169. };
  170. }
  171. }
  172.  
  173. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement