macro_interpreter.cpp 9.1 KB

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