CPMemory.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/CPMemory.h"
  4. #include <cstring>
  5. #include <type_traits>
  6. #include "Common/ChunkFile.h"
  7. #include "Common/EnumUtils.h"
  8. #include "Common/Logging/Log.h"
  9. #include "Core/DolphinAnalytics.h"
  10. #include "Core/System.h"
  11. #include "VideoCommon/CommandProcessor.h"
  12. #include "VideoCommon/VertexLoaderManager.h"
  13. // CP state
  14. CPState g_main_cp_state;
  15. CPState g_preprocess_cp_state;
  16. void CopyPreprocessCPStateFromMain()
  17. {
  18. static_assert(std::is_trivially_copyable_v<CPState>);
  19. std::memcpy(static_cast<void*>(&g_preprocess_cp_state),
  20. static_cast<const void*>(&g_main_cp_state), sizeof(CPState));
  21. }
  22. std::pair<std::string, std::string> GetCPRegInfo(u8 cmd, u32 value)
  23. {
  24. switch (cmd & CP_COMMAND_MASK)
  25. {
  26. case MATINDEX_A:
  27. return std::make_pair("MATINDEX_A", fmt::to_string(TMatrixIndexA{.Hex = value}));
  28. case MATINDEX_B:
  29. return std::make_pair("MATINDEX_B", fmt::to_string(TMatrixIndexB{.Hex = value}));
  30. case VCD_LO:
  31. return std::make_pair("VCD_LO", fmt::to_string(TVtxDesc::Low{.Hex = value}));
  32. case VCD_HI:
  33. return std::make_pair("VCD_HI", fmt::to_string(TVtxDesc::High{.Hex = value}));
  34. case CP_VAT_REG_A:
  35. if (cmd - CP_VAT_REG_A >= CP_NUM_VAT_REG)
  36. return std::make_pair("CP_VAT_REG_A invalid", "");
  37. return std::make_pair(fmt::format("CP_VAT_REG_A - Format {}", cmd & CP_VAT_MASK),
  38. fmt::to_string(UVAT_group0{.Hex = value}));
  39. case CP_VAT_REG_B:
  40. if (cmd - CP_VAT_REG_B >= CP_NUM_VAT_REG)
  41. return std::make_pair("CP_VAT_REG_B invalid", "");
  42. return std::make_pair(fmt::format("CP_VAT_REG_B - Format {}", cmd & CP_VAT_MASK),
  43. fmt::to_string(UVAT_group1{.Hex = value}));
  44. case CP_VAT_REG_C:
  45. if (cmd - CP_VAT_REG_C >= CP_NUM_VAT_REG)
  46. return std::make_pair("CP_VAT_REG_C invalid", "");
  47. return std::make_pair(fmt::format("CP_VAT_REG_C - Format {}", cmd & CP_VAT_MASK),
  48. fmt::to_string(UVAT_group2{.Hex = value}));
  49. case ARRAY_BASE:
  50. return std::make_pair(
  51. fmt::format("ARRAY_BASE Array {}", static_cast<CPArray>(cmd & CP_ARRAY_MASK)),
  52. fmt::format("Base address {:08x}", value));
  53. case ARRAY_STRIDE:
  54. return std::make_pair(
  55. fmt::format("ARRAY_STRIDE Array {}", static_cast<CPArray>(cmd & CP_ARRAY_MASK)),
  56. fmt::format("Stride {:02x}", value & 0xff));
  57. default:
  58. return std::make_pair(fmt::format("Invalid CP register {:02x} = {:08x}", cmd, value), "");
  59. }
  60. }
  61. CPState::CPState(const u32* memory) : CPState()
  62. {
  63. matrix_index_a.Hex = memory[MATINDEX_A];
  64. matrix_index_b.Hex = memory[MATINDEX_B];
  65. vtx_desc.low.Hex = memory[VCD_LO];
  66. vtx_desc.high.Hex = memory[VCD_HI];
  67. for (u32 i = 0; i < CP_NUM_VAT_REG; i++)
  68. {
  69. vtx_attr[i].g0.Hex = memory[CP_VAT_REG_A + i];
  70. vtx_attr[i].g1.Hex = memory[CP_VAT_REG_B + i];
  71. vtx_attr[i].g2.Hex = memory[CP_VAT_REG_C + i];
  72. }
  73. for (u32 i = 0; i < CP_NUM_ARRAYS; i++)
  74. {
  75. array_bases[static_cast<CPArray>(i)] = memory[ARRAY_BASE + i];
  76. array_strides[static_cast<CPArray>(i)] = memory[ARRAY_STRIDE + i];
  77. }
  78. }
  79. void CPState::LoadCPReg(u8 sub_cmd, u32 value)
  80. {
  81. switch (sub_cmd & CP_COMMAND_MASK)
  82. {
  83. case UNKNOWN_00:
  84. case UNKNOWN_10:
  85. case UNKNOWN_20:
  86. if (!(sub_cmd == UNKNOWN_20 && value == 0))
  87. {
  88. // All titles using libogc or the official SDK issue 0x20 with value=0 on startup
  89. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_CP_PERF_COMMAND);
  90. DEBUG_LOG_FMT(VIDEO, "Unknown CP command possibly relating to perf queries used: {:02x}",
  91. sub_cmd);
  92. }
  93. break;
  94. case MATINDEX_A:
  95. if (sub_cmd != MATINDEX_A)
  96. {
  97. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  98. WARN_LOG_FMT(VIDEO,
  99. "CP MATINDEX_A: an exact value of {:02x} was expected "
  100. "but instead a value of {:02x} was seen",
  101. Common::ToUnderlying(MATINDEX_A), sub_cmd);
  102. }
  103. matrix_index_a.Hex = value;
  104. break;
  105. case MATINDEX_B:
  106. if (sub_cmd != MATINDEX_B)
  107. {
  108. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  109. WARN_LOG_FMT(VIDEO,
  110. "CP MATINDEX_B: an exact value of {:02x} was expected "
  111. "but instead a value of {:02x} was seen",
  112. Common::ToUnderlying(MATINDEX_B), sub_cmd);
  113. }
  114. matrix_index_b.Hex = value;
  115. break;
  116. case VCD_LO:
  117. if (sub_cmd != VCD_LO) // Stricter than YAGCD
  118. {
  119. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  120. WARN_LOG_FMT(VIDEO,
  121. "CP VCD_LO: an exact value of {:02x} was expected "
  122. "but instead a value of {:02x} was seen",
  123. Common::ToUnderlying(VCD_LO), sub_cmd);
  124. }
  125. vtx_desc.low.Hex = value;
  126. break;
  127. case VCD_HI:
  128. if (sub_cmd != VCD_HI) // Stricter than YAGCD
  129. {
  130. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  131. WARN_LOG_FMT(VIDEO,
  132. "CP VCD_HI: an exact value of {:02x} was expected "
  133. "but instead a value of {:02x} was seen",
  134. Common::ToUnderlying(VCD_HI), sub_cmd);
  135. }
  136. vtx_desc.high.Hex = value;
  137. break;
  138. case CP_VAT_REG_A:
  139. if ((sub_cmd - CP_VAT_REG_A) >= CP_NUM_VAT_REG)
  140. {
  141. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  142. WARN_LOG_FMT(VIDEO, "CP_VAT_REG_A: Invalid VAT {}", sub_cmd - CP_VAT_REG_A);
  143. }
  144. vtx_attr[sub_cmd & CP_VAT_MASK].g0.Hex = value;
  145. break;
  146. case CP_VAT_REG_B:
  147. if ((sub_cmd - CP_VAT_REG_B) >= CP_NUM_VAT_REG)
  148. {
  149. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  150. WARN_LOG_FMT(VIDEO, "CP_VAT_REG_B: Invalid VAT {}", sub_cmd - CP_VAT_REG_B);
  151. }
  152. vtx_attr[sub_cmd & CP_VAT_MASK].g1.Hex = value;
  153. break;
  154. case CP_VAT_REG_C:
  155. if ((sub_cmd - CP_VAT_REG_C) >= CP_NUM_VAT_REG)
  156. {
  157. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_MAYBE_INVALID_CP_COMMAND);
  158. WARN_LOG_FMT(VIDEO, "CP_VAT_REG_C: Invalid VAT {}", sub_cmd - CP_VAT_REG_C);
  159. }
  160. vtx_attr[sub_cmd & CP_VAT_MASK].g2.Hex = value;
  161. break;
  162. // Pointers to vertex arrays in GC RAM
  163. case ARRAY_BASE:
  164. array_bases[static_cast<CPArray>(sub_cmd & CP_ARRAY_MASK)] =
  165. value & CommandProcessor::GetPhysicalAddressMask(Core::System::GetInstance().IsWii());
  166. break;
  167. case ARRAY_STRIDE:
  168. array_strides[static_cast<CPArray>(sub_cmd & CP_ARRAY_MASK)] = value & 0xFF;
  169. break;
  170. default:
  171. DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::USES_UNKNOWN_CP_COMMAND);
  172. WARN_LOG_FMT(VIDEO, "Unknown CP register {:02x} set to {:08x}", sub_cmd, value);
  173. }
  174. }
  175. void CPState::FillCPMemoryArray(u32* memory) const
  176. {
  177. memory[MATINDEX_A] = matrix_index_a.Hex;
  178. memory[MATINDEX_B] = matrix_index_b.Hex;
  179. memory[VCD_LO] = vtx_desc.low.Hex;
  180. memory[VCD_HI] = vtx_desc.high.Hex;
  181. for (int i = 0; i < CP_NUM_VAT_REG; ++i)
  182. {
  183. memory[CP_VAT_REG_A + i] = vtx_attr[i].g0.Hex;
  184. memory[CP_VAT_REG_B + i] = vtx_attr[i].g1.Hex;
  185. memory[CP_VAT_REG_C + i] = vtx_attr[i].g2.Hex;
  186. }
  187. for (int i = 0; i < CP_NUM_ARRAYS; ++i)
  188. {
  189. memory[ARRAY_BASE + i] = array_bases[static_cast<CPArray>(i)];
  190. memory[ARRAY_STRIDE + i] = array_strides[static_cast<CPArray>(i)];
  191. }
  192. }