macro_interpreter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(const std::vector<u32>& code, 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(code, 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. }
  32. bool MacroInterpreter::Step(const std::vector<u32>& code, bool is_delay_slot) {
  33. u32 base_address = pc;
  34. Opcode opcode = GetOpcode(code);
  35. pc += 4;
  36. // Update the program counter if we were delayed
  37. if (delayed_pc) {
  38. ASSERT(is_delay_slot);
  39. pc = *delayed_pc;
  40. delayed_pc = {};
  41. }
  42. switch (opcode.operation) {
  43. case Operation::ALU: {
  44. u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a),
  45. GetRegister(opcode.src_b));
  46. ProcessResult(opcode.result_operation, opcode.dst, result);
  47. break;
  48. }
  49. case Operation::AddImmediate: {
  50. ProcessResult(opcode.result_operation, opcode.dst,
  51. GetRegister(opcode.src_a) + opcode.immediate);
  52. break;
  53. }
  54. case Operation::ExtractInsert: {
  55. u32 dst = GetRegister(opcode.src_a);
  56. u32 src = GetRegister(opcode.src_b);
  57. src = (src >> opcode.bf_src_bit) & opcode.GetBitfieldMask();
  58. dst &= ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit);
  59. dst |= src << opcode.bf_dst_bit;
  60. ProcessResult(opcode.result_operation, opcode.dst, dst);
  61. break;
  62. }
  63. case Operation::ExtractShiftLeftImmediate: {
  64. u32 dst = GetRegister(opcode.src_a);
  65. u32 src = GetRegister(opcode.src_b);
  66. u32 result = ((src >> dst) & opcode.GetBitfieldMask()) << opcode.bf_dst_bit;
  67. ProcessResult(opcode.result_operation, opcode.dst, result);
  68. break;
  69. }
  70. case Operation::ExtractShiftLeftRegister: {
  71. u32 dst = GetRegister(opcode.src_a);
  72. u32 src = GetRegister(opcode.src_b);
  73. u32 result = ((src >> opcode.bf_src_bit) & opcode.GetBitfieldMask()) << dst;
  74. ProcessResult(opcode.result_operation, opcode.dst, result);
  75. break;
  76. }
  77. case Operation::Read: {
  78. u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate);
  79. ProcessResult(opcode.result_operation, opcode.dst, result);
  80. break;
  81. }
  82. case Operation::Branch: {
  83. ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
  84. u32 value = GetRegister(opcode.src_a);
  85. bool taken = EvaluateBranchCondition(opcode.branch_condition, value);
  86. if (taken) {
  87. // Ignore the delay slot if the branch has the annul bit.
  88. if (opcode.branch_annul) {
  89. pc = base_address + opcode.GetBranchTarget();
  90. return true;
  91. }
  92. delayed_pc = base_address + opcode.GetBranchTarget();
  93. // Execute one more instruction due to the delay slot.
  94. return Step(code, true);
  95. }
  96. break;
  97. }
  98. default:
  99. UNIMPLEMENTED_MSG("Unimplemented macro operation {}",
  100. static_cast<u32>(opcode.operation.Value()));
  101. }
  102. if (opcode.is_exit) {
  103. // Exit has a delay slot, execute the next instruction
  104. // Note: Executing an exit during a branch delay slot will cause the instruction at the
  105. // branch target to be executed before exiting.
  106. Step(code, true);
  107. return false;
  108. }
  109. return true;
  110. }
  111. MacroInterpreter::Opcode MacroInterpreter::GetOpcode(const std::vector<u32>& code) const {
  112. ASSERT((pc % sizeof(u32)) == 0);
  113. ASSERT(pc < code.size() * sizeof(u32));
  114. return {code[pc / sizeof(u32)]};
  115. }
  116. u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) const {
  117. switch (operation) {
  118. case ALUOperation::Add:
  119. return src_a + src_b;
  120. // TODO(Subv): Implement AddWithCarry
  121. case ALUOperation::Subtract:
  122. return src_a - src_b;
  123. // TODO(Subv): Implement SubtractWithBorrow
  124. case ALUOperation::Xor:
  125. return src_a ^ src_b;
  126. case ALUOperation::Or:
  127. return src_a | src_b;
  128. case ALUOperation::And:
  129. return src_a & src_b;
  130. case ALUOperation::AndNot:
  131. return src_a & ~src_b;
  132. case ALUOperation::Nand:
  133. return ~(src_a & src_b);
  134. default:
  135. UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", static_cast<u32>(operation));
  136. }
  137. }
  138. void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 result) {
  139. switch (operation) {
  140. case ResultOperation::IgnoreAndFetch:
  141. // Fetch parameter and ignore result.
  142. SetRegister(reg, FetchParameter());
  143. break;
  144. case ResultOperation::Move:
  145. // Move result.
  146. SetRegister(reg, result);
  147. break;
  148. case ResultOperation::MoveAndSetMethod:
  149. // Move result and use as Method Address.
  150. SetRegister(reg, result);
  151. SetMethodAddress(result);
  152. break;
  153. case ResultOperation::FetchAndSend:
  154. // Fetch parameter and send result.
  155. SetRegister(reg, FetchParameter());
  156. Send(result);
  157. break;
  158. case ResultOperation::MoveAndSend:
  159. // Move and send result.
  160. SetRegister(reg, result);
  161. Send(result);
  162. break;
  163. case ResultOperation::FetchAndSetMethod:
  164. // Fetch parameter and use result as Method Address.
  165. SetRegister(reg, FetchParameter());
  166. SetMethodAddress(result);
  167. break;
  168. case ResultOperation::MoveAndSetMethodFetchAndSend:
  169. // Move result and use as Method Address, then fetch and send parameter.
  170. SetRegister(reg, result);
  171. SetMethodAddress(result);
  172. Send(FetchParameter());
  173. break;
  174. case ResultOperation::MoveAndSetMethodSend:
  175. // Move result and use as Method Address, then send bits 12:17 of result.
  176. SetRegister(reg, result);
  177. SetMethodAddress(result);
  178. Send((result >> 12) & 0b111111);
  179. break;
  180. default:
  181. UNIMPLEMENTED_MSG("Unimplemented result operation {}", static_cast<u32>(operation));
  182. }
  183. }
  184. u32 MacroInterpreter::FetchParameter() {
  185. ASSERT(next_parameter_index < parameters.size());
  186. return parameters[next_parameter_index++];
  187. }
  188. u32 MacroInterpreter::GetRegister(u32 register_id) const {
  189. // Register 0 is supposed to always return 0.
  190. if (register_id == 0)
  191. return 0;
  192. ASSERT(register_id < registers.size());
  193. return registers[register_id];
  194. }
  195. void MacroInterpreter::SetRegister(u32 register_id, u32 value) {
  196. // Register 0 is supposed to always return 0. NOP is implemented as a store to the zero
  197. // register.
  198. if (register_id == 0)
  199. return;
  200. ASSERT(register_id < registers.size());
  201. registers[register_id] = value;
  202. }
  203. void MacroInterpreter::SetMethodAddress(u32 address) {
  204. method_address.raw = address;
  205. }
  206. void MacroInterpreter::Send(u32 value) {
  207. maxwell3d.WriteReg(method_address.address, value, 0);
  208. // Increment the method address by the method increment.
  209. method_address.address.Assign(method_address.address.Value() +
  210. method_address.increment.Value());
  211. }
  212. u32 MacroInterpreter::Read(u32 method) const {
  213. return maxwell3d.GetRegisterValue(method);
  214. }
  215. bool MacroInterpreter::EvaluateBranchCondition(BranchCondition cond, u32 value) const {
  216. switch (cond) {
  217. case BranchCondition::Zero:
  218. return value == 0;
  219. case BranchCondition::NotZero:
  220. return value != 0;
  221. }
  222. UNREACHABLE();
  223. }
  224. } // namespace Tegra