WorldDisassembler.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/format.hpp>
  16. #include <stdexcept>
  17. #include "decompiler/world/WorldDisassembler.h"
  18. #include "decompiler/world/WorldEngine.h"
  19. #include "decompiler/world/instruction/WorldBinaryEqualStackInstruction.h"
  20. #include "decompiler/world/instruction/WorldCondJumpInstruction.h"
  21. #include "decompiler/world/instruction/WorldKernelCallInstruction.h"
  22. #include "decompiler/world/instruction/WorldLoadBankInstruction.h"
  23. #include "decompiler/world/instruction/WorldLoadInstruction.h"
  24. #include "decompiler/world/instruction/WorldNoOutputInstruction.h"
  25. #include "decompiler/world/instruction/WorldStackInstruction.h"
  26. #include "decompiler/world/instruction/WorldStoreInstruction.h"
  27. #include "decompiler/world/instruction/WorldSubStackInstruction.h"
  28. #include "decompiler/world/instruction/WorldUncondJumpInstruction.h"
  29. WorldDisassembler::WorldDisassembler(WorldEngine* engine, InstVec &insts, int script_number):
  30. Disassembler(insts), engine_(engine), script_number_(script_number){}
  31. WorldDisassembler::~WorldDisassembler(){}
  32. /*
  33. 00000202: reset stack (0)
  34. 00000203: push byte from bank 0 897 (2)
  35. 00000205: jump if false 25 (0)
  36. 00000207: reset stack (0)
  37. 00000208: push byte from bank 0 897 (2)
  38. 0000020a: push byte from bank 0 897 (2)
  39. 0000020c: push constant 1 (2)
  40. 0000020e: push sub (2)
  41. 0000020f: write bank (0)
  42. 00000210: reset stack (0)
  43. 00000211: push byte from bank 0 897 (2)
  44. 00000213: push constant 0 (2)
  45. 00000215: push equal (2)
  46. 00000216: jump if false 25 (0)
  47. 00000218: unknown 320 (0)
  48. 00000219: return (0)
  49. if(!Read(897))
  50. {
  51. Write(897-1)
  52. if (!(Read(897) == 0))
  53. {
  54. unknown320()
  55. }
  56. }
  57. */
  58. void WorldDisassembler::DoDisassemble(){
  59. struct WorldScriptData{
  60. uint16 type = 0;
  61. uint16 instruction_pointer = 0;
  62. };
  63. std::vector<WorldScriptData> world_data;
  64. world_data.reserve(256);
  65. for (int i = 0; i < 256; i ++){
  66. WorldScriptData tmp;
  67. tmp.type = this->stream_->ReadU16();
  68. tmp.instruction_pointer = this->stream_->ReadU16();
  69. world_data.emplace_back(tmp);
  70. }
  71. if (script_number_ < 0 || script_number_ >= static_cast<int>(world_data.size())){
  72. throw std::out_of_range("World-map script index is outside the call table.");
  73. }
  74. const WorldScriptData& data = world_data[script_number_];
  75. if (data.type == 0 && data.instruction_pointer == 0) return;
  76. const unsigned int code_offset = static_cast<unsigned int>(
  77. 256 * sizeof(uint32) + data.instruction_pointer * sizeof(uint16)
  78. );
  79. if (code_offset >= stream_->GetSize()) return;
  80. this->stream_->Seek(code_offset);
  81. this->address_ = this->stream_->GetPosition();
  82. bool end = false;
  83. // The farthest address that a jump of the script goes to. A return doesn't end the script
  84. // while a jump goes after it.
  85. uint32 farthest_jump_target = 0;
  86. for (;;){
  87. if (end) break;
  88. uint32 full_opcode = 0;
  89. uint16 opcode = this->stream_->ReadU16();
  90. std::string opcode_prefix;
  91. if (opcode == 0x200 || opcode == 0x201){
  92. const auto parameter_position = this->stream_->GetPosition();
  93. const uint16 target = this->stream_->ReadU16();
  94. this->stream_->Seek(parameter_position);
  95. // Same address as the instruction addresses before they are divided by 2.
  96. farthest_jump_target = std::max<uint32>(
  97. farthest_jump_target, (0x200 + target) * 2
  98. );
  99. }
  100. if (opcode == 0x203) end = this->address_ >= farthest_jump_target;
  101. if (opcode >= 0x204 && opcode < 0x300)
  102. ParseOpcode(opcode, "reset stack", new WorldKernelCallInstruction(), 0, "");
  103. else{
  104. switch (opcode){
  105. case 0x100:
  106. ParseOpcode(opcode, "reset stack", new WorldNoOutputInstruction(), 0, "");
  107. break;
  108. case 0x201:
  109. ParseOpcode(opcode, "jump if false", new WorldCondJumpInstruction(), 0, "w");
  110. break;
  111. case 0x203:
  112. ParseOpcode(opcode, "return", new WorldKernelCallInstruction(), 0, "");
  113. break;
  114. case 0x200:
  115. ParseOpcode(opcode, "jump", new WorldUncondJumpInstruction(), 0, "w");
  116. break;
  117. case 0x118:
  118. ParseOpcode(opcode, "push byte from bank 0", new WorldLoadBankInstruction(), 2, "w");
  119. break;
  120. case 0x110:
  121. ParseOpcode(opcode, "push constant", new WorldLoadInstruction(), 2, "w");
  122. break;
  123. case 0x119:
  124. ParseOpcode(opcode, "push byte from bank 1", new WorldLoadBankInstruction(), 2, "w");
  125. break;
  126. case 0x11c:
  127. ParseOpcode(opcode, "push word from bank 0", new WorldLoadBankInstruction(), 2, "w");
  128. break;
  129. case 0x11d:
  130. ParseOpcode(opcode, "push word from bank 1", new WorldLoadBankInstruction(), 2, "w");
  131. break;
  132. case 0x114:
  133. ParseOpcode(opcode, "push bit from bank 0", new WorldLoadBankInstruction(), 2, "w");
  134. break;
  135. case 0x11b:
  136. ParseOpcode(opcode, "push special", new WorldLoadBankInstruction(), 2, "w");
  137. break;
  138. case 0x11f:
  139. ParseOpcode(opcode, "push special", new WorldLoadInstruction(), 2, "w");
  140. break;
  141. case 0x117:
  142. ParseOpcode(opcode, "push special", new WorldLoadBankInstruction(), 2, "w");
  143. break;
  144. case 0x0e0:
  145. ParseOpcode(opcode, "write bank", new WorldStoreInstruction(), 0, "");
  146. break;
  147. case 0x019:
  148. ParseOpcode(
  149. opcode, "push distance from active entity to entity by model id",
  150. new WorldKernelCallInstruction(), 2, ""
  151. );
  152. break;
  153. case 0x018:
  154. ParseOpcode(
  155. opcode, "push distance from active entity to light",
  156. new WorldKernelCallInstruction(), 2, ""
  157. );
  158. break;
  159. case 0x01b:
  160. ParseOpcode(opcode, "push unknown 1b", new WorldSubStackInstruction(), 2, "");
  161. break;
  162. case 0x061:
  163. ParseOpcode(opcode, "push greater", new WorldSubStackInstruction(), 2, "");
  164. break;
  165. case 0x0b0:
  166. ParseOpcode(opcode, "push logicand", new WorldSubStackInstruction(), 2, "");
  167. break;
  168. case 0x062:
  169. ParseOpcode(opcode, "push lessequal", new WorldSubStackInstruction(), 2, "");
  170. break;
  171. case 0x030:
  172. ParseOpcode(opcode, "push mul", new WorldSubStackInstruction(), 2, "");
  173. break;
  174. case 0x051:
  175. ParseOpcode(opcode, "push shr", new WorldSubStackInstruction(), 2, "");
  176. break;
  177. case 0x080:
  178. ParseOpcode(opcode, "push and", new WorldSubStackInstruction(), 2, "");
  179. break;
  180. case 0x0a0:
  181. ParseOpcode(opcode, "push or", new WorldSubStackInstruction(), 2, "");
  182. break;
  183. case 0x070:
  184. ParseOpcode(opcode, "push equal", new WorldBinaryEqualStackInstruction(), 2, "");
  185. break;
  186. case 0x041:
  187. ParseOpcode(opcode, "push sub", new WorldSubStackInstruction(), 2, "");
  188. break;
  189. case 0x063:
  190. ParseOpcode(opcode, "push greaterequal", new WorldSubStackInstruction(), 2, "");
  191. break;
  192. case 0x017:
  193. ParseOpcode(opcode, "push logicnot", new WorldSubStackInstruction(), 2, "");
  194. break;
  195. case 0x060:
  196. ParseOpcode(opcode, "push less", new WorldSubStackInstruction(), 2, "");
  197. break;
  198. case 0x0c0:
  199. ParseOpcode(opcode, "push logicor", new WorldSubStackInstruction(), 2, "");
  200. break;
  201. case 0x040:
  202. ParseOpcode(opcode, "push add", new WorldSubStackInstruction(), 2, "");
  203. break;
  204. case 0x015:
  205. ParseOpcode(opcode, "push neg", new WorldSubStackInstruction(), 2, "");
  206. break;
  207. case 0x31b:
  208. ParseOpcode(opcode, "unknown 31b", new WorldKernelCallInstruction(), 0, "");
  209. break;
  210. case 0x33c:
  211. ParseOpcode(opcode, "unknown 33c", new WorldKernelCallInstruction(), 0, "");
  212. break;
  213. case 0x355:
  214. ParseOpcode(opcode, "unknown 355", new WorldKernelCallInstruction(), 0, "");
  215. break;
  216. case 0x320:
  217. ParseOpcode(opcode, "unknown 320", new WorldKernelCallInstruction(), 0, "");
  218. break;
  219. case 0x34a:
  220. ParseOpcode(opcode, "unknown 34a", new WorldKernelCallInstruction(), 0, "");
  221. break;
  222. case 0x334:
  223. ParseOpcode(opcode, "unknown 334", new WorldKernelCallInstruction(), 0, "");
  224. break;
  225. case 0x33a:
  226. ParseOpcode(opcode, "unknown 33a", new WorldKernelCallInstruction(), 0, "");
  227. break;
  228. case 0x31c:
  229. ParseOpcode(opcode, "unknown 31c", new WorldKernelCallInstruction(), 0, "");
  230. break;
  231. case 0x32f:
  232. ParseOpcode(opcode, "unknown 32f", new WorldKernelCallInstruction(), 0, "");
  233. break;
  234. case 0x339:
  235. ParseOpcode(opcode, "unknown 339", new WorldKernelCallInstruction(), 0, "");
  236. break;
  237. case 0x30d:
  238. ParseOpcode(opcode, "unknown 30d", new WorldKernelCallInstruction(), 0, "");
  239. break;
  240. case 0x354:
  241. ParseOpcode(opcode, "unknown 354", new WorldKernelCallInstruction(), 0, "");
  242. break;
  243. case 0x332:
  244. ParseOpcode(opcode, "unknown 332", new WorldKernelCallInstruction(), 0, "");
  245. break;
  246. case 0x31f:
  247. ParseOpcode(opcode, "unknown 31f", new WorldKernelCallInstruction(), 0, "");
  248. break;
  249. case 0x329:
  250. ParseOpcode(opcode, "unknown 329", new WorldKernelCallInstruction(), 0, "");
  251. break;
  252. case 0x32a:
  253. ParseOpcode(opcode, "unknown 32a", new WorldKernelCallInstruction(), 0, "");
  254. break;
  255. case 0x34d:
  256. ParseOpcode(opcode, "unknown 3d4", new WorldKernelCallInstruction(), 0, "");
  257. break;
  258. case 0x34e:
  259. ParseOpcode(opcode, "unknown 3de", new WorldKernelCallInstruction(), 0, "");
  260. break;
  261. case 0x30a:
  262. ParseOpcode(opcode, "unknown 30a", new WorldKernelCallInstruction(), 0, "");
  263. break;
  264. case 0x333:
  265. ParseOpcode(opcode, "unknown 333", new WorldKernelCallInstruction(), 0, "");
  266. break;
  267. case 0x331:
  268. ParseOpcode(opcode, "unknown 331", new WorldKernelCallInstruction(), 0, "");
  269. break;
  270. case 0x319:
  271. ParseOpcode(opcode, "unknown 319", new WorldKernelCallInstruction(), 0, "");
  272. break;
  273. case 0x353:
  274. ParseOpcode(opcode, "unknown 353", new WorldKernelCallInstruction(), 0, "");
  275. break;
  276. case 0x33e:
  277. ParseOpcode(opcode, "unknown 33e", new WorldKernelCallInstruction(), 0, "");
  278. break;
  279. case 0x321:
  280. ParseOpcode(opcode, "unknown 321", new WorldKernelCallInstruction(), 0, "");
  281. break;
  282. case 0x31d:
  283. ParseOpcode(opcode, "play sound effect", new WorldKernelCallInstruction(), 2, "w");
  284. break;
  285. case 0x336:
  286. ParseOpcode(
  287. opcode, "set active entity movespeed (honor walkmesh)",
  288. new WorldKernelCallInstruction(), 0, ""
  289. );
  290. break;
  291. case 0x328:
  292. ParseOpcode(opcode, "set active entity direction", new WorldKernelCallInstruction(), 0, "");
  293. break;
  294. case 0x33d:
  295. ParseOpcode(opcode, "set field entry point2?", new WorldKernelCallInstruction(), 0, "");
  296. break;
  297. case 0x351:
  298. ParseOpcode(opcode, "set music volume", new WorldKernelCallInstruction(), 0, "");
  299. break;
  300. case 0x352:
  301. ParseOpcode(opcode, "shake camera on / off", new WorldKernelCallInstruction(), 0, "");
  302. break;
  303. case 0x34f:
  304. ParseOpcode(opcode, "set active entity y position", new WorldKernelCallInstruction(), 0, "");
  305. break;
  306. case 0x30b:
  307. ParseOpcode(opcode, "set active entity y offset", new WorldKernelCallInstruction(), 0, "");
  308. break;
  309. case 0x347:
  310. ParseOpcode(
  311. opcode, "move active entity to entity by model id ?", new WorldKernelCallInstruction(), 0, ""
  312. );
  313. break;
  314. case 0x30e:
  315. ParseOpcode(opcode, "active entity play animation", new WorldKernelCallInstruction(), 0, "");
  316. break;
  317. case 0x305:
  318. ParseOpcode(opcode, "set wait frames", new WorldKernelCallInstruction(), 0, "");
  319. break;
  320. case 0x306:
  321. ParseOpcode(opcode, "wait?", new WorldKernelCallInstruction(), 0, "");
  322. break;
  323. case 0x307:
  324. ParseOpcode(opcode, "set control lock", new WorldKernelCallInstruction(), 0, "");
  325. break;
  326. case 0x303:
  327. ParseOpcode(opcode, "set active entity movespeed", new WorldKernelCallInstruction(), 0, "");
  328. break;
  329. case 0x32c:
  330. ParseOpcode(opcode, "set window parameters", new WorldKernelCallInstruction(), 0, "");
  331. break;
  332. case 0x32d:
  333. ParseOpcode(opcode, "wait for window ready", new WorldKernelCallInstruction(), 0, "");
  334. break;
  335. case 0x324:
  336. ParseOpcode(opcode, "set window dimensions", new WorldKernelCallInstruction(), 0, "");
  337. break;
  338. case 0x325:
  339. ParseOpcode(opcode, "set window message", new WorldKernelCallInstruction(), 0, "");
  340. break;
  341. case 0x32e:
  342. ParseOpcode(opcode, "wait for message acknowledge", new WorldKernelCallInstruction(), 0, "");
  343. break;
  344. case 0x32b:
  345. ParseOpcode(opcode, "set battle lock", new WorldKernelCallInstruction(), 0, "");
  346. break;
  347. case 0x326:
  348. ParseOpcode(opcode, "set window prompt", new WorldKernelCallInstruction(), 0, "");
  349. break;
  350. case 0x327:
  351. ParseOpcode(opcode, "wait for prompt acknowledge ?", new WorldKernelCallInstruction(), 0, "");
  352. break;
  353. case 0x318:
  354. ParseOpcode(opcode, "enter field scene", new WorldKernelCallInstruction(), 0, "");
  355. break;
  356. case 0x33b:
  357. ParseOpcode(opcode, "fade out?", new WorldKernelCallInstruction(), 0, "");
  358. break;
  359. case 0x348:
  360. ParseOpcode(opcode, "fade in ?", new WorldKernelCallInstruction(), 0, "");
  361. break;
  362. case 0x330:
  363. ParseOpcode(opcode, "set active entity", new WorldKernelCallInstruction(), 0, "");
  364. break;
  365. case 0x350:
  366. ParseOpcode(opcode, "set meteor texture on/off", new WorldKernelCallInstruction(), 0, "");
  367. break;
  368. case 0x34b:
  369. ParseOpcode(opcode, "set chocobo type", new WorldKernelCallInstruction(), 0, "");
  370. break;
  371. case 0x34c:
  372. ParseOpcode(opcode, "set submarine color", new WorldKernelCallInstruction(), 0, "");
  373. break;
  374. case 0x349:
  375. ParseOpcode(opcode, "set world progress", new WorldKernelCallInstruction(), 0, ""
  376. );
  377. break;
  378. case 0x300:
  379. ParseOpcode(opcode, "load model", new WorldKernelCallInstruction(), 0, "");
  380. break;
  381. case 0x302:
  382. ParseOpcode(opcode, "set player entity", new WorldKernelCallInstruction(), 0, "");
  383. break;
  384. case 0x30c:
  385. ParseOpcode(opcode, "enter vehicle?", new WorldKernelCallInstruction(), 0, "");
  386. break;
  387. case 0x308:
  388. ParseOpcode(
  389. opcode, "set active entity mesh coordinates", new WorldKernelCallInstruction(), 0, ""
  390. );
  391. break;
  392. case 0x309:
  393. ParseOpcode(
  394. opcode, "set active entity coordinates in mesh", new WorldKernelCallInstruction(), 0, ""
  395. );
  396. break;
  397. case 0x304:
  398. ParseOpcode(
  399. opcode, "set active entity direction & facing", new WorldKernelCallInstruction(), 0, ""
  400. );
  401. break;
  402. case 0x310:
  403. ParseOpcode(opcode, "set active point", new WorldKernelCallInstruction(), 0, "");
  404. break;
  405. case 0x311:
  406. ParseOpcode(opcode, "set point mesh coordinates", new WorldKernelCallInstruction(), 0, "" );
  407. break;
  408. case 0x312:
  409. ParseOpcode(opcode, "set point coordinates in mesh", new WorldKernelCallInstruction(), 0, "" );
  410. break;
  411. case 0x313:
  412. ParseOpcode(opcode, "set point terrain BGR", new WorldKernelCallInstruction(), 0, "" );
  413. break;
  414. case 0x314:
  415. ParseOpcode(opcode, "set point dropoff parameters", new WorldKernelCallInstruction(), 0, "");
  416. break;
  417. case 0x315:
  418. ParseOpcode(opcode, "set point sky BGR", new WorldKernelCallInstruction(), 0, "");
  419. break;
  420. case 0x316:
  421. ParseOpcode(opcode, "set point BGR3?", new WorldKernelCallInstruction(), 0, "");
  422. break;
  423. case 0x317:
  424. ParseOpcode(opcode, "trigger battle", new WorldKernelCallInstruction(), 0, "");
  425. break;
  426. default: throw UnknownOpcodeException(this->address_, opcode);
  427. }
  428. }
  429. this->address_ += 2;
  430. }
  431. for (auto& instruction : this->insts_) instruction->SetAddress(instruction->GetAddress() / 2);
  432. if (!insts_.empty()){
  433. const uint32 start_address = insts_.front()->GetAddress();
  434. const uint32 end_address = insts_.back()->GetAddress();
  435. Function function(start_address, end_address);
  436. function.name = "world_function_" + std::to_string(script_number_);
  437. engine_->functions[start_address] = function;
  438. }
  439. }