summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar David Marcec2020-05-29 14:53:27 +1000
committerGravatar David Marcec2020-05-30 11:40:04 +1000
commitb032ebdfee1928c4458eaf15faa0cff299371e65 (patch)
tree2528faa725bf604cf3e44dc38c8f20d6f69aee70 /src
parentMerge pull request #4017 from ogniK5377/xbyak (diff)
downloadyuzu-b032ebdfee1928c4458eaf15faa0cff299371e65.tar.gz
yuzu-b032ebdfee1928c4458eaf15faa0cff299371e65.tar.xz
yuzu-b032ebdfee1928c4458eaf15faa0cff299371e65.zip
Implement macro JIT
Diffstat (limited to 'src')
-rw-r--r--src/core/settings.h1
-rw-r--r--src/video_core/CMakeLists.txt8
-rw-r--r--src/video_core/engines/maxwell_3d.cpp19
-rw-r--r--src/video_core/engines/maxwell_3d.h19
-rw-r--r--src/video_core/macro/macro.cpp45
-rw-r--r--src/video_core/macro/macro.h128
-rw-r--r--src/video_core/macro/macro_interpreter.cpp (renamed from src/video_core/macro_interpreter.cpp)198
-rw-r--r--src/video_core/macro/macro_interpreter.h (renamed from src/video_core/macro_interpreter.h)51
-rw-r--r--src/video_core/macro/macro_jit_x64.cpp633
-rw-r--r--src/video_core/macro/macro_jit_x64.h98
-rw-r--r--src/yuzu/configuration/config.cpp3
-rw-r--r--src/yuzu/configuration/configure_debug.cpp3
-rw-r--r--src/yuzu/configuration/configure_debug.ui13
-rw-r--r--src/yuzu_cmd/config.cpp2
-rw-r--r--src/yuzu_cmd/default_ini.h2
15 files changed, 1034 insertions, 189 deletions
diff --git a/src/core/settings.h b/src/core/settings.h
index 78eb33737..36cd66fd4 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -474,6 +474,7 @@ struct Values {
474 bool reporting_services; 474 bool reporting_services;
475 bool quest_flag; 475 bool quest_flag;
476 bool disable_cpu_opt; 476 bool disable_cpu_opt;
477 bool disable_macro_jit;
477 478
478 // BCAT 479 // BCAT
479 std::string bcat_backend; 480 std::string bcat_backend;
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index d6ee82836..2bf8d68ce 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -25,6 +25,12 @@ add_library(video_core STATIC
25 engines/shader_bytecode.h 25 engines/shader_bytecode.h
26 engines/shader_header.h 26 engines/shader_header.h
27 engines/shader_type.h 27 engines/shader_type.h
28 macro/macro.cpp
29 macro/macro.h
30 macro/macro_interpreter.cpp
31 macro/macro_interpreter.h
32 macro/macro_jit_x64.cpp
33 macro/macro_jit_x64.h
28 fence_manager.h 34 fence_manager.h
29 gpu.cpp 35 gpu.cpp
30 gpu.h 36 gpu.h
@@ -36,8 +42,6 @@ add_library(video_core STATIC
36 gpu_thread.h 42 gpu_thread.h
37 guest_driver.cpp 43 guest_driver.cpp
38 guest_driver.h 44 guest_driver.h
39 macro_interpreter.cpp
40 macro_interpreter.h
41 memory_manager.cpp 45 memory_manager.cpp
42 memory_manager.h 46 memory_manager.h
43 morton.cpp 47 morton.cpp
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index 004f6b261..934a1d6db 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -25,9 +25,8 @@ constexpr u32 MacroRegistersStart = 0xE00;
25Maxwell3D::Maxwell3D(Core::System& system, VideoCore::RasterizerInterface& rasterizer, 25Maxwell3D::Maxwell3D(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
26 MemoryManager& memory_manager) 26 MemoryManager& memory_manager)
27 : system{system}, rasterizer{rasterizer}, memory_manager{memory_manager}, 27 : system{system}, rasterizer{rasterizer}, memory_manager{memory_manager},
28 macro_interpreter{*this}, upload_state{memory_manager, regs.upload} { 28 macro_engine(GetMacroEngine(*this)), upload_state{memory_manager, regs.upload} {
29 dirty.flags.flip(); 29 dirty.flags.flip();
30
31 InitializeRegisterDefaults(); 30 InitializeRegisterDefaults();
32} 31}
33 32
@@ -116,7 +115,7 @@ void Maxwell3D::InitializeRegisterDefaults() {
116 mme_inline[MAXWELL3D_REG_INDEX(index_array.count)] = true; 115 mme_inline[MAXWELL3D_REG_INDEX(index_array.count)] = true;
117} 116}
118 117
119void Maxwell3D::CallMacroMethod(u32 method, std::size_t num_parameters, const u32* parameters) { 118void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32>&& parameters) {
120 // Reset the current macro. 119 // Reset the current macro.
121 executing_macro = 0; 120 executing_macro = 0;
122 121
@@ -125,7 +124,7 @@ void Maxwell3D::CallMacroMethod(u32 method, std::size_t num_parameters, const u3
125 ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size()); 124 ((method - MacroRegistersStart) >> 1) % static_cast<u32>(macro_positions.size());
126 125
127 // Execute the current macro. 126 // Execute the current macro.
128 macro_interpreter.Execute(macro_positions[entry], num_parameters, parameters); 127 macro_engine->Execute(macro_positions[entry], std::move(parameters));
129 if (mme_draw.current_mode != MMEDrawMode::Undefined) { 128 if (mme_draw.current_mode != MMEDrawMode::Undefined) {
130 FlushMMEInlineDraw(); 129 FlushMMEInlineDraw();
131 } 130 }
@@ -161,8 +160,7 @@ void Maxwell3D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
161 160
162 // Call the macro when there are no more parameters in the command buffer 161 // Call the macro when there are no more parameters in the command buffer
163 if (is_last_call) { 162 if (is_last_call) {
164 CallMacroMethod(executing_macro, macro_params.size(), macro_params.data()); 163 CallMacroMethod(executing_macro, std::move(macro_params));
165 macro_params.clear();
166 } 164 }
167 return; 165 return;
168 } 166 }
@@ -197,7 +195,7 @@ void Maxwell3D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
197 break; 195 break;
198 } 196 }
199 case MAXWELL3D_REG_INDEX(macros.data): { 197 case MAXWELL3D_REG_INDEX(macros.data): {
200 ProcessMacroUpload(arg); 198 macro_engine->AddCode(regs.macros.upload_address, arg);
201 break; 199 break;
202 } 200 }
203 case MAXWELL3D_REG_INDEX(macros.bind): { 201 case MAXWELL3D_REG_INDEX(macros.bind): {
@@ -306,8 +304,7 @@ void Maxwell3D::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
306 304
307 // Call the macro when there are no more parameters in the command buffer 305 // Call the macro when there are no more parameters in the command buffer
308 if (amount == methods_pending) { 306 if (amount == methods_pending) {
309 CallMacroMethod(executing_macro, macro_params.size(), macro_params.data()); 307 CallMacroMethod(executing_macro, std::move(macro_params));
310 macro_params.clear();
311 } 308 }
312 return; 309 return;
313 } 310 }
@@ -420,9 +417,7 @@ void Maxwell3D::FlushMMEInlineDraw() {
420} 417}
421 418
422void Maxwell3D::ProcessMacroUpload(u32 data) { 419void Maxwell3D::ProcessMacroUpload(u32 data) {
423 ASSERT_MSG(regs.macros.upload_address < macro_memory.size(), 420 macro_engine->AddCode(regs.macros.upload_address++, data);
424 "upload_address exceeded macro_memory size!");
425 macro_memory[regs.macros.upload_address++] = data;
426} 421}
427 422
428void Maxwell3D::ProcessMacroBind(u32 data) { 423void Maxwell3D::ProcessMacroBind(u32 data) {
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h
index 05dd6b39b..077bc9841 100644
--- a/src/video_core/engines/maxwell_3d.h
+++ b/src/video_core/engines/maxwell_3d.h
@@ -23,7 +23,7 @@
23#include "video_core/engines/engine_upload.h" 23#include "video_core/engines/engine_upload.h"
24#include "video_core/engines/shader_type.h" 24#include "video_core/engines/shader_type.h"
25#include "video_core/gpu.h" 25#include "video_core/gpu.h"
26#include "video_core/macro_interpreter.h" 26#include "video_core/macro/macro.h"
27#include "video_core/textures/texture.h" 27#include "video_core/textures/texture.h"
28 28
29namespace Core { 29namespace Core {
@@ -1411,15 +1411,6 @@ public:
1411 1411
1412 const VideoCore::GuestDriverProfile& AccessGuestDriverProfile() const override; 1412 const VideoCore::GuestDriverProfile& AccessGuestDriverProfile() const override;
1413 1413
1414 /// Memory for macro code - it's undetermined how big this is, however 1MB is much larger than
1415 /// we've seen used.
1416 using MacroMemory = std::array<u32, 0x40000>;
1417
1418 /// Gets a reference to macro memory.
1419 const MacroMemory& GetMacroMemory() const {
1420 return macro_memory;
1421 }
1422
1423 bool ShouldExecute() const { 1414 bool ShouldExecute() const {
1424 return execute_on; 1415 return execute_on;
1425 } 1416 }
@@ -1468,16 +1459,14 @@ private:
1468 1459
1469 std::array<bool, Regs::NUM_REGS> mme_inline{}; 1460 std::array<bool, Regs::NUM_REGS> mme_inline{};
1470 1461
1471 /// Memory for macro code
1472 MacroMemory macro_memory;
1473
1474 /// Macro method that is currently being executed / being fed parameters. 1462 /// Macro method that is currently being executed / being fed parameters.
1475 u32 executing_macro = 0; 1463 u32 executing_macro = 0;
1476 /// Parameters that have been submitted to the macro call so far. 1464 /// Parameters that have been submitted to the macro call so far.
1477 std::vector<u32> macro_params; 1465 std::vector<u32> macro_params;
1478 1466
1479 /// Interpreter for the macro codes uploaded to the GPU. 1467 /// Interpreter for the macro codes uploaded to the GPU.
1480 MacroInterpreter macro_interpreter; 1468 std::unique_ptr<MacroEngine> macro_engine;
1469 // MacroInterpreter macro_interpreter;
1481 1470
1482 static constexpr u32 null_cb_data = 0xFFFFFFFF; 1471 static constexpr u32 null_cb_data = 0xFFFFFFFF;
1483 struct { 1472 struct {
@@ -1506,7 +1495,7 @@ private:
1506 * @param num_parameters Number of arguments 1495 * @param num_parameters Number of arguments
1507 * @param parameters Arguments to the method call 1496 * @param parameters Arguments to the method call
1508 */ 1497 */
1509 void CallMacroMethod(u32 method, std::size_t num_parameters, const u32* parameters); 1498 void CallMacroMethod(u32 method, std::vector<u32>&& parameters);
1510 1499
1511 /// Handles writes to the macro uploading register. 1500 /// Handles writes to the macro uploading register.
1512 void ProcessMacroUpload(u32 data); 1501 void ProcessMacroUpload(u32 data);
diff --git a/src/video_core/macro/macro.cpp b/src/video_core/macro/macro.cpp
new file mode 100644
index 000000000..85a6e5dd4
--- /dev/null
+++ b/src/video_core/macro/macro.cpp
@@ -0,0 +1,45 @@
1// Copyright 2020 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 "core/settings.h"
8#include "video_core/macro/macro.h"
9#include "video_core/macro/macro_interpreter.h"
10#include "video_core/macro/macro_jit_x64.h"
11
12namespace Tegra {
13
14void MacroEngine::AddCode(u32 method, u32 data) {
15 uploaded_macro_code[method].push_back(data);
16}
17
18void MacroEngine::Execute(u32 method, std::vector<u32> parameters) {
19 auto compiled_macro = macro_cache.find(method);
20 if (compiled_macro != macro_cache.end()) {
21 compiled_macro->second->Execute(parameters, method);
22 } else {
23 // Macro not compiled, check if it's uploaded and if so, compile it
24 auto macro_code = uploaded_macro_code.find(method);
25 if (macro_code == uploaded_macro_code.end()) {
26 UNREACHABLE_MSG("Macro 0x{0:x} was not uploaded", method);
27 return;
28 }
29 macro_cache[method] = Compile(macro_code->second);
30 macro_cache[method]->Execute(parameters, method);
31 }
32}
33
34std::unique_ptr<MacroEngine> GetMacroEngine(Engines::Maxwell3D& maxwell3d) {
35 if (Settings::values.disable_macro_jit) {
36 return std::make_unique<MacroInterpreter>(maxwell3d);
37 }
38#ifdef ARCHITECTURE_x86_64
39 return std::make_unique<MacroJITx64>(maxwell3d);
40#else
41 return std::make_unique<MacroInterpreter>(maxwell3d);
42#endif
43}
44
45} // namespace Tegra
diff --git a/src/video_core/macro/macro.h b/src/video_core/macro/macro.h
new file mode 100644
index 000000000..28ca243d1
--- /dev/null
+++ b/src/video_core/macro/macro.h
@@ -0,0 +1,128 @@
1// Copyright 2020 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 <memory>
8#include <unordered_map>
9#include <vector>
10#include "common/bit_field.h"
11#include "common/common_types.h"
12
13namespace Tegra {
14namespace Engines {
15class Maxwell3D;
16}
17namespace Macro {
18constexpr std::size_t NUM_MACRO_REGISTERS = 8;
19enum class Operation : u32 {
20 ALU = 0,
21 AddImmediate = 1,
22 ExtractInsert = 2,
23 ExtractShiftLeftImmediate = 3,
24 ExtractShiftLeftRegister = 4,
25 Read = 5,
26 Unused = 6, // This operation doesn't seem to be a valid encoding.
27 Branch = 7,
28};
29
30enum class ALUOperation : u32 {
31 Add = 0,
32 AddWithCarry = 1,
33 Subtract = 2,
34 SubtractWithBorrow = 3,
35 // Operations 4-7 don't seem to be valid encodings.
36 Xor = 8,
37 Or = 9,
38 And = 10,
39 AndNot = 11,
40 Nand = 12
41};
42
43enum class ResultOperation : u32 {
44 IgnoreAndFetch = 0,
45 Move = 1,
46 MoveAndSetMethod = 2,
47 FetchAndSend = 3,
48 MoveAndSend = 4,
49 FetchAndSetMethod = 5,
50 MoveAndSetMethodFetchAndSend = 6,
51 MoveAndSetMethodSend = 7
52};
53
54enum class BranchCondition : u32 {
55 Zero = 0,
56 NotZero = 1,
57};
58
59union Opcode {
60 u32 raw;
61 BitField<0, 3, Operation> operation;
62 BitField<4, 3, ResultOperation> result_operation;
63 BitField<4, 1, BranchCondition> branch_condition;
64 // If set on a branch, then the branch doesn't have a delay slot.
65 BitField<5, 1, u32> branch_annul;
66 BitField<7, 1, u32> is_exit;
67 BitField<8, 3, u32> dst;
68 BitField<11, 3, u32> src_a;
69 BitField<14, 3, u32> src_b;
70 // The signed immediate overlaps the second source operand and the alu operation.
71 BitField<14, 18, s32> immediate;
72
73 BitField<17, 5, ALUOperation> alu_operation;
74
75 // Bitfield instructions data
76 BitField<17, 5, u32> bf_src_bit;
77 BitField<22, 5, u32> bf_size;
78 BitField<27, 5, u32> bf_dst_bit;
79
80 u32 GetBitfieldMask() const {
81 return (1 << bf_size) - 1;
82 }
83
84 s32 GetBranchTarget() const {
85 return static_cast<s32>(immediate * sizeof(u32));
86 }
87};
88
89union MethodAddress {
90 u32 raw;
91 BitField<0, 12, u32> address;
92 BitField<12, 6, u32> increment;
93};
94
95} // namespace Macro
96
97class CachedMacro {
98public:
99 virtual ~CachedMacro() = default;
100 /**
101 * Executes the macro code with the specified input parameters.
102 * @param code The macro byte code to execute
103 * @param parameters The parameters of the macro
104 */
105 virtual void Execute(std::vector<u32>& parameters, u32 method) = 0;
106};
107
108class MacroEngine {
109public:
110 virtual ~MacroEngine() = default;
111
112 // Store the uploaded macro code to compile them when they're called.
113 void AddCode(u32 method, u32 data);
114
115 // Compiles the macro if its not in the cache, and executes the compiled macro
116 void Execute(u32 method, std::vector<u32> parameters);
117
118protected:
119 virtual std::unique_ptr<CachedMacro> Compile(const std::vector<u32>& code) = 0;
120
121private:
122 std::unordered_map<u32, std::unique_ptr<CachedMacro>> macro_cache;
123 std::unordered_map<u32, std::vector<u32>> uploaded_macro_code;
124};
125
126std::unique_ptr<MacroEngine> GetMacroEngine(Engines::Maxwell3D& maxwell3d);
127
128} // namespace Tegra
diff --git a/src/video_core/macro_interpreter.cpp b/src/video_core/macro/macro_interpreter.cpp
index 947364928..e63296a21 100644
--- a/src/video_core/macro_interpreter.cpp
+++ b/src/video_core/macro/macro_interpreter.cpp
@@ -1,4 +1,4 @@
1// Copyright 2018 yuzu Emulator Project 1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
@@ -6,109 +6,46 @@
6#include "common/logging/log.h" 6#include "common/logging/log.h"
7#include "common/microprofile.h" 7#include "common/microprofile.h"
8#include "video_core/engines/maxwell_3d.h" 8#include "video_core/engines/maxwell_3d.h"
9#include "video_core/macro_interpreter.h" 9#include "video_core/macro/macro_interpreter.h"
10 10
11MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192)); 11MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192));
12 12
13namespace Tegra { 13namespace Tegra {
14namespace { 14MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d) : maxwell3d(maxwell3d) {}
15enum 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
27enum 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
40enum 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
51enum class MacroInterpreter::BranchCondition : u32 {
52 Zero = 0,
53 NotZero = 1,
54};
55
56union 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 15
81 s32 GetBranchTarget() const { 16std::unique_ptr<CachedMacro> MacroInterpreter::Compile(const std::vector<u32>& code) {
82 return static_cast<s32>(immediate * sizeof(u32)); 17 return std::make_unique<MacroInterpreterImpl>(maxwell3d, code);
83 } 18}
84};
85 19
86MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d) : maxwell3d(maxwell3d) {} 20MacroInterpreterImpl::MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d,
21 const std::vector<u32>& code)
22 : maxwell3d(maxwell3d), code(code) {}
87 23
88void MacroInterpreter::Execute(u32 offset, std::size_t num_parameters, const u32* parameters) { 24void MacroInterpreterImpl::Execute(std::vector<u32>& parameters, u32 method) {
89 MICROPROFILE_SCOPE(MacroInterp); 25 MICROPROFILE_SCOPE(MacroInterp);
90 Reset(); 26 Reset();
91 27
92 registers[1] = parameters[0]; 28 registers[1] = parameters[0];
29 num_parameters = parameters.size();
93 30
94 if (num_parameters > parameters_capacity) { 31 if (num_parameters > parameters_capacity) {
95 parameters_capacity = num_parameters; 32 parameters_capacity = num_parameters;
96 this->parameters = std::make_unique<u32[]>(num_parameters); 33 this->parameters = std::make_unique<u32[]>(num_parameters);
97 } 34 }
98 std::memcpy(this->parameters.get(), parameters, num_parameters * sizeof(u32)); 35 std::memcpy(this->parameters.get(), parameters.data(), num_parameters * sizeof(u32));
99 this->num_parameters = num_parameters; 36 this->num_parameters = num_parameters;
100 37
101 // Execute the code until we hit an exit condition. 38 // Execute the code until we hit an exit condition.
102 bool keep_executing = true; 39 bool keep_executing = true;
103 while (keep_executing) { 40 while (keep_executing) {
104 keep_executing = Step(offset, false); 41 keep_executing = Step(false);
105 } 42 }
106 43
107 // Assert the the macro used all the input parameters 44 // Assert the the macro used all the input parameters
108 ASSERT(next_parameter_index == num_parameters); 45 ASSERT(next_parameter_index == num_parameters);
109} 46}
110 47
111void MacroInterpreter::Reset() { 48void MacroInterpreterImpl::Reset() {
112 registers = {}; 49 registers = {};
113 pc = 0; 50 pc = 0;
114 delayed_pc = {}; 51 delayed_pc = {};
@@ -120,10 +57,10 @@ void MacroInterpreter::Reset() {
120 carry_flag = false; 57 carry_flag = false;
121} 58}
122 59
123bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) { 60bool MacroInterpreterImpl::Step(bool is_delay_slot) {
124 u32 base_address = pc; 61 u32 base_address = pc;
125 62
126 Opcode opcode = GetOpcode(offset); 63 Macro::Opcode opcode = GetOpcode();
127 pc += 4; 64 pc += 4;
128 65
129 // Update the program counter if we were delayed 66 // Update the program counter if we were delayed
@@ -134,18 +71,18 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
134 } 71 }
135 72
136 switch (opcode.operation) { 73 switch (opcode.operation) {
137 case Operation::ALU: { 74 case Macro::Operation::ALU: {
138 u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a), 75 u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a),
139 GetRegister(opcode.src_b)); 76 GetRegister(opcode.src_b));
140 ProcessResult(opcode.result_operation, opcode.dst, result); 77 ProcessResult(opcode.result_operation, opcode.dst, result);
141 break; 78 break;
142 } 79 }
143 case Operation::AddImmediate: { 80 case Macro::Operation::AddImmediate: {
144 ProcessResult(opcode.result_operation, opcode.dst, 81 ProcessResult(opcode.result_operation, opcode.dst,
145 GetRegister(opcode.src_a) + opcode.immediate); 82 GetRegister(opcode.src_a) + opcode.immediate);
146 break; 83 break;
147 } 84 }
148 case Operation::ExtractInsert: { 85 case Macro::Operation::ExtractInsert: {
149 u32 dst = GetRegister(opcode.src_a); 86 u32 dst = GetRegister(opcode.src_a);
150 u32 src = GetRegister(opcode.src_b); 87 u32 src = GetRegister(opcode.src_b);
151 88
@@ -155,7 +92,7 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
155 ProcessResult(opcode.result_operation, opcode.dst, dst); 92 ProcessResult(opcode.result_operation, opcode.dst, dst);
156 break; 93 break;
157 } 94 }
158 case Operation::ExtractShiftLeftImmediate: { 95 case Macro::Operation::ExtractShiftLeftImmediate: {
159 u32 dst = GetRegister(opcode.src_a); 96 u32 dst = GetRegister(opcode.src_a);
160 u32 src = GetRegister(opcode.src_b); 97 u32 src = GetRegister(opcode.src_b);
161 98
@@ -164,7 +101,7 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
164 ProcessResult(opcode.result_operation, opcode.dst, result); 101 ProcessResult(opcode.result_operation, opcode.dst, result);
165 break; 102 break;
166 } 103 }
167 case Operation::ExtractShiftLeftRegister: { 104 case Macro::Operation::ExtractShiftLeftRegister: {
168 u32 dst = GetRegister(opcode.src_a); 105 u32 dst = GetRegister(opcode.src_a);
169 u32 src = GetRegister(opcode.src_b); 106 u32 src = GetRegister(opcode.src_b);
170 107
@@ -173,12 +110,12 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
173 ProcessResult(opcode.result_operation, opcode.dst, result); 110 ProcessResult(opcode.result_operation, opcode.dst, result);
174 break; 111 break;
175 } 112 }
176 case Operation::Read: { 113 case Macro::Operation::Read: {
177 u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate); 114 u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate);
178 ProcessResult(opcode.result_operation, opcode.dst, result); 115 ProcessResult(opcode.result_operation, opcode.dst, result);
179 break; 116 break;
180 } 117 }
181 case Operation::Branch: { 118 case Macro::Operation::Branch: {
182 ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid"); 119 ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
183 u32 value = GetRegister(opcode.src_a); 120 u32 value = GetRegister(opcode.src_a);
184 bool taken = EvaluateBranchCondition(opcode.branch_condition, value); 121 bool taken = EvaluateBranchCondition(opcode.branch_condition, value);
@@ -191,7 +128,7 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
191 128
192 delayed_pc = base_address + opcode.GetBranchTarget(); 129 delayed_pc = base_address + opcode.GetBranchTarget();
193 // Execute one more instruction due to the delay slot. 130 // Execute one more instruction due to the delay slot.
194 return Step(offset, true); 131 return Step(true);
195 } 132 }
196 break; 133 break;
197 } 134 }
@@ -204,51 +141,44 @@ bool MacroInterpreter::Step(u32 offset, bool is_delay_slot) {
204 // cause an exit if it's executed inside a delay slot. 141 // cause an exit if it's executed inside a delay slot.
205 if (opcode.is_exit && !is_delay_slot) { 142 if (opcode.is_exit && !is_delay_slot) {
206 // Exit has a delay slot, execute the next instruction 143 // Exit has a delay slot, execute the next instruction
207 Step(offset, true); 144 Step(true);
208 return false; 145 return false;
209 } 146 }
210 147
211 return true; 148 return true;
212} 149}
213 150
214MacroInterpreter::Opcode MacroInterpreter::GetOpcode(u32 offset) const { 151u32 MacroInterpreterImpl::GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b) {
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
221u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b) {
222 switch (operation) { 152 switch (operation) {
223 case ALUOperation::Add: { 153 case Macro::ALUOperation::Add: {
224 const u64 result{static_cast<u64>(src_a) + src_b}; 154 const u64 result{static_cast<u64>(src_a) + src_b};
225 carry_flag = result > 0xffffffff; 155 carry_flag = result > 0xffffffff;
226 return static_cast<u32>(result); 156 return static_cast<u32>(result);
227 } 157 }
228 case ALUOperation::AddWithCarry: { 158 case Macro::ALUOperation::AddWithCarry: {
229 const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)}; 159 const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)};
230 carry_flag = result > 0xffffffff; 160 carry_flag = result > 0xffffffff;
231 return static_cast<u32>(result); 161 return static_cast<u32>(result);
232 } 162 }
233 case ALUOperation::Subtract: { 163 case Macro::ALUOperation::Subtract: {
234 const u64 result{static_cast<u64>(src_a) - src_b}; 164 const u64 result{static_cast<u64>(src_a) - src_b};
235 carry_flag = result < 0x100000000; 165 carry_flag = result < 0x100000000;
236 return static_cast<u32>(result); 166 return static_cast<u32>(result);
237 } 167 }
238 case ALUOperation::SubtractWithBorrow: { 168 case Macro::ALUOperation::SubtractWithBorrow: {
239 const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)}; 169 const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)};
240 carry_flag = result < 0x100000000; 170 carry_flag = result < 0x100000000;
241 return static_cast<u32>(result); 171 return static_cast<u32>(result);
242 } 172 }
243 case ALUOperation::Xor: 173 case Macro::ALUOperation::Xor:
244 return src_a ^ src_b; 174 return src_a ^ src_b;
245 case ALUOperation::Or: 175 case Macro::ALUOperation::Or:
246 return src_a | src_b; 176 return src_a | src_b;
247 case ALUOperation::And: 177 case Macro::ALUOperation::And:
248 return src_a & src_b; 178 return src_a & src_b;
249 case ALUOperation::AndNot: 179 case Macro::ALUOperation::AndNot:
250 return src_a & ~src_b; 180 return src_a & ~src_b;
251 case ALUOperation::Nand: 181 case Macro::ALUOperation::Nand:
252 return ~(src_a & src_b); 182 return ~(src_a & src_b);
253 183
254 default: 184 default:
@@ -257,43 +187,43 @@ u32 MacroInterpreter::GetALUResult(ALUOperation operation, u32 src_a, u32 src_b)
257 } 187 }
258} 188}
259 189
260void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 result) { 190void MacroInterpreterImpl::ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result) {
261 switch (operation) { 191 switch (operation) {
262 case ResultOperation::IgnoreAndFetch: 192 case Macro::ResultOperation::IgnoreAndFetch:
263 // Fetch parameter and ignore result. 193 // Fetch parameter and ignore result.
264 SetRegister(reg, FetchParameter()); 194 SetRegister(reg, FetchParameter());
265 break; 195 break;
266 case ResultOperation::Move: 196 case Macro::ResultOperation::Move:
267 // Move result. 197 // Move result.
268 SetRegister(reg, result); 198 SetRegister(reg, result);
269 break; 199 break;
270 case ResultOperation::MoveAndSetMethod: 200 case Macro::ResultOperation::MoveAndSetMethod:
271 // Move result and use as Method Address. 201 // Move result and use as Method Address.
272 SetRegister(reg, result); 202 SetRegister(reg, result);
273 SetMethodAddress(result); 203 SetMethodAddress(result);
274 break; 204 break;
275 case ResultOperation::FetchAndSend: 205 case Macro::ResultOperation::FetchAndSend:
276 // Fetch parameter and send result. 206 // Fetch parameter and send result.
277 SetRegister(reg, FetchParameter()); 207 SetRegister(reg, FetchParameter());
278 Send(result); 208 Send(result);
279 break; 209 break;
280 case ResultOperation::MoveAndSend: 210 case Macro::ResultOperation::MoveAndSend:
281 // Move and send result. 211 // Move and send result.
282 SetRegister(reg, result); 212 SetRegister(reg, result);
283 Send(result); 213 Send(result);
284 break; 214 break;
285 case ResultOperation::FetchAndSetMethod: 215 case Macro::ResultOperation::FetchAndSetMethod:
286 // Fetch parameter and use result as Method Address. 216 // Fetch parameter and use result as Method Address.
287 SetRegister(reg, FetchParameter()); 217 SetRegister(reg, FetchParameter());
288 SetMethodAddress(result); 218 SetMethodAddress(result);
289 break; 219 break;
290 case ResultOperation::MoveAndSetMethodFetchAndSend: 220 case Macro::ResultOperation::MoveAndSetMethodFetchAndSend:
291 // Move result and use as Method Address, then fetch and send parameter. 221 // Move result and use as Method Address, then fetch and send parameter.
292 SetRegister(reg, result); 222 SetRegister(reg, result);
293 SetMethodAddress(result); 223 SetMethodAddress(result);
294 Send(FetchParameter()); 224 Send(FetchParameter());
295 break; 225 break;
296 case ResultOperation::MoveAndSetMethodSend: 226 case Macro::ResultOperation::MoveAndSetMethodSend:
297 // Move result and use as Method Address, then send bits 12:17 of result. 227 // Move result and use as Method Address, then send bits 12:17 of result.
298 SetRegister(reg, result); 228 SetRegister(reg, result);
299 SetMethodAddress(result); 229 SetMethodAddress(result);
@@ -304,16 +234,28 @@ void MacroInterpreter::ProcessResult(ResultOperation operation, u32 reg, u32 res
304 } 234 }
305} 235}
306 236
307u32 MacroInterpreter::FetchParameter() { 237bool MacroInterpreterImpl::EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const {
308 ASSERT(next_parameter_index < num_parameters); 238 switch (cond) {
309 return parameters[next_parameter_index++]; 239 case Macro::BranchCondition::Zero:
240 return value == 0;
241 case Macro::BranchCondition::NotZero:
242 return value != 0;
243 }
244 UNREACHABLE();
245 return true;
310} 246}
311 247
312u32 MacroInterpreter::GetRegister(u32 register_id) const { 248Macro::Opcode MacroInterpreterImpl::GetOpcode() const {
249 ASSERT((pc % sizeof(u32)) == 0);
250 ASSERT(pc < code.size() * sizeof(u32));
251 return {code[pc / sizeof(u32)]};
252}
253
254u32 MacroInterpreterImpl::GetRegister(u32 register_id) const {
313 return registers.at(register_id); 255 return registers.at(register_id);
314} 256}
315 257
316void MacroInterpreter::SetRegister(u32 register_id, u32 value) { 258void MacroInterpreterImpl::SetRegister(u32 register_id, u32 value) {
317 // Register 0 is hardwired as the zero register. 259 // Register 0 is hardwired as the zero register.
318 // Ensure no writes to it actually occur. 260 // Ensure no writes to it actually occur.
319 if (register_id == 0) { 261 if (register_id == 0) {
@@ -323,30 +265,24 @@ void MacroInterpreter::SetRegister(u32 register_id, u32 value) {
323 registers.at(register_id) = value; 265 registers.at(register_id) = value;
324} 266}
325 267
326void MacroInterpreter::SetMethodAddress(u32 address) { 268void MacroInterpreterImpl::SetMethodAddress(u32 address) {
327 method_address.raw = address; 269 method_address.raw = address;
328} 270}
329 271
330void MacroInterpreter::Send(u32 value) { 272void MacroInterpreterImpl::Send(u32 value) {
331 maxwell3d.CallMethodFromMME(method_address.address, value); 273 maxwell3d.CallMethodFromMME(method_address.address, value);
332 // Increment the method address by the method increment. 274 // Increment the method address by the method increment.
333 method_address.address.Assign(method_address.address.Value() + 275 method_address.address.Assign(method_address.address.Value() +
334 method_address.increment.Value()); 276 method_address.increment.Value());
335} 277}
336 278
337u32 MacroInterpreter::Read(u32 method) const { 279u32 MacroInterpreterImpl::Read(u32 method) const {
338 return maxwell3d.GetRegisterValue(method); 280 return maxwell3d.GetRegisterValue(method);
339} 281}
340 282
341bool MacroInterpreter::EvaluateBranchCondition(BranchCondition cond, u32 value) const { 283u32 MacroInterpreterImpl::FetchParameter() {
342 switch (cond) { 284 ASSERT(next_parameter_index < num_parameters);
343 case BranchCondition::Zero: 285 return parameters[next_parameter_index++];
344 return value == 0;
345 case BranchCondition::NotZero:
346 return value != 0;
347 }
348 UNREACHABLE();
349 return true;
350} 286}
351 287
352} // namespace Tegra 288} // namespace Tegra
diff --git a/src/video_core/macro_interpreter.h b/src/video_core/macro/macro_interpreter.h
index 631146d89..fb923f7b9 100644
--- a/src/video_core/macro_interpreter.h
+++ b/src/video_core/macro/macro_interpreter.h
@@ -1,44 +1,37 @@
1// Copyright 2018 yuzu Emulator Project 1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#pragma once 5#pragma once
6
7#include <array> 6#include <array>
8#include <optional> 7#include <optional>
9 8#include <vector>
10#include "common/bit_field.h" 9#include "common/bit_field.h"
11#include "common/common_types.h" 10#include "common/common_types.h"
11#include "video_core/macro/macro.h"
12 12
13namespace Tegra { 13namespace Tegra {
14namespace Engines { 14namespace Engines {
15class Maxwell3D; 15class Maxwell3D;
16} 16}
17 17
18class MacroInterpreter final { 18class MacroInterpreter final : public MacroEngine {
19public: 19public:
20 explicit MacroInterpreter(Engines::Maxwell3D& maxwell3d); 20 explicit MacroInterpreter(Engines::Maxwell3D& maxwell3d);
21 21
22 /** 22protected:
23 * Executes the macro code with the specified input parameters. 23 std::unique_ptr<CachedMacro> Compile(const std::vector<u32>& code) override;
24 * @param offset Offset to start execution at.
25 * @param parameters The parameters of the macro.
26 */
27 void Execute(u32 offset, std::size_t num_parameters, const u32* parameters);
28 24
29private: 25private:
30 enum class ALUOperation : u32; 26 Engines::Maxwell3D& maxwell3d;
31 enum class BranchCondition : u32; 27};
32 enum class ResultOperation : u32;
33
34 union Opcode;
35 28
36 union MethodAddress { 29class MacroInterpreterImpl : public CachedMacro {
37 u32 raw; 30public:
38 BitField<0, 12, u32> address; 31 MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& code);
39 BitField<12, 6, u32> increment; 32 void Execute(std::vector<u32>& parameters, u32 method) override;
40 };
41 33
34private:
42 /// Resets the execution engine state, zeroing registers, etc. 35 /// Resets the execution engine state, zeroing registers, etc.
43 void Reset(); 36 void Reset();
44 37
@@ -49,20 +42,20 @@ private:
49 * @param is_delay_slot Whether the current step is being executed due to a delay slot in a 42 * @param is_delay_slot Whether the current step is being executed due to a delay slot in a
50 * previous instruction. 43 * previous instruction.
51 */ 44 */
52 bool Step(u32 offset, bool is_delay_slot); 45 bool Step(bool is_delay_slot);
53 46
54 /// Calculates the result of an ALU operation. src_a OP src_b; 47 /// Calculates the result of an ALU operation. src_a OP src_b;
55 u32 GetALUResult(ALUOperation operation, u32 src_a, u32 src_b); 48 u32 GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b);
56 49
57 /// Performs the result operation on the input result and stores it in the specified register 50 /// Performs the result operation on the input result and stores it in the specified register
58 /// (if necessary). 51 /// (if necessary).
59 void ProcessResult(ResultOperation operation, u32 reg, u32 result); 52 void ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result);
60 53
61 /// Evaluates the branch condition and returns whether the branch should be taken or not. 54 /// Evaluates the branch condition and returns whether the branch should be taken or not.
62 bool EvaluateBranchCondition(BranchCondition cond, u32 value) const; 55 bool EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const;
63 56
64 /// Reads an opcode at the current program counter location. 57 /// Reads an opcode at the current program counter location.
65 Opcode GetOpcode(u32 offset) const; 58 Macro::Opcode GetOpcode() const;
66 59
67 /// Returns the specified register's value. Register 0 is hardcoded to always return 0. 60 /// Returns the specified register's value. Register 0 is hardcoded to always return 0.
68 u32 GetRegister(u32 register_id) const; 61 u32 GetRegister(u32 register_id) const;
@@ -89,13 +82,11 @@ private:
89 /// Program counter to execute at after the delay slot is executed. 82 /// Program counter to execute at after the delay slot is executed.
90 std::optional<u32> delayed_pc; 83 std::optional<u32> delayed_pc;
91 84
92 static constexpr std::size_t NumMacroRegisters = 8;
93
94 /// General purpose macro registers. 85 /// General purpose macro registers.
95 std::array<u32, NumMacroRegisters> registers = {}; 86 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers = {};
96 87
97 /// Method address to use for the next Send instruction. 88 /// Method address to use for the next Send instruction.
98 MethodAddress method_address = {}; 89 Macro::MethodAddress method_address = {};
99 90
100 /// Input parameters of the current macro. 91 /// Input parameters of the current macro.
101 std::unique_ptr<u32[]> parameters; 92 std::unique_ptr<u32[]> parameters;
@@ -105,5 +96,7 @@ private:
105 u32 next_parameter_index = 0; 96 u32 next_parameter_index = 0;
106 97
107 bool carry_flag = false; 98 bool carry_flag = false;
99 const std::vector<u32>& code;
108}; 100};
101
109} // namespace Tegra 102} // namespace Tegra
diff --git a/src/video_core/macro/macro_jit_x64.cpp b/src/video_core/macro/macro_jit_x64.cpp
new file mode 100644
index 000000000..1b657236a
--- /dev/null
+++ b/src/video_core/macro/macro_jit_x64.cpp
@@ -0,0 +1,633 @@
1// Copyright 2020 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 "common/x64/xbyak_util.h"
9#include "video_core/engines/maxwell_3d.h"
10#include "video_core/macro/macro_interpreter.h"
11#include "video_core/macro/macro_jit_x64.h"
12
13MICROPROFILE_DEFINE(MacroJitCompile, "GPU", "Compile macro JIT", MP_RGB(173, 255, 47));
14MICROPROFILE_DEFINE(MacroJitExecute, "GPU", "Execute macro JIT", MP_RGB(255, 255, 0));
15
16namespace Tegra {
17using JitFunction = void (MacroJITx64Impl::*)(Macro::Opcode opcode);
18const std::array<JitFunction, 8> InstructionTable{
19 &MacroJITx64Impl::Compile_ALU,
20 &MacroJITx64Impl::Compile_AddImmediate,
21 &MacroJITx64Impl::Compile_ExtractInsert,
22 &MacroJITx64Impl::Compile_ExtractShiftLeftImmediate,
23 &MacroJITx64Impl::Compile_ExtractShiftLeftRegister,
24 &MacroJITx64Impl::Compile_Read,
25 nullptr,
26 &MacroJITx64Impl::Compile_Branch,
27};
28
29static const Xbyak::Reg64 PARAMETERS = Xbyak::util::r9;
30static const Xbyak::Reg64 REGISTERS = Xbyak::util::r10;
31static const Xbyak::Reg64 STATE = Xbyak::util::r11;
32static const Xbyak::Reg64 NEXT_PARAMETER = Xbyak::util::r12;
33static const Xbyak::Reg32 RESULT = Xbyak::util::r13d;
34static const Xbyak::Reg64 RESULT_64 = Xbyak::util::r13;
35static const Xbyak::Reg32 METHOD_ADDRESS = Xbyak::util::r14d;
36static const Xbyak::Reg64 METHOD_ADDRESS_64 = Xbyak::util::r14;
37static const Xbyak::Reg64 BRANCH_HOLDER = Xbyak::util::r15;
38
39static const std::bitset<32> PERSISTENT_REGISTERS = Common::X64::BuildRegSet({
40 PARAMETERS,
41 REGISTERS,
42 STATE,
43 NEXT_PARAMETER,
44 RESULT,
45 METHOD_ADDRESS,
46 BRANCH_HOLDER,
47});
48
49MacroJITx64::MacroJITx64(Engines::Maxwell3D& maxwell3d) : maxwell3d(maxwell3d) {}
50
51std::unique_ptr<CachedMacro> MacroJITx64::Compile(const std::vector<u32>& code) {
52 return std::make_unique<MacroJITx64Impl>(maxwell3d, code);
53}
54
55MacroJITx64Impl::MacroJITx64Impl(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& code)
56 : Xbyak::CodeGenerator(MAX_CODE_SIZE), code(code), maxwell3d(maxwell3d) {
57 Compile();
58}
59
60MacroJITx64Impl::~MacroJITx64Impl() = default;
61
62void MacroJITx64Impl::Execute(std::vector<u32>& parameters, u32 method) {
63 MICROPROFILE_SCOPE(MacroJitExecute);
64 ASSERT_OR_EXECUTE(program != nullptr, { return; });
65 JITState state{};
66 state.maxwell3d = &maxwell3d;
67 state.registers = {};
68 state.parameters = parameters.data();
69 program(&state);
70}
71
72void MacroJITx64Impl::Compile_ALU(Macro::Opcode opcode) {
73 const bool is_a_zero = opcode.src_a == 0;
74 const bool is_b_zero = opcode.src_b == 0;
75 const bool valid_operation = !is_a_zero && !is_b_zero;
76 const bool is_move_operation = !is_a_zero && is_b_zero;
77 const bool has_zero_register = is_a_zero || is_b_zero;
78
79 Xbyak::Reg64 src_a;
80 Xbyak::Reg32 src_b;
81
82 if (!optimizer.zero_reg_skip) {
83 src_a = Compile_GetRegister(opcode.src_a, RESULT_64);
84 src_b = Compile_GetRegister(opcode.src_b, ebx);
85 } else {
86 if (!is_a_zero) {
87 src_a = Compile_GetRegister(opcode.src_a, RESULT_64);
88 }
89 if (!is_b_zero) {
90 src_b = Compile_GetRegister(opcode.src_b, ebx);
91 }
92 }
93 Xbyak::Label skip_carry{};
94
95 bool has_emitted = false;
96
97 switch (opcode.alu_operation) {
98 case Macro::ALUOperation::Add:
99 if (optimizer.zero_reg_skip) {
100 if (valid_operation) {
101 add(src_a, src_b);
102 }
103 } else {
104 add(src_a, src_b);
105 }
106
107 if (!optimizer.can_skip_carry) {
108 setc(byte[STATE + offsetof(JITState, carry_flag)]);
109 }
110 break;
111 case Macro::ALUOperation::AddWithCarry:
112 bt(dword[STATE + offsetof(JITState, carry_flag)], 0);
113 adc(src_a, src_b);
114 setc(byte[STATE + offsetof(JITState, carry_flag)]);
115 break;
116 case Macro::ALUOperation::Subtract:
117 if (optimizer.zero_reg_skip) {
118 if (valid_operation) {
119 sub(src_a, src_b);
120 has_emitted = true;
121 }
122 } else {
123 sub(src_a, src_b);
124 has_emitted = true;
125 }
126 if (!optimizer.can_skip_carry && has_emitted) {
127 setc(byte[STATE + offsetof(JITState, carry_flag)]);
128 }
129 break;
130 case Macro::ALUOperation::SubtractWithBorrow:
131 bt(dword[STATE + offsetof(JITState, carry_flag)], 0);
132 sbb(src_a, src_b);
133 setc(byte[STATE + offsetof(JITState, carry_flag)]);
134 break;
135 case Macro::ALUOperation::Xor:
136 if (optimizer.zero_reg_skip) {
137 if (valid_operation) {
138 xor_(src_a, src_b);
139 }
140 } else {
141 xor_(src_a, src_b);
142 }
143 break;
144 case Macro::ALUOperation::Or:
145 if (optimizer.zero_reg_skip) {
146 if (valid_operation) {
147 or_(src_a, src_b);
148 }
149 } else {
150 or_(src_a, src_b);
151 }
152 break;
153 case Macro::ALUOperation::And:
154 if (optimizer.zero_reg_skip) {
155 if (!has_zero_register) {
156 and_(src_a, src_b);
157 }
158 } else {
159 and_(src_a, src_b);
160 }
161 break;
162 case Macro::ALUOperation::AndNot:
163 if (optimizer.zero_reg_skip) {
164 if (!is_a_zero) {
165 not_(src_b);
166 and_(src_a, src_b);
167 }
168 } else {
169 not_(src_b);
170 and_(src_a, src_b);
171 }
172 break;
173 case Macro::ALUOperation::Nand:
174 if (optimizer.zero_reg_skip) {
175 if (!is_a_zero) {
176 and_(src_a, src_b);
177 not_(src_a);
178 }
179 } else {
180 and_(src_a, src_b);
181 not_(src_a);
182 }
183 break;
184 default:
185 UNIMPLEMENTED_MSG("Unimplemented ALU operation {}",
186 static_cast<std::size_t>(opcode.alu_operation.Value()));
187 break;
188 }
189 Compile_ProcessResult(opcode.result_operation, opcode.dst);
190}
191
192void MacroJITx64Impl::Compile_AddImmediate(Macro::Opcode opcode) {
193 if (optimizer.skip_dummy_addimmediate) {
194 // Games tend to use this as an exit instruction placeholder. It's to encode an instruction
195 // without doing anything. In our case we can just not emit anything.
196 if (opcode.result_operation == Macro::ResultOperation::Move && opcode.dst == 0) {
197 return;
198 }
199 }
200 // Check for redundant moves
201 if (optimizer.optimize_for_method_move &&
202 opcode.result_operation == Macro::ResultOperation::MoveAndSetMethod) {
203 if (next_opcode.has_value()) {
204 const auto next = *next_opcode;
205 if (next.result_operation == Macro::ResultOperation::MoveAndSetMethod) {
206 return;
207 }
208 }
209 }
210 if (optimizer.zero_reg_skip && opcode.src_a == 0) {
211 if (opcode.immediate == 0) {
212 xor_(RESULT, RESULT);
213 } else {
214 mov(RESULT, opcode.immediate);
215 }
216 } else {
217 auto result = Compile_GetRegister(opcode.src_a, RESULT);
218 if (opcode.immediate > 2) {
219 add(result, opcode.immediate);
220 } else if (opcode.immediate == 1) {
221 inc(result);
222 } else if (opcode.immediate < 0) {
223 sub(result, opcode.immediate * -1);
224 }
225 }
226 Compile_ProcessResult(opcode.result_operation, opcode.dst);
227}
228
229void MacroJITx64Impl::Compile_ExtractInsert(Macro::Opcode opcode) {
230 auto dst = Compile_GetRegister(opcode.src_a, RESULT);
231 auto src = Compile_GetRegister(opcode.src_b, eax);
232
233 if (opcode.bf_src_bit != 0 && opcode.bf_src_bit != 31) {
234 shr(src, opcode.bf_src_bit);
235 } else if (opcode.bf_src_bit == 31) {
236 xor_(src, src);
237 }
238 // Don't bother masking the whole register since we're using a 32 bit register
239 if (opcode.bf_size != 31 && opcode.bf_size != 0) {
240 and_(src, opcode.GetBitfieldMask());
241 } else if (opcode.bf_size == 0) {
242 xor_(src, src);
243 }
244 if (opcode.bf_dst_bit != 31 && opcode.bf_dst_bit != 0) {
245 shl(src, opcode.bf_dst_bit);
246 } else if (opcode.bf_dst_bit == 31) {
247 xor_(src, src);
248 }
249
250 const u32 mask = ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit);
251 if (mask != 0xffffffff) {
252 and_(dst, mask);
253 }
254 or_(dst, src);
255 Compile_ProcessResult(opcode.result_operation, opcode.dst);
256}
257
258void MacroJITx64Impl::Compile_ExtractShiftLeftImmediate(Macro::Opcode opcode) {
259 auto dst = Compile_GetRegister(opcode.src_a, eax);
260 auto src = Compile_GetRegister(opcode.src_b, RESULT);
261
262 shr(src, al);
263 if (opcode.bf_size != 0 && opcode.bf_size != 31) {
264 and_(src, opcode.GetBitfieldMask());
265 } else if (opcode.bf_size == 0) {
266 xor_(src, src);
267 }
268
269 if (opcode.bf_dst_bit != 0 && opcode.bf_dst_bit != 31) {
270 shl(src, opcode.bf_dst_bit);
271 } else if (opcode.bf_dst_bit == 31) {
272 xor_(src, src);
273 }
274 Compile_ProcessResult(opcode.result_operation, opcode.dst);
275}
276
277void MacroJITx64Impl::Compile_ExtractShiftLeftRegister(Macro::Opcode opcode) {
278 auto dst = Compile_GetRegister(opcode.src_a, eax);
279 auto src = Compile_GetRegister(opcode.src_b, RESULT);
280
281 if (opcode.bf_src_bit != 0) {
282 shr(src, opcode.bf_src_bit);
283 }
284
285 if (opcode.bf_size != 31) {
286 and_(src, opcode.GetBitfieldMask());
287 }
288 shl(src, al);
289 Compile_ProcessResult(opcode.result_operation, opcode.dst);
290}
291
292static u32 Read(Engines::Maxwell3D* maxwell3d, u32 method) {
293 return maxwell3d->GetRegisterValue(method);
294}
295
296static void Send(Engines::Maxwell3D* maxwell3d, Macro::MethodAddress method_address, u32 value) {
297 maxwell3d->CallMethodFromMME(method_address.address, value);
298}
299
300void MacroJITx64Impl::Compile_Read(Macro::Opcode opcode) {
301 if (optimizer.zero_reg_skip && opcode.src_a == 0) {
302 if (opcode.immediate == 0) {
303 xor_(RESULT, RESULT);
304 } else {
305 mov(RESULT, opcode.immediate);
306 }
307 } else {
308 auto result = Compile_GetRegister(opcode.src_a, RESULT);
309 if (opcode.immediate > 2) {
310 add(result, opcode.immediate);
311 } else if (opcode.immediate == 1) {
312 inc(result);
313 } else if (opcode.immediate < 0) {
314 sub(result, opcode.immediate * -1);
315 }
316 }
317 Common::X64::ABI_PushRegistersAndAdjustStackGPS(*this, PersistentCallerSavedRegs(), 0);
318 mov(Common::X64::ABI_PARAM1, qword[STATE]);
319 mov(Common::X64::ABI_PARAM2, RESULT);
320 Common::X64::CallFarFunction(*this, &Read);
321 Common::X64::ABI_PopRegistersAndAdjustStackGPS(*this, PersistentCallerSavedRegs(), 0);
322 mov(RESULT, Common::X64::ABI_RETURN.cvt32());
323 Compile_ProcessResult(opcode.result_operation, opcode.dst);
324}
325
326void Tegra::MacroJITx64Impl::Compile_Send(Xbyak::Reg32 value) {
327 Common::X64::ABI_PushRegistersAndAdjustStackGPS(*this, PersistentCallerSavedRegs(), 0);
328 mov(Common::X64::ABI_PARAM1, qword[STATE]);
329 mov(Common::X64::ABI_PARAM2, METHOD_ADDRESS);
330 mov(Common::X64::ABI_PARAM3, value);
331 Common::X64::CallFarFunction(*this, &Send);
332 Common::X64::ABI_PopRegistersAndAdjustStackGPS(*this, PersistentCallerSavedRegs(), 0);
333
334 Xbyak::Label dont_process{};
335 // Get increment
336 test(METHOD_ADDRESS, 0x3f000);
337 // If zero, method address doesn't update
338 je(dont_process);
339
340 mov(ecx, METHOD_ADDRESS);
341 and_(METHOD_ADDRESS, 0xfff);
342 shr(ecx, 12);
343 and_(ecx, 0x3f);
344 lea(eax, ptr[rcx + METHOD_ADDRESS_64]);
345 sal(ecx, 12);
346 or_(eax, ecx);
347
348 mov(METHOD_ADDRESS, eax);
349
350 L(dont_process);
351}
352
353void Tegra::MacroJITx64Impl::Compile_Branch(Macro::Opcode opcode) {
354 ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
355 const s32 jump_address =
356 static_cast<s32>(pc) + static_cast<s32>(opcode.GetBranchTarget() / sizeof(s32));
357
358 Xbyak::Label end;
359 auto value = Compile_GetRegister(opcode.src_a, eax);
360 test(value, value);
361 if (optimizer.has_delayed_pc) {
362 switch (opcode.branch_condition) {
363 case Macro::BranchCondition::Zero:
364 jne(end, T_NEAR);
365 break;
366 case Macro::BranchCondition::NotZero:
367 je(end, T_NEAR);
368 break;
369 }
370
371 if (opcode.branch_annul) {
372 xor_(BRANCH_HOLDER, BRANCH_HOLDER);
373 jmp(labels[jump_address], T_NEAR);
374 } else {
375 Xbyak::Label handle_post_exit{};
376 Xbyak::Label skip{};
377 jmp(skip, T_NEAR);
378 if (opcode.is_exit) {
379 L(handle_post_exit);
380 // Execute 1 instruction
381 mov(BRANCH_HOLDER, end_of_code);
382 // Jump to next instruction to skip delay slot check
383 jmp(labels[jump_address], T_NEAR);
384 } else {
385 L(handle_post_exit);
386 xor_(BRANCH_HOLDER, BRANCH_HOLDER);
387 jmp(labels[jump_address], T_NEAR);
388 }
389 L(skip);
390 mov(BRANCH_HOLDER, handle_post_exit);
391 jmp(delay_skip[pc], T_NEAR);
392 }
393 } else {
394 switch (opcode.branch_condition) {
395 case Macro::BranchCondition::Zero:
396 je(labels[jump_address], T_NEAR);
397 break;
398 case Macro::BranchCondition::NotZero:
399 jne(labels[jump_address], T_NEAR);
400 break;
401 }
402 }
403
404 L(end);
405}
406
407void Tegra::MacroJITx64Impl::Optimizer_ScanFlags() {
408 optimizer.can_skip_carry = true;
409 optimizer.has_delayed_pc = false;
410 for (auto raw_op : code) {
411 Macro::Opcode op{};
412 op.raw = raw_op;
413
414 if (op.operation == Macro::Operation::ALU) {
415 // Scan for any ALU operations which actually use the carry flag, if they don't exist in
416 // our current code we can skip emitting the carry flag handling operations
417 if (op.alu_operation == Macro::ALUOperation::AddWithCarry ||
418 op.alu_operation == Macro::ALUOperation::SubtractWithBorrow) {
419 optimizer.can_skip_carry = false;
420 }
421 }
422
423 if (op.operation == Macro::Operation::Branch) {
424 if (!op.branch_annul) {
425 optimizer.has_delayed_pc = true;
426 }
427 }
428 }
429}
430
431void MacroJITx64Impl::Compile() {
432 MICROPROFILE_SCOPE(MacroJitCompile);
433 bool keep_executing = true;
434 labels.fill(Xbyak::Label());
435
436 Common::X64::ABI_PushRegistersAndAdjustStackGPS(*this, Common::X64::ABI_ALL_CALLEE_SAVED, 8);
437 // JIT state
438 mov(STATE, Common::X64::ABI_PARAM1);
439 mov(PARAMETERS, qword[Common::X64::ABI_PARAM1 +
440 static_cast<Xbyak::uint32>(offsetof(JITState, parameters))]);
441 mov(REGISTERS, Common::X64::ABI_PARAM1);
442 add(REGISTERS, static_cast<Xbyak::uint32>(offsetof(JITState, registers)));
443 xor_(RESULT, RESULT);
444 xor_(METHOD_ADDRESS, METHOD_ADDRESS);
445 xor_(NEXT_PARAMETER, NEXT_PARAMETER);
446 xor_(BRANCH_HOLDER, BRANCH_HOLDER);
447
448 mov(dword[REGISTERS + 4], Compile_FetchParameter());
449
450 // Track get register for zero registers and mark it as no-op
451 optimizer.zero_reg_skip = true;
452
453 // AddImmediate tends to be used as a NOP instruction, if we detect this we can
454 // completely skip the entire code path and no emit anything
455 optimizer.skip_dummy_addimmediate = true;
456
457 // SMO tends to emit a lot of unnecessary method moves, we can mitigate this by only emitting
458 // one if our register isn't "dirty"
459 optimizer.optimize_for_method_move = true;
460
461 // Check to see if we can skip emitting certain instructions
462 Optimizer_ScanFlags();
463
464 const u32 op_count = static_cast<u32>(code.size());
465 for (u32 i = 0; i < op_count; i++) {
466 if (i < op_count - 1) {
467 pc = i + 1;
468 next_opcode = GetOpCode();
469 } else {
470 next_opcode = {};
471 }
472 pc = i;
473 Compile_NextInstruction();
474 }
475
476 L(end_of_code);
477
478 Common::X64::ABI_PopRegistersAndAdjustStackGPS(*this, Common::X64::ABI_ALL_CALLEE_SAVED, 8);
479 ret();
480 ready();
481 program = getCode<ProgramType>();
482}
483
484bool MacroJITx64Impl::Compile_NextInstruction() {
485 const auto opcode = GetOpCode();
486 if (labels[pc].getAddress()) {
487 return false;
488 }
489
490 L(labels[pc]);
491
492 const std::size_t op = static_cast<std::size_t>(opcode.operation.Value());
493
494 if (InstructionTable[op] == nullptr) {
495 UNIMPLEMENTED_MSG("Unimplemented opcode {}", op);
496 } else {
497 ((*this).*InstructionTable[op])(opcode);
498 }
499
500 if (optimizer.has_delayed_pc) {
501 if (opcode.is_exit) {
502 mov(rax, end_of_code);
503 test(BRANCH_HOLDER, BRANCH_HOLDER);
504 cmove(BRANCH_HOLDER, rax);
505 // Jump to next instruction to skip delay slot check
506 je(labels[pc + 1], T_NEAR);
507 } else {
508 // TODO(ogniK): Optimize delay slot branching
509 Xbyak::Label no_delay_slot{};
510 test(BRANCH_HOLDER, BRANCH_HOLDER);
511 je(no_delay_slot, T_NEAR);
512 mov(rax, BRANCH_HOLDER);
513 xor_(BRANCH_HOLDER, BRANCH_HOLDER);
514 jmp(rax);
515 L(no_delay_slot);
516 }
517 L(delay_skip[pc]);
518 if (opcode.is_exit) {
519 return false;
520 }
521 } else {
522 test(BRANCH_HOLDER, BRANCH_HOLDER);
523 jne(end_of_code, T_NEAR);
524 if (opcode.is_exit) {
525 inc(BRANCH_HOLDER);
526 return false;
527 }
528 }
529 return true;
530}
531
532Xbyak::Reg32 Tegra::MacroJITx64Impl::Compile_FetchParameter() {
533 mov(eax, dword[PARAMETERS + NEXT_PARAMETER * sizeof(u32)]);
534 inc(NEXT_PARAMETER);
535 return eax;
536}
537
538Xbyak::Reg32 MacroJITx64Impl::Compile_GetRegister(u32 index, Xbyak::Reg32 dst) {
539 if (index == 0) {
540 // Register 0 is always zero
541 xor_(dst, dst);
542 } else {
543 mov(dst, dword[REGISTERS + index * sizeof(u32)]);
544 }
545
546 return dst;
547}
548
549Xbyak::Reg64 Tegra::MacroJITx64Impl::Compile_GetRegister(u32 index, Xbyak::Reg64 dst) {
550 if (index == 0) {
551 // Register 0 is always zero
552 xor_(dst, dst);
553 } else {
554 mov(dst, dword[REGISTERS + index * sizeof(u32)]);
555 }
556
557 return dst;
558}
559
560void Tegra::MacroJITx64Impl::Compile_WriteCarry(Xbyak::Reg64 dst) {
561 Xbyak::Label zero{}, end{};
562 xor_(ecx, ecx);
563 shr(dst, 32);
564 setne(cl);
565 mov(dword[STATE + offsetof(JITState, carry_flag)], ecx);
566}
567
568void MacroJITx64Impl::Compile_ProcessResult(Macro::ResultOperation operation, u32 reg) {
569 auto SetRegister = [=](u32 reg, Xbyak::Reg32 result) {
570 // Register 0 is supposed to always return 0. NOP is implemented as a store to the zero
571 // register.
572 if (reg == 0) {
573 return;
574 }
575 mov(dword[REGISTERS + reg * sizeof(u32)], result);
576 };
577 auto SetMethodAddress = [=](Xbyak::Reg32 reg) { mov(METHOD_ADDRESS, reg); };
578
579 switch (operation) {
580 case Macro::ResultOperation::IgnoreAndFetch:
581 SetRegister(reg, Compile_FetchParameter());
582 break;
583 case Macro::ResultOperation::Move:
584 SetRegister(reg, RESULT);
585 break;
586 case Macro::ResultOperation::MoveAndSetMethod:
587 SetRegister(reg, RESULT);
588 SetMethodAddress(RESULT);
589 break;
590 case Macro::ResultOperation::FetchAndSend:
591 // Fetch parameter and send result.
592 SetRegister(reg, Compile_FetchParameter());
593 Compile_Send(RESULT);
594 break;
595 case Macro::ResultOperation::MoveAndSend:
596 // Move and send result.
597 SetRegister(reg, RESULT);
598 Compile_Send(RESULT);
599 break;
600 case Macro::ResultOperation::FetchAndSetMethod:
601 // Fetch parameter and use result as Method Address.
602 SetRegister(reg, Compile_FetchParameter());
603 SetMethodAddress(RESULT);
604 break;
605 case Macro::ResultOperation::MoveAndSetMethodFetchAndSend:
606 // Move result and use as Method Address, then fetch and send parameter.
607 SetRegister(reg, RESULT);
608 SetMethodAddress(RESULT);
609 Compile_Send(Compile_FetchParameter());
610 break;
611 case Macro::ResultOperation::MoveAndSetMethodSend:
612 // Move result and use as Method Address, then send bits 12:17 of result.
613 SetRegister(reg, RESULT);
614 SetMethodAddress(RESULT);
615 shr(RESULT, 12);
616 and_(RESULT, 0b111111);
617 Compile_Send(RESULT);
618 break;
619 default:
620 UNIMPLEMENTED_MSG("Unimplemented macro operation {}", static_cast<std::size_t>(operation));
621 }
622}
623
624Macro::Opcode MacroJITx64Impl::GetOpCode() const {
625 ASSERT(pc < code.size());
626 return {code[pc]};
627}
628
629std::bitset<32> MacroJITx64Impl::PersistentCallerSavedRegs() const {
630 return PERSISTENT_REGISTERS & Common::X64::ABI_ALL_CALLER_SAVED;
631}
632
633} // namespace Tegra
diff --git a/src/video_core/macro/macro_jit_x64.h b/src/video_core/macro/macro_jit_x64.h
new file mode 100644
index 000000000..71cd6a3b0
--- /dev/null
+++ b/src/video_core/macro/macro_jit_x64.h
@@ -0,0 +1,98 @@
1// Copyright 2020 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 <bitset>
9#include <xbyak.h>
10#include "common/bit_field.h"
11#include "common/common_types.h"
12#include "common/x64/xbyak_abi.h"
13#include "video_core/macro/macro.h"
14
15namespace Tegra {
16namespace Engines {
17class Maxwell3D;
18}
19
20/// MAX_CODE_SIZE is arbitrarily chosen based on current booting games
21constexpr size_t MAX_CODE_SIZE = 0x10000;
22
23class MacroJITx64 final : public MacroEngine {
24public:
25 explicit MacroJITx64(Engines::Maxwell3D& maxwell3d);
26
27protected:
28 std::unique_ptr<CachedMacro> Compile(const std::vector<u32>& code) override;
29
30private:
31 Engines::Maxwell3D& maxwell3d;
32};
33
34class MacroJITx64Impl : public Xbyak::CodeGenerator, public CachedMacro {
35public:
36 MacroJITx64Impl(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& code);
37 ~MacroJITx64Impl();
38 void Execute(std::vector<u32>& parameters, u32 method) override;
39
40 void Compile_ALU(Macro::Opcode opcode);
41 void Compile_AddImmediate(Macro::Opcode opcode);
42 void Compile_ExtractInsert(Macro::Opcode opcode);
43 void Compile_ExtractShiftLeftImmediate(Macro::Opcode opcode);
44 void Compile_ExtractShiftLeftRegister(Macro::Opcode opcode);
45 void Compile_Read(Macro::Opcode opcode);
46 void Compile_Branch(Macro::Opcode opcode);
47
48private:
49 void Optimizer_ScanFlags();
50
51 void Compile();
52 bool Compile_NextInstruction();
53
54 Xbyak::Reg32 Compile_FetchParameter();
55 Xbyak::Reg32 Compile_GetRegister(u32 index, Xbyak::Reg32 dst);
56 Xbyak::Reg64 Compile_GetRegister(u32 index, Xbyak::Reg64 dst);
57 void Compile_WriteCarry(Xbyak::Reg64 dst);
58
59 void Compile_ProcessResult(Macro::ResultOperation operation, u32 reg);
60 void Compile_Send(Xbyak::Reg32 value);
61
62 Macro::Opcode GetOpCode() const;
63 std::bitset<32> PersistentCallerSavedRegs() const;
64
65 struct JITState {
66 Engines::Maxwell3D* maxwell3d{};
67 std::array<u32, Macro::NUM_MACRO_REGISTERS> registers{};
68 u32* parameters{};
69 u32 carry_flag{};
70 };
71 static_assert(offsetof(JITState, maxwell3d) == 0, "Maxwell3D is not at 0x0");
72 using ProgramType = void (*)(JITState*);
73
74 struct OptimizerState {
75 bool can_skip_carry{};
76 bool has_delayed_pc{};
77 bool zero_reg_skip{};
78 bool skip_dummy_addimmediate{};
79 bool optimize_for_method_move{};
80 };
81 OptimizerState optimizer{};
82
83 std::optional<Macro::Opcode> next_opcode{};
84 ProgramType program{nullptr};
85
86 std::array<Xbyak::Label, MAX_CODE_SIZE> labels;
87 std::array<Xbyak::Label, MAX_CODE_SIZE> delay_skip{};
88 Xbyak::Label end_of_code{};
89
90 bool is_delay_slot{};
91 u32 pc{};
92 std::optional<u32> delayed_pc;
93
94 const std::vector<u32>& code;
95 Engines::Maxwell3D& maxwell3d;
96};
97
98} // namespace Tegra
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index b08b87426..7e9073cc3 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -533,6 +533,8 @@ void Config::ReadDebuggingValues() {
533 Settings::values.quest_flag = ReadSetting(QStringLiteral("quest_flag"), false).toBool(); 533 Settings::values.quest_flag = ReadSetting(QStringLiteral("quest_flag"), false).toBool();
534 Settings::values.disable_cpu_opt = 534 Settings::values.disable_cpu_opt =
535 ReadSetting(QStringLiteral("disable_cpu_opt"), false).toBool(); 535 ReadSetting(QStringLiteral("disable_cpu_opt"), false).toBool();
536 Settings::values.disable_macro_jit =
537 ReadSetting(QStringLiteral("disable_macro_jit"), false).toBool();
536 538
537 qt_config->endGroup(); 539 qt_config->endGroup();
538} 540}
@@ -1011,6 +1013,7 @@ void Config::SaveDebuggingValues() {
1011 WriteSetting(QStringLiteral("dump_nso"), Settings::values.dump_nso, false); 1013 WriteSetting(QStringLiteral("dump_nso"), Settings::values.dump_nso, false);
1012 WriteSetting(QStringLiteral("quest_flag"), Settings::values.quest_flag, false); 1014 WriteSetting(QStringLiteral("quest_flag"), Settings::values.quest_flag, false);
1013 WriteSetting(QStringLiteral("disable_cpu_opt"), Settings::values.disable_cpu_opt, false); 1015 WriteSetting(QStringLiteral("disable_cpu_opt"), Settings::values.disable_cpu_opt, false);
1016 WriteSetting(QStringLiteral("disable_macro_jit"), Settings::values.disable_macro_jit, false);
1014 1017
1015 qt_config->endGroup(); 1018 qt_config->endGroup();
1016} 1019}
diff --git a/src/yuzu/configuration/configure_debug.cpp b/src/yuzu/configuration/configure_debug.cpp
index c2026763e..2c77441fd 100644
--- a/src/yuzu/configuration/configure_debug.cpp
+++ b/src/yuzu/configuration/configure_debug.cpp
@@ -39,6 +39,8 @@ void ConfigureDebug::SetConfiguration() {
39 ui->disable_cpu_opt->setChecked(Settings::values.disable_cpu_opt); 39 ui->disable_cpu_opt->setChecked(Settings::values.disable_cpu_opt);
40 ui->enable_graphics_debugging->setEnabled(!Core::System::GetInstance().IsPoweredOn()); 40 ui->enable_graphics_debugging->setEnabled(!Core::System::GetInstance().IsPoweredOn());
41 ui->enable_graphics_debugging->setChecked(Settings::values.renderer_debug); 41 ui->enable_graphics_debugging->setChecked(Settings::values.renderer_debug);
42 ui->disable_macro_jit->setEnabled(!Core::System::GetInstance().IsPoweredOn());
43 ui->disable_macro_jit->setChecked(Settings::values.disable_macro_jit);
42} 44}
43 45
44void ConfigureDebug::ApplyConfiguration() { 46void ConfigureDebug::ApplyConfiguration() {
@@ -51,6 +53,7 @@ void ConfigureDebug::ApplyConfiguration() {
51 Settings::values.quest_flag = ui->quest_flag->isChecked(); 53 Settings::values.quest_flag = ui->quest_flag->isChecked();
52 Settings::values.disable_cpu_opt = ui->disable_cpu_opt->isChecked(); 54 Settings::values.disable_cpu_opt = ui->disable_cpu_opt->isChecked();
53 Settings::values.renderer_debug = ui->enable_graphics_debugging->isChecked(); 55 Settings::values.renderer_debug = ui->enable_graphics_debugging->isChecked();
56 Settings::values.disable_macro_jit = ui->disable_macro_jit->isChecked();
54 Debugger::ToggleConsole(); 57 Debugger::ToggleConsole();
55 Log::Filter filter; 58 Log::Filter filter;
56 filter.ParseFilterString(Settings::values.log_filter); 59 filter.ParseFilterString(Settings::values.log_filter);
diff --git a/src/yuzu/configuration/configure_debug.ui b/src/yuzu/configuration/configure_debug.ui
index e0d4c4a44..46f0208c6 100644
--- a/src/yuzu/configuration/configure_debug.ui
+++ b/src/yuzu/configuration/configure_debug.ui
@@ -148,6 +148,19 @@
148 </property> 148 </property>
149 </widget> 149 </widget>
150 </item> 150 </item>
151 <item>
152 <widget class="QCheckBox" name="disable_macro_jit">
153 <property name="enabled">
154 <bool>true</bool>
155 </property>
156 <property name="whatsThis">
157 <string>When checked, it disables the macro Just In Time compiler. Enabled this makes games run slower</string>
158 </property>
159 <property name="text">
160 <string>Disable Macro JIT</string>
161 </property>
162 </widget>
163 </item>
151 </layout> 164 </layout>
152 </widget> 165 </widget>
153 </item> 166 </item>
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index c20d48c42..7240270f5 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -432,6 +432,8 @@ void Config::ReadValues() {
432 Settings::values.quest_flag = sdl2_config->GetBoolean("Debugging", "quest_flag", false); 432 Settings::values.quest_flag = sdl2_config->GetBoolean("Debugging", "quest_flag", false);
433 Settings::values.disable_cpu_opt = 433 Settings::values.disable_cpu_opt =
434 sdl2_config->GetBoolean("Debugging", "disable_cpu_opt", false); 434 sdl2_config->GetBoolean("Debugging", "disable_cpu_opt", false);
435 Settings::values.disable_macro_jit =
436 sdl2_config->GetBoolean("Debugging", "disable_macro_jit", false);
435 437
436 const auto title_list = sdl2_config->Get("AddOns", "title_ids", ""); 438 const auto title_list = sdl2_config->Get("AddOns", "title_ids", "");
437 std::stringstream ss(title_list); 439 std::stringstream ss(title_list);
diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h
index abc6e6e65..6f53e9659 100644
--- a/src/yuzu_cmd/default_ini.h
+++ b/src/yuzu_cmd/default_ini.h
@@ -291,6 +291,8 @@ quest_flag =
291# Determines whether or not JIT CPU optimizations are enabled 291# Determines whether or not JIT CPU optimizations are enabled
292# false: Optimizations Enabled, true: Optimizations Disabled 292# false: Optimizations Enabled, true: Optimizations Disabled
293disable_cpu_opt = 293disable_cpu_opt =
294# Enables/Disables the macro JIT compiler
295disable_macro_jit=false
294 296
295[WebService] 297[WebService]
296# Whether or not to enable telemetry 298# Whether or not to enable telemetry