ff7_world_disassembler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "ff7_world_disassembler.h"
  2. #include "ff7_world_engine.h"
  3. #include "decompiler_engine.h"
  4. #include <boost/format.hpp>
  5. FF7::FF7WorldDisassembler::FF7WorldDisassembler(FF7WorldEngine*, InstVec &insts, int scriptNumber)
  6. : SimpleDisassembler(insts), mScriptNumber(scriptNumber)
  7. {
  8. }
  9. FF7::FF7WorldDisassembler::~FF7WorldDisassembler()
  10. {
  11. }
  12. /*
  13. 00000202: reset stack (0)
  14. 00000203: push byte from bank 0 897 (2)
  15. 00000205: jump if false 25 (0)
  16. 00000207: reset stack (0)
  17. 00000208: push byte from bank 0 897 (2)
  18. 0000020a: push byte from bank 0 897 (2)
  19. 0000020c: push constant 1 (2)
  20. 0000020e: push sub (2)
  21. 0000020f: write bank (0)
  22. 00000210: reset stack (0)
  23. 00000211: push byte from bank 0 897 (2)
  24. 00000213: push constant 0 (2)
  25. 00000215: push equal (2)
  26. 00000216: jump if false 25 (0)
  27. 00000218: unknown 320 (0)
  28. 00000219: return (0)
  29. if(!Read(897))
  30. {
  31. Write(897-1)
  32. if (!(Read(897) == 0))
  33. {
  34. unknown320()
  35. }
  36. }
  37. */
  38. void FF7::FF7WorldDisassembler::doDisassemble()
  39. {
  40. struct wmScriptData
  41. {
  42. uint16 mType = 0;
  43. uint16 mInstructionPointer = 0;
  44. };
  45. std::vector<wmScriptData> mData;
  46. mData.reserve(256);
  47. for (int i = 0; i < 256; i++)
  48. {
  49. wmScriptData tmp;
  50. tmp.mType = this->mStream->ReadU16();
  51. tmp.mInstructionPointer = this->mStream->ReadU16();
  52. mData.emplace_back(tmp);
  53. }
  54. int i = this->mScriptNumber;
  55. // for (int i = 1; i < 256; i++)
  56. {
  57. wmScriptData data = mData[i];
  58. this->mStream->Seek((256 * sizeof(int)) + (data.mInstructionPointer*sizeof(uint16)));
  59. this->_address = this->mStream->Position();
  60. bool end = false;
  61. // while (this->_f.pos() != (int)this->_f.size())
  62. for (;;)
  63. {
  64. uint32 full_opcode = 0;
  65. uint16 opcode = this->mStream->ReadU16();
  66. std::string opcodePrefix;
  67. if (end)
  68. {
  69. break;
  70. }
  71. if (opcode == 0x203)
  72. {
  73. end = true;
  74. }
  75. if (opcode >= 0x204 && opcode < 0x300)
  76. {
  77. this->_insts.push_back(new FF7WorldKernelCallInstruction());
  78. this->_insts.back()->_opcode = opcode;
  79. this->_insts.back()->_address = this->_address;
  80. this->_insts.back()->_stackChange = 0;
  81. this->_insts.back()->_name = std::string("function call");
  82. this->_insts.back()->_codeGenData = "";
  83. this->readParams(this->_insts.back(), "");
  84. }
  85. else
  86. {
  87. switch (opcode)
  88. {
  89. case 0x100:
  90. this->_insts.push_back(new FF7WorldNoOutputInstruction());
  91. this->_insts.back()->_opcode = 0x100;
  92. this->_insts.back()->_address = this->_address;
  93. this->_insts.back()->_stackChange = 0;
  94. this->_insts.back()->_name = std::string("reset stack");
  95. this->_insts.back()->_codeGenData = "";
  96. this->readParams(this->_insts.back(), "");
  97. break;
  98. OPCODE(0x201, "jump if false", FF7WorldCondJumpInstruction, 0, "w");
  99. OPCODE(0x203, "return", FF7WorldKernelCallInstruction, 0, "");
  100. OPCODE(0x200, "jump", FF7WorldUncondJumpInstruction, 0, "w");
  101. OPCODE(0x118, "push byte from bank 0", FF7WorldLoadBankInstruction, 2, "w");
  102. OPCODE(0x110, "push constant", FF7WorldLoadInstruction, 2, "w");
  103. OPCODE(0x119, "push byte from bank 1", FF7WorldLoadBankInstruction, 2, "w");
  104. OPCODE(0x11c, "push word from bank 0", FF7WorldLoadBankInstruction, 2, "w");
  105. OPCODE(0x114, "push bit from bank 0", FF7WorldLoadBankInstruction, 2, "w");
  106. OPCODE(0x11b, "push special", FF7WorldLoadBankInstruction, 2, "w");
  107. OPCODE(0x11f, "push special", FF7WorldLoadInstruction, 2, "w");
  108. OPCODE(0x117, "push special", FF7WorldLoadBankInstruction, 2, "w");
  109. OPCODE(0x0e0, "write bank", FF7WorldStoreInstruction, 0, "");
  110. OPCODE(0x019, "push distance from active entity to entity by model id", FF7WorldKernelCallInstruction, 2, "");
  111. OPCODE(0x018, "push distance from active entity to light", FF7WorldKernelCallInstruction, 2, "");
  112. OPCODE(0x061, "push greater", FF7SubStackInstruction, 2, "");
  113. OPCODE(0x0b0, "push logicand", FF7SubStackInstruction, 2, "");
  114. OPCODE(0x062, "push lessequal", FF7SubStackInstruction, 2, "");
  115. OPCODE(0x030, "push mul", FF7SubStackInstruction, 2, "");
  116. OPCODE(0x051, "push shr", FF7SubStackInstruction, 2, "");
  117. OPCODE(0x080, "push and", FF7SubStackInstruction, 2, "");
  118. OPCODE(0x0a0, "push or", FF7SubStackInstruction, 2, "");
  119. OPCODE(0x070, "push equal", BinaryEqualStackInstruction, 2, "");
  120. OPCODE(0x041, "push sub", FF7SubStackInstruction, 2, "");
  121. OPCODE(0x063, "push greaterequal", FF7SubStackInstruction, 2, "")
  122. OPCODE(0x017, "push logicnot", FF7SubStackInstruction, 2, "");
  123. OPCODE(0x060, "push less", FF7SubStackInstruction, 2, "");
  124. OPCODE(0x0c0, "push logicor", FF7SubStackInstruction, 2, "");
  125. OPCODE(0x040, "push add", FF7SubStackInstruction, 2, "");
  126. OPCODE(0x015, "push neg", FF7SubStackInstruction, 2, "");
  127. OPCODE(0x320, "unknown 320", FF7WorldKernelCallInstruction, 0, "");
  128. OPCODE(0x34a, "unknown 34a", FF7WorldKernelCallInstruction, 0, "");
  129. OPCODE(0x334, "unknown 334", FF7WorldKernelCallInstruction, 0, "");
  130. OPCODE(0x33a, "unknown 33a", FF7WorldKernelCallInstruction, 0, "");
  131. OPCODE(0x31c, "unknown 31c", FF7WorldKernelCallInstruction, 0, "");
  132. OPCODE(0x32f, "unknown 32f", FF7WorldKernelCallInstruction, 0, "");
  133. OPCODE(0x339, "unknown 339", FF7WorldKernelCallInstruction, 0, "");
  134. OPCODE(0x30d, "unknown 30d", FF7WorldKernelCallInstruction, 0, "");
  135. OPCODE(0x354, "unknown 354", FF7WorldKernelCallInstruction, 0, "");
  136. OPCODE(0x332, "unknown 332", FF7WorldKernelCallInstruction, 0, "");
  137. OPCODE(0x31f, "unknown 31f", FF7WorldKernelCallInstruction, 0, "");
  138. OPCODE(0x329, "unknown 329", FF7WorldKernelCallInstruction, 0, "");
  139. OPCODE(0x32a, "unknown 32a", FF7WorldKernelCallInstruction, 0, "");
  140. OPCODE(0x34d, "unknown 3d4", FF7WorldKernelCallInstruction, 0, "");
  141. OPCODE(0x34e, "unknown 3de", FF7WorldKernelCallInstruction, 0, "");
  142. OPCODE(0x30a, "unknown 30a", FF7WorldKernelCallInstruction, 0, "");
  143. OPCODE(0x333, "unknown 333", FF7WorldKernelCallInstruction, 0, "");
  144. OPCODE(0x331, "unknown 331", FF7WorldKernelCallInstruction, 0, "");
  145. OPCODE(0x319, "unknown 319", FF7WorldKernelCallInstruction, 0, "");
  146. OPCODE(0x353, "unknown 353", FF7WorldKernelCallInstruction, 0, "");
  147. OPCODE(0x33e, "unknown 33e", FF7WorldKernelCallInstruction, 0, "");
  148. OPCODE(0x321, "unknown 321", FF7WorldKernelCallInstruction, 0, "");
  149. OPCODE(0x31d, "play sound effect", FF7WorldKernelCallInstruction, 2, "w");
  150. OPCODE(0x336, "set active entity movespeed (honor walkmesh)", FF7WorldKernelCallInstruction, 0, "");
  151. OPCODE(0x328, "set active entity direction", FF7WorldKernelCallInstruction, 0, "");
  152. OPCODE(0x33d, "set field entry point2?", FF7WorldKernelCallInstruction, 0, "");
  153. OPCODE(0x351, "set music volume", FF7WorldKernelCallInstruction, 0, "");
  154. OPCODE(0x352, "shake camera on / off", FF7WorldKernelCallInstruction, 0, "");
  155. OPCODE(0x34f, "set active entity y position", FF7WorldKernelCallInstruction, 0, "");
  156. OPCODE(0x30b, "set active entity y offset", FF7WorldKernelCallInstruction, 0, "");
  157. OPCODE(0x347, "move active entity to entity by model id ?", FF7WorldKernelCallInstruction, 0, "");
  158. OPCODE(0x30e, "active entity play animation", FF7WorldKernelCallInstruction, 0, "");
  159. OPCODE(0x305, "set wait frames", FF7WorldKernelCallInstruction, 0, "");
  160. OPCODE(0x306, "wait?", FF7WorldKernelCallInstruction, 0, "");
  161. OPCODE(0x307, "set control lock", FF7WorldKernelCallInstruction, 0, "");
  162. OPCODE(0x303, "set active entity movespeed", FF7WorldKernelCallInstruction, 0, "");
  163. OPCODE(0x32c, "set window parameters", FF7WorldKernelCallInstruction, 0, "");
  164. OPCODE(0x32d, "wait for window ready", FF7WorldKernelCallInstruction, 0, "");
  165. OPCODE(0x324, "set window dimensions", FF7WorldKernelCallInstruction, 0, "");
  166. OPCODE(0x325, "set window message", FF7WorldKernelCallInstruction, 0, "");
  167. OPCODE(0x32e, "wait for message acknowledge", FF7WorldKernelCallInstruction, 0, "");
  168. OPCODE(0x32b, "set battle lock", FF7WorldKernelCallInstruction, 0, "");
  169. OPCODE(0x326, "set window prompt", FF7WorldKernelCallInstruction, 0, "");
  170. OPCODE(0x327, "wait for prompt acknowledge ?", FF7WorldKernelCallInstruction, 0, "");
  171. OPCODE(0x318, "enter field scene", FF7WorldKernelCallInstruction, 0, "");
  172. OPCODE(0x33b, "fade out?", FF7WorldKernelCallInstruction, 0, "");
  173. OPCODE(0x348, "fade in ?", FF7WorldKernelCallInstruction, 0, "");
  174. OPCODE(0x330, "set active entity", FF7WorldKernelCallInstruction, 0, "");
  175. OPCODE(0x350, "set meteor texture on/off", FF7WorldKernelCallInstruction, 0, "");
  176. OPCODE(0x34b, "set chocobo type", FF7WorldKernelCallInstruction, 0, "");
  177. OPCODE(0x34c, "set submarine color", FF7WorldKernelCallInstruction, 0, "");
  178. OPCODE(0x349, "set world progress", FF7WorldKernelCallInstruction, 0, "");
  179. OPCODE(0x300, "load model", FF7WorldKernelCallInstruction, 0, "");
  180. OPCODE(0x302, "set player entity", FF7WorldKernelCallInstruction, 0, "");
  181. OPCODE(0x30c, "enter vehicle?", FF7WorldKernelCallInstruction, 0, "");
  182. OPCODE(0x308, "set active entity mesh coordinates", FF7WorldKernelCallInstruction, 0, "");
  183. OPCODE(0x309, "set active entity coordinates in mesh", FF7WorldKernelCallInstruction, 0, "");
  184. OPCODE(0x304, "set active entity direction & facing", FF7WorldKernelCallInstruction, 0, "");
  185. OPCODE(0x310, "set active point", FF7WorldKernelCallInstruction, 0, "");
  186. OPCODE(0x311, "set point mesh coordinates", FF7WorldKernelCallInstruction, 0, "");
  187. OPCODE(0x312, "set point coordinates in mesh", FF7WorldKernelCallInstruction, 0, "");
  188. OPCODE(0x313, "set point terrain BGR", FF7WorldKernelCallInstruction, 0, "");
  189. OPCODE(0x314, "set point dropoff parameters", FF7WorldKernelCallInstruction, 0, "");
  190. OPCODE(0x315, "set point sky BGR", FF7WorldKernelCallInstruction, 0, "");
  191. OPCODE(0x316, "set point BGR3?", FF7WorldKernelCallInstruction, 0, "");
  192. OPCODE(0x317, "trigger battle", FF7WorldKernelCallInstruction, 0, "");
  193. default:
  194. throw UnknownOpcodeException(this->_address, opcode);
  195. }
  196. }
  197. this->_address += 2;
  198. }
  199. for (auto& instruction : this->_insts)
  200. {
  201. instruction->_address = instruction->_address / 2;
  202. }
  203. }
  204. }