dmnt_cheat_vm.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <variant>
  5. #include <vector>
  6. #include <fmt/printf.h>
  7. #include "common/common_types.h"
  8. #include "core/memory/dmnt_cheat_types.h"
  9. namespace Core::Memory {
  10. enum class CheatVmOpcodeType : u32 {
  11. StoreStatic = 0,
  12. BeginConditionalBlock = 1,
  13. EndConditionalBlock = 2,
  14. ControlLoop = 3,
  15. LoadRegisterStatic = 4,
  16. LoadRegisterMemory = 5,
  17. StoreStaticToAddress = 6,
  18. PerformArithmeticStatic = 7,
  19. BeginKeypressConditionalBlock = 8,
  20. // These are not implemented by Gateway's VM.
  21. PerformArithmeticRegister = 9,
  22. StoreRegisterToAddress = 10,
  23. Reserved11 = 11,
  24. // This is a meta entry, and not a real opcode.
  25. // This is to facilitate multi-nybble instruction decoding.
  26. ExtendedWidth = 12,
  27. // Extended width opcodes.
  28. BeginRegisterConditionalBlock = 0xC0,
  29. SaveRestoreRegister = 0xC1,
  30. SaveRestoreRegisterMask = 0xC2,
  31. ReadWriteStaticRegister = 0xC3,
  32. // This is a meta entry, and not a real opcode.
  33. // This is to facilitate multi-nybble instruction decoding.
  34. DoubleExtendedWidth = 0xF0,
  35. // Double-extended width opcodes.
  36. DebugLog = 0xFFF,
  37. };
  38. enum class MemoryAccessType : u32 {
  39. MainNso = 0,
  40. Heap = 1,
  41. };
  42. enum class ConditionalComparisonType : u32 {
  43. GT = 1,
  44. GE = 2,
  45. LT = 3,
  46. LE = 4,
  47. EQ = 5,
  48. NE = 6,
  49. };
  50. enum class RegisterArithmeticType : u32 {
  51. Addition = 0,
  52. Subtraction = 1,
  53. Multiplication = 2,
  54. LeftShift = 3,
  55. RightShift = 4,
  56. // These are not supported by Gateway's VM.
  57. LogicalAnd = 5,
  58. LogicalOr = 6,
  59. LogicalNot = 7,
  60. LogicalXor = 8,
  61. None = 9,
  62. };
  63. enum class StoreRegisterOffsetType : u32 {
  64. None = 0,
  65. Reg = 1,
  66. Imm = 2,
  67. MemReg = 3,
  68. MemImm = 4,
  69. MemImmReg = 5,
  70. };
  71. enum class CompareRegisterValueType : u32 {
  72. MemoryRelAddr = 0,
  73. MemoryOfsReg = 1,
  74. RegisterRelAddr = 2,
  75. RegisterOfsReg = 3,
  76. StaticValue = 4,
  77. OtherRegister = 5,
  78. };
  79. enum class SaveRestoreRegisterOpType : u32 {
  80. Restore = 0,
  81. Save = 1,
  82. ClearSaved = 2,
  83. ClearRegs = 3,
  84. };
  85. enum class DebugLogValueType : u32 {
  86. MemoryRelAddr = 0,
  87. MemoryOfsReg = 1,
  88. RegisterRelAddr = 2,
  89. RegisterOfsReg = 3,
  90. RegisterValue = 4,
  91. };
  92. union VmInt {
  93. u8 bit8;
  94. u16 bit16;
  95. u32 bit32;
  96. u64 bit64;
  97. };
  98. struct StoreStaticOpcode {
  99. u32 bit_width{};
  100. MemoryAccessType mem_type{};
  101. u32 offset_register{};
  102. u64 rel_address{};
  103. VmInt value{};
  104. };
  105. struct BeginConditionalOpcode {
  106. u32 bit_width{};
  107. MemoryAccessType mem_type{};
  108. ConditionalComparisonType cond_type{};
  109. u64 rel_address{};
  110. VmInt value{};
  111. };
  112. struct EndConditionalOpcode {};
  113. struct ControlLoopOpcode {
  114. bool start_loop{};
  115. u32 reg_index{};
  116. u32 num_iters{};
  117. };
  118. struct LoadRegisterStaticOpcode {
  119. u32 reg_index{};
  120. u64 value{};
  121. };
  122. struct LoadRegisterMemoryOpcode {
  123. u32 bit_width{};
  124. MemoryAccessType mem_type{};
  125. u32 reg_index{};
  126. bool load_from_reg{};
  127. u64 rel_address{};
  128. };
  129. struct StoreStaticToAddressOpcode {
  130. u32 bit_width{};
  131. u32 reg_index{};
  132. bool increment_reg{};
  133. bool add_offset_reg{};
  134. u32 offset_reg_index{};
  135. u64 value{};
  136. };
  137. struct PerformArithmeticStaticOpcode {
  138. u32 bit_width{};
  139. u32 reg_index{};
  140. RegisterArithmeticType math_type{};
  141. u32 value{};
  142. };
  143. struct BeginKeypressConditionalOpcode {
  144. u32 key_mask{};
  145. };
  146. struct PerformArithmeticRegisterOpcode {
  147. u32 bit_width{};
  148. RegisterArithmeticType math_type{};
  149. u32 dst_reg_index{};
  150. u32 src_reg_1_index{};
  151. u32 src_reg_2_index{};
  152. bool has_immediate{};
  153. VmInt value{};
  154. };
  155. struct StoreRegisterToAddressOpcode {
  156. u32 bit_width{};
  157. u32 str_reg_index{};
  158. u32 addr_reg_index{};
  159. bool increment_reg{};
  160. StoreRegisterOffsetType ofs_type{};
  161. MemoryAccessType mem_type{};
  162. u32 ofs_reg_index{};
  163. u64 rel_address{};
  164. };
  165. struct BeginRegisterConditionalOpcode {
  166. u32 bit_width{};
  167. ConditionalComparisonType cond_type{};
  168. u32 val_reg_index{};
  169. CompareRegisterValueType comp_type{};
  170. MemoryAccessType mem_type{};
  171. u32 addr_reg_index{};
  172. u32 other_reg_index{};
  173. u32 ofs_reg_index{};
  174. u64 rel_address{};
  175. VmInt value{};
  176. };
  177. struct SaveRestoreRegisterOpcode {
  178. u32 dst_index{};
  179. u32 src_index{};
  180. SaveRestoreRegisterOpType op_type{};
  181. };
  182. struct SaveRestoreRegisterMaskOpcode {
  183. SaveRestoreRegisterOpType op_type{};
  184. std::array<bool, 0x10> should_operate{};
  185. };
  186. struct ReadWriteStaticRegisterOpcode {
  187. u32 static_idx{};
  188. u32 idx{};
  189. };
  190. struct DebugLogOpcode {
  191. u32 bit_width{};
  192. u32 log_id{};
  193. DebugLogValueType val_type{};
  194. MemoryAccessType mem_type{};
  195. u32 addr_reg_index{};
  196. u32 val_reg_index{};
  197. u32 ofs_reg_index{};
  198. u64 rel_address{};
  199. };
  200. struct UnrecognizedInstruction {
  201. CheatVmOpcodeType opcode{};
  202. };
  203. struct CheatVmOpcode {
  204. bool begin_conditional_block{};
  205. std::variant<StoreStaticOpcode, BeginConditionalOpcode, EndConditionalOpcode, ControlLoopOpcode,
  206. LoadRegisterStaticOpcode, LoadRegisterMemoryOpcode, StoreStaticToAddressOpcode,
  207. PerformArithmeticStaticOpcode, BeginKeypressConditionalOpcode,
  208. PerformArithmeticRegisterOpcode, StoreRegisterToAddressOpcode,
  209. BeginRegisterConditionalOpcode, SaveRestoreRegisterOpcode,
  210. SaveRestoreRegisterMaskOpcode, ReadWriteStaticRegisterOpcode, DebugLogOpcode,
  211. UnrecognizedInstruction>
  212. opcode{};
  213. };
  214. class DmntCheatVm {
  215. public:
  216. /// Helper Type for DmntCheatVm <=> yuzu Interface
  217. class Callbacks {
  218. public:
  219. virtual ~Callbacks();
  220. virtual void MemoryRead(VAddr address, void* data, u64 size) = 0;
  221. virtual void MemoryWrite(VAddr address, const void* data, u64 size) = 0;
  222. virtual u64 HidKeysDown() = 0;
  223. virtual void DebugLog(u8 id, u64 value) = 0;
  224. virtual void CommandLog(std::string_view data) = 0;
  225. };
  226. static constexpr std::size_t MaximumProgramOpcodeCount = 0x400;
  227. static constexpr std::size_t NumRegisters = 0x10;
  228. static constexpr std::size_t NumReadableStaticRegisters = 0x80;
  229. static constexpr std::size_t NumWritableStaticRegisters = 0x80;
  230. static constexpr std::size_t NumStaticRegisters =
  231. NumReadableStaticRegisters + NumWritableStaticRegisters;
  232. explicit DmntCheatVm(std::unique_ptr<Callbacks> callbacks_);
  233. ~DmntCheatVm();
  234. std::size_t GetProgramSize() const {
  235. return this->num_opcodes;
  236. }
  237. bool LoadProgram(const std::vector<CheatEntry>& cheats);
  238. void Execute(const CheatProcessMetadata& metadata);
  239. private:
  240. std::unique_ptr<Callbacks> callbacks;
  241. std::size_t num_opcodes = 0;
  242. std::size_t instruction_ptr = 0;
  243. std::size_t condition_depth = 0;
  244. bool decode_success = false;
  245. std::array<u32, MaximumProgramOpcodeCount> program{};
  246. std::array<u64, NumRegisters> registers{};
  247. std::array<u64, NumRegisters> saved_values{};
  248. std::array<u64, NumStaticRegisters> static_registers{};
  249. std::array<std::size_t, NumRegisters> loop_tops{};
  250. bool DecodeNextOpcode(CheatVmOpcode& out);
  251. void SkipConditionalBlock();
  252. void ResetState();
  253. // For implementing the DebugLog opcode.
  254. void DebugLog(u32 log_id, u64 value);
  255. void LogOpcode(const CheatVmOpcode& opcode);
  256. static u64 GetVmInt(VmInt value, u32 bit_width);
  257. static u64 GetCheatProcessAddress(const CheatProcessMetadata& metadata,
  258. MemoryAccessType mem_type, u64 rel_address);
  259. };
  260. }; // namespace Core::Memory