FieldDisassembler.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. #pragma once
  16. #include "decompiler/Disassembler.h"
  17. #include <array>
  18. #include "FieldScriptFormatter.h"
  19. class BinaryReader;
  20. class Function;
  21. /**
  22. * Field opcodes.
  23. */
  24. enum OPCODES{
  25. RET = 0x0,
  26. REQ = 0x01,
  27. REQSW = 0x02,
  28. REQEW = 0x03,
  29. PREQ = 0x04,
  30. PRQSW = 0x05,
  31. PRQEW = 0x06,
  32. RETTO = 0x07,
  33. JOIN = 0x08,
  34. SPLIT = 0x09,
  35. SPTYE = 0x0A,
  36. GTPYE = 0x0B,
  37. DSKCG = 0x0E,
  38. SPECIAL = 0x0F,
  39. JMPF = 0x10,
  40. JMPFL = 0x11,
  41. JMPB = 0x12,
  42. JMPBL = 0x13,
  43. IFUB = 0x14,
  44. IFUBL = 0x15,
  45. IFSW = 0x16,
  46. IFSWL = 0x17,
  47. IFUW = 0x18,
  48. IFUWL = 0x19,
  49. MINIGAME = 0x20,
  50. TUTOR = 0x21,
  51. BTMD2 = 0x22,
  52. BTRLD = 0x23,
  53. WAIT = 0x24,
  54. NFADE = 0x25,
  55. BLINK = 0x26,
  56. BGMOVIE = 0x27,
  57. KAWAI = 0x28,
  58. KAWIW = 0x29,
  59. PMOVA = 0x2A,
  60. SLIP = 0x2B,
  61. BGPDH = 0x2C,
  62. BGSCR = 0x2D,
  63. WCLS = 0x2E,
  64. WSIZW = 0x2F,
  65. IFKEY = 0x30,
  66. IFKEYON = 0x31,
  67. IFKEYOFF = 0x32,
  68. UC = 0x33,
  69. PDIRA = 0x34,
  70. PTURA = 0x35,
  71. WSPCL = 0x36,
  72. WNUMB = 0x37,
  73. STTIM = 0x38,
  74. GOLDU = 0x39,
  75. GOLDD = 0x3A,
  76. CHGLD = 0x3B,
  77. HMPMAX1 = 0x3C,
  78. HMPMAX2 = 0x3D,
  79. MHMMX = 0x3E,
  80. HMPMAX3 = 0x3F,
  81. MESSAGE = 0x40,
  82. MPARA = 0x41,
  83. MPRA2 = 0x42,
  84. MPNAM = 0x43,
  85. MPU = 0x45,
  86. MPD = 0x47,
  87. ASK = 0x48,
  88. MENU = 0x49,
  89. MENU2 = 0x4A,
  90. BTLTB = 0x4B,
  91. HPU = 0x4D,
  92. HPD = 0x4F,
  93. WINDOW = 0x50,
  94. WMOVE = 0x51,
  95. WMODE = 0x52,
  96. WREST = 0x53,
  97. WCLSE = 0x54,
  98. WROW = 0x55,
  99. GWCOL = 0x56,
  100. SWCOL = 0x57,
  101. STITM = 0x58,
  102. DLITM = 0x59,
  103. CKITM = 0x5A,
  104. SMTRA = 0x5B,
  105. DMTRA = 0x5C,
  106. CMTRA = 0x5D,
  107. SHAKE = 0x5E,
  108. NOP = 0x5F,
  109. MAPJUMP = 0x60,
  110. SCRLO = 0x61,
  111. SCRLC = 0x62,
  112. SCRLA = 0x63,
  113. SCR2D = 0x64,
  114. SCRCC = 0x65,
  115. SCR2DC = 0x66,
  116. SCRLW = 0x67,
  117. SCR2DL = 0x68,
  118. MPDSP = 0x69,
  119. VWOFT = 0x6A,
  120. FADE = 0x6B,
  121. FADEW = 0x6C,
  122. IDLCK = 0x6D,
  123. LSTMP = 0x6E,
  124. SCRLP = 0x6F,
  125. BATTLE = 0x70,
  126. BTLON = 0x71,
  127. BTLMD = 0x72,
  128. PGTDR = 0x73,
  129. GETPC = 0x74,
  130. PXYZI = 0x75,
  131. PLUS_ = 0x76, // PLUS!
  132. PLUS2_ = 0x77, // PLUS2!
  133. MINUS_ = 0x78, // MINUS!
  134. MINUS2_ = 0x79, // MINUS2!
  135. INC_ = 0x7A, // INC!
  136. INC2_ = 0x7B, // INC2!
  137. DEC_ = 0x7C, // DEC!
  138. DEC2_ = 0x7D, // DEC2!
  139. TLKON = 0x7E,
  140. RDMSD = 0x7F,
  141. SETBYTE = 0x80,
  142. SETWORD = 0x81,
  143. BITON = 0x82,
  144. BITOFF = 0x83,
  145. BITXOR = 0x84,
  146. PLUS = 0x85,
  147. PLUS2 = 0x86,
  148. MINUS = 0x87,
  149. MINUS2 = 0x88,
  150. MUL = 0x89,
  151. MUL2 = 0x8A,
  152. DIV = 0x8B,
  153. DIV2 = 0x8C,
  154. MOD = 0x8D,
  155. MOD2 = 0x8E,
  156. AND = 0x8F,
  157. AND2 = 0x90,
  158. OR = 0x91,
  159. OR2 = 0x92,
  160. XOR = 0x93,
  161. XOR2 = 0x94,
  162. INC = 0x95,
  163. INC2 = 0x96,
  164. DEC = 0x97,
  165. DEC2 = 0x98,
  166. RANDOM = 0x99,
  167. LBYTE = 0x9A,
  168. HBYTE = 0x9B,
  169. TWOBYTE = 0x9C,
  170. SETX = 0x9D,
  171. GETX = 0x9E,
  172. SEARCHX = 0x9F,
  173. PC = 0xA0,
  174. opCodeCHAR = 0xA1,
  175. DFANM = 0xA2,
  176. ANIME1 = 0xA3,
  177. VISI = 0xA4,
  178. XYZI = 0xA5,
  179. XYI = 0xA6,
  180. XYZ = 0xA7,
  181. MOVE = 0xA8,
  182. CMOVE = 0xA9,
  183. MOVA = 0xAA,
  184. TURA = 0xAB,
  185. ANIMW = 0xAC,
  186. FMOVE = 0xAD,
  187. ANIME2 = 0xAE,
  188. ANIM_1 = 0xAF, // ANIM!1
  189. CANIM1 = 0xB0,
  190. CANM_1 = 0xB1, // CANM!1
  191. MSPED = 0xB2,
  192. DIR = 0xB3,
  193. TURNGEN = 0xB4,
  194. TURN = 0xB5,
  195. DIRA = 0xB6,
  196. GETDIR = 0xB7,
  197. GETAXY = 0xB8,
  198. GETAI = 0xB9,
  199. ANIM_2 = 0xBA, // ANIM!2
  200. CANIM2 = 0xBB,
  201. CANM_2 = 0xBC, // CANM!2
  202. ASPED = 0xBD,
  203. CC = 0xBF,
  204. JUMP = 0xC0,
  205. AXYZI = 0xC1,
  206. LADER = 0xC2,
  207. OFST = 0xC3,
  208. OFSTW = 0xC4,
  209. TALKR = 0xC5,
  210. SLIDR = 0xC6,
  211. SOLID = 0xC7,
  212. PRTYP = 0xC8,
  213. PRTYM = 0xC9,
  214. PRTYE = 0xCA,
  215. IFPRTYQ = 0xCB,
  216. IFMEMBQ = 0xCC,
  217. MMBUD = 0xCD,
  218. MMBLK = 0xCE,
  219. MMBUK = 0xCF,
  220. LINE = 0xD0,
  221. LINON = 0xD1,
  222. MPJPO = 0xD2,
  223. SLINE = 0xD3,
  224. SIN = 0xD4,
  225. COS = 0xD5,
  226. TLKR2 = 0xD6,
  227. SLDR2 = 0xD7,
  228. PMJMP = 0xD8,
  229. PMJMP2 = 0xD9,
  230. AKAO2 = 0xDA,
  231. FCFIX = 0xDB,
  232. CCANM = 0xDC,
  233. ANIMB = 0xDD,
  234. TURNW = 0xDE,
  235. MPPAL = 0xDF,
  236. BGON = 0xE0,
  237. BGOFF = 0xE1,
  238. BGROL = 0xE2,
  239. BGROL2 = 0xE3,
  240. BGCLR = 0xE4,
  241. STPAL = 0xE5,
  242. LDPAL = 0xE6,
  243. CPPAL = 0xE7,
  244. RTPAL = 0xE8,
  245. ADPAL = 0xE9,
  246. MPPAL2 = 0xEA,
  247. STPLS = 0xEB,
  248. LDPLS = 0xEC,
  249. CPPAL2 = 0xED,
  250. RTPAL2 = 0xEE,
  251. ADPAL2 = 0xEF,
  252. MUSIC = 0xF0,
  253. SOUND = 0xF1,
  254. AKAO = 0xF2,
  255. MUSVT = 0xF3,
  256. MUSVM = 0xF4,
  257. MULCK = 0xF5,
  258. BMUSC = 0xF6,
  259. CHMPH = 0xF7,
  260. PMVIE = 0xF8,
  261. MOVIE = 0xF9,
  262. MVIEF = 0xFA,
  263. MVCAM = 0xFB,
  264. FMUSC = 0xFC,
  265. CMUSC = 0xFD,
  266. CHMST = 0xFE,
  267. GAMEOVER = 0xFF
  268. };
  269. /**
  270. * SPECIAL opcodes.
  271. *
  272. * SPECIAL opcode is a special one. It's a multi-purpose opcode, used to
  273. * perform game-specific tasks. It has a sub-opcode, in the next address,
  274. * that determines the operation to perform. This is a list of those
  275. * sub-opcodes.
  276. */
  277. enum OPCODES_SPECIAL{
  278. ARROW = 0xF5,
  279. PNAME = 0xF6,
  280. GMSPD = 0xF7,
  281. SMSPD = 0xF8,
  282. FLMAT = 0xF9,
  283. FLITM = 0xFA,
  284. BTLCK = 0xFB,
  285. MVLCK = 0xFC,
  286. SPCNM = 0xFD,
  287. RSGLB = 0xFE,
  288. CLITM = 0xFF
  289. };
  290. /**
  291. * KAWAI opcodes.
  292. *
  293. * KAWAI opcode is a special one. It's a multi-purpose opcode, used to
  294. * perform graphics related tasks. It has a sub-opcode, in the next
  295. * address, that determines the operation to perform. This is a list of
  296. * those sub-opcodes.
  297. */
  298. enum OPCODES_KAWAI{
  299. EYETX = 0x00,
  300. TRNSP = 0x01,
  301. AMBNT = 0x02,
  302. Unknown03 = 0x03, // Makou Reactor can write these unknowns.
  303. Unknown04 = 0x04,
  304. Unknown05 = 0x05,
  305. LIGHT = 0x06,
  306. Unknown07 = 0x07,
  307. Unknown08 = 0x08,
  308. Unknown09 = 0x09,
  309. SBOBJ = 0x0A,
  310. Unknown0B = 0x0B,
  311. Unknown0C = 0x0C,
  312. SHINE = 0x0D,
  313. RESET = 0xFF
  314. };
  315. class FieldEngine;
  316. /**
  317. * A disassebler for field maps.
  318. */
  319. class FieldDisassembler : public Disassembler{
  320. public:
  321. /**
  322. * Information about an instruction.
  323. */
  324. struct InstructionRecord{
  325. /**
  326. * The size, in bytes, of the opcode.
  327. */
  328. unsigned char opcode_size;
  329. /**
  330. * The opcode code.
  331. */
  332. unsigned int opcode;
  333. /**
  334. * The opcode short name (mnemonic).
  335. */
  336. const char* opcode_name;
  337. /**
  338. * Format of the opcode arguments.
  339. */
  340. const char* argument_format;
  341. /**
  342. * @todo Document.
  343. */
  344. std::function<InstPtr()> factory_function;
  345. };
  346. /**
  347. * A list of flow control opcodes needed by the disassembler.
  348. */
  349. static InstructionRecord FLOW_OPCODES[];
  350. /**
  351. * Generates a map of control flow instructions.
  352. *
  353. * @return A map of control flow instructions, by opcode name.
  354. */
  355. static std::map<std::string, const InstructionRecord*> FieldInstructions();
  356. /**
  357. * Finds the ID of the character a function refers to.
  358. *
  359. * @param start_addr[in] The starting address of the function.
  360. * @param start_addr[in] The starting address of the function.
  361. * @param end_addr[in] The ending address of the instruction.
  362. * @param insts[in] The lsit of instructions to search.
  363. * @return The ID of the character the function refers to, or -1 if
  364. * the function doesn't belong to a character.
  365. */
  366. static int FindId(uint32 start_addr, uint32 end_addr, const InstVec& insts);
  367. /**
  368. * Constructor.
  369. *
  370. * @param formatter[in] The code formatter.
  371. * @param engine[in] The engine to use to disassemble.
  372. * @param insts[in] The list of instructions.
  373. */
  374. FieldDisassembler(FieldScriptFormatter& formatter, FieldEngine* engine, InstVec& insts);
  375. /**
  376. * Constructor.
  377. *
  378. * @param formatter[in] The code formatter.
  379. * @param engine[in] The engine to use to disassemble.
  380. * @param insts[in] The list of instructions.
  381. * @param raw_script_data[in] @todo Understand and document.
  382. */
  383. FieldDisassembler(
  384. FieldScriptFormatter& formatter, FieldEngine* engine,
  385. InstVec& insts, const std::vector<unsigned char>& raw_script_data
  386. );
  387. /**
  388. * Destructor.
  389. */
  390. ~FieldDisassembler();
  391. /**
  392. * Retrieves the scale factor for the field.
  393. */
  394. float GetScaleFactor() const;
  395. /**
  396. * Disassembles the instructions.
  397. */
  398. virtual void DoDisassemble() override;
  399. private:
  400. /**
  401. * Field file identification magic number.
  402. */
  403. static const int MAGIC;
  404. /**
  405. * The number of sections in a field.
  406. */
  407. static const int NUM_SECTIONS;
  408. /**
  409. * The sections of a field.
  410. */
  411. enum SECTIONS{
  412. /**
  413. * The scripts section.
  414. */
  415. SCRIPT = 0,
  416. /**
  417. * The walkmesh section.
  418. */
  419. WALKMESH = 1,
  420. /**
  421. * The tiles section.
  422. */
  423. TILE_MAP = 2,
  424. /**
  425. * The camera section.
  426. */
  427. CAMERA_MATRIX = 3,
  428. /**
  429. * The triggers section.
  430. */
  431. TRIGGERS = 4,
  432. /**
  433. * Encounter data section.
  434. */
  435. ENCOUNTERS = 5,
  436. /**
  437. * Field models section.
  438. */
  439. MODELS = 6
  440. };
  441. /**
  442. * Info about a script.
  443. */
  444. struct ScriptInfo{
  445. /**
  446. * Address of the script entry point.
  447. */
  448. uint16 entry_point;
  449. /**
  450. * Address of the next script entry point.
  451. *
  452. * I.e. The last address of the script, plus one.
  453. */
  454. uint32 next_entry_point;
  455. /**
  456. * Script index.
  457. */
  458. size_t index;
  459. };
  460. /**
  461. * A script header.
  462. */
  463. struct ScriptHeader{
  464. /**
  465. * Field file identification magic number.
  466. */
  467. uint16 magic;
  468. /**
  469. * The number of entities in the field.
  470. */
  471. uint8 number_of_entities;
  472. /**
  473. * The number of models in the field.
  474. */
  475. uint8 number_of_models;
  476. /**
  477. * Offset at which the field text strings begin.
  478. */
  479. uint16 offset_to_strings;
  480. /**
  481. * The number of Akao/tuto blocks/offsets.
  482. */
  483. uint16 number_of_akao_offsets;
  484. /**
  485. * Scale of field.
  486. *
  487. * For move and talk calculation (9bit fixed point).
  488. */
  489. uint16 scale;
  490. /**
  491. * Blank data, unused.
  492. */
  493. std::array<uint16, 3> blank;
  494. /**
  495. * Field creator name.
  496. */
  497. std::array<char, 8> creator;
  498. /**
  499. * The field name.
  500. */
  501. std::array<char, 8> name;
  502. /**
  503. * The list of entity names.
  504. *
  505. * There will be {@see number_of_entities} entities.
  506. */
  507. std::vector<std::array<char, 8>> field_entity_names;
  508. /**
  509. * Akao/Tuto block offsets.
  510. *
  511. * There will be {@see number_of_akao_offsets} offsets.
  512. */
  513. std::vector<uint32> akao_offsets;
  514. /**
  515. * Entity script entry points.
  516. *
  517. * More explicitly, subroutine offsets. There will be
  518. * {@see number_of_entities} entity scripts.
  519. */
  520. std::vector<std::array<uint16, 32>> entity_scripts;
  521. /**
  522. * Reads the header.
  523. *
  524. * @param reader[in] The reader used to read the header.
  525. * @throws ScriptHeaderInvalidException If the magic number for
  526. * the field is wrong.
  527. */
  528. void Read(BinaryReader& reader){
  529. magic = reader.ReadU16();
  530. if (magic != MAGIC) throw ScriptHeaderInvalidException();
  531. number_of_entities = reader.ReadU8();
  532. number_of_models = reader.ReadU8();
  533. offset_to_strings = reader.ReadU16();
  534. number_of_akao_offsets = reader.ReadU16();
  535. scale = reader.ReadU16();
  536. for (int i = 0; i < 3; i ++) blank[i] = reader.ReadU16();
  537. for (int i = 0; i < 8; i ++) creator[i] = reader.ReadU8();
  538. for (int i = 0; i < 8; i ++) name[i] = reader.ReadU8();
  539. for (int i = 0; i < number_of_entities; i ++){
  540. std::array<char, 8> name;
  541. for (int j = 0; j < 8; j ++) name[j] = reader.ReadU8();
  542. field_entity_names.emplace_back(name);
  543. }
  544. for (int i = 0; i < number_of_akao_offsets; i ++)
  545. akao_offsets.emplace_back(reader.ReadU32());
  546. entity_scripts.reserve(number_of_entities);
  547. for (int i = 0; i < number_of_entities; i ++){
  548. std::array<uint16, 32> scripts;
  549. for (int j = 0; j < 32; j ++) scripts[j] = reader.ReadU16();
  550. entity_scripts.push_back(scripts);
  551. }
  552. }
  553. };
  554. /**
  555. * Open a file for disassembly.
  556. *
  557. * @param filename[in] The file to disassemble.
  558. * @deprecated Do not use. Load the instructions from the constructor.
  559. */
  560. virtual void Open(const char *filename) override;
  561. /**
  562. * Disassembles a script (function).
  563. *
  564. * @param entity_name[in] Name of the entity the script belongs to.
  565. * @param entity_index[in] Index of the entity the script belongs to.
  566. * @param script_entry_point[in] Address of the first instruction of
  567. * the script to disassemble.
  568. * @param script index[in] Index of the script, relative to the entity
  569. * scripts.
  570. * @param next_script_entry_point[in] Address of the first instruction
  571. * of the script next to the one to disassemble (i.e. the last address
  572. * of the script to disassemble, plus one).
  573. * @param is_start[in] Indicates if the script is the first one for
  574. * it's entity.
  575. * @param is_end[in] Indicates if the script is the last one for it's
  576. * entity.
  577. * @throws DecompilerException for malformed scripts.
  578. */
  579. void DisassembleIndivdualScript(
  580. std::string entity_name, size_t entity_index, size_t script_index,
  581. size_t script_entry_point, uint32 next_script_entry_point, bool is_start, bool is_end
  582. );
  583. /**
  584. * Adds a function to the engine.
  585. *
  586. * @param entity_name[in] Name of the entity that owns the function.
  587. * @param entity_index[in] Index of the entity that owns the function.
  588. * @param script_index[in] Index of the script within the entity.
  589. * @param next_script_entry_point[in] Start position of the next
  590. * script (or the ending position of this one, plus one).
  591. * @param is_start[in] @todo Understand and document.
  592. * @param is_end[in] @todo Understand and document.
  593. * @param to_return_only[in] True to read the script only until the
  594. * first return.
  595. * @param func_name[in] Name of the function. If the entity is a line,
  596. * the provided name will be overridden and set to the standard line
  597. * entity script names.
  598. */
  599. void AddFunc(
  600. std::string entity_name, size_t entity_index, size_t script_index,
  601. uint32 next_script_entry_point, const bool is_start, const bool is_end,
  602. bool to_return_only, std::string func_name
  603. );
  604. /**
  605. * Reads opcodes from a script, and detects lines.
  606. *
  607. * It stops when it reaches the ending position or a RET opcode,
  608. * whatever comes first. It also detects if one of the read opcodes
  609. * LINE, which means that the entity is a line
  610. *
  611. * @param end_pos[in] The last position to read. Opcodes will be read
  612. * until this, or until a RET is found.
  613. * @param point_a[out] If a LINE opcode is found, the first point will
  614. * be saved here.
  615. * @param point_b[out] If a LINE opcode is found, the second point
  616. * will be saved here.
  617. * @return True if LINE opcode found, false if not.
  618. */
  619. bool ReadOpCodesToPositionOrReturn(
  620. size_t end_pos, std::vector<float>& point_a, std::vector<float>& point_b
  621. );
  622. /**
  623. * Initializes a function for an entity.
  624. *
  625. * @param script_index[in] Index of the script.
  626. */
  627. std::unique_ptr<Function> StartFunction(size_t scriptIndex);
  628. /**
  629. * Reads the header.
  630. */
  631. void ReadHeader();
  632. /**
  633. * Reads the header.
  634. *
  635. * @param reader[in] Reader to use to read.
  636. */
  637. void ReadHeader(BinaryReader& reader);
  638. /**
  639. * Retrieves the offset of the end of a script.
  640. *
  641. * @param cur_entry_point[in] @todo Understand and document.
  642. * @param entity_index[in] Index of the entity the script belongs to.
  643. * @param script_index[in] The script index.
  644. * @return The address of the end of the script.
  645. */
  646. uint32 GetEndOfScriptOffset(
  647. uint16 cur_entry_point, size_t entity_index, size_t script_index
  648. );
  649. /**
  650. * Parses an opcode.
  651. *
  652. * Adds properties to an instruction, and adds the instruction to the
  653. * list.
  654. *
  655. * @param opcode[in] The opcode code.
  656. * @param name[in] the opcode name.
  657. * @param instruction[in][out] A newly created instruction of the type
  658. * the opcode belongs to.
  659. * @param stack_change[in] Indicates how much the instruction changes
  660. * the stack pointer by.
  661. * @param argument_format[in] The opcode argument format.
  662. */
  663. template<typename T> void ParseOpcode(
  664. int opcode, std::string name, T instruction, int stack_change, const char* argument_format
  665. ){
  666. uint32 full_opcode = (full_opcode << 8) + opcode;
  667. this->insts_.push_back(instruction);
  668. this->insts_.back()->SetOpcode(full_opcode);
  669. this->insts_.back()->SetAddress(this->address_);
  670. this->insts_.back()->SetStackChange(0);
  671. this->insts_.back()->SetName(std::string(name));
  672. this->insts_.back()->SetCodeGenData("");
  673. this->ReadParams(this->insts_.back(), argument_format);
  674. }
  675. /**
  676. * The engine.
  677. */
  678. FieldEngine* engine_;
  679. /**
  680. * Last address of the header.
  681. */
  682. uint32 header_end_position_ = 0;
  683. /**
  684. * The list of sections.
  685. */
  686. std::array<uint32, 7> sections_;
  687. /**
  688. * Size of the pointers to sections.
  689. *
  690. * @todo Understand and document.
  691. */
  692. int16 section_pointers_size_;
  693. /**
  694. * The field header.
  695. */
  696. ScriptHeader header_;
  697. /**
  698. * Indicates if the field has been loaded from raw data.
  699. */
  700. bool loaded_from_raw_data_ = false;
  701. /**
  702. * The formatter.
  703. */
  704. FieldScriptFormatter& formatter_;
  705. /**
  706. * The scale factor of the field.
  707. */
  708. float scale_factor_ = 1.0f;
  709. };