macro_interpreter.cpp 8.8 KB

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