macro_interpreter.cpp 8.7 KB

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