macro_interpreter.cpp 8.9 KB

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