macro_interpreter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <optional>
  7. #include <vector>
  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 offset Offset to start execution at.
  20. * @param parameters The parameters of the macro.
  21. */
  22. void Execute(u32 offset, 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. s32 GetBranchTarget() const {
  82. return static_cast<s32>(immediate * sizeof(u32));
  83. }
  84. };
  85. union MethodAddress {
  86. u32 raw;
  87. BitField<0, 12, u32> address;
  88. BitField<12, 6, u32> increment;
  89. };
  90. /// Resets the execution engine state, zeroing registers, etc.
  91. void Reset();
  92. /**
  93. * Executes a single macro instruction located at the current program counter. Returns whether
  94. * the interpreter should keep running.
  95. * @param offset Offset to start execution at.
  96. * @param is_delay_slot Whether the current step is being executed due to a delay slot in a
  97. * previous instruction.
  98. */
  99. bool Step(u32 offset, bool is_delay_slot);
  100. /// Calculates the result of an ALU operation. src_a OP src_b;
  101. u32 GetALUResult(ALUOperation operation, u32 src_a, u32 src_b);
  102. /// Performs the result operation on the input result and stores it in the specified register
  103. /// (if necessary).
  104. void ProcessResult(ResultOperation operation, u32 reg, u32 result);
  105. /// Evaluates the branch condition and returns whether the branch should be taken or not.
  106. bool EvaluateBranchCondition(BranchCondition cond, u32 value) const;
  107. /// Reads an opcode at the current program counter location.
  108. Opcode GetOpcode(u32 offset) const;
  109. /// Returns the specified register's value. Register 0 is hardcoded to always return 0.
  110. u32 GetRegister(u32 register_id) const;
  111. /// Sets the register to the input value.
  112. void SetRegister(u32 register_id, u32 value);
  113. /// Sets the method address to use for the next Send instruction.
  114. void SetMethodAddress(u32 address);
  115. /// Calls a GPU Engine method with the input parameter.
  116. void Send(u32 value);
  117. /// Reads a GPU register located at the method address.
  118. u32 Read(u32 method) const;
  119. /// Returns the next parameter in the parameter queue.
  120. u32 FetchParameter();
  121. Engines::Maxwell3D& maxwell3d;
  122. u32 pc; ///< Current program counter
  123. std::optional<u32>
  124. delayed_pc; ///< Program counter to execute at after the delay slot is executed.
  125. static constexpr std::size_t NumMacroRegisters = 8;
  126. /// General purpose macro registers.
  127. std::array<u32, NumMacroRegisters> registers = {};
  128. /// Method address to use for the next Send instruction.
  129. MethodAddress method_address = {};
  130. /// Input parameters of the current macro.
  131. std::vector<u32> parameters;
  132. /// Index of the next parameter that will be fetched by the 'parm' instruction.
  133. u32 next_parameter_index = 0;
  134. bool carry_flag{};
  135. };
  136. } // namespace Tegra