macro_interpreter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. }
  32. bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
  33. u32 base_address = pc;
  34. Opcode opcode = GetOpcode(offset);
  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(offset, 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(offset, true);
  107. return false;
  108. }
  109. return true;
  110. }
  111. MacroInterpreter::Opcode MacroInterpreter::GetOpcode(u32 offset) const {
  112. const auto& macro_memory{maxwell3d.GetMacroMemory()};
  113. ASSERT((pc % sizeof(u32)) == 0);
  114. ASSERT((pc + offset) < macro_memory.size() * sizeof(u32));
  115. return {macro_memory[offset + pc / sizeof(u32)]};
  116. }
  117. u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) const {
  118. switch (operation) {
  119. case ALUOperation::Add:
  120. return src_a + src_b;
  121. // TODO(Subv): Implement AddWithCarry
  122. case ALUOperation::Subtract:
  123. return src_a - src_b;
  124. // TODO(Subv): Implement SubtractWithBorrow
  125. case ALUOperation::Xor:
  126. return src_a ^ src_b;
  127. case ALUOperation::Or:
  128. return src_a | src_b;
  129. case ALUOperation::And:
  130. return src_a & src_b;
  131. case ALUOperation::AndNot:
  132. return src_a & ~src_b;
  133. case ALUOperation::Nand:
  134. return ~(src_a & src_b);
  135. default:
  136. UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", static_cast<u32>(operation));
  137. }
  138. }
  139. void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 result) {
  140. switch (operation) {
  141. case ResultOperation::IgnoreAndFetch:
  142. // Fetch parameter and ignore result.
  143. SetRegister(reg, FetchParameter());
  144. break;
  145. case ResultOperation::Move:
  146. // Move result.
  147. SetRegister(reg, result);
  148. break;
  149. case ResultOperation::MoveAndSetMethod:
  150. // Move result and use as Method Address.
  151. SetRegister(reg, result);
  152. SetMethodAddress(result);
  153. break;
  154. case ResultOperation::FetchAndSend:
  155. // Fetch parameter and send result.
  156. SetRegister(reg, FetchParameter());
  157. Send(result);
  158. break;
  159. case ResultOperation::MoveAndSend:
  160. // Move and send result.
  161. SetRegister(reg, result);
  162. Send(result);
  163. break;
  164. case ResultOperation::FetchAndSetMethod:
  165. // Fetch parameter and use result as Method Address.
  166. SetRegister(reg, FetchParameter());
  167. SetMethodAddress(result);
  168. break;
  169. case ResultOperation::MoveAndSetMethodFetchAndSend:
  170. // Move result and use as Method Address, then fetch and send parameter.
  171. SetRegister(reg, result);
  172. SetMethodAddress(result);
  173. Send(FetchParameter());
  174. break;
  175. case ResultOperation::MoveAndSetMethodSend:
  176. // Move result and use as Method Address, then send bits 12:17 of result.
  177. SetRegister(reg, result);
  178. SetMethodAddress(result);
  179. Send((result >> 12) & 0b111111);
  180. break;
  181. default:
  182. UNIMPLEMENTED_MSG("Unimplemented result operation {}", static_cast<u32>(operation));
  183. }
  184. }
  185. u32 MacroInterpreter::FetchParameter() {
  186. ASSERT(next_parameter_index < parameters.size());
  187. return parameters[next_parameter_index++];
  188. }
  189. u32 MacroInterpreter::GetRegister(u32 register_id) const {
  190. // Register 0 is supposed to always return 0.
  191. if (register_id == 0)
  192. return 0;
  193. ASSERT(register_id < registers.size());
  194. return registers[register_id];
  195. }
  196. void MacroInterpreter::SetRegister(u32 register_id, u32 value) {
  197. // Register 0 is supposed to always return 0. NOP is implemented as a store to the zero
  198. // register.
  199. if (register_id == 0)
  200. return;
  201. ASSERT(register_id < registers.size());
  202. registers[register_id] = value;
  203. }
  204. void MacroInterpreter::SetMethodAddress(u32 address) {
  205. method_address.raw = address;
  206. }
  207. void MacroInterpreter::Send(u32 value) {
  208. maxwell3d.WriteReg(method_address.address, value, 0);
  209. // Increment the method address by the method increment.
  210. method_address.address.Assign(method_address.address.Value() +
  211. method_address.increment.Value());
  212. }
  213. u32 MacroInterpreter::Read(u32 method) const {
  214. return maxwell3d.GetRegisterValue(method);
  215. }
  216. bool MacroInterpreter::EvaluateBranchCondition(BranchCondition cond, u32 value) const {
  217. switch (cond) {
  218. case BranchCondition::Zero:
  219. return value == 0;
  220. case BranchCondition::NotZero:
  221. return value != 0;
  222. }
  223. UNREACHABLE();
  224. }
  225. } // namespace Tegra