ff7_field_disassembler.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #include "ff7_field_disassembler.h"
  2. #include "ff7_field_engine.h"
  3. #include "decompiler_engine.h"
  4. #include <boost/format.hpp>
  5. #include "lzs.h"
  6. #include "make_unique.h"
  7. #include <boost/algorithm/string/split.hpp>
  8. #include <boost/algorithm/string.hpp>
  9. #include "ff7_field_codegen.h"
  10. FF7::FF7Disassembler::FF7Disassembler(SUDM::IScriptFormatter& formatter, FF7FieldEngine* engine, InstVec& insts, const std::vector<unsigned char>& rawScriptData)
  11. : SimpleDisassembler(insts),
  12. mEngine(engine),
  13. mFormatter(formatter)
  14. {
  15. mbFromRaw = true;
  16. kSectionPointersSize = 0; // If loading a raw section then we don't have a "sections header" to skip
  17. auto dataCopy = rawScriptData;
  18. mStream = std::make_unique<BinaryReader>(std::move(dataCopy));
  19. ReadHeader();
  20. }
  21. FF7::FF7Disassembler::FF7Disassembler(SUDM::IScriptFormatter& formatter, FF7FieldEngine *engine, InstVec &insts)
  22. : SimpleDisassembler(insts),
  23. mEngine(engine),
  24. mFormatter(formatter)
  25. {
  26. }
  27. FF7::FF7Disassembler::~FF7Disassembler()
  28. {
  29. }
  30. void FF7::FF7Disassembler::ReadHeader()
  31. {
  32. if (!mbFromRaw)
  33. {
  34. // First read the file section pointers
  35. for (int i = 0; i < kNumSections; i++)
  36. {
  37. mSections[i] = mStream->ReadU32();
  38. }
  39. // Now fix up from PSX RAM pointers to simple file offsets
  40. const uint32 basePtr = mSections[0];
  41. for (int i = 0; i < kNumSections; i++)
  42. {
  43. mSections[i] = (mSections[i] - basePtr) + kSectionPointersSize;
  44. }
  45. // Now seek to the script section
  46. mStream->Seek(mSections[eScript]);
  47. }
  48. // Read the script header
  49. mHeader.Read(*mStream);
  50. mScaleFactor = static_cast<float>(mHeader.mScale) / 512.0f;
  51. }
  52. void FF7::FF7Disassembler::open(const char *filename)
  53. {
  54. // Read all of the file, decompress it, then stuff it into a stream
  55. mStream = std::make_unique<BinaryReader>(Lzs::Decompress(BinaryReader::ReadAll(filename)));
  56. ReadHeader();
  57. }
  58. uint32 FF7::FF7Disassembler::GetEndOfScriptOffset(uint16 curEntryPoint, size_t entityIndex, size_t scriptIndex)
  59. {
  60. uint16 nextEntryPoint = curEntryPoint;
  61. do
  62. {
  63. if (scriptIndex + 1 >= 32)
  64. {
  65. if (entityIndex + 1 >= mHeader.mEntityScripts.size())
  66. {
  67. // This is the very last script, so use the end of its data which is the offset to strings
  68. return mHeader.mOffsetToStrings;
  69. }
  70. else
  71. {
  72. // Wrap around to the next entity
  73. entityIndex++;
  74. scriptIndex = 0;
  75. }
  76. }
  77. else
  78. {
  79. // Get the next script in the same entity
  80. scriptIndex++;
  81. }
  82. nextEntryPoint = mHeader.mEntityScripts[entityIndex][scriptIndex];
  83. } while (nextEntryPoint == curEntryPoint);
  84. return nextEntryPoint;
  85. }
  86. std::unique_ptr<Function> FF7::FF7Disassembler::StartFunction(size_t scriptIndex)
  87. {
  88. auto func = std::make_unique<Function>();
  89. func->_retVal = false;
  90. func->_args = 0;
  91. func->_name = "script_" + std::to_string(scriptIndex);
  92. func->mStartAddr = _address;
  93. return func;
  94. }
  95. struct ScriptInfo
  96. {
  97. uint16 mEntryPoint;
  98. uint32 mNextEntryPoint;
  99. size_t mIndex;
  100. };
  101. void FF7::FF7Disassembler::doDisassemble()
  102. {
  103. // Loop through the scripts for each entity
  104. for (size_t entityNumber = 0; entityNumber < mHeader.mEntityScripts.size(); entityNumber++)
  105. {
  106. std::string strOriginalName = mHeader.mFieldEntityNames[entityNumber].data();
  107. if (strOriginalName.empty())
  108. {
  109. // If the entity name was blank in the file then use a consistent generated name
  110. strOriginalName = "entity_" + std::to_string(entityNumber);
  111. }
  112. const std::string entityName = mFormatter.EntityName(strOriginalName);
  113. // Only parse each script one
  114. std::set<uint16> parsedScripts;
  115. std::vector<ScriptInfo> scriptInfo;
  116. // Collect the scripts to parse
  117. for (size_t scriptIndex = 0; scriptIndex < mHeader.mEntityScripts[entityNumber].size(); scriptIndex++)
  118. {
  119. uint16 scriptEntryPoint = mHeader.mEntityScripts[entityNumber][scriptIndex];
  120. if (parsedScripts.find(scriptEntryPoint) != std::end(parsedScripts))
  121. {
  122. // If we've already parsed this scripts entry point, then don't do it again as it means
  123. // two scripts have the same entry which only seems to be true for "empty" scripts
  124. continue;
  125. }
  126. parsedScripts.insert(scriptEntryPoint);
  127. const uint32 nextScriptEntryPoint = GetEndOfScriptOffset(scriptEntryPoint, entityNumber, scriptIndex);
  128. const uint32 scriptSize = nextScriptEntryPoint - scriptEntryPoint;
  129. if (scriptSize > 0)
  130. {
  131. ScriptInfo info = { scriptEntryPoint, nextScriptEntryPoint, scriptIndex };
  132. scriptInfo.push_back(info);
  133. }
  134. }
  135. for (auto it = scriptInfo.begin(); it != scriptInfo.end(); it++)
  136. {
  137. const bool isStart = it == scriptInfo.begin();
  138. const bool isEnd = it == (--scriptInfo.end());
  139. DisassembleIndivdualScript(entityName, entityNumber, it->mIndex, it->mEntryPoint, it->mNextEntryPoint, isStart, isEnd);
  140. }
  141. }
  142. }
  143. static int FindId(uint32 startAddr, uint32 endAddr, const InstVec& insts)
  144. {
  145. for (const InstPtr& instruction : insts)
  146. {
  147. if (instruction->_address >= startAddr && instruction->_address <= endAddr)
  148. {
  149. if (instruction->_opcode == FF7::eOpcodes::opCodeCHAR)
  150. {
  151. return instruction->_params[0]->getSigned();
  152. }
  153. }
  154. }
  155. return -1;
  156. }
  157. void FF7::FF7Disassembler::AddFunc(std::string entityName, size_t entityIndex, size_t scriptIndex, uint32 nextScriptEntryPoint, const bool isStart, bool isEnd, bool toReturnOnly, std::string funcName)
  158. {
  159. const auto kScriptEntryPoint = mStream->Position();
  160. // Read each block of opcodes up to a return
  161. const size_t oldNumInstructions = _insts.size();
  162. auto func = StartFunction(scriptIndex);
  163. if (toReturnOnly)
  164. {
  165. // Read opcodes to the end or bail at the first return
  166. ReadOpCodesToPositionOrReturn(nextScriptEntryPoint + kSectionPointersSize);
  167. auto streamPos = mStream->Position();
  168. const size_t endPos = nextScriptEntryPoint + kSectionPointersSize;
  169. if (streamPos != endPos)
  170. {
  171. // Can't be the end if there is more data
  172. isEnd = false;
  173. }
  174. }
  175. else
  176. {
  177. while (mStream->Position() != nextScriptEntryPoint + kSectionPointersSize)
  178. {
  179. // Keep going till we have all of the script, i.e if we bail at a return then call
  180. // again till we have everything
  181. ReadOpCodesToPositionOrReturn(nextScriptEntryPoint + kSectionPointersSize);
  182. }
  183. }
  184. std::string metaData;
  185. if (isStart && isEnd)
  186. {
  187. metaData = "start_end_";
  188. }
  189. else if (isStart)
  190. {
  191. metaData = "start_";
  192. }
  193. else if (isEnd)
  194. {
  195. metaData = "end_";
  196. }
  197. const size_t newNumInstructions = _insts.size();
  198. func->mNumInstructions = newNumInstructions - oldNumInstructions;
  199. func->mEndAddr = _insts.back()->_address;
  200. if (!funcName.empty())
  201. {
  202. func->_name = funcName;
  203. }
  204. int id = FindId(func->mStartAddr, func->mEndAddr, _insts);
  205. // If there is no ID check if there was an ID for this entity in any of its other functions and use that instead
  206. if (id == -1)
  207. {
  208. for (auto& func : mEngine->_functions)
  209. {
  210. FunctionMetaData metaData(func.second._metadata);
  211. if (metaData.EntityName() == entityName && metaData.CharacterId() != -1)
  212. {
  213. id = metaData.CharacterId();
  214. break;
  215. }
  216. }
  217. }
  218. metaData += std::to_string(id) + "_" + entityName;
  219. func->_metadata = metaData;
  220. mEngine->_functions[kScriptEntryPoint] = *func;
  221. mEngine->AddEntityFunction(entityName, entityIndex, func->_name, scriptIndex);
  222. }
  223. void FF7::FF7Disassembler::DisassembleIndivdualScript(std::string entityName,
  224. size_t entityIndex,
  225. size_t scriptIndex,
  226. size_t scriptEntryPoint,
  227. uint32 nextScriptEntryPoint,
  228. bool isStart,
  229. bool isEnd)
  230. {
  231. scriptEntryPoint += kSectionPointersSize;
  232. mStream->Seek(scriptEntryPoint);
  233. _addressBase = mStream->Position();
  234. _address = _addressBase;
  235. // "Normal" script
  236. if (scriptIndex > 0)
  237. {
  238. if (scriptIndex == 1)
  239. {
  240. AddFunc(entityName, entityIndex, scriptIndex, nextScriptEntryPoint, isStart, isEnd, false, "on_interact");
  241. }
  242. else
  243. {
  244. AddFunc(entityName, entityIndex, scriptIndex, nextScriptEntryPoint, isStart, isEnd, false, "");
  245. }
  246. }
  247. else
  248. {
  249. const size_t endPos = nextScriptEntryPoint + kSectionPointersSize;
  250. // Read the init script, which means stop at the first return
  251. AddFunc(entityName, entityIndex, scriptIndex, nextScriptEntryPoint, isStart, isEnd, true, "on_start");
  252. // Not at the end of this script? Then the remaining data is the "main" script
  253. auto streamPos = mStream->Position();
  254. if (streamPos != endPos)
  255. {
  256. // The "main" script can have more than one return statement
  257. AddFunc(entityName, entityIndex, scriptIndex, nextScriptEntryPoint, false, isEnd, false, "on_update");
  258. streamPos = mStream->Position();
  259. // But should end exactly on the end pos
  260. if (streamPos != endPos)
  261. {
  262. throw InternalDecompilerError();
  263. }
  264. }
  265. }
  266. }
  267. const FF7::TInstructRecord kOpcodes[] =
  268. {
  269. // Flow
  270. { 1, FF7::eOpcodes::RET, "RET", "", FF7::FF7ControlFlowInstruction::Create },
  271. { 1, FF7::eOpcodes::REQ, "REQ", "BU", FF7::FF7ControlFlowInstruction::Create },
  272. { 1, FF7::eOpcodes::REQSW, "REQSW", "BU", FF7::FF7ControlFlowInstruction::Create },
  273. { 1, FF7::eOpcodes::REQEW, "REQEW", "BU", FF7::FF7ControlFlowInstruction::Create },
  274. { 1, FF7::eOpcodes::NOP, "NOP", "", FF7::FF7NoOperationInstruction::Create },
  275. { 1, FF7::eOpcodes::IFUB, "IFUB", "NNBBBL", FF7::FF7NoOperationInstruction::Create }
  276. };
  277. std::map<std::string, const FF7::TInstructRecord*> FF7::FieldInstructions()
  278. {
  279. // Convert the array to a map that we can query on by mnemonic
  280. std::map<std::string, const TInstructRecord*> mnemonicToInstructionRecords;
  281. for (size_t i = 0; i < boost::size(kOpcodes); i++)
  282. {
  283. mnemonicToInstructionRecords[kOpcodes[i].mMnemonic] = &kOpcodes[i];
  284. }
  285. return mnemonicToInstructionRecords;
  286. }
  287. void FF7::FF7Disassembler::ReadOpCodesToPositionOrReturn(size_t endPos)
  288. {
  289. /* Need all opcodes in the array before this will work
  290. // Convert the array to a map that we can query on by opcode
  291. std::map<unsigned int, const TInstructRecord*> opcodeToInstructionRecords;
  292. for (size_t i = 0; i < boost::size(kOpcodes); i++)
  293. {
  294. opcodeToInstructionRecords[kOpcodes[i].mOpCode] = &kOpcodes[i];
  295. }
  296. while (mStream->Position() < endPos)
  297. {
  298. // See if the next data is a 1 byte opcode
  299. uint16 opcode = mStream->ReadU8();
  300. auto it = opcodeToInstructionRecords.find(opcode);
  301. _address++;
  302. if (it == std::end(opcodeToInstructionRecords))
  303. {
  304. // No, is it a 2 byte opcode?
  305. opcode = (mStream->ReadU8() << 8) + opcode;
  306. _address++;
  307. it = opcodeToInstructionRecords.find(opcode);
  308. if (it == std::end(opcodeToInstructionRecords))
  309. {
  310. // There are no instructions bigger than 2 bytes, so fail
  311. throw UnknownSubOpcodeException(_address, opcode);
  312. }
  313. }
  314. InstPtr inst = it->second->mFactoryFunc();
  315. inst->_opcode = opcode;
  316. inst->_address = _address;
  317. inst->_stackChange = 0;
  318. inst->_name = it->second->mMnemonic;
  319. readParams(inst, it->second->mArgumentFormat);
  320. _insts.push_back(inst);
  321. }
  322. */
  323. std::vector<unsigned int> exitAddrs;
  324. while (mStream->Position() < endPos)
  325. {
  326. uint8 opcode = mStream->ReadU8();
  327. uint32 full_opcode = 0;
  328. std::string opcodePrefix;
  329. switch (opcode)
  330. {
  331. // Flow
  332. OPCODE(eOpcodes::IFUB, "IFUB", FF7CondJumpInstruction, 0, "NBBBB");
  333. OPCODE(eOpcodes::RET, "RET", FF7ControlFlowInstruction, 0, "");
  334. OPCODE(eOpcodes::REQ, "REQ", FF7ControlFlowInstruction, 0, "BU");
  335. OPCODE(eOpcodes::REQSW, "REQSW", FF7ControlFlowInstruction, 0, "BU");
  336. OPCODE(eOpcodes::REQEW, "REQEW", FF7ControlFlowInstruction, 0, "BU");
  337. OPCODE(eOpcodes::PREQ, "PREQ", FF7ControlFlowInstruction, 0, "BU");
  338. OPCODE(eOpcodes::PRQSW, "PRQSW", FF7ControlFlowInstruction, 0, "BU");
  339. OPCODE(eOpcodes::PRQEW, "PRQEW", FF7ControlFlowInstruction, 0, "BU");
  340. OPCODE(eOpcodes::RETTO, "RETTO", FF7ControlFlowInstruction, 0, "U");
  341. OPCODE(eOpcodes::JMPF, "JMPF", FF7UncondJumpInstruction, 0, "B");
  342. OPCODE(eOpcodes::JMPFL, "JMPFL", FF7UncondJumpInstruction, 0, "w");
  343. OPCODE(eOpcodes::JMPB, "JMPB", FF7UncondJumpInstruction, 0, "B");
  344. OPCODE(eOpcodes::JMPBL, "JMPBL", FF7UncondJumpInstruction, 0, "w");
  345. OPCODE(eOpcodes::IFUBL, "IFUBL", FF7CondJumpInstruction, 0, "NBBBw");
  346. OPCODE(eOpcodes::IFSW, "IFSW", FF7CondJumpInstruction, 0, "NwwBB");
  347. OPCODE(eOpcodes::IFSWL, "IFSWL", FF7CondJumpInstruction, 0, "NwwBw");
  348. OPCODE(eOpcodes::IFUW, "IFUW", FF7CondJumpInstruction, 0, "NwwBB");
  349. OPCODE(eOpcodes::IFUWL, "IFUWL", FF7CondJumpInstruction, 0, "NwwBw");
  350. OPCODE(eOpcodes::WAIT, "WAIT", FF7ControlFlowInstruction, 0, "w");
  351. OPCODE(eOpcodes::IFKEY, "IFKEY", FF7CondJumpInstruction, 0, "wB");
  352. OPCODE(eOpcodes::IFKEYON, "IFKEYON", FF7CondJumpInstruction, 0, "wB");
  353. OPCODE(eOpcodes::IFKEYOFF, "IFKEYOFF", FF7CondJumpInstruction, 0, "wB");
  354. OPCODE(eOpcodes::NOP, "NOP", FF7NoOperationInstruction, 0, "");
  355. OPCODE(eOpcodes::IFPRTYQ, "IFPRTYQ", FF7CondJumpInstruction, 0, "BB");
  356. OPCODE(eOpcodes::IFMEMBQ, "IFMEMBQ", FF7CondJumpInstruction, 0, "BB");
  357. // Module
  358. OPCODE(eOpcodes::DSKCG, "DSKCG", FF7ModuleInstruction, 0, "B");
  359. START_SUBOPCODE(eOpcodes::SPECIAL)
  360. OPCODE(eSpecialOpcodes::ARROW, "ARROW", FF7ModuleInstruction, 0, "B");
  361. OPCODE(eSpecialOpcodes::PNAME, "PNAME", FF7ModuleInstruction, 0, "B");
  362. OPCODE(eSpecialOpcodes::GMSPD, "GMSPD", FF7ModuleInstruction, 0, "B");
  363. OPCODE(eSpecialOpcodes::SMSPD, "SMSPD", FF7ModuleInstruction, 0, "BB");
  364. OPCODE(eSpecialOpcodes::FLMAT, "FLMAT", FF7ModuleInstruction, 0, "");
  365. OPCODE(eSpecialOpcodes::FLITM, "FLITM", FF7ModuleInstruction, 0, "");
  366. OPCODE(eSpecialOpcodes::BTLCK, "BTLCK", FF7ModuleInstruction, 0, "B");
  367. OPCODE(eSpecialOpcodes::MVLCK, "MVLCK", FF7ModuleInstruction, 0, "B");
  368. OPCODE(eSpecialOpcodes::SPCNM, "SPCNM", FF7ModuleInstruction, 0, "BB");
  369. OPCODE(eSpecialOpcodes::RSGLB, "RSGLB", FF7ModuleInstruction, 0, "");
  370. OPCODE(eSpecialOpcodes::CLITM, "CLITM", FF7ModuleInstruction, 0, "");
  371. END_SUBOPCODE
  372. OPCODE(eOpcodes::MINIGAME, "MINIGAME", FF7ModuleInstruction, 0, "wsswBB");
  373. OPCODE(eOpcodes::BTMD2, "BTMD2", FF7ModuleInstruction, 0, "d");
  374. OPCODE(eOpcodes::BTRLD, "BTRLD", FF7ModuleInstruction, 0, "NB");
  375. OPCODE(eOpcodes::BTLTB, "BTLTB", FF7ModuleInstruction, 0, "B");
  376. OPCODE(eOpcodes::MAPJUMP, "MAPJUMP", FF7ModuleInstruction, 0, "wsswB");
  377. OPCODE(eOpcodes::LSTMP, "LSTMP", FF7ModuleInstruction, 0, "NB");
  378. OPCODE(eOpcodes::BATTLE, "BATTLE", FF7ModuleInstruction, 0, "Nw");
  379. OPCODE(eOpcodes::BTLON, "BTLON", FF7ModuleInstruction, 0, "B");
  380. OPCODE(eOpcodes::BTLMD, "BTLMD", FF7ModuleInstruction, 0, "w");
  381. OPCODE(eOpcodes::MPJPO, "MPJPO", FF7ModuleInstruction, 0, "B");
  382. OPCODE(eOpcodes::PMJMP, "PMJMP", FF7ModuleInstruction, 0, "w");
  383. OPCODE(eOpcodes::PMJMP2, "PMJMP2", FF7ModuleInstruction, 0, "");
  384. OPCODE(eOpcodes::GAMEOVER, "GAMEOVER", FF7ModuleInstruction, 0, "");
  385. // Math
  386. OPCODE(eOpcodes::PLUS_, "PLUS!", FF7MathInstruction, 0, "NBB");
  387. OPCODE(eOpcodes::PLUS2_, "PLUS2!", FF7MathInstruction, 0, "NBw");
  388. OPCODE(eOpcodes::MINUS_, "MINUS!", FF7MathInstruction, 0, "NBB");
  389. OPCODE(eOpcodes::MINUS2_, "MINUS2!", FF7MathInstruction, 0, "NBw");
  390. OPCODE(eOpcodes::INC_, "INC!", FF7MathInstruction, 0, "BB");
  391. OPCODE(eOpcodes::INC2_, "INC2!", FF7MathInstruction, 0, "BB");
  392. OPCODE(eOpcodes::DEC_, "DEC!", FF7MathInstruction, 0, "BB");
  393. OPCODE(eOpcodes::DEC2_, "DEC2!", FF7MathInstruction, 0, "BB");
  394. OPCODE(eOpcodes::RDMSD, "RDMSD", FF7MathInstruction, 0, "NB");
  395. OPCODE(eOpcodes::SETBYTE, "SETBYTE", FF7MathInstruction, 0, "NBB");
  396. OPCODE(eOpcodes::SETWORD, "SETWORD", FF7MathInstruction, 0, "NBw");
  397. OPCODE(eOpcodes::BITON, "BITON", FF7MathInstruction, 0, "NBB");
  398. OPCODE(eOpcodes::BITOFF, "BITOFF", FF7MathInstruction, 0, "NBB");
  399. OPCODE(eOpcodes::BITXOR, "BITXOR", FF7MathInstruction, 0, "NBB");
  400. OPCODE(eOpcodes::PLUS, "PLUS", FF7MathInstruction, 0, "NBB");
  401. OPCODE(eOpcodes::PLUS2, "PLUS2", FF7MathInstruction, 0, "NBw");
  402. OPCODE(eOpcodes::MINUS, "MINUS", FF7MathInstruction, 0, "NBB");
  403. OPCODE(eOpcodes::MINUS2, "MINUS2", FF7MathInstruction, 0, "NBw");
  404. OPCODE(eOpcodes::MUL, "MUL", FF7MathInstruction, 0, "NBB");
  405. OPCODE(eOpcodes::MUL2, "MUL2", FF7MathInstruction, 0, "NBw");
  406. OPCODE(eOpcodes::DIV, "DIV", FF7MathInstruction, 0, "NBB");
  407. OPCODE(eOpcodes::DIV2, "DIV2", FF7MathInstruction, 0, "NBw");
  408. OPCODE(eOpcodes::MOD, "MOD", FF7MathInstruction, 0, "NBB");
  409. OPCODE(eOpcodes::MOD2, "MOD2", FF7MathInstruction, 0, "NBw");
  410. OPCODE(eOpcodes::AND, "AND", FF7MathInstruction, 0, "NBB");
  411. OPCODE(eOpcodes::AND2, "AND2", FF7MathInstruction, 0, "NBw");
  412. OPCODE(eOpcodes::OR, "OR", FF7MathInstruction, 0, "NBB");
  413. OPCODE(eOpcodes::OR2, "OR2", FF7MathInstruction, 0, "NBw");
  414. OPCODE(eOpcodes::XOR, "XOR", FF7MathInstruction, 0, "NBB");
  415. OPCODE(eOpcodes::XOR2, "XOR2", FF7MathInstruction, 0, "NBw");
  416. OPCODE(eOpcodes::INC, "INC", FF7MathInstruction, 0, "BB");
  417. OPCODE(eOpcodes::INC2, "INC2", FF7MathInstruction, 0, "BB");
  418. OPCODE(eOpcodes::DEC, "DEC", FF7MathInstruction, 0, "BB");
  419. OPCODE(eOpcodes::DEC2, "DEC2", FF7MathInstruction, 0, "BB");
  420. OPCODE(eOpcodes::RANDOM, "RANDOM", FF7MathInstruction, 0, "BB");
  421. OPCODE(eOpcodes::LBYTE, "LBYTE", FF7MathInstruction, 0, "NBB");
  422. OPCODE(eOpcodes::HBYTE, "HBYTE", FF7MathInstruction, 0, "NBw");
  423. OPCODE(eOpcodes::TWOBYTE, "2BYTE", FF7MathInstruction, 0, "NNBBB");
  424. OPCODE(eOpcodes::SIN, "SIN", FF7MathInstruction, 0, "NNwwwB");
  425. OPCODE(eOpcodes::COS, "COS", FF7MathInstruction, 0, "NNwwwB");
  426. // Window
  427. OPCODE(eOpcodes::TUTOR, "TUTOR", FF7WindowInstruction, 0, "B");
  428. OPCODE(eOpcodes::WCLS, "WCLS", FF7WindowInstruction, 0, "B");
  429. OPCODE(eOpcodes::WSIZW, "WSIZW", FF7WindowInstruction, 0, "Bwwww");
  430. OPCODE(eOpcodes::WSPCL, "WSPCL", FF7WindowInstruction, 0, "BBBB");
  431. OPCODE(eOpcodes::WNUMB, "WNUMB", FF7WindowInstruction, 0, "NBwwB"); // NBdB when N == 0
  432. OPCODE(eOpcodes::STTIM, "STTIM", FF7WindowInstruction, 0, "NNBBB");
  433. OPCODE(eOpcodes::MESSAGE, "MESSAGE", FF7WindowInstruction, 0, "BB");
  434. OPCODE(eOpcodes::MPARA, "MPARA", FF7WindowInstruction, 0, "NBBB");
  435. OPCODE(eOpcodes::MPRA2, "MPRA2", FF7WindowInstruction, 0, "NBBw");
  436. OPCODE(eOpcodes::MPNAM, "MPNAM", FF7WindowInstruction, 0, "B");
  437. OPCODE(eOpcodes::ASK, "ASK", FF7WindowInstruction, 0, "NBBBBB");
  438. OPCODE(eOpcodes::MENU, "MENU", FF7WindowInstruction, 0, "NBB");
  439. OPCODE(eOpcodes::MENU2, "MENU2", FF7WindowInstruction, 0, "B");
  440. OPCODE(eOpcodes::WINDOW, "WINDOW", FF7WindowInstruction, 0, "Bwwww");
  441. OPCODE(eOpcodes::WMOVE, "WMOVE", FF7WindowInstruction, 0, "Bss");
  442. OPCODE(eOpcodes::WMODE, "WMODE", FF7WindowInstruction, 0, "BBB");
  443. OPCODE(eOpcodes::WREST, "WREST", FF7WindowInstruction, 0, "B");
  444. OPCODE(eOpcodes::WCLSE, "WCLSE", FF7WindowInstruction, 0, "B");
  445. OPCODE(eOpcodes::WROW, "WROW", FF7WindowInstruction, 0, "BB");
  446. OPCODE(eOpcodes::GWCOL, "GWCOL", FF7WindowInstruction, 0, "NNBBBB");
  447. OPCODE(eOpcodes::SWCOL, "SWCOL", FF7WindowInstruction, 0, "NNBBBB");
  448. // Party
  449. OPCODE(eOpcodes::SPTYE, "SPTYE", FF7PartyInstruction, 0, "NNBBB");
  450. OPCODE(eOpcodes::GTPYE, "GTPYE", FF7PartyInstruction, 0, "NNBBB");
  451. OPCODE(eOpcodes::GOLDU, "GOLDU", FF7PartyInstruction, 0, "Nww"); // Nd when N == 0
  452. OPCODE(eOpcodes::GOLDD, "GOLDD", FF7PartyInstruction, 0, "Nww"); // Nd when N == 0
  453. OPCODE(eOpcodes::CHGLD, "CHGLD", FF7PartyInstruction, 0, "NBB");
  454. OPCODE(eOpcodes::HMPMAX1, "HMPMAX1", FF7PartyInstruction, 0, "");
  455. OPCODE(eOpcodes::HMPMAX2, "HMPMAX2", FF7PartyInstruction, 0, "");
  456. OPCODE(eOpcodes::MHMMX, "MHMMX", FF7PartyInstruction, 0, "");
  457. OPCODE(eOpcodes::HMPMAX3, "HMPMAX3", FF7PartyInstruction, 0, "");
  458. OPCODE(eOpcodes::MPU, "MPU", FF7PartyInstruction, 0, "NBw");
  459. OPCODE(eOpcodes::MPD, "MPD", FF7PartyInstruction, 0, "NBw");
  460. OPCODE(eOpcodes::HPU, "HPU", FF7PartyInstruction, 0, "NBw");
  461. OPCODE(eOpcodes::HPD, "HPD", FF7PartyInstruction, 0, "NBw");
  462. OPCODE(eOpcodes::STITM, "STITM", FF7PartyInstruction, 0, "NwB");
  463. OPCODE(eOpcodes::DLITM, "DLITM", FF7PartyInstruction, 0, "NwB");
  464. OPCODE(eOpcodes::CKITM, "CKITM", FF7PartyInstruction, 0, "NwB");
  465. OPCODE(eOpcodes::SMTRA, "SMTRA", FF7PartyInstruction, 0, "NNBBBB");
  466. OPCODE(eOpcodes::DMTRA, "DMTRA", FF7PartyInstruction, 0, "NNBBBBB");
  467. OPCODE(eOpcodes::CMTRA, "CMTRA", FF7PartyInstruction, 0, "NNNBBBBBB");
  468. OPCODE(eOpcodes::GETPC, "GETPC", FF7PartyInstruction, 0, "NBB");
  469. OPCODE(eOpcodes::PRTYP, "PRTYP", FF7PartyInstruction, 0, "B");
  470. OPCODE(eOpcodes::PRTYM, "PRTYM", FF7PartyInstruction, 0, "B");
  471. OPCODE(eOpcodes::PRTYE, "PRTYE", FF7PartyInstruction, 0, "BBB");
  472. OPCODE(eOpcodes::MMBUD, "MMBUD", FF7PartyInstruction, 0, "BB");
  473. OPCODE(eOpcodes::MMBLK, "MMBLK", FF7PartyInstruction, 0, "B");
  474. OPCODE(eOpcodes::MMBUK, "MMBUK", FF7PartyInstruction, 0, "B");
  475. // Model
  476. OPCODE(eOpcodes::JOIN, "JOIN", FF7ModelInstruction, 0, "B");
  477. OPCODE(eOpcodes::SPLIT, "SPLIT", FF7ModelInstruction, 0, "NNNssBssBB");
  478. OPCODE(eOpcodes::BLINK, "BLINK", FF7ModelInstruction, 0, "B");
  479. OPCODE_BASE(eOpcodes::KAWAI)
  480. {
  481. int length = this->mStream->ReadU8();
  482. assert(length >= 3);
  483. std::ostringstream paramStream;
  484. for (int i = 3; i < length; ++i)
  485. {
  486. paramStream << "B";
  487. }
  488. auto parameters = paramStream.str();
  489. opcode = this->mStream->ReadU8();
  490. switch (opcode)
  491. {
  492. OPCODE(eKawaiOpcodes::EYETX, "EYETX", FF7ModelInstruction, 0, parameters.c_str()); // was BBBB
  493. OPCODE(eKawaiOpcodes::TRNSP, "TRNSP", FF7ModelInstruction, 0, parameters.c_str()); // was B
  494. OPCODE(eKawaiOpcodes::AMBNT, "AMBNT", FF7ModelInstruction, 0, parameters.c_str()); // was BBBBBBB
  495. OPCODE(eKawaiOpcodes::Unknown03, "Unknown03", FF7ModelInstruction, 0, parameters.c_str());
  496. OPCODE(eKawaiOpcodes::Unknown04, "Unknown04", FF7ModelInstruction, 0, parameters.c_str());
  497. OPCODE(eKawaiOpcodes::Unknown05, "Unknown05", FF7ModelInstruction, 0, parameters.c_str());
  498. OPCODE(eKawaiOpcodes::LIGHT, "LIGHT", FF7ModelInstruction, 0, parameters.c_str());
  499. OPCODE(eKawaiOpcodes::Unknown07, "Unknown07", FF7ModelInstruction, 0, parameters.c_str());
  500. OPCODE(eKawaiOpcodes::Unknown08, "Unknown08", FF7ModelInstruction, 0, parameters.c_str());
  501. OPCODE(eKawaiOpcodes::Unknown09, "Unknown09", FF7ModelInstruction, 0, parameters.c_str());
  502. OPCODE(eKawaiOpcodes::SBOBJ, "SBOBJ", FF7ModelInstruction, 0, parameters.c_str());
  503. OPCODE(eKawaiOpcodes::Unknown0B, "Unknown0B", FF7ModelInstruction, 0, parameters.c_str());
  504. OPCODE(eKawaiOpcodes::Unknown0C, "Unknown0C", FF7ModelInstruction, 0, parameters.c_str());
  505. OPCODE(eKawaiOpcodes::SHINE, "SHINE", FF7ModelInstruction, 0, parameters.c_str());
  506. OPCODE(eKawaiOpcodes::RESET, "RESET", FF7ModelInstruction, 0, parameters.c_str());
  507. default:
  508. throw UnknownSubOpcodeException(this->_address, opcode);
  509. }
  510. INC_ADDR;
  511. INC_ADDR;
  512. }
  513. OPCODE_END
  514. OPCODE(eOpcodes::KAWIW, "KAWIW", FF7ModelInstruction, 0, "");
  515. OPCODE(eOpcodes::PMOVA, "PMOVA", FF7ModelInstruction, 0, "B");
  516. OPCODE(eOpcodes::PDIRA, "PDIRA", FF7ModelInstruction, 0, "B");
  517. OPCODE(eOpcodes::PTURA, "PTURA", FF7ModelInstruction, 0, "BBB");
  518. OPCODE(eOpcodes::PGTDR, "PGTDR", FF7ModelInstruction, 0, "NBB");
  519. OPCODE(eOpcodes::PXYZI, "PXYZI", FF7ModelInstruction, 0, "NNBBBBB");
  520. OPCODE(eOpcodes::TLKON, "TLKON", FF7ModelInstruction, 0, "B");
  521. OPCODE(eOpcodes::PC, "PC", FF7ModelInstruction, 0, "B");
  522. OPCODE(eOpcodes::opCodeCHAR, "CHAR", FF7ModelInstruction, 0, "B");
  523. OPCODE(eOpcodes::DFANM, "DFANM", FF7ModelInstruction, 0, "BB");
  524. OPCODE(eOpcodes::ANIME1, "ANIME1", FF7ModelInstruction, 0, "BB");
  525. OPCODE(eOpcodes::VISI, "VISI", FF7ModelInstruction, 0, "B");
  526. OPCODE(eOpcodes::XYZI, "XYZI", FF7ModelInstruction, 0, "NNsssw");
  527. OPCODE(eOpcodes::XYI, "XYI", FF7ModelInstruction, 0, "NNssw");
  528. OPCODE(eOpcodes::XYZ, "XYZ", FF7ModelInstruction, 0, "NNsss");
  529. OPCODE(eOpcodes::MOVE, "MOVE", FF7ModelInstruction, 0, "Nss");
  530. OPCODE(eOpcodes::CMOVE, "CMOVE", FF7ModelInstruction, 0, "Nss");
  531. OPCODE(eOpcodes::MOVA, "MOVA", FF7ModelInstruction, 0, "B");
  532. OPCODE(eOpcodes::TURA, "TURA", FF7ModelInstruction, 0, "BBB");
  533. OPCODE(eOpcodes::ANIMW, "ANIMW", FF7ModelInstruction, 0, "");
  534. OPCODE(eOpcodes::FMOVE, "FMOVE", FF7ModelInstruction, 0, "Nss");
  535. OPCODE(eOpcodes::ANIME2, "ANIME2", FF7ModelInstruction, 0, "BB");
  536. OPCODE(eOpcodes::ANIM_1, "ANIM!1", FF7ModelInstruction, 0, "BB");
  537. OPCODE(eOpcodes::CANIM1, "CANIM1", FF7ModelInstruction, 0, "BBBB");
  538. OPCODE(eOpcodes::CANM_1, "CANM!1", FF7ModelInstruction, 0, "BBBB");
  539. OPCODE(eOpcodes::MSPED, "MSPED", FF7ModelInstruction, 0, "Nw");
  540. OPCODE(eOpcodes::DIR, "DIR", FF7ModelInstruction, 0, "BB");
  541. OPCODE(eOpcodes::TURNGEN, "TURNGEN", FF7ModelInstruction, 0, "NBBBB");
  542. OPCODE(eOpcodes::TURN, "TURN", FF7ModelInstruction, 0, "NBBBB");
  543. OPCODE(eOpcodes::DIRA, "DIRA", FF7ModelInstruction, 0, "B");
  544. OPCODE(eOpcodes::GETDIR, "GETDIR", FF7ModelInstruction, 0, "NBB");
  545. OPCODE(eOpcodes::GETAXY, "GETAXY", FF7ModelInstruction, 0, "NBBB");
  546. OPCODE(eOpcodes::GETAI, "GETAI", FF7ModelInstruction, 0, "NBB");
  547. OPCODE(eOpcodes::ANIM_2, "ANIM!2", FF7ModelInstruction, 0, "BB");
  548. OPCODE(eOpcodes::CANIM2, "CANIM2", FF7ModelInstruction, 0, "BBBB");
  549. OPCODE(eOpcodes::CANM_2, "CANM!2", FF7ModelInstruction, 0, "BBBB");
  550. OPCODE(eOpcodes::ASPED, "ASPED", FF7ModelInstruction, 0, "Nw");
  551. OPCODE(eOpcodes::CC, "CC", FF7ModelInstruction, 0, "B");
  552. OPCODE(eOpcodes::JUMP, "JUMP", FF7ModelInstruction, 0, "NNssww");
  553. OPCODE(eOpcodes::AXYZI, "AXYZI", FF7ModelInstruction, 0, "NNBBBBB");
  554. OPCODE(eOpcodes::LADER, "LADER", FF7ModelInstruction, 0, "NNssswBBBB");
  555. OPCODE(eOpcodes::OFST, "OFST", FF7ModelInstruction, 0, "NNBsssw");
  556. OPCODE(eOpcodes::OFSTW, "OFSTW", FF7ModelInstruction, 0, "");
  557. OPCODE(eOpcodes::TALKR, "TALKR", FF7ModelInstruction, 0, "NB");
  558. OPCODE(eOpcodes::SLIDR, "SLIDR", FF7ModelInstruction, 0, "NB");
  559. OPCODE(eOpcodes::SOLID, "SOLID", FF7ModelInstruction, 0, "B");
  560. OPCODE(eOpcodes::TLKR2, "TLKR2", FF7ModelInstruction, 0, "Nw");
  561. OPCODE(eOpcodes::SLDR2, "SLDR2", FF7ModelInstruction, 0, "Nw");
  562. OPCODE(eOpcodes::CCANM, "CCANM", FF7ModelInstruction, 0, "BBB");
  563. OPCODE(eOpcodes::FCFIX, "FCFIX", FF7ModelInstruction, 0, "B");
  564. OPCODE(eOpcodes::ANIMB, "ANIMB", FF7ModelInstruction, 0, "");
  565. OPCODE(eOpcodes::TURNW, "TURNW", FF7ModelInstruction, 0, "");
  566. // Walkmesh
  567. OPCODE(eOpcodes::SLIP, "SLIP", FF7WalkmeshInstruction, 0, "B");
  568. OPCODE(eOpcodes::UC, "UC", FF7WalkmeshInstruction, 0, "B");
  569. OPCODE(eOpcodes::IDLCK, "IDLCK", FF7WalkmeshInstruction, 0, "wB");
  570. OPCODE(eOpcodes::LINE, "LINE", FF7WalkmeshInstruction, 0, "ssssss");
  571. OPCODE(eOpcodes::LINON, "LINON", FF7WalkmeshInstruction, 0, "B");
  572. OPCODE(eOpcodes::SLINE, "SLINE", FF7WalkmeshInstruction, 0, "NNNssssss");
  573. // Backgnd
  574. OPCODE(eOpcodes::BGPDH, "BGPDH", FF7BackgroundInstruction, 0, "NBs");
  575. OPCODE(eOpcodes::BGSCR, "BGSCR", FF7BackgroundInstruction, 0, "NBss");
  576. OPCODE(eOpcodes::MPPAL, "MPPAL", FF7BackgroundInstruction, 0, "NNNBBBBBBB");
  577. OPCODE(eOpcodes::BGON, "BGON", FF7BackgroundInstruction, 0, "NBB");
  578. OPCODE(eOpcodes::BGOFF, "BGOFF", FF7BackgroundInstruction, 0, "NBB");
  579. OPCODE(eOpcodes::BGROL, "BGROL", FF7BackgroundInstruction, 0, "NB");
  580. OPCODE(eOpcodes::BGROL2, "BGROL2", FF7BackgroundInstruction, 0, "NB");
  581. OPCODE(eOpcodes::BGCLR, "BGCLR", FF7BackgroundInstruction, 0, "NB");
  582. OPCODE(eOpcodes::STPAL, "STPAL", FF7BackgroundInstruction, 0, "NBBB");
  583. OPCODE(eOpcodes::LDPAL, "LDPAL", FF7BackgroundInstruction, 0, "NBBB");
  584. OPCODE(eOpcodes::CPPAL, "CPPAL", FF7BackgroundInstruction, 0, "NBBB");
  585. OPCODE(eOpcodes::RTPAL, "RTPAL", FF7BackgroundInstruction, 0, "NNBBBB");
  586. OPCODE(eOpcodes::ADPAL, "ADPAL", FF7BackgroundInstruction, 0, "NNNBBBBBB");
  587. OPCODE(eOpcodes::MPPAL2, "MPPAL2", FF7BackgroundInstruction, 0, "NNNBBBBBB");
  588. OPCODE(eOpcodes::STPLS, "STPLS", FF7BackgroundInstruction, 0, "BBBB");
  589. OPCODE(eOpcodes::LDPLS, "LDPLS", FF7BackgroundInstruction, 0, "BBBB");
  590. OPCODE(eOpcodes::CPPAL2, "CPPAL2", FF7BackgroundInstruction, 0, "BBBBBBB");
  591. OPCODE(eOpcodes::RTPAL2, "RTPAL2", FF7BackgroundInstruction, 0, "BBBBBBB");
  592. OPCODE(eOpcodes::ADPAL2, "ADPAL2", FF7BackgroundInstruction, 0, "BBBBBBBBBB");
  593. // Camera
  594. OPCODE(eOpcodes::NFADE, "NFADE", FF7CameraInstruction, 0, "NNBBBBBB");
  595. OPCODE(eOpcodes::SHAKE, "SHAKE", FF7CameraInstruction, 0, "BBBBBBB");
  596. OPCODE(eOpcodes::SCRLO, "SCRLO", FF7CameraInstruction, 0, "B");
  597. OPCODE(eOpcodes::SCRLC, "SCRLC", FF7CameraInstruction, 0, "BBBB");
  598. OPCODE(eOpcodes::SCRLA, "SCRLA", FF7CameraInstruction, 0, "NwBB");
  599. OPCODE(eOpcodes::SCR2D, "SCR2D", FF7CameraInstruction, 0, "Nss");
  600. OPCODE(eOpcodes::SCRCC, "SCRCC", FF7CameraInstruction, 0, "");
  601. OPCODE(eOpcodes::SCR2DC, "SCR2DC", FF7CameraInstruction, 0, "NNssw");
  602. OPCODE(eOpcodes::SCRLW, "SCRLW", FF7CameraInstruction, 0, "");
  603. OPCODE(eOpcodes::SCR2DL, "SCR2DL", FF7CameraInstruction, 0, "NNssw");
  604. OPCODE(eOpcodes::VWOFT, "VWOFT", FF7CameraInstruction, 0, "NssB");
  605. OPCODE(eOpcodes::FADE, "FADE", FF7CameraInstruction, 0, "NNBBBBBB");
  606. OPCODE(eOpcodes::FADEW, "FADEW", FF7CameraInstruction, 0, "");
  607. OPCODE(eOpcodes::SCRLP, "SCRLP", FF7CameraInstruction, 0, "NwBB");
  608. OPCODE(eOpcodes::MVCAM, "MVCAM", FF7CameraInstruction, 0, "B");
  609. // AV
  610. OPCODE(eOpcodes::BGMOVIE, "BGMOVIE", FF7AudioVideoInstruction, 0, "B");
  611. OPCODE(eOpcodes::AKAO2, "AKAO2", FF7AudioVideoInstruction, 0, "NNNBwwwww");
  612. OPCODE(eOpcodes::MUSIC, "MUSIC", FF7AudioVideoInstruction, 0, "B");
  613. OPCODE(eOpcodes::SOUND, "SOUND", FF7AudioVideoInstruction, 0, "NwB");
  614. OPCODE(eOpcodes::AKAO, "AKAO", FF7AudioVideoInstruction, 0, "NNNBBwwww");
  615. OPCODE(eOpcodes::MUSVT, "MUSVT", FF7AudioVideoInstruction, 0, "B");
  616. OPCODE(eOpcodes::MUSVM, "MUSVM", FF7AudioVideoInstruction, 0, "B");
  617. OPCODE(eOpcodes::MULCK, "MULCK", FF7AudioVideoInstruction, 0, "B");
  618. OPCODE(eOpcodes::BMUSC, "BMUSC", FF7AudioVideoInstruction, 0, "B");
  619. OPCODE(eOpcodes::CHMPH, "CHMPH", FF7AudioVideoInstruction, 0, "BBB");
  620. OPCODE(eOpcodes::PMVIE, "PMVIE", FF7AudioVideoInstruction, 0, "B");
  621. OPCODE(eOpcodes::MOVIE, "MOVIE", FF7AudioVideoInstruction, 0, "");
  622. OPCODE(eOpcodes::MVIEF, "MVIEF", FF7AudioVideoInstruction, 0, "NB");
  623. OPCODE(eOpcodes::FMUSC, "FMUSC", FF7AudioVideoInstruction, 0, "B");
  624. OPCODE(eOpcodes::CMUSC, "CMUSC", FF7AudioVideoInstruction, 0, "BBBBBBB");
  625. OPCODE(eOpcodes::CHMST, "CHMST", FF7AudioVideoInstruction, 0, "NB");
  626. // Uncat
  627. OPCODE(eOpcodes::MPDSP, "MPDSP", FF7UncategorizedInstruction, 0, "B");
  628. OPCODE(eOpcodes::SETX, "SETX", FF7UncategorizedInstruction, 0, "BBBBBB");
  629. OPCODE(eOpcodes::GETX, "GETX", FF7UncategorizedInstruction, 0, "BBBBBB");
  630. OPCODE(eOpcodes::SEARCHX, "SEARCHX", FF7UncategorizedInstruction, 0, "BBBBBBBBBB");
  631. default:
  632. throw UnknownOpcodeException(this->_address, opcode);
  633. }
  634. INC_ADDR;
  635. // Are we within an "if" statement tracking
  636. InstPtr i = this->_insts.back();
  637. if (i->isCondJump())
  638. {
  639. exitAddrs.push_back(i->getDestAddress());
  640. }
  641. if (!exitAddrs.empty())
  642. {
  643. if (i->_address == exitAddrs.back())
  644. {
  645. exitAddrs.pop_back();
  646. }
  647. }
  648. // Only bail if its the first RET that isn't within an "if" block
  649. if (full_opcode == eOpcodes::RET && exitAddrs.empty())
  650. {
  651. return;
  652. }
  653. }
  654. }