FieldBackgroundInstruction.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include <sstream>
  20. #include <boost/format.hpp>
  21. #include "decompiler/field/instruction/FieldBackgroundInstruction.h"
  22. #include "decompiler/field/FieldEngine.h"
  23. #include "decompiler/field/FieldCodeGenerator.h"
  24. #include "decompiler/field/FieldDisassembler.h"
  25. void FF7::FieldBackgroundInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FunctionMetaData md(func.metadata);
  29. switch (opcode_){
  30. case OPCODES::BGPDH: code_gen->WriteTodo(md.GetEntityName(), "BGPDH");break;
  31. case OPCODES::BGSCR: code_gen->WriteTodo(md.GetEntityName(), "BGSCR"); break;
  32. case OPCODES::MPPAL: code_gen->WriteTodo(md.GetEntityName(), "MPPAL"); break;
  33. case OPCODES::BGON: ProcessBGON(code_gen); break;
  34. case OPCODES::BGOFF: ProcessBGOFF(code_gen); break;
  35. case OPCODES::BGROL: code_gen->WriteTodo(md.GetEntityName(), "BGROL"); break;
  36. case OPCODES::BGROL2: code_gen->WriteTodo(md.GetEntityName(), "BGROL2"); break;
  37. case OPCODES::BGCLR: ProcessBGCLR(code_gen); break;
  38. case OPCODES::STPAL: ProcessSTPAL(code_gen); break;
  39. case OPCODES::LDPAL: ProcessLDPAL(code_gen); break;
  40. case OPCODES::CPPAL: ProcessCPPAL(code_gen); break;
  41. case OPCODES::RTPAL: code_gen->WriteTodo(md.GetEntityName(), "RTPAL"); break;
  42. case OPCODES::ADPAL: ProcessADPAL(code_gen); break;
  43. case OPCODES::MPPAL2: ProcessMPPAL2(code_gen); break;
  44. case OPCODES::STPLS: ProcessSTPLS(code_gen); break;
  45. case OPCODES::LDPLS: ProcessLDPLS(code_gen); break;
  46. case OPCODES::CPPAL2: code_gen->WriteTodo(md.GetEntityName(), "CPPAL2"); break;
  47. case OPCODES::ADPAL2: code_gen->WriteTodo(md.GetEntityName(), "ADPAL2"); break;
  48. case OPCODES::RTPAL2: code_gen->WriteTodo(md.GetEntityName(), "RTPAL2"); break;
  49. default:
  50. code_gen->AddOutputLine(FF7::FieldCodeGenerator::FormatInstructionNotImplemented(
  51. md.GetEntityName(), address_, opcode_
  52. ));
  53. }
  54. }
  55. void FF7::FieldBackgroundInstruction::ProcessBGON(CodeGenerator* code_gen){
  56. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  57. const auto& background_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  58. cg->GetFormatter(), params_[0]->getUnsigned(), params_[2]->getUnsigned()
  59. );
  60. const auto& layer_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  61. cg->GetFormatter(), params_[1]->getUnsigned(), params_[3]->getUnsigned()
  62. );
  63. code_gen->AddOutputLine(
  64. (boost::format("-- field:background_on(%1%, %2%)") % background_id % layer_id).str()
  65. );
  66. }
  67. void FF7::FieldBackgroundInstruction::ProcessBGOFF(CodeGenerator* code_gen){
  68. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  69. const auto& background_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  70. cg->GetFormatter(), params_[0]->getUnsigned(), params_[2]->getUnsigned()
  71. );
  72. const auto& layer_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  73. cg->GetFormatter(), params_[1]->getUnsigned(), params_[3]->getUnsigned()
  74. );
  75. code_gen->AddOutputLine(
  76. (boost::format("-- field:background_off(%1%, %2%)") % background_id % layer_id).str()
  77. );
  78. }
  79. void FF7::FieldBackgroundInstruction::ProcessBGCLR(CodeGenerator* code_gen){
  80. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  81. const auto& background_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  82. cg->GetFormatter(), params_[1]->getUnsigned(), params_[2]->getUnsigned()
  83. );
  84. code_gen->AddOutputLine(
  85. (boost::format("-- field:background_clear(%1%)") % background_id).str()
  86. );
  87. }
  88. void FF7::FieldBackgroundInstruction::ProcessSTPAL(CodeGenerator* code_gen){
  89. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  90. const auto& source = FF7::FieldCodeGenerator::FormatValueOrVariable(
  91. cg->GetFormatter(), params_[0]->getUnsigned(), params_[2]->getUnsigned()
  92. );
  93. const auto& destination = FF7::FieldCodeGenerator::FormatValueOrVariable(
  94. cg->GetFormatter(), params_[1]->getUnsigned(), params_[3]->getUnsigned()
  95. );
  96. auto num_entries = params_[4]->getUnsigned() + 1;
  97. code_gen->AddOutputLine((
  98. boost::format("-- store palette %1% to position %2%, start CLUT index 0, %3% entries")
  99. % source % destination % num_entries
  100. ).str());
  101. }
  102. void FF7::FieldBackgroundInstruction::ProcessLDPAL(CodeGenerator* code_gen){
  103. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  104. const auto& source = FF7::FieldCodeGenerator::FormatValueOrVariable(
  105. cg->GetFormatter(), params_[0]->getUnsigned(), params_[2]->getUnsigned()
  106. );
  107. const auto& destination = FF7::FieldCodeGenerator::FormatValueOrVariable(
  108. cg->GetFormatter(), params_[1]->getUnsigned(), params_[3]->getUnsigned()
  109. );
  110. auto num_entries = params_[4]->getUnsigned() + 1;
  111. code_gen->AddOutputLine((
  112. boost::format("-- load palette %2% from position %1%, start CLUT index 0, %3% entries")
  113. % source % destination % num_entries
  114. ).str());
  115. }
  116. void FF7::FieldBackgroundInstruction::ProcessCPPAL(CodeGenerator* code_gen){
  117. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  118. const auto& source = FF7::FieldCodeGenerator::FormatValueOrVariable(
  119. cg->GetFormatter(), params_[0]->getUnsigned(), params_[2]->getUnsigned()
  120. );
  121. const auto& destination = FF7::FieldCodeGenerator::FormatValueOrVariable(
  122. cg->GetFormatter(), params_[1]->getUnsigned(), params_[3]->getUnsigned()
  123. );
  124. auto num_entries = params_[4]->getUnsigned() + 1;
  125. code_gen->AddOutputLine((
  126. boost::format("-- copy palette %1% to palette %2%, %3% entries")
  127. % source % destination % num_entries
  128. ).str());
  129. }
  130. void FF7::FieldBackgroundInstruction::ProcessADPAL(CodeGenerator* code_gen){
  131. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  132. const auto& source = FF7::FieldCodeGenerator::FormatValueOrVariable(
  133. cg->GetFormatter(), params_[0]->getUnsigned(), params_[6]->getUnsigned()
  134. );
  135. const auto& destination = FF7::FieldCodeGenerator::FormatValueOrVariable(
  136. cg->GetFormatter(), params_[1]->getUnsigned(), params_[7]->getUnsigned()
  137. );
  138. const auto& r = FF7::FieldCodeGenerator::FormatValueOrVariable(
  139. cg->GetFormatter(), params_[4]->getUnsigned(), params_[10]->getUnsigned()
  140. );
  141. const auto& g = FF7::FieldCodeGenerator::FormatValueOrVariable(
  142. cg->GetFormatter(), params_[3]->getUnsigned(), params_[9]->getUnsigned()
  143. );
  144. const auto& b = FF7::FieldCodeGenerator::FormatValueOrVariable(
  145. cg->GetFormatter(), params_[2]->getUnsigned(), params_[8]->getUnsigned()
  146. );
  147. auto num_entries = params_[11]->getUnsigned() + 1;
  148. code_gen->AddOutputLine((
  149. boost::format(
  150. "-- add RGB(%3%, %4%, %5%) to %6% entries of palette stored at position %1%, "
  151. "storing result in position %2%"
  152. ) % source % destination % r % g % b % num_entries
  153. ).str());
  154. }
  155. void FF7::FieldBackgroundInstruction::ProcessMPPAL2(CodeGenerator* code_gen){
  156. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  157. const auto& source = FF7::FieldCodeGenerator::FormatValueOrVariable(
  158. cg->GetFormatter(), params_[0]->getUnsigned(), params_[6]->getUnsigned()
  159. );
  160. const auto& destination = FF7::FieldCodeGenerator::FormatValueOrVariable(
  161. cg->GetFormatter(), params_[1]->getUnsigned(), params_[7]->getUnsigned()
  162. );
  163. const auto& r = FF7::FieldCodeGenerator::FormatValueOrVariable(
  164. cg->GetFormatter(), params_[4]->getUnsigned(), params_[10]->getUnsigned()
  165. );
  166. const auto& g = FF7::FieldCodeGenerator::FormatValueOrVariable(
  167. cg->GetFormatter(), params_[3]->getUnsigned(), params_[9]->getUnsigned()
  168. );
  169. const auto& b = FF7::FieldCodeGenerator::FormatValueOrVariable(
  170. cg->GetFormatter(), params_[2]->getUnsigned(), params_[8]->getUnsigned()
  171. );
  172. auto num_entries = params_[11]->getUnsigned() + 1;
  173. code_gen->AddOutputLine((
  174. boost::format(
  175. "-- multiply RGB(%3%, %4%, %5%) by %6% entries of palette stored at position %1%, "
  176. "storing result in position %2%"
  177. ) % source % destination % r % g % b % num_entries).str());
  178. }
  179. void FF7::FieldBackgroundInstruction::ProcessSTPLS(CodeGenerator* code_gen){
  180. auto source = params_[0]->getUnsigned();
  181. auto destination = params_[1]->getUnsigned();
  182. auto start_clut = params_[2]->getUnsigned();
  183. auto num_entries = params_[3]->getUnsigned() + 1;
  184. code_gen->AddOutputLine((
  185. boost::format("-- store palette %1% to position %2%, start CLUT index %3%, %4% entries")
  186. % source % destination % start_clut % num_entries
  187. ).str());
  188. }
  189. void FF7::FieldBackgroundInstruction::ProcessLDPLS(CodeGenerator* code_gen){
  190. auto source = params_[0]->getUnsigned();
  191. auto destination = params_[1]->getUnsigned();
  192. auto startClut = params_[2]->getUnsigned();
  193. auto num_entries = params_[3]->getUnsigned() + 1;
  194. code_gen->AddOutputLine((
  195. boost::format("-- load palette %2% from position %1%, start CLUT index %3%, %4% entries")
  196. % source % destination % startClut % num_entries
  197. ).str());
  198. }