macro_interpreter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. namespace {
  12. enum class Operation : u32 {
  13. ALU = 0,
  14. AddImmediate = 1,
  15. ExtractInsert = 2,
  16. ExtractShiftLeftImmediate = 3,
  17. ExtractShiftLeftRegister = 4,
  18. Read = 5,
  19. Unused = 6, // This operation doesn't seem to be a valid encoding.
  20. Branch = 7,
  21. };
  22. } // Anonymous namespace
  23. enum class MacroInterpreter::ALUOperation : u32 {
  24. Add = 0,
  25. AddWithCarry = 1,
  26. Subtract = 2,
  27. SubtractWithBorrow = 3,
  28. // Operations 4-7 don't seem to be valid encodings.
  29. Xor = 8,
  30. Or = 9,
  31. And = 10,
  32. AndNot = 11,
  33. Nand = 12
  34. };
  35. enum class MacroInterpreter::ResultOperation : u32 {
  36. IgnoreAndFetch = 0,
  37. Move = 1,
  38. MoveAndSetMethod = 2,
  39. FetchAndSend = 3,
  40. MoveAndSend = 4,
  41. FetchAndSetMethod = 5,
  42. MoveAndSetMethodFetchAndSend = 6,
  43. MoveAndSetMethodSend = 7
  44. };
  45. enum class MacroInterpreter::BranchCondition : u32 {
  46. Zero = 0,
  47. NotZero = 1,
  48. };
  49. union MacroInterpreter::Opcode {
  50. u32 raw;
  51. BitField<0, 3, Operation> operation;
  52. BitField<4, 3, ResultOperation> result_operation;
  53. BitField<4, 1, BranchCondition> branch_condition;
  54. // If set on a branch, then the branch doesn't have a delay slot.
  55. BitField<5, 1, u32> branch_annul;
  56. BitField<7, 1, u32> is_exit;
  57. BitField<8, 3, u32> dst;
  58. BitField<11, 3, u32> src_a;
  59. BitField<14, 3, u32> src_b;
  60. // The signed immediate overlaps the second source operand and the alu operation.
  61. BitField<14, 18, s32> immediate;
  62. BitField<17, 5, ALUOperation> alu_operation;
  63. // Bitfield instructions data
  64. BitField<17, 5, u32> bf_src_bit;
  65. BitField<22, 5, u32> bf_size;
  66. BitField<27, 5, u32> bf_dst_bit;
  67. u32 GetBitfieldMask() const {
  68. return (1 << bf_size) - 1;
  69. }
  70. s32 GetBranchTarget() const {
  71. return static_cast<s32>(immediate * sizeof(u32));
  72. }
  73. };
  74. MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d) : maxwell3d(maxwell3d) {}
  75. void MacroInterpreter::Execute(u32 offset, std::size_t num_parameters, const u32* parameters) {
  76. MICROPROFILE_SCOPE(MacroInterp);
  77. Reset();
  78. registers[1] = parameters[0];
  79. if (num_parameters > parameters_capacity) {
  80. parameters_capacity = num_parameters;
  81. this->parameters = std::make_unique<u32[]>(num_parameters);
  82. }
  83. std::memcpy(this->parameters.get(), parameters, num_parameters * sizeof(u32));
  84. this->num_parameters = num_parameters;
  85. // Execute the code until we hit an exit condition.
  86. bool keep_executing = true;
  87. while (keep_executing) {
  88. keep_executing = Step(offset, false);
  89. }
  90. // Assert the the macro used all the input parameters
  91. ASSERT(next_parameter_index == num_parameters);
  92. }
  93. void MacroInterpreter::Reset() {
  94. registers = {};
  95. pc = 0;
  96. delayed_pc = {};
  97. method_address.raw = 0;
  98. num_parameters = 0;
  99. // The next parameter index starts at 1, because $r1 already has the value of the first
  100. // parameter.
  101. next_parameter_index = 1;
  102. carry_flag = false;
  103. }
  104. bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
  105. u32 base_address = pc;
  106. Opcode opcode = GetOpcode(offset);
  107. pc += 4;
  108. // Update the program counter if we were delayed
  109. if (delayed_pc) {
  110. ASSERT(is_delay_slot);
  111. pc = *delayed_pc;
  112. delayed_pc = {};
  113. }
  114. switch (opcode.operation) {
  115. case Operation::ALU: {
  116. u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a),
  117. GetRegister(opcode.src_b));
  118. ProcessResult(opcode.result_operation, opcode.dst, result);
  119. break;
  120. }
  121. case Operation::AddImmediate: {
  122. ProcessResult(opcode.result_operation, opcode.dst,
  123. GetRegister(opcode.src_a) + opcode.immediate);
  124. break;
  125. }
  126. case Operation::ExtractInsert: {
  127. u32 dst = GetRegister(opcode.src_a);
  128. u32 src = GetRegister(opcode.src_b);
  129. src = (src >> opcode.bf_src_bit) & opcode.GetBitfieldMask();
  130. dst &= ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit);
  131. dst |= src << opcode.bf_dst_bit;
  132. ProcessResult(opcode.result_operation, opcode.dst, dst);
  133. break;
  134. }
  135. case Operation::ExtractShiftLeftImmediate: {
  136. u32 dst = GetRegister(opcode.src_a);
  137. u32 src = GetRegister(opcode.src_b);
  138. u32 result = ((src >> dst) & opcode.GetBitfieldMask()) << opcode.bf_dst_bit;
  139. ProcessResult(opcode.result_operation, opcode.dst, result);
  140. break;
  141. }
  142. case Operation::ExtractShiftLeftRegister: {
  143. u32 dst = GetRegister(opcode.src_a);
  144. u32 src = GetRegister(opcode.src_b);
  145. u32 result = ((src >> opcode.bf_src_bit) & opcode.GetBitfieldMask()) << dst;
  146. ProcessResult(opcode.result_operation, opcode.dst, result);
  147. break;
  148. }
  149. case Operation::Read: {
  150. u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate);
  151. ProcessResult(opcode.result_operation, opcode.dst, result);
  152. break;
  153. }
  154. case Operation::Branch: {
  155. ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
  156. u32 value = GetRegister(opcode.src_a);
  157. bool taken = EvaluateBranchCondition(opcode.branch_condition, value);
  158. if (taken) {
  159. // Ignore the delay slot if the branch has the annul bit.
  160. if (opcode.branch_annul) {
  161. pc = base_address + opcode.GetBranchTarget();
  162. return true;
  163. }
  164. delayed_pc = base_address + opcode.GetBranchTarget();
  165. // Execute one more instruction due to the delay slot.
  166. return Step(offset, true);
  167. }
  168. break;
  169. }
  170. default:
  171. UNIMPLEMENTED_MSG("Unimplemented macro operation {}",
  172. static_cast<u32>(opcode.operation.Value()));
  173. }
  174. // An instruction with the Exit flag will not actually
  175. // cause an exit if it's executed inside a delay slot.
  176. if (opcode.is_exit && !is_delay_slot) {
  177. // Exit has a delay slot, execute the next instruction
  178. Step(offset, true);
  179. return false;
  180. }
  181. return true;
  182. }
  183. MacroInterpreter::Opcode MacroInterpreter::GetOpcode(u32 offset) const {
  184. const auto& macro_memory{maxwell3d.GetMacroMemory()};
  185. ASSERT((pc % sizeof(u32)) == 0);
  186. ASSERT((pc + offset) < macro_memory.size() * sizeof(u32));
  187. return {macro_memory[offset + pc / sizeof(u32)]};
  188. }
  189. u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) {
  190. switch (operation) {
  191. case ALUOperation::Add: {
  192. const u64 result{static_cast<u64>(src_a) + src_b};
  193. carry_flag = result > 0xffffffff;
  194. return static_cast<u32>(result);
  195. }
  196. case ALUOperation::AddWithCarry: {
  197. const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)};
  198. carry_flag = result > 0xffffffff;
  199. return static_cast<u32>(result);
  200. }
  201. case ALUOperation::Subtract: {
  202. const u64 result{static_cast<u64>(src_a) - src_b};
  203. carry_flag = result < 0x100000000;
  204. return static_cast<u32>(result);
  205. }
  206. case ALUOperation::SubtractWithBorrow: {
  207. const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)};
  208. carry_flag = result < 0x100000000;
  209. return static_cast<u32>(result);
  210. }
  211. case ALUOperation::Xor:
  212. return src_a ^ src_b;
  213. case ALUOperation::Or:
  214. return src_a | src_b;
  215. case ALUOperation::And:
  216. return src_a & src_b;
  217. case ALUOperation::AndNot:
  218. return src_a & ~src_b;
  219. case ALUOperation::Nand:
  220. return ~(src_a & src_b);
  221. default:
  222. UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", static_cast<u32>(operation));
  223. return 0;
  224. }
  225. }
  226. void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 result) {
  227. switch (operation) {
  228. case ResultOperation::IgnoreAndFetch:
  229. // Fetch parameter and ignore result.
  230. SetRegister(reg, FetchParameter());
  231. break;
  232. case ResultOperation::Move:
  233. // Move result.
  234. SetRegister(reg, result);
  235. break;
  236. case ResultOperation::MoveAndSetMethod:
  237. // Move result and use as Method Address.
  238. SetRegister(reg, result);
  239. SetMethodAddress(result);
  240. break;
  241. case ResultOperation::FetchAndSend:
  242. // Fetch parameter and send result.
  243. SetRegister(reg, FetchParameter());
  244. Send(result);
  245. break;
  246. case ResultOperation::MoveAndSend:
  247. // Move and send result.
  248. SetRegister(reg, result);
  249. Send(result);
  250. break;
  251. case ResultOperation::FetchAndSetMethod:
  252. // Fetch parameter and use result as Method Address.
  253. SetRegister(reg, FetchParameter());
  254. SetMethodAddress(result);
  255. break;
  256. case ResultOperation::MoveAndSetMethodFetchAndSend:
  257. // Move result and use as Method Address, then fetch and send parameter.
  258. SetRegister(reg, result);
  259. SetMethodAddress(result);
  260. Send(FetchParameter());
  261. break;
  262. case ResultOperation::MoveAndSetMethodSend:
  263. // Move result and use as Method Address, then send bits 12:17 of result.
  264. SetRegister(reg, result);
  265. SetMethodAddress(result);
  266. Send((result >> 12) & 0b111111);
  267. break;
  268. default:
  269. UNIMPLEMENTED_MSG("Unimplemented result operation {}", static_cast<u32>(operation));
  270. }
  271. }
  272. u32 MacroInterpreter::FetchParameter() {
  273. ASSERT(next_parameter_index < num_parameters);
  274. return parameters[next_parameter_index++];
  275. }
  276. u32 MacroInterpreter::GetRegister(u32 register_id) const {
  277. return registers.at(register_id);
  278. }
  279. void MacroInterpreter::SetRegister(u32 register_id, u32 value) {
  280. // Register 0 is hardwired as the zero register.
  281. // Ensure no writes to it actually occur.
  282. if (register_id == 0) {
  283. return;
  284. }
  285. registers.at(register_id) = value;
  286. }
  287. void MacroInterpreter::SetMethodAddress(u32 address) {
  288. method_address.raw = address;
  289. }
  290. void MacroInterpreter::Send(u32 value) {
  291. maxwell3d.CallMethodFromMME({method_address.address, value});
  292. // Increment the method address by the method increment.
  293. method_address.address.Assign(method_address.address.Value() +
  294. method_address.increment.Value());
  295. }
  296. u32 MacroInterpreter::Read(u32 method) const {
  297. return maxwell3d.GetRegisterValue(method);
  298. }
  299. bool MacroInterpreter::EvaluateBranchCondition(BranchCondition cond, u32 value) const {
  300. switch (cond) {
  301. case BranchCondition::Zero:
  302. return value == 0;
  303. case BranchCondition::NotZero:
  304. return value != 0;
  305. }
  306. UNREACHABLE();
  307. return true;
  308. }
  309. } // namespace Tegra