diff options
| author | 2018-04-02 10:04:19 -0400 | |
|---|---|---|
| committer | 2018-04-02 10:04:19 -0400 | |
| commit | 3413f1f7ceef0d35f0dc867f26c185643829918b (patch) | |
| tree | f16ce5af90c2872ed2f0c709f520d98f66f416d1 /src | |
| parent | Merge pull request #293 from N00byKing/drkthm (diff) | |
| parent | GPU: Use the MacroInterpreter class to execute the GPU macros instead of HLEi... (diff) | |
| download | yuzu-3413f1f7ceef0d35f0dc867f26c185643829918b.tar.gz yuzu-3413f1f7ceef0d35f0dc867f26c185643829918b.tar.xz yuzu-3413f1f7ceef0d35f0dc867f26c185643829918b.zip | |
Merge pull request #288 from Subv/macro_interpreter
GPU: Implemented a gpu macro interpreter
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/engines/maxwell_3d.cpp | 113 | ||||
| -rw-r--r-- | src/video_core/engines/maxwell_3d.h | 29 | ||||
| -rw-r--r-- | src/video_core/macro_interpreter.cpp | 257 | ||||
| -rw-r--r-- | src/video_core/macro_interpreter.h | 164 |
5 files changed, 444 insertions, 121 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 841f27d7f..a710c4bc5 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -11,6 +11,8 @@ add_library(video_core STATIC | |||
| 11 | engines/maxwell_compute.h | 11 | engines/maxwell_compute.h |
| 12 | gpu.cpp | 12 | gpu.cpp |
| 13 | gpu.h | 13 | gpu.h |
| 14 | macro_interpreter.cpp | ||
| 15 | macro_interpreter.h | ||
| 14 | memory_manager.cpp | 16 | memory_manager.cpp |
| 15 | memory_manager.h | 17 | memory_manager.h |
| 16 | rasterizer_interface.h | 18 | rasterizer_interface.h |
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index 5359d21a2..124753032 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp | |||
| @@ -19,35 +19,21 @@ namespace Engines { | |||
| 19 | /// First register id that is actually a Macro call. | 19 | /// First register id that is actually a Macro call. |
| 20 | constexpr u32 MacroRegistersStart = 0xE00; | 20 | constexpr u32 MacroRegistersStart = 0xE00; |
| 21 | 21 | ||
| 22 | const std::unordered_map<u32, Maxwell3D::MethodInfo> Maxwell3D::method_handlers = { | 22 | Maxwell3D::Maxwell3D(MemoryManager& memory_manager) |
| 23 | {0xE1A, {"BindTextureInfoBuffer", 1, &Maxwell3D::BindTextureInfoBuffer}}, | 23 | : memory_manager(memory_manager), macro_interpreter(*this) {} |
| 24 | {0xE24, {"SetShader", 5, &Maxwell3D::SetShader}}, | ||
| 25 | {0xE2A, {"BindStorageBuffer", 1, &Maxwell3D::BindStorageBuffer}}, | ||
| 26 | }; | ||
| 27 | |||
| 28 | Maxwell3D::Maxwell3D(MemoryManager& memory_manager) : memory_manager(memory_manager) {} | ||
| 29 | 24 | ||
| 30 | void Maxwell3D::SubmitMacroCode(u32 entry, std::vector<u32> code) { | 25 | void Maxwell3D::SubmitMacroCode(u32 entry, std::vector<u32> code) { |
| 31 | uploaded_macros[entry * 2 + MacroRegistersStart] = std::move(code); | 26 | uploaded_macros[entry * 2 + MacroRegistersStart] = std::move(code); |
| 32 | } | 27 | } |
| 33 | 28 | ||
| 34 | void Maxwell3D::CallMacroMethod(u32 method, const std::vector<u32>& parameters) { | 29 | void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32> parameters) { |
| 35 | // TODO(Subv): Write an interpreter for the macros uploaded via registers 0x45 and 0x47 | 30 | auto macro_code = uploaded_macros.find(method); |
| 36 | |||
| 37 | // The requested macro must have been uploaded already. | 31 | // The requested macro must have been uploaded already. |
| 38 | ASSERT_MSG(uploaded_macros.find(method) != uploaded_macros.end(), "Macro %08X was not uploaded", | 32 | ASSERT_MSG(macro_code != uploaded_macros.end(), "Macro %08X was not uploaded", method); |
| 39 | method); | ||
| 40 | |||
| 41 | auto itr = method_handlers.find(method); | ||
| 42 | ASSERT_MSG(itr != method_handlers.end(), "Unhandled method call %08X", method); | ||
| 43 | |||
| 44 | ASSERT(itr->second.arguments == parameters.size()); | ||
| 45 | |||
| 46 | (this->*itr->second.handler)(parameters); | ||
| 47 | 33 | ||
| 48 | // Reset the current macro and its parameters. | 34 | // Reset the current macro and execute it. |
| 49 | executing_macro = 0; | 35 | executing_macro = 0; |
| 50 | macro_params.clear(); | 36 | macro_interpreter.Execute(macro_code->second, std::move(parameters)); |
| 51 | } | 37 | } |
| 52 | 38 | ||
| 53 | void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) { | 39 | void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) { |
| @@ -77,7 +63,7 @@ void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) { | |||
| 77 | 63 | ||
| 78 | // Call the macro when there are no more parameters in the command buffer | 64 | // Call the macro when there are no more parameters in the command buffer |
| 79 | if (remaining_params == 0) { | 65 | if (remaining_params == 0) { |
| 80 | CallMacroMethod(executing_macro, macro_params); | 66 | CallMacroMethod(executing_macro, std::move(macro_params)); |
| 81 | } | 67 | } |
| 82 | return; | 68 | return; |
| 83 | } | 69 | } |
| @@ -193,84 +179,6 @@ void Maxwell3D::DrawArrays() { | |||
| 193 | VideoCore::g_renderer->Rasterizer()->AccelerateDrawBatch(false /*is_indexed*/); | 179 | VideoCore::g_renderer->Rasterizer()->AccelerateDrawBatch(false /*is_indexed*/); |
| 194 | } | 180 | } |
| 195 | 181 | ||
| 196 | void Maxwell3D::BindTextureInfoBuffer(const std::vector<u32>& parameters) { | ||
| 197 | /** | ||
| 198 | * Parameters description: | ||
| 199 | * [0] = Shader stage, usually 4 for FragmentShader | ||
| 200 | */ | ||
| 201 | |||
| 202 | u32 stage = parameters[0]; | ||
| 203 | |||
| 204 | // Perform the same operations as the real macro code. | ||
| 205 | GPUVAddr address = static_cast<GPUVAddr>(regs.tex_info_buffers.address[stage]) << 8; | ||
| 206 | u32 size = regs.tex_info_buffers.size[stage]; | ||
| 207 | |||
| 208 | regs.const_buffer.cb_size = size; | ||
| 209 | regs.const_buffer.cb_address_high = address >> 32; | ||
| 210 | regs.const_buffer.cb_address_low = address & 0xFFFFFFFF; | ||
| 211 | } | ||
| 212 | |||
| 213 | void Maxwell3D::SetShader(const std::vector<u32>& parameters) { | ||
| 214 | /** | ||
| 215 | * Parameters description: | ||
| 216 | * [0] = Shader Program. | ||
| 217 | * [1] = Unknown, presumably the shader id. | ||
| 218 | * [2] = Offset to the start of the shader, after the 0x30 bytes header. | ||
| 219 | * [3] = Shader Stage. | ||
| 220 | * [4] = Const Buffer Address >> 8. | ||
| 221 | */ | ||
| 222 | auto shader_program = static_cast<Regs::ShaderProgram>(parameters[0]); | ||
| 223 | // TODO(Subv): This address is probably an offset from the CODE_ADDRESS register. | ||
| 224 | GPUVAddr address = parameters[2]; | ||
| 225 | auto shader_stage = static_cast<Regs::ShaderStage>(parameters[3]); | ||
| 226 | GPUVAddr cb_address = parameters[4] << 8; | ||
| 227 | |||
| 228 | auto& shader = state.shader_programs[static_cast<size_t>(shader_program)]; | ||
| 229 | shader.program = shader_program; | ||
| 230 | shader.stage = shader_stage; | ||
| 231 | shader.address = address; | ||
| 232 | |||
| 233 | // Perform the same operations as the real macro code. | ||
| 234 | // TODO(Subv): Early exit if register 0xD1C + shader_program contains the same as params[1]. | ||
| 235 | auto& shader_regs = regs.shader_config[static_cast<size_t>(shader_program)]; | ||
| 236 | shader_regs.start_id = address; | ||
| 237 | // TODO(Subv): Write params[1] to register 0xD1C + shader_program. | ||
| 238 | // TODO(Subv): Write params[2] to register 0xD22 + shader_program. | ||
| 239 | |||
| 240 | // Note: This value is hardcoded in the macro's code. | ||
| 241 | static constexpr u32 DefaultCBSize = 0x10000; | ||
| 242 | regs.const_buffer.cb_size = DefaultCBSize; | ||
| 243 | regs.const_buffer.cb_address_high = cb_address >> 32; | ||
| 244 | regs.const_buffer.cb_address_low = cb_address & 0xFFFFFFFF; | ||
| 245 | |||
| 246 | // Write a hardcoded 0x11 to CB_BIND, this binds the current const buffer to buffer c1[] in the | ||
| 247 | // shader. It's likely that these are the constants for the shader. | ||
| 248 | regs.cb_bind[static_cast<size_t>(shader_stage)].valid.Assign(1); | ||
| 249 | regs.cb_bind[static_cast<size_t>(shader_stage)].index.Assign(1); | ||
| 250 | |||
| 251 | ProcessCBBind(shader_stage); | ||
| 252 | } | ||
| 253 | |||
| 254 | void Maxwell3D::BindStorageBuffer(const std::vector<u32>& parameters) { | ||
| 255 | /** | ||
| 256 | * Parameters description: | ||
| 257 | * [0] = Buffer offset >> 2 | ||
| 258 | */ | ||
| 259 | |||
| 260 | u32 buffer_offset = parameters[0] << 2; | ||
| 261 | |||
| 262 | // Perform the same operations as the real macro code. | ||
| 263 | // Note: This value is hardcoded in the macro's code. | ||
| 264 | static constexpr u32 DefaultCBSize = 0x5F00; | ||
| 265 | regs.const_buffer.cb_size = DefaultCBSize; | ||
| 266 | |||
| 267 | GPUVAddr address = regs.ssbo_info.BufferAddress(); | ||
| 268 | regs.const_buffer.cb_address_high = address >> 32; | ||
| 269 | regs.const_buffer.cb_address_low = address & 0xFFFFFFFF; | ||
| 270 | |||
| 271 | regs.const_buffer.cb_pos = buffer_offset; | ||
| 272 | } | ||
| 273 | |||
| 274 | void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) { | 182 | void Maxwell3D::ProcessCBBind(Regs::ShaderStage stage) { |
| 275 | // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. | 183 | // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. |
| 276 | auto& shader = state.shader_stages[static_cast<size_t>(stage)]; | 184 | auto& shader = state.shader_stages[static_cast<size_t>(stage)]; |
| @@ -386,5 +294,10 @@ std::vector<Texture::FullTextureInfo> Maxwell3D::GetStageTextures(Regs::ShaderSt | |||
| 386 | return textures; | 294 | return textures; |
| 387 | } | 295 | } |
| 388 | 296 | ||
| 297 | u32 Maxwell3D::GetRegisterValue(u32 method) const { | ||
| 298 | ASSERT_MSG(method < Regs::NUM_REGS, "Invalid Maxwell3D register"); | ||
| 299 | return regs.reg_array[method]; | ||
| 300 | } | ||
| 301 | |||
| 389 | } // namespace Engines | 302 | } // namespace Engines |
| 390 | } // namespace Tegra | 303 | } // namespace Tegra |
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h index 3066bc606..98b39b2ff 100644 --- a/src/video_core/engines/maxwell_3d.h +++ b/src/video_core/engines/maxwell_3d.h | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | #include "common/common_types.h" | 13 | #include "common/common_types.h" |
| 14 | #include "common/math_util.h" | 14 | #include "common/math_util.h" |
| 15 | #include "video_core/gpu.h" | 15 | #include "video_core/gpu.h" |
| 16 | #include "video_core/macro_interpreter.h" | ||
| 16 | #include "video_core/memory_manager.h" | 17 | #include "video_core/memory_manager.h" |
| 17 | #include "video_core/textures/texture.h" | 18 | #include "video_core/textures/texture.h" |
| 18 | 19 | ||
| @@ -498,22 +499,18 @@ public: | |||
| 498 | bool enabled; | 499 | bool enabled; |
| 499 | }; | 500 | }; |
| 500 | 501 | ||
| 501 | struct ShaderProgramInfo { | ||
| 502 | Regs::ShaderStage stage; | ||
| 503 | Regs::ShaderProgram program; | ||
| 504 | GPUVAddr address; | ||
| 505 | }; | ||
| 506 | |||
| 507 | struct ShaderStageInfo { | 502 | struct ShaderStageInfo { |
| 508 | std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers; | 503 | std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers; |
| 509 | }; | 504 | }; |
| 510 | 505 | ||
| 511 | std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages; | 506 | std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages; |
| 512 | std::array<ShaderProgramInfo, Regs::MaxShaderProgram> shader_programs; | ||
| 513 | }; | 507 | }; |
| 514 | 508 | ||
| 515 | State state{}; | 509 | State state{}; |
| 516 | 510 | ||
| 511 | /// Reads a register value located at the input method address | ||
| 512 | u32 GetRegisterValue(u32 method) const; | ||
| 513 | |||
| 517 | /// Write the value to the register identified by method. | 514 | /// Write the value to the register identified by method. |
| 518 | void WriteReg(u32 method, u32 value, u32 remaining_params); | 515 | void WriteReg(u32 method, u32 value, u32 remaining_params); |
| 519 | 516 | ||
| @@ -533,6 +530,9 @@ private: | |||
| 533 | /// Parameters that have been submitted to the macro call so far. | 530 | /// Parameters that have been submitted to the macro call so far. |
| 534 | std::vector<u32> macro_params; | 531 | std::vector<u32> macro_params; |
| 535 | 532 | ||
| 533 | /// Interpreter for the macro codes uploaded to the GPU. | ||
| 534 | MacroInterpreter macro_interpreter; | ||
| 535 | |||
| 536 | /// Retrieves information about a specific TIC entry from the TIC buffer. | 536 | /// Retrieves information about a specific TIC entry from the TIC buffer. |
| 537 | Texture::TICEntry GetTICEntry(u32 tic_index) const; | 537 | Texture::TICEntry GetTICEntry(u32 tic_index) const; |
| 538 | 538 | ||
| @@ -544,7 +544,7 @@ private: | |||
| 544 | * @param method Method to call | 544 | * @param method Method to call |
| 545 | * @param parameters Arguments to the method call | 545 | * @param parameters Arguments to the method call |
| 546 | */ | 546 | */ |
| 547 | void CallMacroMethod(u32 method, const std::vector<u32>& parameters); | 547 | void CallMacroMethod(u32 method, std::vector<u32> parameters); |
| 548 | 548 | ||
| 549 | /// Handles a write to the QUERY_GET register. | 549 | /// Handles a write to the QUERY_GET register. |
| 550 | void ProcessQueryGet(); | 550 | void ProcessQueryGet(); |
| @@ -557,19 +557,6 @@ private: | |||
| 557 | 557 | ||
| 558 | /// Handles a write to the VERTEX_END_GL register, triggering a draw. | 558 | /// Handles a write to the VERTEX_END_GL register, triggering a draw. |
| 559 | void DrawArrays(); | 559 | void DrawArrays(); |
| 560 | |||
| 561 | /// Method call handlers | ||
| 562 | void BindTextureInfoBuffer(const std::vector<u32>& parameters); | ||
| 563 | void SetShader(const std::vector<u32>& parameters); | ||
| 564 | void BindStorageBuffer(const std::vector<u32>& parameters); | ||
| 565 | |||
| 566 | struct MethodInfo { | ||
| 567 | const char* name; | ||
| 568 | u32 arguments; | ||
| 569 | void (Maxwell3D::*handler)(const std::vector<u32>& parameters); | ||
| 570 | }; | ||
| 571 | |||
| 572 | static const std::unordered_map<u32, MethodInfo> method_handlers; | ||
| 573 | }; | 560 | }; |
| 574 | 561 | ||
| 575 | #define ASSERT_REG_POSITION(field_name, position) \ | 562 | #define ASSERT_REG_POSITION(field_name, position) \ |
diff --git a/src/video_core/macro_interpreter.cpp b/src/video_core/macro_interpreter.cpp new file mode 100644 index 000000000..993a67746 --- /dev/null +++ b/src/video_core/macro_interpreter.cpp | |||
| @@ -0,0 +1,257 @@ | |||
| 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 "video_core/engines/maxwell_3d.h" | ||
| 8 | #include "video_core/macro_interpreter.h" | ||
| 9 | |||
| 10 | namespace Tegra { | ||
| 11 | |||
| 12 | MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d) : maxwell3d(maxwell3d) {} | ||
| 13 | |||
| 14 | void MacroInterpreter::Execute(const std::vector<u32>& code, std::vector<u32> parameters) { | ||
| 15 | Reset(); | ||
| 16 | registers[1] = parameters[0]; | ||
| 17 | this->parameters = std::move(parameters); | ||
| 18 | |||
| 19 | // Execute the code until we hit an exit condition. | ||
| 20 | bool keep_executing = true; | ||
| 21 | while (keep_executing) { | ||
| 22 | keep_executing = Step(code, false); | ||
| 23 | } | ||
| 24 | |||
| 25 | // Assert the the macro used all the input parameters | ||
| 26 | ASSERT(next_parameter_index == this->parameters.size()); | ||
| 27 | } | ||
| 28 | |||
| 29 | void MacroInterpreter::Reset() { | ||
| 30 | registers = {}; | ||
| 31 | pc = 0; | ||
| 32 | delayed_pc = boost::none; | ||
| 33 | method_address.raw = 0; | ||
| 34 | parameters.clear(); | ||
| 35 | // The next parameter index starts at 1, because $r1 already has the value of the first | ||
| 36 | // parameter. | ||
| 37 | next_parameter_index = 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | bool MacroInterpreter::Step(const std::vector<u32>& code, bool is_delay_slot) { | ||
| 41 | u32 base_address = pc; | ||
| 42 | |||
| 43 | Opcode opcode = GetOpcode(code); | ||
| 44 | pc += 4; | ||
| 45 | |||
| 46 | // Update the program counter if we were delayed | ||
| 47 | if (delayed_pc != boost::none) { | ||
| 48 | ASSERT(is_delay_slot); | ||
| 49 | pc = *delayed_pc; | ||
| 50 | delayed_pc = boost::none; | ||
| 51 | } | ||
| 52 | |||
| 53 | switch (opcode.operation) { | ||
| 54 | case Operation::ALU: { | ||
| 55 | u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a), | ||
| 56 | GetRegister(opcode.src_b)); | ||
| 57 | ProcessResult(opcode.result_operation, opcode.dst, result); | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | case Operation::AddImmediate: { | ||
| 61 | ProcessResult(opcode.result_operation, opcode.dst, | ||
| 62 | GetRegister(opcode.src_a) + opcode.immediate); | ||
| 63 | break; | ||
| 64 | } | ||
| 65 | case Operation::ExtractInsert: { | ||
| 66 | u32 dst = GetRegister(opcode.src_a); | ||
| 67 | u32 src = GetRegister(opcode.src_b); | ||
| 68 | |||
| 69 | src = (src >> opcode.bf_src_bit) & opcode.GetBitfieldMask(); | ||
| 70 | dst &= ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit); | ||
| 71 | dst |= src << opcode.bf_dst_bit; | ||
| 72 | ProcessResult(opcode.result_operation, opcode.dst, dst); | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | case Operation::ExtractShiftLeftImmediate: { | ||
| 76 | u32 dst = GetRegister(opcode.src_a); | ||
| 77 | u32 src = GetRegister(opcode.src_b); | ||
| 78 | |||
| 79 | u32 result = ((src >> dst) & opcode.GetBitfieldMask()) << opcode.bf_dst_bit; | ||
| 80 | |||
| 81 | ProcessResult(opcode.result_operation, opcode.dst, result); | ||
| 82 | break; | ||
| 83 | } | ||
| 84 | case Operation::ExtractShiftLeftRegister: { | ||
| 85 | u32 dst = GetRegister(opcode.src_a); | ||
| 86 | u32 src = GetRegister(opcode.src_b); | ||
| 87 | |||
| 88 | u32 result = ((src >> opcode.bf_src_bit) & opcode.GetBitfieldMask()) << dst; | ||
| 89 | |||
| 90 | ProcessResult(opcode.result_operation, opcode.dst, result); | ||
| 91 | break; | ||
| 92 | } | ||
| 93 | case Operation::Read: { | ||
| 94 | u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate); | ||
| 95 | ProcessResult(opcode.result_operation, opcode.dst, result); | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | case Operation::Branch: { | ||
| 99 | ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid"); | ||
| 100 | u32 value = GetRegister(opcode.src_a); | ||
| 101 | bool taken = EvaluateBranchCondition(opcode.branch_condition, value); | ||
| 102 | if (taken) { | ||
| 103 | // Ignore the delay slot if the branch has the annul bit. | ||
| 104 | if (opcode.branch_annul) { | ||
| 105 | pc = base_address + (opcode.immediate << 2); | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 | delayed_pc = base_address + (opcode.immediate << 2); | ||
| 110 | // Execute one more instruction due to the delay slot. | ||
| 111 | return Step(code, true); | ||
| 112 | } | ||
| 113 | break; | ||
| 114 | } | ||
| 115 | default: | ||
| 116 | UNIMPLEMENTED_MSG("Unimplemented macro operation %u", | ||
| 117 | static_cast<u32>(opcode.operation.Value())); | ||
| 118 | } | ||
| 119 | |||
| 120 | if (opcode.is_exit) { | ||
| 121 | // Exit has a delay slot, execute the next instruction | ||
| 122 | // Note: Executing an exit during a branch delay slot will cause the instruction at the | ||
| 123 | // branch target to be executed before exiting. | ||
| 124 | Step(code, true); | ||
| 125 | return false; | ||
| 126 | } | ||
| 127 | |||
| 128 | return true; | ||
| 129 | } | ||
| 130 | |||
| 131 | MacroInterpreter::Opcode MacroInterpreter::GetOpcode(const std::vector<u32>& code) const { | ||
| 132 | ASSERT((pc % sizeof(u32)) == 0); | ||
| 133 | ASSERT(pc < code.size() * sizeof(u32)); | ||
| 134 | return {code[pc / sizeof(u32)]}; | ||
| 135 | } | ||
| 136 | |||
| 137 | u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) const { | ||
| 138 | switch (operation) { | ||
| 139 | case ALUOperation::Add: | ||
| 140 | return src_a + src_b; | ||
| 141 | // TODO(Subv): Implement AddWithCarry | ||
| 142 | case ALUOperation::Subtract: | ||
| 143 | return src_a - src_b; | ||
| 144 | // TODO(Subv): Implement SubtractWithBorrow | ||
| 145 | case ALUOperation::Xor: | ||
| 146 | return src_a ^ src_b; | ||
| 147 | case ALUOperation::Or: | ||
| 148 | return src_a | src_b; | ||
| 149 | case ALUOperation::And: | ||
| 150 | return src_a & src_b; | ||
| 151 | case ALUOperation::AndNot: | ||
| 152 | return src_a & ~src_b; | ||
| 153 | case ALUOperation::Nand: | ||
| 154 | return ~(src_a & src_b); | ||
| 155 | |||
| 156 | default: | ||
| 157 | UNIMPLEMENTED_MSG("Unimplemented ALU operation %u", static_cast<u32>(operation)); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 result) { | ||
| 162 | switch (operation) { | ||
| 163 | case ResultOperation::IgnoreAndFetch: | ||
| 164 | // Fetch parameter and ignore result. | ||
| 165 | SetRegister(reg, FetchParameter()); | ||
| 166 | break; | ||
| 167 | case ResultOperation::Move: | ||
| 168 | // Move result. | ||
| 169 | SetRegister(reg, result); | ||
| 170 | break; | ||
| 171 | case ResultOperation::MoveAndSetMethod: | ||
| 172 | // Move result and use as Method Address. | ||
| 173 | SetRegister(reg, result); | ||
| 174 | SetMethodAddress(result); | ||
| 175 | break; | ||
| 176 | case ResultOperation::FetchAndSend: | ||
| 177 | // Fetch parameter and send result. | ||
| 178 | SetRegister(reg, FetchParameter()); | ||
| 179 | Send(result); | ||
| 180 | break; | ||
| 181 | case ResultOperation::MoveAndSend: | ||
| 182 | // Move and send result. | ||
| 183 | SetRegister(reg, result); | ||
| 184 | Send(result); | ||
| 185 | break; | ||
| 186 | case ResultOperation::FetchAndSetMethod: | ||
| 187 | // Fetch parameter and use result as Method Address. | ||
| 188 | SetRegister(reg, FetchParameter()); | ||
| 189 | SetMethodAddress(result); | ||
| 190 | break; | ||
| 191 | case ResultOperation::MoveAndSetMethodFetchAndSend: | ||
| 192 | // Move result and use as Method Address, then fetch and send parameter. | ||
| 193 | SetRegister(reg, result); | ||
| 194 | SetMethodAddress(result); | ||
| 195 | Send(FetchParameter()); | ||
| 196 | break; | ||
| 197 | case ResultOperation::MoveAndSetMethodSend: | ||
| 198 | // Move result and use as Method Address, then send bits 12:17 of result. | ||
| 199 | SetRegister(reg, result); | ||
| 200 | SetMethodAddress(result); | ||
| 201 | Send((result >> 12) & 0b111111); | ||
| 202 | break; | ||
| 203 | default: | ||
| 204 | UNIMPLEMENTED_MSG("Unimplemented result operation %u", static_cast<u32>(operation)); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | u32 MacroInterpreter::FetchParameter() { | ||
| 209 | ASSERT(next_parameter_index < parameters.size()); | ||
| 210 | return parameters[next_parameter_index++]; | ||
| 211 | } | ||
| 212 | |||
| 213 | u32 MacroInterpreter::GetRegister(u32 register_id) const { | ||
| 214 | // Register 0 is supposed to always return 0. | ||
| 215 | if (register_id == 0) | ||
| 216 | return 0; | ||
| 217 | |||
| 218 | ASSERT(register_id < registers.size()); | ||
| 219 | return registers[register_id]; | ||
| 220 | } | ||
| 221 | |||
| 222 | void MacroInterpreter::SetRegister(u32 register_id, u32 value) { | ||
| 223 | // Register 0 is supposed to always return 0. NOP is implemented as a store to the zero | ||
| 224 | // register. | ||
| 225 | if (register_id == 0) | ||
| 226 | return; | ||
| 227 | |||
| 228 | ASSERT(register_id < registers.size()); | ||
| 229 | registers[register_id] = value; | ||
| 230 | } | ||
| 231 | |||
| 232 | void MacroInterpreter::SetMethodAddress(u32 address) { | ||
| 233 | method_address.raw = address; | ||
| 234 | } | ||
| 235 | |||
| 236 | void MacroInterpreter::Send(u32 value) { | ||
| 237 | maxwell3d.WriteReg(method_address.address, value, 0); | ||
| 238 | // Increment the method address by the method increment. | ||
| 239 | method_address.address.Assign(method_address.address.Value() + | ||
| 240 | method_address.increment.Value()); | ||
| 241 | } | ||
| 242 | |||
| 243 | u32 MacroInterpreter::Read(u32 method) const { | ||
| 244 | return maxwell3d.GetRegisterValue(method); | ||
| 245 | } | ||
| 246 | |||
| 247 | bool MacroInterpreter::EvaluateBranchCondition(BranchCondition cond, u32 value) const { | ||
| 248 | switch (cond) { | ||
| 249 | case BranchCondition::Zero: | ||
| 250 | return value == 0; | ||
| 251 | case BranchCondition::NotZero: | ||
| 252 | return value != 0; | ||
| 253 | } | ||
| 254 | UNREACHABLE(); | ||
| 255 | } | ||
| 256 | |||
| 257 | } // namespace Tegra | ||
diff --git a/src/video_core/macro_interpreter.h b/src/video_core/macro_interpreter.h new file mode 100644 index 000000000..a71e359d8 --- /dev/null +++ b/src/video_core/macro_interpreter.h | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <vector> | ||
| 9 | #include <boost/optional.hpp> | ||
| 10 | #include "common/bit_field.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | |||
| 13 | namespace Tegra { | ||
| 14 | namespace Engines { | ||
| 15 | class Maxwell3D; | ||
| 16 | } | ||
| 17 | |||
| 18 | class MacroInterpreter final { | ||
| 19 | public: | ||
| 20 | explicit MacroInterpreter(Engines::Maxwell3D& maxwell3d); | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Executes the macro code with the specified input parameters. | ||
| 24 | * @param code The macro byte code to execute | ||
| 25 | * @param parameters The parameters of the macro | ||
| 26 | */ | ||
| 27 | void Execute(const std::vector<u32>& code, std::vector<u32> parameters); | ||
| 28 | |||
| 29 | private: | ||
| 30 | enum class Operation : u32 { | ||
| 31 | ALU = 0, | ||
| 32 | AddImmediate = 1, | ||
| 33 | ExtractInsert = 2, | ||
| 34 | ExtractShiftLeftImmediate = 3, | ||
| 35 | ExtractShiftLeftRegister = 4, | ||
| 36 | Read = 5, | ||
| 37 | Unused = 6, // This operation doesn't seem to be a valid encoding. | ||
| 38 | Branch = 7, | ||
| 39 | }; | ||
| 40 | |||
| 41 | enum class ALUOperation : u32 { | ||
| 42 | Add = 0, | ||
| 43 | AddWithCarry = 1, | ||
| 44 | Subtract = 2, | ||
| 45 | SubtractWithBorrow = 3, | ||
| 46 | // Operations 4-7 don't seem to be valid encodings. | ||
| 47 | Xor = 8, | ||
| 48 | Or = 9, | ||
| 49 | And = 10, | ||
| 50 | AndNot = 11, | ||
| 51 | Nand = 12 | ||
| 52 | }; | ||
| 53 | |||
| 54 | enum class ResultOperation : u32 { | ||
| 55 | IgnoreAndFetch = 0, | ||
| 56 | Move = 1, | ||
| 57 | MoveAndSetMethod = 2, | ||
| 58 | FetchAndSend = 3, | ||
| 59 | MoveAndSend = 4, | ||
| 60 | FetchAndSetMethod = 5, | ||
| 61 | MoveAndSetMethodFetchAndSend = 6, | ||
| 62 | MoveAndSetMethodSend = 7 | ||
| 63 | }; | ||
| 64 | |||
| 65 | enum class BranchCondition : u32 { | ||
| 66 | Zero = 0, | ||
| 67 | NotZero = 1, | ||
| 68 | }; | ||
| 69 | |||
| 70 | union Opcode { | ||
| 71 | u32 raw; | ||
| 72 | BitField<0, 3, Operation> operation; | ||
| 73 | BitField<4, 3, ResultOperation> result_operation; | ||
| 74 | BitField<4, 1, BranchCondition> branch_condition; | ||
| 75 | BitField<5, 1, u32> | ||
| 76 | branch_annul; // If set on a branch, then the branch doesn't have a delay slot. | ||
| 77 | BitField<7, 1, u32> is_exit; | ||
| 78 | BitField<8, 3, u32> dst; | ||
| 79 | BitField<11, 3, u32> src_a; | ||
| 80 | BitField<14, 3, u32> src_b; | ||
| 81 | // The signed immediate overlaps the second source operand and the alu operation. | ||
| 82 | BitField<14, 18, s32> immediate; | ||
| 83 | |||
| 84 | BitField<17, 5, ALUOperation> alu_operation; | ||
| 85 | |||
| 86 | // Bitfield instructions data | ||
| 87 | BitField<17, 5, u32> bf_src_bit; | ||
| 88 | BitField<22, 5, u32> bf_size; | ||
| 89 | BitField<27, 5, u32> bf_dst_bit; | ||
| 90 | |||
| 91 | u32 GetBitfieldMask() const { | ||
| 92 | return (1 << bf_size) - 1; | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | |||
| 96 | union MethodAddress { | ||
| 97 | u32 raw; | ||
| 98 | BitField<0, 12, u32> address; | ||
| 99 | BitField<12, 6, u32> increment; | ||
| 100 | }; | ||
| 101 | |||
| 102 | /// Resets the execution engine state, zeroing registers, etc. | ||
| 103 | void Reset(); | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Executes a single macro instruction located at the current program counter. Returns whether | ||
| 107 | * the interpreter should keep running. | ||
| 108 | * @param code The macro code to execute. | ||
| 109 | * @param is_delay_slot Whether the current step is being executed due to a delay slot in a | ||
| 110 | * previous instruction. | ||
| 111 | */ | ||
| 112 | bool Step(const std::vector<u32>& code, bool is_delay_slot); | ||
| 113 | |||
| 114 | /// Calculates the result of an ALU operation. src_a OP src_b; | ||
| 115 | u32 GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) const; | ||
| 116 | |||
| 117 | /// Performs the result operation on the input result and stores it in the specified register | ||
| 118 | /// (if necessary). | ||
| 119 | void ProcessResult(ResultOperation operation, u32 reg, u32 result); | ||
| 120 | |||
| 121 | /// Evaluates the branch condition and returns whether the branch should be taken or not. | ||
| 122 | bool EvaluateBranchCondition(BranchCondition cond, u32 value) const; | ||
| 123 | |||
| 124 | /// Reads an opcode at the current program counter location. | ||
| 125 | Opcode GetOpcode(const std::vector<u32>& code) const; | ||
| 126 | |||
| 127 | /// Returns the specified register's value. Register 0 is hardcoded to always return 0. | ||
| 128 | u32 GetRegister(u32 register_id) const; | ||
| 129 | |||
| 130 | /// Sets the register to the input value. | ||
| 131 | void SetRegister(u32 register_id, u32 value); | ||
| 132 | |||
| 133 | /// Sets the method address to use for the next Send instruction. | ||
| 134 | void SetMethodAddress(u32 address); | ||
| 135 | |||
| 136 | /// Calls a GPU Engine method with the input parameter. | ||
| 137 | void Send(u32 value); | ||
| 138 | |||
| 139 | /// Reads a GPU register located at the method address. | ||
| 140 | u32 Read(u32 method) const; | ||
| 141 | |||
| 142 | /// Returns the next parameter in the parameter queue. | ||
| 143 | u32 FetchParameter(); | ||
| 144 | |||
| 145 | Engines::Maxwell3D& maxwell3d; | ||
| 146 | |||
| 147 | u32 pc; ///< Current program counter | ||
| 148 | boost::optional<u32> | ||
| 149 | delayed_pc; ///< Program counter to execute at after the delay slot is executed. | ||
| 150 | |||
| 151 | static constexpr size_t NumMacroRegisters = 8; | ||
| 152 | |||
| 153 | /// General purpose macro registers. | ||
| 154 | std::array<u32, NumMacroRegisters> registers = {}; | ||
| 155 | |||
| 156 | /// Method address to use for the next Send instruction. | ||
| 157 | MethodAddress method_address = {}; | ||
| 158 | |||
| 159 | /// Input parameters of the current macro. | ||
| 160 | std::vector<u32> parameters; | ||
| 161 | /// Index of the next parameter that will be fetched by the 'parm' instruction. | ||
| 162 | u32 next_parameter_index = 0; | ||
| 163 | }; | ||
| 164 | } // namespace Tegra | ||