macro_interpreter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 {}",
  116. static_cast<u32>(opcode.operation.Value()));
  117. }
  118. // An instruction with the Exit flag will not actually
  119. // cause an exit if it's executed inside a delay slot.
  120. if (opcode.is_exit && !is_delay_slot) {
  121. // Exit has a delay slot, execute the next instruction
  122. Step(true);
  123. return false;
  124. }
  125. return true;
  126. }
  127. u32 MacroInterpreterImpl::GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b) {
  128. switch (operation) {
  129. case Macro::ALUOperation::Add: {
  130. const u64 result{static_cast<u64>(src_a) + src_b};
  131. carry_flag = result > 0xffffffff;
  132. return static_cast<u32>(result);
  133. }
  134. case Macro::ALUOperation::AddWithCarry: {
  135. const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)};
  136. carry_flag = result > 0xffffffff;
  137. return static_cast<u32>(result);
  138. }
  139. case Macro::ALUOperation::Subtract: {
  140. const u64 result{static_cast<u64>(src_a) - src_b};
  141. carry_flag = result < 0x100000000;
  142. return static_cast<u32>(result);
  143. }
  144. case Macro::ALUOperation::SubtractWithBorrow: {
  145. const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)};
  146. carry_flag = result < 0x100000000;
  147. return static_cast<u32>(result);
  148. }
  149. case Macro::ALUOperation::Xor:
  150. return src_a ^ src_b;
  151. case Macro::ALUOperation::Or:
  152. return src_a | src_b;
  153. case Macro::ALUOperation::And:
  154. return src_a & src_b;
  155. case Macro::ALUOperation::AndNot:
  156. return src_a & ~src_b;
  157. case Macro::ALUOperation::Nand:
  158. return ~(src_a & src_b);
  159. default:
  160. UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", static_cast<u32>(operation));
  161. return 0;
  162. }
  163. }
  164. void MacroInterpreterImpl::ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result) {
  165. switch (operation) {
  166. case Macro::ResultOperation::IgnoreAndFetch:
  167. // Fetch parameter and ignore result.
  168. SetRegister(reg, FetchParameter());
  169. break;
  170. case Macro::ResultOperation::Move:
  171. // Move result.
  172. SetRegister(reg, result);
  173. break;
  174. case Macro::ResultOperation::MoveAndSetMethod:
  175. // Move result and use as Method Address.
  176. SetRegister(reg, result);
  177. SetMethodAddress(result);
  178. break;
  179. case Macro::ResultOperation::FetchAndSend:
  180. // Fetch parameter and send result.
  181. SetRegister(reg, FetchParameter());
  182. Send(result);
  183. break;
  184. case Macro::ResultOperation::MoveAndSend:
  185. // Move and send result.
  186. SetRegister(reg, result);
  187. Send(result);
  188. break;
  189. case Macro::ResultOperation::FetchAndSetMethod:
  190. // Fetch parameter and use result as Method Address.
  191. SetRegister(reg, FetchParameter());
  192. SetMethodAddress(result);
  193. break;
  194. case Macro::ResultOperation::MoveAndSetMethodFetchAndSend:
  195. // Move result and use as Method Address, then fetch and send parameter.
  196. SetRegister(reg, result);
  197. SetMethodAddress(result);
  198. Send(FetchParameter());
  199. break;
  200. case Macro::ResultOperation::MoveAndSetMethodSend:
  201. // Move result and use as Method Address, then send bits 12:17 of result.
  202. SetRegister(reg, result);
  203. SetMethodAddress(result);
  204. Send((result >> 12) & 0b111111);
  205. break;
  206. default:
  207. UNIMPLEMENTED_MSG("Unimplemented result operation {}", static_cast<u32>(operation));
  208. }
  209. }
  210. bool MacroInterpreterImpl::EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const {
  211. switch (cond) {
  212. case Macro::BranchCondition::Zero:
  213. return value == 0;
  214. case Macro::BranchCondition::NotZero:
  215. return value != 0;
  216. }
  217. UNREACHABLE();
  218. return true;
  219. }
  220. Macro::Opcode MacroInterpreterImpl::GetOpcode() const {
  221. ASSERT((pc % sizeof(u32)) == 0);
  222. ASSERT(pc < code.size() * sizeof(u32));
  223. return {code[pc / sizeof(u32)]};
  224. }
  225. u32 MacroInterpreterImpl::GetRegister(u32 register_id) const {
  226. return registers.at(register_id);
  227. }
  228. void MacroInterpreterImpl::SetRegister(u32 register_id, u32 value) {
  229. // Register 0 is hardwired as the zero register.
  230. // Ensure no writes to it actually occur.
  231. if (register_id == 0) {
  232. return;
  233. }
  234. registers.at(register_id) = value;
  235. }
  236. void MacroInterpreterImpl::SetMethodAddress(u32 address) {
  237. method_address.raw = address;
  238. }
  239. void MacroInterpreterImpl::Send(u32 value) {
  240. maxwell3d.CallMethodFromMME(method_address.address, value);
  241. // Increment the method address by the method increment.
  242. method_address.address.Assign(method_address.address.Value() +
  243. method_address.increment.Value());
  244. }
  245. u32 MacroInterpreterImpl::Read(u32 method) const {
  246. return maxwell3d.GetRegisterValue(method);
  247. }
  248. u32 MacroInterpreterImpl::FetchParameter() {
  249. ASSERT(next_parameter_index < num_parameters);
  250. return parameters[next_parameter_index++];
  251. }
  252. } // namespace Tegra