macro_interpreter.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/logging/log.h"
  6. #include "common/microprofile.h"
  7. #include "video_core/engines/maxwell_3d.h"
  8. #include "video_core/macro/macro_interpreter.h"
  9. MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192));
  10. namespace Tegra {
  11. MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d_)
  12. : MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {}
  13. std::unique_ptr<CachedMacro> MacroInterpreter::Compile(const std::vector<u32>& code) {
  14. return std::make_unique<MacroInterpreterImpl>(maxwell3d, code);
  15. }
  16. MacroInterpreterImpl::MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d_,
  17. const std::vector<u32>& code_)
  18. : maxwell3d{maxwell3d_}, code{code_} {}
  19. void MacroInterpreterImpl::Execute(const std::vector<u32>& params, u32 method) {
  20. MICROPROFILE_SCOPE(MacroInterp);
  21. Reset();
  22. registers[1] = params[0];
  23. num_parameters = params.size();
  24. if (num_parameters > parameters_capacity) {
  25. parameters_capacity = num_parameters;
  26. parameters = std::make_unique<u32[]>(num_parameters);
  27. }
  28. std::memcpy(parameters.get(), params.data(), num_parameters * sizeof(u32));
  29. // Execute the code until we hit an exit condition.
  30. bool keep_executing = true;
  31. while (keep_executing) {
  32. keep_executing = Step(false);
  33. }
  34. // Assert the the macro used all the input parameters
  35. ASSERT(next_parameter_index == num_parameters);
  36. }
  37. void MacroInterpreterImpl::Reset() {
  38. registers = {};
  39. pc = 0;
  40. delayed_pc = {};
  41. method_address.raw = 0;
  42. num_parameters = 0;
  43. // The next parameter index starts at 1, because $r1 already has the value of the first
  44. // parameter.
  45. next_parameter_index = 1;
  46. carry_flag = false;
  47. }
  48. bool MacroInterpreterImpl::Step(bool is_delay_slot) {
  49. u32 base_address = pc;
  50. Macro::Opcode opcode = GetOpcode();
  51. pc += 4;
  52. // Update the program counter if we were delayed
  53. if (delayed_pc) {
  54. ASSERT(is_delay_slot);
  55. pc = *delayed_pc;
  56. delayed_pc = {};
  57. }
  58. switch (opcode.operation) {
  59. case Macro::Operation::ALU: {
  60. u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a),
  61. GetRegister(opcode.src_b));
  62. ProcessResult(opcode.result_operation, opcode.dst, result);
  63. break;
  64. }
  65. case Macro::Operation::AddImmediate: {
  66. ProcessResult(opcode.result_operation, opcode.dst,
  67. GetRegister(opcode.src_a) + opcode.immediate);
  68. break;
  69. }
  70. case Macro::Operation::ExtractInsert: {
  71. u32 dst = GetRegister(opcode.src_a);
  72. u32 src = GetRegister(opcode.src_b);
  73. src = (src >> opcode.bf_src_bit) & opcode.GetBitfieldMask();
  74. dst &= ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit);
  75. dst |= src << opcode.bf_dst_bit;
  76. ProcessResult(opcode.result_operation, opcode.dst, dst);
  77. break;
  78. }
  79. case Macro::Operation::ExtractShiftLeftImmediate: {
  80. u32 dst = GetRegister(opcode.src_a);
  81. u32 src = GetRegister(opcode.src_b);
  82. u32 result = ((src >> dst) & opcode.GetBitfieldMask()) << opcode.bf_dst_bit;
  83. ProcessResult(opcode.result_operation, opcode.dst, result);
  84. break;
  85. }
  86. case Macro::Operation::ExtractShiftLeftRegister: {
  87. u32 dst = GetRegister(opcode.src_a);
  88. u32 src = GetRegister(opcode.src_b);
  89. u32 result = ((src >> opcode.bf_src_bit) & opcode.GetBitfieldMask()) << dst;
  90. ProcessResult(opcode.result_operation, opcode.dst, result);
  91. break;
  92. }
  93. case Macro::Operation::Read: {
  94. u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate);
  95. ProcessResult(opcode.result_operation, opcode.dst, result);
  96. break;
  97. }
  98. case Macro::Operation::Branch: {
  99. ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
  100. u32 value = GetRegister(opcode.src_a);
  101. bool taken = EvaluateBranchCondition(opcode.branch_condition, value);
  102. if (taken) {
  103. // Ignore the delay slot if the branch has the annul bit.
  104. if (opcode.branch_annul) {
  105. pc = base_address + opcode.GetBranchTarget();
  106. return true;
  107. }
  108. delayed_pc = base_address + opcode.GetBranchTarget();
  109. // Execute one more instruction due to the delay slot.
  110. return Step(true);
  111. }
  112. break;
  113. }
  114. default:
  115. UNIMPLEMENTED_MSG("Unimplemented macro operation {}", opcode.operation.Value());
  116. }
  117. // An instruction with the Exit flag will not actually
  118. // cause an exit if it's executed inside a delay slot.
  119. if (opcode.is_exit && !is_delay_slot) {
  120. // Exit has a delay slot, execute the next instruction
  121. Step(true);
  122. return false;
  123. }
  124. return true;
  125. }
  126. u32 MacroInterpreterImpl::GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b) {
  127. switch (operation) {
  128. case Macro::ALUOperation::Add: {
  129. const u64 result{static_cast<u64>(src_a) + src_b};
  130. carry_flag = result > 0xffffffff;
  131. return static_cast<u32>(result);
  132. }
  133. case Macro::ALUOperation::AddWithCarry: {
  134. const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)};
  135. carry_flag = result > 0xffffffff;
  136. return static_cast<u32>(result);
  137. }
  138. case Macro::ALUOperation::Subtract: {
  139. const u64 result{static_cast<u64>(src_a) - src_b};
  140. carry_flag = result < 0x100000000;
  141. return static_cast<u32>(result);
  142. }
  143. case Macro::ALUOperation::SubtractWithBorrow: {
  144. const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)};
  145. carry_flag = result < 0x100000000;
  146. return static_cast<u32>(result);
  147. }
  148. case Macro::ALUOperation::Xor:
  149. return src_a ^ src_b;
  150. case Macro::ALUOperation::Or:
  151. return src_a | src_b;
  152. case Macro::ALUOperation::And:
  153. return src_a & src_b;
  154. case Macro::ALUOperation::AndNot:
  155. return src_a & ~src_b;
  156. case Macro::ALUOperation::Nand:
  157. return ~(src_a & src_b);
  158. default:
  159. UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", operation);
  160. return 0;
  161. }
  162. }
  163. void MacroInterpreterImpl::ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result) {
  164. switch (operation) {
  165. case Macro::ResultOperation::IgnoreAndFetch:
  166. // Fetch parameter and ignore result.
  167. SetRegister(reg, FetchParameter());
  168. break;
  169. case Macro::ResultOperation::Move:
  170. // Move result.
  171. SetRegister(reg, result);
  172. break;
  173. case Macro::ResultOperation::MoveAndSetMethod:
  174. // Move result and use as Method Address.
  175. SetRegister(reg, result);
  176. SetMethodAddress(result);
  177. break;
  178. case Macro::ResultOperation::FetchAndSend:
  179. // Fetch parameter and send result.
  180. SetRegister(reg, FetchParameter());
  181. Send(result);
  182. break;
  183. case Macro::ResultOperation::MoveAndSend:
  184. // Move and send result.
  185. SetRegister(reg, result);
  186. Send(result);
  187. break;
  188. case Macro::ResultOperation::FetchAndSetMethod:
  189. // Fetch parameter and use result as Method Address.
  190. SetRegister(reg, FetchParameter());
  191. SetMethodAddress(result);
  192. break;
  193. case Macro::ResultOperation::MoveAndSetMethodFetchAndSend:
  194. // Move result and use as Method Address, then fetch and send parameter.
  195. SetRegister(reg, result);
  196. SetMethodAddress(result);
  197. Send(FetchParameter());
  198. break;
  199. case Macro::ResultOperation::MoveAndSetMethodSend:
  200. // Move result and use as Method Address, then send bits 12:17 of result.
  201. SetRegister(reg, result);
  202. SetMethodAddress(result);
  203. Send((result >> 12) & 0b111111);
  204. break;
  205. default:
  206. UNIMPLEMENTED_MSG("Unimplemented result operation {}", operation);
  207. }
  208. }
  209. bool MacroInterpreterImpl::EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const {
  210. switch (cond) {
  211. case Macro::BranchCondition::Zero:
  212. return value == 0;
  213. case Macro::BranchCondition::NotZero:
  214. return value != 0;
  215. }
  216. UNREACHABLE();
  217. return true;
  218. }
  219. Macro::Opcode MacroInterpreterImpl::GetOpcode() const {
  220. ASSERT((pc % sizeof(u32)) == 0);
  221. ASSERT(pc < code.size() * sizeof(u32));
  222. return {code[pc / sizeof(u32)]};
  223. }
  224. u32 MacroInterpreterImpl::GetRegister(u32 register_id) const {
  225. return registers.at(register_id);
  226. }
  227. void MacroInterpreterImpl::SetRegister(u32 register_id, u32 value) {
  228. // Register 0 is hardwired as the zero register.
  229. // Ensure no writes to it actually occur.
  230. if (register_id == 0) {
  231. return;
  232. }
  233. registers.at(register_id) = value;
  234. }
  235. void MacroInterpreterImpl::SetMethodAddress(u32 address) {
  236. method_address.raw = address;
  237. }
  238. void MacroInterpreterImpl::Send(u32 value) {
  239. maxwell3d.CallMethodFromMME(method_address.address, value);
  240. // Increment the method address by the method increment.
  241. method_address.address.Assign(method_address.address.Value() +
  242. method_address.increment.Value());
  243. }
  244. u32 MacroInterpreterImpl::Read(u32 method) const {
  245. return maxwell3d.GetRegisterValue(method);
  246. }
  247. u32 MacroInterpreterImpl::FetchParameter() {
  248. ASSERT(next_parameter_index < num_parameters);
  249. return parameters[next_parameter_index++];
  250. }
  251. } // namespace Tegra