FieldDisassembler.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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 <vector>
  16. #include <boost/format.hpp>
  17. #include <boost/algorithm/string/split.hpp>
  18. #include <boost/algorithm/string.hpp>
  19. #include "FieldCodeGenerator.h"
  20. #include "FieldDisassembler.h"
  21. #include "installer/decompiler/Engine.h"
  22. #include "FieldEngine.h"
  23. #include "instruction/FieldBackgroundInstruction.h"
  24. #include "instruction/FieldCameraInstruction.h"
  25. #include "instruction/FieldCondJumpInstruction.h"
  26. #include "instruction/FieldControlFlowInstruction.h"
  27. #include "instruction/FieldMathInstruction.h"
  28. #include "instruction/FieldMediaInstruction.h"
  29. #include "instruction/FieldModelInstruction.h"
  30. #include "instruction/FieldModuleInstruction.h"
  31. #include "instruction/FieldNoOperationInstruction.h"
  32. #include "instruction/FieldPartyInstruction.h"
  33. #include "instruction/FieldUncategorizedInstruction.h"
  34. #include "instruction/FieldUncondJumpInstruction.h"
  35. #include "instruction/FieldWalkmeshInstruction.h"
  36. #include "instruction/FieldWindowInstruction.h"
  37. #include "common/Lzs.h"
  38. const int FieldDisassembler::MAGIC(0x0502);
  39. const int FieldDisassembler::NUM_SECTIONS(7);
  40. FieldDisassembler::FieldDisassembler(
  41. FieldScriptFormatter& formatter, FieldEngine* engine,
  42. InstVec& insts, const std::vector<unsigned char>& raw_script_data
  43. ): Disassembler(insts), engine_(engine), formatter_(formatter){
  44. loaded_from_raw_data_ = true;
  45. // If loading a raw section then skip the header section.
  46. section_pointers_size_ = 0;
  47. auto data_copy = raw_script_data;
  48. stream_ = std::make_unique<BinaryReader>(std::move(data_copy));
  49. ReadHeader();
  50. }
  51. FieldDisassembler::FieldDisassembler(FieldScriptFormatter& formatter, FieldEngine *engine, InstVec &insts):
  52. Disassembler(insts), engine_(engine), formatter_(formatter)
  53. {section_pointers_size_ = (sizeof(uint32) * NUM_SECTIONS);}
  54. float FieldDisassembler::GetScaleFactor() const{return scale_factor_;}
  55. FieldDisassembler::~FieldDisassembler(){}
  56. void FieldDisassembler::ReadHeader(){
  57. if (!loaded_from_raw_data_){
  58. // First read the file section pointers.
  59. for (int i = 0; i < NUM_SECTIONS; i ++) sections_[i] = stream_->ReadU32();
  60. // Now fix up from PSX RAM pointers to simple file offsets.
  61. const uint32 basePtr = sections_[0];
  62. for (int i = 0; i < NUM_SECTIONS; i++) sections_[i] = (sections_[i] - basePtr) + section_pointers_size_;
  63. // Now seek to the script section.
  64. stream_->Seek(sections_[SCRIPT]);
  65. }
  66. // Read the script header
  67. header_.Read(*stream_);
  68. scale_factor_ = static_cast<float>(header_.scale) / 512.0f;
  69. }
  70. void FieldDisassembler::Open(const char *filename){
  71. // Read all of the file, decompress it, then stuff it into a stream.
  72. stream_ = std::make_unique<BinaryReader>(Lzs::Decompress(BinaryReader::ReadAll(filename)));
  73. ReadHeader();
  74. }
  75. uint32 FieldDisassembler::GetEndOfScriptOffset(uint16 cur_entry_point, size_t entity_index, size_t script_index){
  76. uint16 next_entry_point = cur_entry_point;
  77. do{
  78. if (script_index + 1 >= 32){
  79. // If this is the very last script, so use the end of its
  80. // data which is the offset to strings.
  81. if (entity_index + 1 >= header_.entity_scripts.size()) return header_.offset_to_strings;
  82. else{
  83. // Wrap around to the next entity
  84. entity_index ++;
  85. script_index = 0;
  86. }
  87. }
  88. // Get the next script in the same entity.
  89. else script_index ++;
  90. next_entry_point = header_.entity_scripts[entity_index][script_index];
  91. } while (next_entry_point == cur_entry_point);
  92. return next_entry_point;
  93. }
  94. std::unique_ptr<Function> FieldDisassembler::StartFunction(size_t script_index){
  95. auto func = std::make_unique<Function>();
  96. func->ret_val = false;
  97. func->num_args = 0;
  98. func->name = "script_" + std::to_string(script_index);
  99. func->start_addr = address_;
  100. return func;
  101. }
  102. void FieldDisassembler::DoDisassemble(){
  103. // Loop through the scripts for each entity.
  104. std::set<std::string> used_entity_names;
  105. for (size_t entity_number = 0; entity_number < header_.entity_scripts.size(); entity_number ++){
  106. std::string original_name = header_.field_entity_names[entity_number].data();
  107. // If the entity name was blank in the file then use a consistent generated name
  108. if (original_name.empty()) original_name = "entity_" + std::to_string(entity_number);
  109. std::string entity_name = formatter_.GetFriendlyEntityName(original_name);
  110. // The original game allows several entities to share a name, such as a line and a model.
  111. // Here a name identifies the entity in scripts, the map and the engine, so keep it unique.
  112. if (!used_entity_names.insert(entity_name).second){
  113. entity_name += "_" + std::to_string(entity_number);
  114. used_entity_names.insert(entity_name);
  115. }
  116. // Only parse each script one.
  117. std::set<uint16> parsed_scripts;
  118. // The first script that has each entry point, and the scripts that repeat it.
  119. std::map<uint16, size_t> first_script_with_entry_point;
  120. std::vector<std::pair<size_t, size_t>> repeated_scripts;
  121. // The unused scripts at the end of an entity point to its last script, as the ones that
  122. // are empty point to the one after them. Those are not names for it.
  123. const uint16 last_script_entry_point = header_.entity_scripts[entity_number].back();
  124. std::vector<ScriptInfo> script_info;
  125. // Collect the scripts to parse
  126. for (
  127. size_t script_index = 0; script_index < header_.entity_scripts[entity_number].size(); script_index ++
  128. ){
  129. uint16 script_entry_point = header_.entity_scripts[entity_number][script_index];
  130. // If this scripts entry point is already parsed, don't do it again as it means
  131. // two scripts have the same entry, which only seems to be true for "empty" scripts.
  132. const uint32 next_script_entry_point = GetEndOfScriptOffset(
  133. script_entry_point, entity_number, script_index
  134. );
  135. const uint32 script_size = next_script_entry_point - script_entry_point;
  136. if (parsed_scripts.find(script_entry_point) != std::end(parsed_scripts)){
  137. // It is the same code as another script. If it is more than a return it has to
  138. // be a name for it, or the event that starts it would be lost. For example, a
  139. // line that starts a scene when it is crossed does it with the same code as when
  140. // OK is pressed.
  141. const size_t first = first_script_with_entry_point[script_entry_point];
  142. if (script_size > 1 && first > 0 && script_index > 0){
  143. const bool unused_tail = script_entry_point == last_script_entry_point;
  144. // Only the events of a line can be one of the unused ones: they have a name
  145. // (they are checked below, when it is known that the entity is a line).
  146. if (!unused_tail || script_index <= 6)
  147. repeated_scripts.push_back(std::make_pair(script_index, first));
  148. }
  149. continue;
  150. }
  151. parsed_scripts.insert(script_entry_point);
  152. first_script_with_entry_point[script_entry_point] = script_index;
  153. if (script_size > 0){
  154. ScriptInfo info = {script_entry_point, next_script_entry_point, script_index};
  155. script_info.push_back(info);
  156. }
  157. }
  158. for (auto it = script_info.begin(); it != script_info.end(); it ++){
  159. const bool is_start = it == script_info.begin();
  160. const bool is_end = it == (-- script_info.end());
  161. DisassembleIndivdualScript(
  162. entity_name, entity_number, it->index, it->entry_point, it->next_entry_point, is_start, is_end
  163. );
  164. }
  165. // Name the repeated scripts, now that it is known if the entity is a line.
  166. for (const auto& repeated : repeated_scripts){
  167. const bool is_line = engine_->EntityIsLine(entity_number);
  168. // The slots of an entity that is not a line don't have a name, only the ones of a
  169. // line do. The ones in the unused tail are kept only if they have one.
  170. if (
  171. header_.entity_scripts[entity_number][repeated.first]
  172. == header_.entity_scripts[entity_number].back()
  173. && !(is_line && repeated.first <= 6)
  174. ) continue;
  175. std::string alias = "script_" + std::to_string(repeated.first);
  176. if (repeated.first == 1) alias = "on_interact";
  177. else if (is_line){
  178. switch (repeated.first){
  179. case 2: alias = "on_approach"; break;
  180. case 3: alias = "on_cross"; break;
  181. case 4: alias = "on_near"; break;
  182. case 5: alias = "on_near_once"; break;
  183. case 6: alias = "on_leave"; break;
  184. default: break;
  185. }
  186. }
  187. std::string target;
  188. try{target = engine_->EntityByIndex(entity_number).FunctionByIndex(repeated.second);}
  189. catch (const DecompilerException&){continue;}
  190. if (alias == target) continue;
  191. engine_->AddEntityFunction(entity_name, entity_number, alias, repeated.first);
  192. engine_->AddFunctionAlias(entity_name, alias, target);
  193. }
  194. }
  195. }
  196. int FieldDisassembler::FindId(uint32 start_addr, uint32 end_addr, const InstVec& insts){
  197. for (const InstPtr& instruction : insts){
  198. if (instruction->GetAddress() >= start_addr && instruction->GetAddress() <= end_addr){
  199. if (instruction->GetOpcode() == OPCODES::opCodeCHAR) return instruction->GetParam(0)->GetSigned();
  200. }
  201. }
  202. return -1;
  203. }
  204. void FieldDisassembler::AddFunc(
  205. std::string entity_name, size_t entity_index, size_t script_index, uint32 next_script_entry_point,
  206. const bool is_start, bool is_end, bool to_return_only, std::string func_name
  207. ){
  208. bool is_line = false;
  209. std::vector<float> point_a = {0, 0, 0};
  210. std::vector<float> point_b = {0, 0, 0};
  211. const auto SCRIPT_ENTRY_POINT = stream_->GetPosition();
  212. // Read each block of opcodes up to a return.
  213. const size_t old_num_instructions = insts_.size();
  214. // Initialize the function.
  215. std::unique_ptr<Function> func = StartFunction(script_index);
  216. // Read.
  217. if (to_return_only){
  218. // Read opcodes to the end or bail at the first return.
  219. is_line = ReadOpCodesToPositionOrReturn(next_script_entry_point + section_pointers_size_, point_a, point_b);
  220. auto stream_pos = stream_->GetPosition();
  221. const size_t endPos = next_script_entry_point + section_pointers_size_;
  222. // Can't be the end if there is more data:
  223. if (stream_pos != endPos) is_end = false;
  224. }
  225. else{
  226. // Keep going until all of the scriptis fectched, i.e if bailed at a
  227. // return, then call again until everything is parsed.
  228. while (stream_->GetPosition() != next_script_entry_point + section_pointers_size_)
  229. is_line = ReadOpCodesToPositionOrReturn(next_script_entry_point + section_pointers_size_, point_a, point_b);
  230. }
  231. // Read metadata, mark start and/or end.
  232. std::string meta_data;
  233. if (is_start && is_end) meta_data = "start_end_";
  234. else if (is_start) meta_data = "start_";
  235. else if (is_end) meta_data = "end_";
  236. const size_t new_num_instructions = insts_.size();
  237. func->num_instructions = new_num_instructions - old_num_instructions;
  238. func->end_addr = insts_.back()->GetAddress();
  239. if (!func_name.empty()) func->name = func_name;
  240. if (engine_->EntityIsLine(entity_index)){
  241. switch (script_index){
  242. // main - on_update
  243. case 0: break;
  244. // [OK] - on_interact
  245. case 1: break;
  246. // Move - on_approach
  247. case 2: func->name = "on_approach"; break;
  248. // Move - on_cross
  249. case 3: func->name = "on_cross"; break;
  250. // Go - on_near
  251. case 4: func->name = "on_near"; break;
  252. // Go1x - on_near
  253. case 5: func->name = "on_near_once"; break;
  254. // GoAway - on_leave
  255. case 6: func->name = "on_leave"; break;
  256. }
  257. }
  258. int id = FindId(func->start_addr, func->end_addr, insts_);
  259. // If there is no ID check if there was an ID for this entity in any of
  260. // its other functions and use that instead.
  261. if (id == -1){
  262. for (auto& func : engine_->functions){
  263. FunctionMetaData func_meta_data(func.second.metadata);
  264. if (func_meta_data.GetEntityName() == entity_name && func_meta_data.GetCharacterId() != -1){
  265. id = func_meta_data.GetCharacterId();
  266. break;
  267. }
  268. }
  269. }
  270. meta_data += std::to_string(id) + "_" + entity_name;
  271. func->metadata = meta_data;
  272. engine_->functions[SCRIPT_ENTRY_POINT] = *func;
  273. engine_->AddEntityFunction(entity_name, entity_index, func->name, script_index);
  274. // If the entity is a line, mark it as so.
  275. if (is_line) engine_->MarkEntityAsLine(entity_index, true, point_a, point_b);
  276. }
  277. void FieldDisassembler::DisassembleIndivdualScript(
  278. std::string entity_name, size_t entity_index, size_t script_index,
  279. size_t script_entry_point, uint32 next_script_entry_point, bool is_start, bool is_end)
  280. {
  281. script_entry_point += section_pointers_size_;
  282. stream_->Seek(script_entry_point);
  283. address_base_ = stream_->GetPosition();
  284. address_ = address_base_;
  285. // "Normal" script
  286. if (script_index > 0){
  287. if (script_index == 1){
  288. AddFunc(
  289. entity_name, entity_index, script_index, next_script_entry_point, is_start, is_end, false, "on_interact"
  290. );
  291. }
  292. else AddFunc(entity_name, entity_index, script_index, next_script_entry_point, is_start, is_end, false, "");
  293. }
  294. else{
  295. const size_t end_pos = next_script_entry_point + section_pointers_size_;
  296. // Read the init script, which means stop at the first return.
  297. AddFunc(entity_name, entity_index, script_index, next_script_entry_point, is_start, is_end, true, "on_start");
  298. // Not at the end of this script? Then the remaining data is the "main" script
  299. auto stream_pos = stream_->GetPosition();
  300. if (stream_pos != end_pos){
  301. // The "main" script can have more than one return statement...
  302. AddFunc(
  303. entity_name, entity_index, script_index, next_script_entry_point, false, is_end, false, "on_update"
  304. );
  305. stream_pos = stream_->GetPosition();
  306. // ... but should end exactly on the end pos
  307. if (stream_pos != end_pos) throw DecompilerException();
  308. }
  309. }
  310. }
  311. FieldDisassembler::InstructionRecord FieldDisassembler::FLOW_OPCODES[] ={
  312. {1, OPCODES::RET, "RET", "", FieldControlFlowInstruction::Create},
  313. {1, OPCODES::REQ, "REQ", "BU", FieldControlFlowInstruction::Create},
  314. {1, OPCODES::REQSW, "REQSW", "BU", FieldControlFlowInstruction::Create},
  315. {1, OPCODES::REQEW, "REQEW", "BU", FieldControlFlowInstruction::Create},
  316. {1, OPCODES::NOP, "NOP", "", FieldNoOperationInstruction::Create},
  317. {1, OPCODES::IFUB, "IFUB", "NNBBBL", FieldNoOperationInstruction::Create}
  318. };
  319. std::map<std::string, const FieldDisassembler::InstructionRecord*>
  320. FieldDisassembler::FieldInstructions(){
  321. // Convert the array to a map that we can query on by opcode name.
  322. std::map<std::string, const InstructionRecord*> name_to_instruction_records;
  323. for (size_t i = 0; i < boost::size(FLOW_OPCODES); i ++)
  324. name_to_instruction_records[FLOW_OPCODES[i].opcode_name] = &FLOW_OPCODES[i];
  325. return name_to_instruction_records;
  326. }
  327. bool FieldDisassembler::ReadOpCodesToPositionOrReturn(
  328. size_t end_pos, std::vector<float>& point_a, std::vector<float>& point_b
  329. ){
  330. bool is_line = false;
  331. while (stream_->GetPosition() < end_pos){
  332. uint8 opcode = stream_->ReadU8();
  333. uint32 full_opcode = 0;
  334. full_opcode = (full_opcode << 8) + opcode;
  335. switch (opcode){
  336. // Flow
  337. case OPCODES::IFUB:
  338. ParseOpcode(full_opcode, "IFUB", new FieldCondJumpInstruction(), 0, "NBBBB");
  339. break;
  340. case OPCODES::RET:
  341. ParseOpcode(full_opcode, "RET", new FieldControlFlowInstruction(), 0, "");
  342. break;
  343. case OPCODES::REQ:
  344. ParseOpcode(full_opcode, "REQ", new FieldControlFlowInstruction(), 0, "BU");
  345. break;
  346. case OPCODES::REQSW:
  347. ParseOpcode(full_opcode, "REQSW", new FieldControlFlowInstruction(), 0, "BU");
  348. break;
  349. case OPCODES::REQEW:
  350. ParseOpcode(full_opcode, "REQEW", new FieldControlFlowInstruction(), 0, "BU");
  351. break;
  352. case OPCODES::PREQ:
  353. ParseOpcode(full_opcode, "PREQ", new FieldControlFlowInstruction(), 0, "BU");
  354. break;
  355. case OPCODES::PRQSW:
  356. ParseOpcode(full_opcode, "PRQSW", new FieldControlFlowInstruction(), 0, "BU");
  357. break;
  358. case OPCODES::PRQEW:
  359. ParseOpcode(full_opcode, "PRQEW", new FieldControlFlowInstruction(), 0, "BU");
  360. break;
  361. case OPCODES::RETTO:
  362. ParseOpcode(full_opcode, "RETTO", new FieldControlFlowInstruction(), 0, "U");
  363. break;
  364. case OPCODES::JMPF:
  365. ParseOpcode(full_opcode, "JMPF", new FieldUncondJumpInstruction(), 0, "B");
  366. break;
  367. case OPCODES::JMPFL:
  368. ParseOpcode(full_opcode, "JMPFL", new FieldUncondJumpInstruction(), 0, "w");
  369. break;
  370. case OPCODES::JMPB:
  371. ParseOpcode(full_opcode, "JMPB", new FieldUncondJumpInstruction(), 0, "B");
  372. break;
  373. case OPCODES::JMPBL:
  374. ParseOpcode(full_opcode, "JMPBL", new FieldUncondJumpInstruction(), 0, "w");
  375. break;
  376. case OPCODES::IFUBL:
  377. ParseOpcode(full_opcode, "IFUBL", new FieldCondJumpInstruction(), 0, "NBBBw");
  378. break;
  379. case OPCODES::IFSW:
  380. ParseOpcode(full_opcode, "IFSW", new FieldCondJumpInstruction(), 0, "NwwBB");
  381. break;
  382. case OPCODES::IFSWL:
  383. ParseOpcode(full_opcode, "IFSWL", new FieldCondJumpInstruction(), 0, "NwwBw");
  384. break;
  385. case OPCODES::IFUW:
  386. ParseOpcode(full_opcode, "IFUW", new FieldCondJumpInstruction(), 0, "NwwBB");
  387. break;
  388. case OPCODES::IFUWL:
  389. ParseOpcode(full_opcode, "IFUWL", new FieldCondJumpInstruction(), 0, "NwwBw");
  390. break;
  391. case OPCODES::WAIT:
  392. ParseOpcode(full_opcode, "WAIT", new FieldControlFlowInstruction(), 0, "w");
  393. break;
  394. case OPCODES::IFKEY:
  395. ParseOpcode(full_opcode, "IFKEY", new FieldCondJumpInstruction(), 0, "wB");
  396. break;
  397. case OPCODES::IFKEYON:
  398. ParseOpcode(full_opcode, "IFKEYON", new FieldCondJumpInstruction(), 0, "wB");
  399. break;
  400. case OPCODES::IFKEYOFF:
  401. ParseOpcode(full_opcode, "IFKEYOFF", new FieldCondJumpInstruction(), 0, "wB");
  402. break;
  403. case OPCODES::NOP:
  404. ParseOpcode(full_opcode, "NOP", new FieldNoOperationInstruction(), 0, "");
  405. break;
  406. case OPCODES::IFPRTYQ:
  407. ParseOpcode(full_opcode, "IFPRTYQ", new FieldCondJumpInstruction(), 0, "BB");
  408. break;
  409. case OPCODES::IFMEMBQ:
  410. ParseOpcode(full_opcode, "IFMEMBQ", new FieldCondJumpInstruction(), 0, "BB");
  411. break;
  412. // Module
  413. case OPCODES::DSKCG:
  414. ParseOpcode(full_opcode, "DSKCG", new FieldModuleInstruction(), 0, "B");
  415. break;
  416. case OPCODES::SPECIAL:
  417. full_opcode = 0;
  418. full_opcode = (full_opcode << 8) + OPCODES::SPECIAL;
  419. opcode = this->stream_->ReadU8();
  420. switch (opcode){
  421. case OPCODES_SPECIAL::ARROW:
  422. ParseOpcode(full_opcode, "ARROW", new FieldModuleInstruction(), 0, "B");
  423. break;
  424. case OPCODES_SPECIAL::PNAME:
  425. ParseOpcode(full_opcode, "PNAME", new FieldModuleInstruction(), 0, "B");
  426. break;
  427. case OPCODES_SPECIAL::GMSPD:
  428. ParseOpcode(full_opcode, "GMSPD", new FieldModuleInstruction(), 0, "B");
  429. break;
  430. case OPCODES_SPECIAL::SMSPD:
  431. ParseOpcode(full_opcode, "SMSPD", new FieldModuleInstruction(), 0, "BB");
  432. break;
  433. case OPCODES_SPECIAL::FLMAT:
  434. ParseOpcode(full_opcode, "FLMAT", new FieldModuleInstruction(), 0, "");
  435. break;
  436. case OPCODES_SPECIAL::FLITM:
  437. ParseOpcode(full_opcode, "FLITM", new FieldModuleInstruction(), 0, "");
  438. break;
  439. case OPCODES_SPECIAL::BTLCK:
  440. ParseOpcode(full_opcode, "BTLCK", new FieldModuleInstruction(), 0, "B");
  441. break;
  442. case OPCODES_SPECIAL::MVLCK:
  443. ParseOpcode(full_opcode, "MVLCK", new FieldModuleInstruction(), 0, "B");
  444. break;
  445. case OPCODES_SPECIAL::SPCNM:
  446. ParseOpcode(full_opcode, "SPCNM", new FieldModuleInstruction(), 0, "BB");
  447. break;
  448. case OPCODES_SPECIAL::RSGLB:
  449. ParseOpcode(full_opcode, "RSGLB", new FieldModuleInstruction(), 0, "");
  450. break;
  451. case OPCODES_SPECIAL::CLITM:
  452. ParseOpcode(full_opcode, "CLITM", new FieldModuleInstruction(), 0, "");
  453. break;
  454. default:
  455. throw UnknownSubOpcodeException(this->address_, opcode);\
  456. }
  457. this->address_++ ;
  458. break;
  459. case OPCODES::MINIGAME:
  460. ParseOpcode(full_opcode, "MINIGAME", new FieldModuleInstruction(), 0, "wsswBB");
  461. break;
  462. case OPCODES::BTMD2:
  463. ParseOpcode(full_opcode, "BTMD2", new FieldModuleInstruction(), 0, "d");
  464. break;
  465. case OPCODES::BTRLD:
  466. ParseOpcode(full_opcode, "BTRLD", new FieldModuleInstruction(), 0, "NB");
  467. break;
  468. case OPCODES::BTLTB:
  469. ParseOpcode(full_opcode, "BTLTB", new FieldModuleInstruction(), 0, "B");
  470. break;
  471. case OPCODES::MAPJUMP:
  472. ParseOpcode(full_opcode, "MAPJUMP", new FieldModuleInstruction(), 0, "wsswB");
  473. break;
  474. case OPCODES::LSTMP:
  475. ParseOpcode(full_opcode, "LSTMP", new FieldModuleInstruction(), 0, "NB");
  476. break;
  477. case OPCODES::BATTLE:
  478. ParseOpcode(full_opcode, "BATTLE", new FieldModuleInstruction(), 0, "Nw");
  479. break;
  480. case OPCODES::BTLON:
  481. ParseOpcode(full_opcode, "BTLON", new FieldModuleInstruction(), 0, "B");
  482. break;
  483. case OPCODES::BTLMD:
  484. ParseOpcode(full_opcode, "BTLMD", new FieldModuleInstruction(), 0, "w");
  485. break;
  486. case OPCODES::MPJPO:
  487. ParseOpcode(full_opcode, "MPJPO", new FieldModuleInstruction(), 0, "B");
  488. break;
  489. case OPCODES::PMJMP:
  490. ParseOpcode(full_opcode, "PMJMP", new FieldModuleInstruction(), 0, "w");
  491. break;
  492. case OPCODES::PMJMP2:
  493. ParseOpcode(full_opcode, "PMJMP2", new FieldModuleInstruction(), 0, "");
  494. break;
  495. case OPCODES::GAMEOVER:
  496. ParseOpcode(full_opcode, "GAMEOVER", new FieldModuleInstruction(), 0, "");
  497. break;
  498. // Math
  499. case OPCODES::PLUS_:
  500. ParseOpcode(full_opcode, "PLUS!", new FieldMathInstruction(), 0, "NBB");
  501. break;
  502. case OPCODES::PLUS2_:
  503. ParseOpcode(full_opcode, "PLUS2!", new FieldMathInstruction(), 0, "NBw");
  504. break;
  505. case OPCODES::MINUS_:
  506. ParseOpcode(full_opcode, "MINUS!", new FieldMathInstruction(), 0, "NBB");
  507. break;
  508. case OPCODES::MINUS2_:
  509. ParseOpcode(full_opcode, "MINUS2!", new FieldMathInstruction(), 0, "NBw");
  510. break;
  511. case OPCODES::INC_:
  512. ParseOpcode(full_opcode, "INC!", new FieldMathInstruction(), 0, "BB");
  513. break;
  514. case OPCODES::INC2_:
  515. ParseOpcode(full_opcode, "INC2!", new FieldMathInstruction(), 0, "BB");
  516. break;
  517. case OPCODES::DEC_:
  518. ParseOpcode(full_opcode, "DEC!", new FieldMathInstruction(), 0, "BB");
  519. break;
  520. case OPCODES::DEC2_:
  521. ParseOpcode(full_opcode, "DEC2!", new FieldMathInstruction(), 0, "BB");
  522. break;
  523. case OPCODES::RDMSD:
  524. ParseOpcode(full_opcode, "RDMSD", new FieldMathInstruction(), 0, "NB");
  525. break;
  526. case OPCODES::SETBYTE:
  527. ParseOpcode(full_opcode, "SETBYTE", new FieldMathInstruction(), 0, "NBB");
  528. break;
  529. case OPCODES::SETWORD:
  530. ParseOpcode(full_opcode, "SETWORD", new FieldMathInstruction(), 0, "NBw");
  531. break;
  532. case OPCODES::BITON:
  533. ParseOpcode(full_opcode, "BITON", new FieldMathInstruction(), 0, "NBB");
  534. break;
  535. case OPCODES::BITOFF:
  536. ParseOpcode(full_opcode, "BITOFF", new FieldMathInstruction(), 0, "NBB");
  537. break;
  538. case OPCODES::BITXOR:
  539. ParseOpcode(full_opcode, "BITXOR", new FieldMathInstruction(), 0, "NBB");
  540. break;
  541. case OPCODES::PLUS:
  542. ParseOpcode(full_opcode, "PLUS", new FieldMathInstruction(), 0, "NBB");
  543. break;
  544. case OPCODES::PLUS2:
  545. ParseOpcode(full_opcode, "PLUS2", new FieldMathInstruction(), 0, "NBw");
  546. break;
  547. case OPCODES::MINUS:
  548. ParseOpcode(full_opcode, "MINUS", new FieldMathInstruction(), 0, "NBB");
  549. break;
  550. case OPCODES::MINUS2:
  551. ParseOpcode(full_opcode, "MINUS2", new FieldMathInstruction(), 0, "NBw");
  552. break;
  553. case OPCODES::MUL:
  554. ParseOpcode(full_opcode, "MUL", new FieldMathInstruction(), 0, "NBB");
  555. break;
  556. case OPCODES::MUL2:
  557. ParseOpcode(full_opcode, "MUL2", new FieldMathInstruction(), 0, "NBw");
  558. break;
  559. case OPCODES::DIV:
  560. ParseOpcode(full_opcode, "DIV", new FieldMathInstruction(), 0, "NBB");
  561. break;
  562. case OPCODES::DIV2:
  563. ParseOpcode(full_opcode, "DIV2", new FieldMathInstruction(), 0, "NBw");
  564. break;
  565. case OPCODES::MOD:
  566. ParseOpcode(full_opcode, "MOD", new FieldMathInstruction(), 0, "NBB");
  567. break;
  568. case OPCODES::MOD2:
  569. ParseOpcode(full_opcode, "MOD2", new FieldMathInstruction(), 0, "NBw");
  570. break;
  571. case OPCODES::AND:
  572. ParseOpcode(full_opcode, "AND", new FieldMathInstruction(), 0, "NBB");
  573. break;
  574. case OPCODES::AND2:
  575. ParseOpcode(full_opcode, "AND2", new FieldMathInstruction(), 0, "NBw");
  576. break;
  577. case OPCODES::OR:
  578. ParseOpcode(full_opcode, "OR", new FieldMathInstruction(), 0, "NBB");
  579. break;
  580. case OPCODES::OR2:
  581. ParseOpcode(full_opcode, "OR2", new FieldMathInstruction(), 0, "NBw");
  582. break;
  583. case OPCODES::XOR:
  584. ParseOpcode(full_opcode, "XOR", new FieldMathInstruction(), 0, "NBB");
  585. break;
  586. case OPCODES::XOR2:
  587. ParseOpcode(full_opcode, "XOR2", new FieldMathInstruction(), 0, "NBw");
  588. break;
  589. case OPCODES::INC:
  590. ParseOpcode(full_opcode, "INC", new FieldMathInstruction(), 0, "BB");
  591. break;
  592. case OPCODES::INC2:
  593. ParseOpcode(full_opcode, "INC2", new FieldMathInstruction(), 0, "BB");
  594. break;
  595. case OPCODES::DEC:
  596. ParseOpcode(full_opcode, "DEC", new FieldMathInstruction(), 0, "BB");
  597. break;
  598. case OPCODES::DEC2:
  599. ParseOpcode(full_opcode, "DEC2", new FieldMathInstruction(), 0, "BB");
  600. break;
  601. case OPCODES::RANDOM:
  602. ParseOpcode(full_opcode, "RANDOM", new FieldMathInstruction(), 0, "BB");
  603. break;
  604. case OPCODES::LBYTE:
  605. ParseOpcode(full_opcode, "LBYTE", new FieldMathInstruction(), 0, "NBB");
  606. break;
  607. case OPCODES::HBYTE:
  608. ParseOpcode(full_opcode, "HBYTE", new FieldMathInstruction(), 0, "NBw");
  609. break;
  610. case OPCODES::TWOBYTE:
  611. ParseOpcode(full_opcode, "2BYTE", new FieldMathInstruction(), 0, "NNBBB");
  612. break;
  613. case OPCODES::SIN:
  614. ParseOpcode(full_opcode, "SIN", new FieldMathInstruction(), 0, "NNwwwB");
  615. break;
  616. case OPCODES::COS:
  617. ParseOpcode(full_opcode, "COS", new FieldMathInstruction(), 0, "NNwwwB");
  618. break;
  619. // Window
  620. case OPCODES::TUTOR:
  621. ParseOpcode(full_opcode, "TUTOR", new FieldWindowInstruction(), 0, "B");
  622. break;
  623. case OPCODES::WCLS:
  624. ParseOpcode(full_opcode, "WCLS", new FieldWindowInstruction(), 0, "B");
  625. break;
  626. case OPCODES::WSIZW:
  627. ParseOpcode(full_opcode, "WSIZW", new FieldWindowInstruction(), 0, "Bwwww");
  628. break;
  629. case OPCODES::WSPCL:
  630. ParseOpcode(full_opcode, "WSPCL", new FieldWindowInstruction(), 0, "BBBB");
  631. break;
  632. case OPCODES::WNUMB:
  633. ParseOpcode(full_opcode, "WNUMB", new FieldWindowInstruction(), 0, "NBwwB");
  634. break;
  635. case OPCODES::STTIM:
  636. ParseOpcode(full_opcode, "STTIM", new FieldWindowInstruction(), 0, "NNBBB");
  637. break;
  638. case OPCODES::MESSAGE:
  639. ParseOpcode(full_opcode, "MESSAGE", new FieldWindowInstruction(), 0, "BB");
  640. break;
  641. case OPCODES::MPARA:
  642. ParseOpcode(full_opcode, "MPARA", new FieldWindowInstruction(), 0, "NBBB");
  643. break;
  644. case OPCODES::MPRA2:
  645. ParseOpcode(full_opcode, "MPRA2", new FieldWindowInstruction(), 0, "NBBw");
  646. break;
  647. case OPCODES::MPNAM:
  648. ParseOpcode(full_opcode, "MPNAM", new FieldWindowInstruction(), 0, "B");
  649. break;
  650. case OPCODES::ASK:
  651. ParseOpcode(full_opcode, "ASK", new FieldWindowInstruction(), 0, "NBBBBB");
  652. break;
  653. case OPCODES::MENU:
  654. ParseOpcode(full_opcode, "MENU", new FieldWindowInstruction(), 0, "NBB");
  655. break;
  656. case OPCODES::MENU2:
  657. ParseOpcode(full_opcode, "MENU2", new FieldWindowInstruction(), 0, "B");
  658. break;
  659. case OPCODES::WINDOW:
  660. ParseOpcode(full_opcode, "WINDOW", new FieldWindowInstruction(), 0, "Bwwww");
  661. break;
  662. case OPCODES::WMOVE:
  663. ParseOpcode(full_opcode, "WMOVE", new FieldWindowInstruction(), 0, "Bss");
  664. break;
  665. case OPCODES::WMODE:
  666. ParseOpcode(full_opcode, "WMODE", new FieldWindowInstruction(), 0, "BBB");
  667. break;
  668. case OPCODES::WREST:
  669. ParseOpcode(full_opcode, "WREST", new FieldWindowInstruction(), 0, "B");
  670. break;
  671. case OPCODES::WCLSE:
  672. ParseOpcode(full_opcode, "WCLSE", new FieldWindowInstruction(), 0, "B");
  673. break;
  674. case OPCODES::WROW:
  675. ParseOpcode(full_opcode, "WROW", new FieldWindowInstruction(), 0, "BB");
  676. break;
  677. case OPCODES::GWCOL:
  678. ParseOpcode(full_opcode, "GWCOL", new FieldWindowInstruction(), 0, "NNBBBB");
  679. break;
  680. case OPCODES::SWCOL:
  681. ParseOpcode(full_opcode, "SWCOL", new FieldWindowInstruction(), 0, "NNBBBB");
  682. break;
  683. // Party
  684. case OPCODES::SPTYE:
  685. ParseOpcode(full_opcode, "SPTYE", new FieldPartyInstruction(), 0, "NNBBB");
  686. break;
  687. case OPCODES::GTPYE:
  688. ParseOpcode(full_opcode, "GTPYE", new FieldPartyInstruction(), 0, "NNBBB");
  689. break;
  690. case OPCODES::GOLDU:
  691. // Goldu can have two argument formats: (nybble, word) or (nybble, byte, 3 bytes)
  692. // Read it as nybble, byte, byte, byte, byte and operate later
  693. ParseOpcode(full_opcode, "GOLDU", new FieldPartyInstruction(), 0, "NBBBB");
  694. break;
  695. case OPCODES::GOLDD:
  696. ParseOpcode(full_opcode, "GOLDD", new FieldPartyInstruction(), 0, "NBBBB");
  697. break;
  698. case OPCODES::CHGLD:
  699. ParseOpcode(full_opcode, "CHGLD", new FieldPartyInstruction(), 0, "NBB");
  700. break;
  701. case OPCODES::HMPMAX1:
  702. ParseOpcode(full_opcode, "HMPMAX1", new FieldPartyInstruction(), 0, "");
  703. break;
  704. case OPCODES::HMPMAX2:
  705. ParseOpcode(full_opcode, "HMPMAX2", new FieldPartyInstruction(), 0, "");
  706. break;
  707. case OPCODES::MHMMX:
  708. ParseOpcode(full_opcode, "MHMMX", new FieldPartyInstruction(), 0, "");
  709. break;
  710. case OPCODES::HMPMAX3:
  711. ParseOpcode(full_opcode, "HMPMAX3", new FieldPartyInstruction(), 0, "");
  712. break;
  713. case OPCODES::MPU:
  714. ParseOpcode(full_opcode, "MPU", new FieldPartyInstruction(), 0, "NBw");
  715. break;
  716. case OPCODES::MPD:
  717. ParseOpcode(full_opcode, "MPD", new FieldPartyInstruction(), 0, "NBw");
  718. break;
  719. case OPCODES::HPU:
  720. ParseOpcode(full_opcode, "HPU", new FieldPartyInstruction(), 0, "NBw");
  721. break;
  722. case OPCODES::HPD:
  723. ParseOpcode(full_opcode, "HPD", new FieldPartyInstruction(), 0, "NBw");
  724. break;
  725. case OPCODES::STITM:
  726. ParseOpcode(full_opcode, "STITM", new FieldPartyInstruction(), 0, "NwB");
  727. break;
  728. case OPCODES::DLITM:
  729. ParseOpcode(full_opcode, "DLITM", new FieldPartyInstruction(), 0, "NwB");
  730. break;
  731. case OPCODES::CKITM:
  732. ParseOpcode(full_opcode, "CKITM", new FieldPartyInstruction(), 0, "NwB");
  733. break;
  734. case OPCODES::SMTRA:
  735. ParseOpcode(full_opcode, "SMTRA", new FieldPartyInstruction(), 0, "NNBBBB");
  736. break;
  737. case OPCODES::DMTRA:
  738. ParseOpcode(full_opcode, "DMTRA", new FieldPartyInstruction(), 0, "NNBBBBB");
  739. break;
  740. case OPCODES::CMTRA:
  741. ParseOpcode(full_opcode, "CMTRA", new FieldPartyInstruction(), 0, "NNNBBBBBB");
  742. break;
  743. case OPCODES::GETPC:
  744. ParseOpcode(full_opcode, "GETPC", new FieldPartyInstruction(), 0, "NBB");
  745. break;
  746. case OPCODES::PRTYP:
  747. ParseOpcode(full_opcode, "PRTYP", new FieldPartyInstruction(), 0, "B");
  748. break;
  749. case OPCODES::PRTYM:
  750. ParseOpcode(full_opcode, "PRTYM", new FieldPartyInstruction(), 0, "B");
  751. break;
  752. case OPCODES::PRTYE:
  753. ParseOpcode(full_opcode, "PRTYE", new FieldPartyInstruction(), 0, "BBB");
  754. break;
  755. case OPCODES::MMBUD:
  756. ParseOpcode(full_opcode, "MMBUD", new FieldPartyInstruction(), 0, "BB");
  757. break;
  758. case OPCODES::MMBLK:
  759. ParseOpcode(full_opcode, "MMBLK", new FieldPartyInstruction(), 0, "B");
  760. break;
  761. case OPCODES::MMBUK:
  762. ParseOpcode(full_opcode, "MMBUK", new FieldPartyInstruction(), 0, "B");
  763. break;
  764. // Model
  765. case OPCODES::JOIN:
  766. ParseOpcode(full_opcode, "JOIN", new FieldModelInstruction(), 0, "B");
  767. break;
  768. case OPCODES::SPLIT:
  769. ParseOpcode(full_opcode, "SPLIT", new FieldModelInstruction(), 0, "NNNssBssBB");
  770. break;
  771. case OPCODES::BLINK:
  772. ParseOpcode(full_opcode, "BLINK", new FieldModelInstruction(), 0, "B");
  773. break;
  774. case OPCODES::KAWAI:
  775. {
  776. full_opcode = 0;
  777. full_opcode = (full_opcode << 8) + OPCODES::KAWAI;
  778. int length = this->stream_->ReadU8();
  779. assert(length >= 3);
  780. std::ostringstream param_stream;
  781. for (int i = 3; i < length; ++ i) param_stream << "B";
  782. auto parameters = param_stream.str();
  783. opcode = this->stream_->ReadU8();
  784. switch (opcode){
  785. case OPCODES_KAWAI::EYETX:
  786. // Parameters were "BBBB"
  787. ParseOpcode(opcode, "EYETX", new FieldModelInstruction(), 0, parameters.c_str());
  788. break;
  789. case OPCODES_KAWAI::TRNSP:
  790. // Parameters were "B"
  791. ParseOpcode(opcode, "TRNSP", new FieldModelInstruction(), 0, parameters.c_str());
  792. break;
  793. case OPCODES_KAWAI::AMBNT:
  794. // Parameters were "AMBNT"
  795. ParseOpcode(opcode, "AMBNT", new FieldModelInstruction(), 0, parameters.c_str());
  796. break;
  797. case OPCODES_KAWAI::Unknown03:
  798. ParseOpcode(opcode, "Unknown03", new FieldModelInstruction(), 0, parameters.c_str());
  799. break;
  800. case OPCODES_KAWAI::Unknown04:
  801. ParseOpcode(opcode, "Unknown04", new FieldModelInstruction(), 0, parameters.c_str());
  802. break;
  803. case OPCODES_KAWAI::Unknown05:
  804. ParseOpcode(opcode, "Unknown05", new FieldModelInstruction(), 0, parameters.c_str());
  805. break;
  806. case OPCODES_KAWAI::LIGHT:
  807. ParseOpcode(opcode, "LIGHT", new FieldModelInstruction(), 0, parameters.c_str());
  808. break;
  809. case OPCODES_KAWAI::Unknown07:
  810. ParseOpcode(opcode, "Unknown07", new FieldModelInstruction(), 0, parameters.c_str());
  811. break;
  812. case OPCODES_KAWAI::Unknown08:
  813. ParseOpcode(opcode, "Unknown08", new FieldModelInstruction(), 0, parameters.c_str());
  814. break;
  815. case OPCODES_KAWAI::Unknown09:
  816. ParseOpcode(opcode, "Unknown09", new FieldModelInstruction(), 0, parameters.c_str());
  817. break;
  818. case OPCODES_KAWAI::SBOBJ:
  819. ParseOpcode(opcode, "SBOBJ", new FieldModelInstruction(), 0, parameters.c_str());
  820. break;
  821. case OPCODES_KAWAI::Unknown0B:
  822. ParseOpcode(opcode, "Unknown0B", new FieldModelInstruction(), 0, parameters.c_str());
  823. break;
  824. case OPCODES_KAWAI::Unknown0C:
  825. ParseOpcode(opcode, "Unknown0C", new FieldModelInstruction(), 0, parameters.c_str());
  826. break;
  827. case OPCODES_KAWAI::SHINE:
  828. ParseOpcode(opcode, "SHINE", new FieldModelInstruction(), 0, parameters.c_str());
  829. break;
  830. case OPCODES_KAWAI::RESET:
  831. ParseOpcode(opcode, "RESET", new FieldModelInstruction(), 0, parameters.c_str());
  832. break;
  833. default:
  834. throw UnknownSubOpcodeException(this->address_, opcode);
  835. }
  836. this->address_ ++;
  837. this->address_ ++;
  838. break;
  839. }
  840. case OPCODES::KAWIW:
  841. ParseOpcode(full_opcode, "KAWIW", new FieldModelInstruction(), 0, "");
  842. break;
  843. case OPCODES::PMOVA:
  844. ParseOpcode(full_opcode, "PMOVA", new FieldModelInstruction(), 0, "B");
  845. break;
  846. case OPCODES::PDIRA:
  847. ParseOpcode(full_opcode, "PDIRA", new FieldModelInstruction(), 0, "B");
  848. break;
  849. case OPCODES::PTURA:
  850. ParseOpcode(full_opcode, "PTURA", new FieldModelInstruction(), 0, "BBB");
  851. break;
  852. case OPCODES::PGTDR:
  853. ParseOpcode(full_opcode, "PGTDR", new FieldModelInstruction(), 0, "NBB");
  854. break;
  855. case OPCODES::PXYZI:
  856. ParseOpcode(full_opcode, "PXYZI", new FieldModelInstruction(), 0, "NNBBBBB");
  857. break;
  858. case OPCODES::TLKON:
  859. ParseOpcode(full_opcode, "TLKON", new FieldModelInstruction(), 0, "B");
  860. break;
  861. case OPCODES::PC:
  862. ParseOpcode(full_opcode, "PC", new FieldModelInstruction(), 0, "B");
  863. break;
  864. case OPCODES::opCodeCHAR:
  865. ParseOpcode(full_opcode, "CHAR", new FieldModelInstruction(), 0, "B");
  866. break;
  867. case OPCODES::DFANM:
  868. ParseOpcode(full_opcode, "DFANM", new FieldModelInstruction(), 0, "BB");
  869. break;
  870. case OPCODES::ANIME1:
  871. ParseOpcode(full_opcode, "ANIME1", new FieldModelInstruction(), 0, "BB");
  872. break;
  873. case OPCODES::VISI:
  874. ParseOpcode(full_opcode, "VISI", new FieldModelInstruction(), 0, "B");
  875. break;
  876. case OPCODES::XYZI:
  877. ParseOpcode(full_opcode, "XYZI", new FieldModelInstruction(), 0, "NNsssw");
  878. break;
  879. case OPCODES::XYI:
  880. ParseOpcode(full_opcode, "XYI", new FieldModelInstruction(), 0, "NNssw");
  881. break;
  882. case OPCODES::XYZ:
  883. ParseOpcode(full_opcode, "XYZ", new FieldModelInstruction(), 0, "NNsss");
  884. break;
  885. case OPCODES::MOVE:
  886. ParseOpcode(full_opcode, "MOVE", new FieldModelInstruction(), 0, "Nss");
  887. break;
  888. case OPCODES::CMOVE:
  889. ParseOpcode(full_opcode, "CMOVE", new FieldModelInstruction(), 0, "Nss");
  890. break;
  891. case OPCODES::MOVA:
  892. ParseOpcode(full_opcode, "MOVA", new FieldModelInstruction(), 0, "B");
  893. break;
  894. case OPCODES::TURA:
  895. ParseOpcode(full_opcode, "TURA", new FieldModelInstruction(), 0, "BBB");
  896. break;
  897. case OPCODES::ANIMW:
  898. ParseOpcode(full_opcode, "ANIMW", new FieldModelInstruction(), 0, "");
  899. break;
  900. case OPCODES::FMOVE:
  901. ParseOpcode(full_opcode, "FMOVE", new FieldModelInstruction(), 0, "Nss");
  902. break;
  903. case OPCODES::ANIME2:
  904. ParseOpcode(full_opcode, "ANIME2", new FieldModelInstruction(), 0, "BB");
  905. break;
  906. case OPCODES::ANIM_1:
  907. ParseOpcode(full_opcode, "ANIM!1", new FieldModelInstruction(), 0, "BB");
  908. break;
  909. case OPCODES::CANIM1:
  910. ParseOpcode(full_opcode, "CANIM1", new FieldModelInstruction(), 0, "BBBB");
  911. break;
  912. case OPCODES::CANM_1:
  913. ParseOpcode(full_opcode, "CANM!1", new FieldModelInstruction(), 0, "BBBB");
  914. break;
  915. case OPCODES::MSPED:
  916. ParseOpcode(full_opcode, "MSPED", new FieldModelInstruction(), 0, "Nw");
  917. break;
  918. case OPCODES::DIR:
  919. ParseOpcode(full_opcode, "DIR", new FieldModelInstruction(), 0, "BB");
  920. break;
  921. case OPCODES::TURNGEN:
  922. ParseOpcode(full_opcode, "TURNGEN", new FieldModelInstruction(), 0, "NBBBB");
  923. break;
  924. case OPCODES::TURN:
  925. ParseOpcode(full_opcode, "TURN", new FieldModelInstruction(), 0, "NBBBB");
  926. break;
  927. case OPCODES::DIRA:
  928. ParseOpcode(full_opcode, "DIRA", new FieldModelInstruction(), 0, "B");
  929. break;
  930. case OPCODES::GETDIR:
  931. ParseOpcode(full_opcode, "GETDIR", new FieldModelInstruction(), 0, "NBB");
  932. break;
  933. case OPCODES::GETAXY:
  934. ParseOpcode(full_opcode, "GETAXY", new FieldModelInstruction(), 0, "NBBB");
  935. break;
  936. case OPCODES::GETAI:
  937. ParseOpcode(full_opcode, "GETAI", new FieldModelInstruction(), 0, "NBB");
  938. break;
  939. case OPCODES::ANIM_2:
  940. ParseOpcode(full_opcode, "ANIM!2", new FieldModelInstruction(), 0, "BB");
  941. break;
  942. case OPCODES::CANIM2:
  943. ParseOpcode(full_opcode, "CANIM2", new FieldModelInstruction(), 0, "BBBB");
  944. break;
  945. case OPCODES::CANM_2:
  946. ParseOpcode(full_opcode, "CANM!2", new FieldModelInstruction(), 0, "BBBB");
  947. break;
  948. case OPCODES::ASPED:
  949. ParseOpcode(full_opcode, "ASPED", new FieldModelInstruction(), 0, "Nw");
  950. break;
  951. case OPCODES::CC:
  952. ParseOpcode(full_opcode, "CC", new FieldModelInstruction(), 0, "B");
  953. break;
  954. case OPCODES::JUMP:
  955. ParseOpcode(full_opcode, "JUMP", new FieldModelInstruction(), 0, "NNssww");
  956. break;
  957. case OPCODES::AXYZI:
  958. ParseOpcode(full_opcode, "AXYZI", new FieldModelInstruction(), 0, "NNBBBBB");
  959. break;
  960. case OPCODES::LADER:
  961. ParseOpcode(full_opcode, "LADER", new FieldModelInstruction(), 0, "NNssswBBBB");
  962. break;
  963. case OPCODES::OFST:
  964. ParseOpcode(full_opcode, "OFST", new FieldModelInstruction(), 0, "NNBsssw");
  965. break;
  966. case OPCODES::OFSTW:
  967. ParseOpcode(full_opcode, "OFSTW", new FieldModelInstruction(), 0, "");
  968. break;
  969. case OPCODES::TALKR:
  970. ParseOpcode(full_opcode, "TALKR", new FieldModelInstruction(), 0, "NB");
  971. break;
  972. case OPCODES::SLIDR:
  973. ParseOpcode(full_opcode, "SLIDR", new FieldModelInstruction(), 0, "NB");
  974. break;
  975. case OPCODES::SOLID:
  976. ParseOpcode(full_opcode, "SOLID", new FieldModelInstruction(), 0, "B");
  977. break;
  978. case OPCODES::TLKR2:
  979. ParseOpcode(full_opcode, "TLKR2", new FieldModelInstruction(), 0, "Nw");
  980. break;
  981. case OPCODES::SLDR2:
  982. ParseOpcode(full_opcode, "SLDR2", new FieldModelInstruction(), 0, "Nw");
  983. break;
  984. case OPCODES::CCANM:
  985. ParseOpcode(full_opcode, "CCANM", new FieldModelInstruction(), 0, "BBB");
  986. break;
  987. case OPCODES::FCFIX:
  988. ParseOpcode(full_opcode, "FCFIX", new FieldModelInstruction(), 0, "B");
  989. break;
  990. case OPCODES::ANIMB:
  991. ParseOpcode(full_opcode, "ANIMB", new FieldModelInstruction(), 0, "");
  992. break;
  993. case OPCODES::TURNW:
  994. ParseOpcode(full_opcode, "TURNW", new FieldModelInstruction(), 0, "");
  995. break;
  996. // Walkmesh
  997. case OPCODES::SLIP:
  998. ParseOpcode(full_opcode, "SLIP", new FieldWalkmeshInstruction(), 0, "B");
  999. break;
  1000. case OPCODES::UC:
  1001. ParseOpcode(full_opcode, "UC", new FieldWalkmeshInstruction(), 0, "B");
  1002. break;
  1003. case OPCODES::IDLCK:
  1004. ParseOpcode(full_opcode, "IDLCK", new FieldWalkmeshInstruction(), 0, "wB");
  1005. break;
  1006. case OPCODES::LINON:
  1007. ParseOpcode(full_opcode, "LINON", new FieldWalkmeshInstruction(), 0, "B");
  1008. break;
  1009. case OPCODES::SLINE:
  1010. ParseOpcode(full_opcode, "SLINE", new FieldWalkmeshInstruction(), 0, "NNNssssss");
  1011. break;
  1012. case OPCODES::LINE:
  1013. // Mark function entity owner as line.
  1014. is_line = true;
  1015. ParseOpcode(full_opcode, "LINE", new FieldWalkmeshInstruction(), 0, "ssssss");
  1016. point_a[0] = this->insts_.back()->GetParam(0)->GetSigned();
  1017. point_a[1] = this->insts_.back()->GetParam(1)->GetSigned();
  1018. point_a[2] = this->insts_.back()->GetParam(2)->GetSigned();
  1019. point_b[0] = this->insts_.back()->GetParam(3)->GetSigned();
  1020. point_b[1] = this->insts_.back()->GetParam(4)->GetSigned();
  1021. point_b[2] = this->insts_.back()->GetParam(5)->GetSigned();
  1022. break;
  1023. // Backgnd
  1024. case OPCODES::BGPDH:
  1025. ParseOpcode(full_opcode, "BGPDH", new FieldBackgroundInstruction(), 0, "NBs");
  1026. break;
  1027. case OPCODES::BGSCR:
  1028. ParseOpcode(full_opcode, "BGSCR", new FieldBackgroundInstruction(), 0, "NBss");
  1029. break;
  1030. case OPCODES::MPPAL:
  1031. ParseOpcode(full_opcode, "MPPAL", new FieldBackgroundInstruction(), 0, "NNNBBBBBBB");
  1032. break;
  1033. case OPCODES::BGON:
  1034. ParseOpcode(full_opcode, "BGON", new FieldBackgroundInstruction(), 0, "NBB");
  1035. break;
  1036. case OPCODES::BGOFF:
  1037. ParseOpcode(full_opcode, "BGOFF", new FieldBackgroundInstruction(), 0, "NBB");
  1038. break;
  1039. case OPCODES::BGROL:
  1040. ParseOpcode(full_opcode, "BGROL", new FieldBackgroundInstruction(), 0, "NB");
  1041. break;
  1042. case OPCODES::BGROL2:
  1043. ParseOpcode(full_opcode, "BGROL2", new FieldBackgroundInstruction(), 0, "NB");
  1044. break;
  1045. case OPCODES::BGCLR:
  1046. ParseOpcode(full_opcode, "BGCLR", new FieldBackgroundInstruction(), 0, "NB");
  1047. break;
  1048. case OPCODES::STPAL:
  1049. ParseOpcode(full_opcode, "STPAL", new FieldBackgroundInstruction(), 0, "NBBB");
  1050. break;
  1051. case OPCODES::LDPAL:
  1052. ParseOpcode(full_opcode, "LDPAL", new FieldBackgroundInstruction(), 0, "NBBB");
  1053. break;
  1054. case OPCODES::CPPAL:
  1055. ParseOpcode(full_opcode, "CPPAL", new FieldBackgroundInstruction(), 0, "NBBB");
  1056. break;
  1057. case OPCODES::RTPAL:
  1058. ParseOpcode(full_opcode, "RTPAL", new FieldBackgroundInstruction(), 0, "NNBBBB");
  1059. break;
  1060. case OPCODES::ADPAL:
  1061. ParseOpcode(full_opcode, "ADPAL", new FieldBackgroundInstruction(), 0, "NNNBBBBBB");
  1062. break;
  1063. case OPCODES::MPPAL2:
  1064. ParseOpcode(full_opcode, "MPPAL2", new FieldBackgroundInstruction(), 0, "NNNBBBBBB");
  1065. break;
  1066. case OPCODES::STPLS:
  1067. ParseOpcode(full_opcode, "STPLS", new FieldBackgroundInstruction(), 0, "BBBB");
  1068. break;
  1069. case OPCODES::LDPLS:
  1070. ParseOpcode(full_opcode, "LDPLS", new FieldBackgroundInstruction(), 0, "BBBB");
  1071. break;
  1072. case OPCODES::CPPAL2:
  1073. ParseOpcode(full_opcode, "CPPAL2", new FieldBackgroundInstruction(), 0, "BBBBBBB");
  1074. break;
  1075. case OPCODES::RTPAL2:
  1076. ParseOpcode(full_opcode, "RTPAL2", new FieldBackgroundInstruction(), 0, "BBBBBBB");
  1077. break;
  1078. case OPCODES::ADPAL2:
  1079. ParseOpcode(full_opcode, "ADPAL2", new FieldBackgroundInstruction(), 0, "BBBBBBBBBB");
  1080. break;
  1081. // Camera
  1082. case OPCODES::NFADE:
  1083. ParseOpcode(full_opcode, "NFADE", new FieldCameraInstruction(), 0, "NNBBBBBB");
  1084. break;
  1085. case OPCODES::SHAKE:
  1086. ParseOpcode(full_opcode, "SHAKE", new FieldCameraInstruction(), 0, "BBBBBBB");
  1087. break;
  1088. case OPCODES::SCRLO:
  1089. ParseOpcode(full_opcode, "SCRLO", new FieldCameraInstruction(), 0, "B");
  1090. break;
  1091. case OPCODES::SCRLC:
  1092. ParseOpcode(full_opcode, "SCRLC", new FieldCameraInstruction(), 0, "BBBB");
  1093. break;
  1094. case OPCODES::SCRLA:
  1095. ParseOpcode(full_opcode, "SCRLA", new FieldCameraInstruction(), 0, "NwBB");
  1096. break;
  1097. case OPCODES::SCR2D:
  1098. ParseOpcode(full_opcode, "SCR2D", new FieldCameraInstruction(), 0, "Nss");
  1099. break;
  1100. case OPCODES::SCRCC:
  1101. ParseOpcode(full_opcode, "SCRCC", new FieldCameraInstruction(), 0, "");
  1102. break;
  1103. case OPCODES::SCR2DC:
  1104. ParseOpcode(full_opcode, "SCR2DC", new FieldCameraInstruction(), 0, "NNssw");
  1105. break;
  1106. case OPCODES::SCRLW:
  1107. ParseOpcode(full_opcode, "SCRLW", new FieldCameraInstruction(), 0, "");
  1108. break;
  1109. case OPCODES::SCR2DL:
  1110. ParseOpcode(full_opcode, "SCR2DL", new FieldCameraInstruction(), 0, "NNssw");
  1111. break;
  1112. case OPCODES::VWOFT:
  1113. ParseOpcode(full_opcode, "VWOFT", new FieldCameraInstruction(), 0, "NssB");
  1114. break;
  1115. case OPCODES::FADE:
  1116. ParseOpcode(full_opcode, "FADE", new FieldCameraInstruction(), 0, "NNBBBBBB");
  1117. break;
  1118. case OPCODES::FADEW:
  1119. ParseOpcode(full_opcode, "FADEW", new FieldCameraInstruction(), 0, "");
  1120. break;
  1121. case OPCODES::SCRLP:
  1122. ParseOpcode(full_opcode, "SCRLP", new FieldCameraInstruction(), 0, "NwBB");
  1123. break;
  1124. case OPCODES::MVCAM:
  1125. ParseOpcode(full_opcode, "MVCAM", new FieldCameraInstruction(), 0, "B");
  1126. break;
  1127. // AV
  1128. case OPCODES::BGMOVIE:
  1129. ParseOpcode(full_opcode, "BGMOVIE", new FieldMediaInstruction(), 0, "B");
  1130. break;
  1131. case OPCODES::AKAO2:
  1132. ParseOpcode(full_opcode, "AKAO2", new FieldMediaInstruction(), 0, "NNNBwwwww");
  1133. break;
  1134. case OPCODES::MUSIC:
  1135. ParseOpcode(full_opcode, "MUSIC", new FieldMediaInstruction(), 0, "B");
  1136. break;
  1137. case OPCODES::SOUND:
  1138. ParseOpcode(full_opcode, "SOUND", new FieldMediaInstruction(), 0, "NwB");
  1139. break;
  1140. case OPCODES::AKAO:
  1141. ParseOpcode(full_opcode, "AKAO", new FieldMediaInstruction(), 0, "NNNBBwwww");
  1142. break;
  1143. case OPCODES::MUSVT:
  1144. ParseOpcode(full_opcode, "MUSVT", new FieldMediaInstruction(), 0, "B");
  1145. break;
  1146. case OPCODES::MUSVM:
  1147. ParseOpcode(full_opcode, "MUSVM", new FieldMediaInstruction(), 0, "B");
  1148. break;
  1149. case OPCODES::MULCK:
  1150. ParseOpcode(full_opcode, "MULCK", new FieldMediaInstruction(), 0, "B");
  1151. break;
  1152. case OPCODES::BMUSC:
  1153. ParseOpcode(full_opcode, "BMUSC", new FieldMediaInstruction(), 0, "B");
  1154. break;
  1155. case OPCODES::CHMPH:
  1156. ParseOpcode(full_opcode, "CHMPH", new FieldMediaInstruction(), 0, "BBB");
  1157. break;
  1158. case OPCODES::PMVIE:
  1159. ParseOpcode(full_opcode, "PMVIE", new FieldMediaInstruction(), 0, "B");
  1160. break;
  1161. case OPCODES::MOVIE:
  1162. ParseOpcode(full_opcode, "MOVIE", new FieldMediaInstruction(), 0, "");
  1163. break;
  1164. case OPCODES::MVIEF:
  1165. ParseOpcode(full_opcode, "MVIEF", new FieldMediaInstruction(), 0, "NB");
  1166. break;
  1167. case OPCODES::FMUSC:
  1168. ParseOpcode(full_opcode, "FMUSC", new FieldMediaInstruction(), 0, "B");
  1169. break;
  1170. case OPCODES::CMUSC:
  1171. ParseOpcode(full_opcode, "CMUSC", new FieldMediaInstruction(), 0, "BBBBBBB");
  1172. break;
  1173. case OPCODES::CHMST:
  1174. ParseOpcode(full_opcode, "CHMST", new FieldMediaInstruction(), 0, "NB");
  1175. break;
  1176. // Uncat
  1177. case OPCODES::MPDSP:
  1178. ParseOpcode(full_opcode, "MPDSP", new FieldUncategorizedInstruction(), 0, "B");
  1179. break;
  1180. case OPCODES::SETX:
  1181. ParseOpcode(full_opcode, "SETX", new FieldUncategorizedInstruction(), 0, "BBBBBB");
  1182. break;
  1183. case OPCODES::GETX:
  1184. ParseOpcode(full_opcode, "GETX", new FieldUncategorizedInstruction(), 0, "BBBBBB");
  1185. break;
  1186. case OPCODES::SEARCHX:
  1187. ParseOpcode(full_opcode, "SEARCHX", new FieldUncategorizedInstruction(), 0, "BBBBBBBBBB");
  1188. break;
  1189. default:
  1190. throw UnknownOpcodeException(this->address_, opcode);
  1191. }
  1192. this->address_++;
  1193. // Exit on return found.
  1194. if (full_opcode == OPCODES::RET) return is_line;
  1195. }
  1196. return is_line;
  1197. }