macro_interpreter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <vector>
  7. #include <boost/optional.hpp>
  8. #include "common/bit_field.h"
  9. #include "common/common_types.h"
  10. namespace Tegra {
  11. namespace Engines {
  12. class Maxwell3D;
  13. }
  14. class MacroInterpreter final {
  15. public:
  16. explicit MacroInterpreter(Engines::Maxwell3D& maxwell3d);
  17. /**
  18. * Executes the macro code with the specified input parameters.
  19. * @param code The macro byte code to execute
  20. * @param parameters The parameters of the macro
  21. */
  22. void Execute(const std::vector<u32>& code, std::vector<u32> parameters);
  23. private:
  24. enum class Operation : u32 {
  25. ALU = 0,
  26. AddImmediate = 1,
  27. ExtractInsert = 2,
  28. ExtractShiftLeftImmediate = 3,
  29. ExtractShiftLeftRegister = 4,
  30. Read = 5,
  31. Unused = 6, // This operation doesn't seem to be a valid encoding.
  32. Branch = 7,
  33. };
  34. enum class ALUOperation : u32 {
  35. Add = 0,
  36. AddWithCarry = 1,
  37. Subtract = 2,
  38. SubtractWithBorrow = 3,
  39. // Operations 4-7 don't seem to be valid encodings.
  40. Xor = 8,
  41. Or = 9,
  42. And = 10,
  43. AndNot = 11,
  44. Nand = 12
  45. };
  46. enum class ResultOperation : u32 {
  47. IgnoreAndFetch = 0,
  48. Move = 1,
  49. MoveAndSetMethod = 2,
  50. FetchAndSend = 3,
  51. MoveAndSend = 4,
  52. FetchAndSetMethod = 5,
  53. MoveAndSetMethodFetchAndSend = 6,
  54. MoveAndSetMethodSend = 7
  55. };
  56. enum class BranchCondition : u32 {
  57. Zero = 0,
  58. NotZero = 1,
  59. };
  60. union Opcode {
  61. u32 raw;
  62. BitField<0, 3, Operation> operation;
  63. BitField<4, 3, ResultOperation> result_operation;
  64. BitField<4, 1, BranchCondition> branch_condition;
  65. BitField<5, 1, u32>
  66. branch_annul; // If set on a branch, then the branch doesn't have a delay slot.
  67. BitField<7, 1, u32> is_exit;
  68. BitField<8, 3, u32> dst;
  69. BitField<11, 3, u32> src_a;
  70. BitField<14, 3, u32> src_b;
  71. // The signed immediate overlaps the second source operand and the alu operation.
  72. BitField<14, 18, s32> immediate;
  73. BitField<17, 5, ALUOperation> alu_operation;
  74. // Bitfield instructions data
  75. BitField<17, 5, u32> bf_src_bit;
  76. BitField<22, 5, u32> bf_size;
  77. BitField<27, 5, u32> bf_dst_bit;
  78. u32 GetBitfieldMask() const {
  79. return (1 << bf_size) - 1;
  80. }
  81. };
  82. union MethodAddress {
  83. u32 raw;
  84. BitField<0, 12, u32> address;
  85. BitField<12, 6, u32> increment;
  86. };
  87. /// Resets the execution engine state, zeroing registers, etc.
  88. void Reset();
  89. /**
  90. * Executes a single macro instruction located at the current program counter. Returns whether
  91. * the interpreter should keep running.
  92. * @param code The macro code to execute.
  93. * @param is_delay_slot Whether the current step is being executed due to a delay slot in a
  94. * previous instruction.
  95. */
  96. bool Step(const std::vector<u32>& code, bool is_delay_slot);
  97. /// Calculates the result of an ALU operation. src_a OP src_b;
  98. u32 GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) const;
  99. /// Performs the result operation on the input result and stores it in the specified register
  100. /// (if necessary).
  101. void ProcessResult(ResultOperation operation, u32 reg, u32 result);
  102. /// Evaluates the branch condition and returns whether the branch should be taken or not.
  103. bool EvaluateBranchCondition(BranchCondition cond, u32 value) const;
  104. /// Reads an opcode at the current program counter location.
  105. Opcode GetOpcode(const std::vector<u32>& code) const;
  106. /// Returns the specified register's value. Register 0 is hardcoded to always return 0.
  107. u32 GetRegister(u32 register_id) const;
  108. /// Sets the register to the input value.
  109. void SetRegister(u32 register_id, u32 value);
  110. /// Sets the method address to use for the next Send instruction.
  111. void SetMethodAddress(u32 address);
  112. /// Calls a GPU Engine method with the input parameter.
  113. void Send(u32 value);
  114. /// Reads a GPU register located at the method address.
  115. u32 Read(u32 method) const;
  116. /// Returns the next parameter in the parameter queue.
  117. u32 FetchParameter();
  118. Engines::Maxwell3D& maxwell3d;
  119. u32 pc; ///< Current program counter
  120. boost::optional<u32>
  121. delayed_pc; ///< Program counter to execute at after the delay slot is executed.
  122. static constexpr size_t NumMacroRegisters = 8;
  123. /// General purpose macro registers.
  124. std::array<u32, NumMacroRegisters> registers = {};
  125. /// Method address to use for the next Send instruction.
  126. MethodAddress method_address = {};
  127. /// Input parameters of the current macro.
  128. std::vector<u32> parameters;
  129. /// Index of the next parameter that will be fetched by the 'parm' instruction.
  130. u32 next_parameter_index = 0;
  131. };
  132. } // namespace Tegra