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