2020-05-29 00:53:27 -04:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
2018-03-28 16:14:47 -04:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2019-07-09 21:27:27 -04:00
|
|
|
#include "common/microprofile.h"
|
2018-03-28 16:14:47 -04:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2020-05-29 00:53:27 -04:00
|
|
|
#include "video_core/macro/macro_interpreter.h"
|
2018-03-28 16:14:47 -04:00
|
|
|
|
2019-07-09 21:27:27 -04:00
|
|
|
MICROPROFILE_DEFINE(MacroInterp, "GPU", "Execute macro interpreter", MP_RGB(128, 128, 192));
|
|
|
|
|
2018-03-28 16:14:47 -04:00
|
|
|
namespace Tegra {
|
2020-12-04 14:39:12 -05:00
|
|
|
MacroInterpreter::MacroInterpreter(Engines::Maxwell3D& maxwell3d_)
|
|
|
|
: MacroEngine{maxwell3d_}, maxwell3d{maxwell3d_} {}
|
2019-10-15 17:36:15 -04:00
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
std::unique_ptr<CachedMacro> MacroInterpreter::Compile(const std::vector<u32>& code) {
|
|
|
|
return std::make_unique<MacroInterpreterImpl>(maxwell3d, code);
|
|
|
|
}
|
2018-03-28 16:14:47 -04:00
|
|
|
|
2020-12-04 14:39:12 -05:00
|
|
|
MacroInterpreterImpl::MacroInterpreterImpl(Engines::Maxwell3D& maxwell3d_,
|
|
|
|
const std::vector<u32>& code_)
|
|
|
|
: maxwell3d{maxwell3d_}, code{code_} {}
|
2018-03-28 16:14:47 -04:00
|
|
|
|
2020-12-04 14:39:12 -05:00
|
|
|
void MacroInterpreterImpl::Execute(const std::vector<u32>& params, u32 method) {
|
2019-07-09 21:27:27 -04:00
|
|
|
MICROPROFILE_SCOPE(MacroInterp);
|
2018-03-28 16:14:47 -04:00
|
|
|
Reset();
|
2019-08-25 00:08:35 -04:00
|
|
|
|
2020-12-04 14:39:12 -05:00
|
|
|
registers[1] = params[0];
|
|
|
|
num_parameters = params.size();
|
2019-08-25 00:08:35 -04:00
|
|
|
|
|
|
|
if (num_parameters > parameters_capacity) {
|
|
|
|
parameters_capacity = num_parameters;
|
2020-12-04 14:39:12 -05:00
|
|
|
parameters = std::make_unique<u32[]>(num_parameters);
|
2019-08-25 00:08:35 -04:00
|
|
|
}
|
2020-12-04 14:39:12 -05:00
|
|
|
std::memcpy(parameters.get(), params.data(), num_parameters * sizeof(u32));
|
2018-03-28 16:14:47 -04:00
|
|
|
|
|
|
|
// Execute the code until we hit an exit condition.
|
|
|
|
bool keep_executing = true;
|
|
|
|
while (keep_executing) {
|
2020-05-29 00:53:27 -04:00
|
|
|
keep_executing = Step(false);
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the the macro used all the input parameters
|
2019-08-25 00:08:35 -04:00
|
|
|
ASSERT(next_parameter_index == num_parameters);
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
void MacroInterpreterImpl::Reset() {
|
2018-03-28 16:14:47 -04:00
|
|
|
registers = {};
|
|
|
|
pc = 0;
|
2018-10-30 00:03:25 -04:00
|
|
|
delayed_pc = {};
|
2018-03-28 16:14:47 -04:00
|
|
|
method_address.raw = 0;
|
2019-08-25 00:08:35 -04:00
|
|
|
num_parameters = 0;
|
2018-03-28 16:14:47 -04:00
|
|
|
// The next parameter index starts at 1, because $r1 already has the value of the first
|
|
|
|
// parameter.
|
|
|
|
next_parameter_index = 1;
|
2018-11-21 14:32:21 -05:00
|
|
|
carry_flag = false;
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
bool MacroInterpreterImpl::Step(bool is_delay_slot) {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 base_address = pc;
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
Macro::Opcode opcode = GetOpcode();
|
2018-03-28 16:14:47 -04:00
|
|
|
pc += 4;
|
|
|
|
|
|
|
|
// Update the program counter if we were delayed
|
2018-10-30 00:03:25 -04:00
|
|
|
if (delayed_pc) {
|
2018-03-28 16:14:47 -04:00
|
|
|
ASSERT(is_delay_slot);
|
|
|
|
pc = *delayed_pc;
|
2018-10-30 00:03:25 -04:00
|
|
|
delayed_pc = {};
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (opcode.operation) {
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::ALU: {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 result = GetALUResult(opcode.alu_operation, GetRegister(opcode.src_a),
|
|
|
|
GetRegister(opcode.src_b));
|
|
|
|
ProcessResult(opcode.result_operation, opcode.dst, result);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::AddImmediate: {
|
2018-03-28 16:14:47 -04:00
|
|
|
ProcessResult(opcode.result_operation, opcode.dst,
|
|
|
|
GetRegister(opcode.src_a) + opcode.immediate);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::ExtractInsert: {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 dst = GetRegister(opcode.src_a);
|
|
|
|
u32 src = GetRegister(opcode.src_b);
|
|
|
|
|
|
|
|
src = (src >> opcode.bf_src_bit) & opcode.GetBitfieldMask();
|
|
|
|
dst &= ~(opcode.GetBitfieldMask() << opcode.bf_dst_bit);
|
|
|
|
dst |= src << opcode.bf_dst_bit;
|
|
|
|
ProcessResult(opcode.result_operation, opcode.dst, dst);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::ExtractShiftLeftImmediate: {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 dst = GetRegister(opcode.src_a);
|
|
|
|
u32 src = GetRegister(opcode.src_b);
|
|
|
|
|
|
|
|
u32 result = ((src >> dst) & opcode.GetBitfieldMask()) << opcode.bf_dst_bit;
|
|
|
|
|
|
|
|
ProcessResult(opcode.result_operation, opcode.dst, result);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::ExtractShiftLeftRegister: {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 dst = GetRegister(opcode.src_a);
|
|
|
|
u32 src = GetRegister(opcode.src_b);
|
|
|
|
|
|
|
|
u32 result = ((src >> opcode.bf_src_bit) & opcode.GetBitfieldMask()) << dst;
|
|
|
|
|
|
|
|
ProcessResult(opcode.result_operation, opcode.dst, result);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::Read: {
|
2018-03-28 16:14:47 -04:00
|
|
|
u32 result = Read(GetRegister(opcode.src_a) + opcode.immediate);
|
|
|
|
ProcessResult(opcode.result_operation, opcode.dst, result);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::Operation::Branch: {
|
2018-03-28 16:14:47 -04:00
|
|
|
ASSERT_MSG(!is_delay_slot, "Executing a branch in a delay slot is not valid");
|
|
|
|
u32 value = GetRegister(opcode.src_a);
|
|
|
|
bool taken = EvaluateBranchCondition(opcode.branch_condition, value);
|
|
|
|
if (taken) {
|
|
|
|
// Ignore the delay slot if the branch has the annul bit.
|
|
|
|
if (opcode.branch_annul) {
|
2018-07-30 21:09:49 -04:00
|
|
|
pc = base_address + opcode.GetBranchTarget();
|
2018-03-28 16:14:47 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 21:09:49 -04:00
|
|
|
delayed_pc = base_address + opcode.GetBranchTarget();
|
2018-03-28 16:14:47 -04:00
|
|
|
// Execute one more instruction due to the delay slot.
|
2020-05-29 00:53:27 -04:00
|
|
|
return Step(true);
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-04-27 07:54:05 -04:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented macro operation {}",
|
2018-03-28 16:14:47 -04:00
|
|
|
static_cast<u32>(opcode.operation.Value()));
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:38:51 -04:00
|
|
|
// An instruction with the Exit flag will not actually
|
|
|
|
// cause an exit if it's executed inside a delay slot.
|
2019-08-31 16:43:19 -04:00
|
|
|
if (opcode.is_exit && !is_delay_slot) {
|
2018-03-28 16:14:47 -04:00
|
|
|
// Exit has a delay slot, execute the next instruction
|
2020-05-29 00:53:27 -04:00
|
|
|
Step(true);
|
2018-03-28 16:14:47 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
u32 MacroInterpreterImpl::GetALUResult(Macro::ALUOperation operation, u32 src_a, u32 src_b) {
|
2018-03-28 16:14:47 -04:00
|
|
|
switch (operation) {
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::Add: {
|
2018-11-21 14:32:21 -05:00
|
|
|
const u64 result{static_cast<u64>(src_a) + src_b};
|
|
|
|
carry_flag = result > 0xffffffff;
|
|
|
|
return static_cast<u32>(result);
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::AddWithCarry: {
|
2018-11-21 14:32:21 -05:00
|
|
|
const u64 result{static_cast<u64>(src_a) + src_b + (carry_flag ? 1ULL : 0ULL)};
|
|
|
|
carry_flag = result > 0xffffffff;
|
|
|
|
return static_cast<u32>(result);
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::Subtract: {
|
2018-11-21 14:32:21 -05:00
|
|
|
const u64 result{static_cast<u64>(src_a) - src_b};
|
|
|
|
carry_flag = result < 0x100000000;
|
|
|
|
return static_cast<u32>(result);
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::SubtractWithBorrow: {
|
2018-11-21 14:32:21 -05:00
|
|
|
const u64 result{static_cast<u64>(src_a) - src_b - (carry_flag ? 0ULL : 1ULL)};
|
|
|
|
carry_flag = result < 0x100000000;
|
|
|
|
return static_cast<u32>(result);
|
|
|
|
}
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::Xor:
|
2018-03-28 16:14:47 -04:00
|
|
|
return src_a ^ src_b;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::Or:
|
2018-03-28 16:14:47 -04:00
|
|
|
return src_a | src_b;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::And:
|
2018-03-28 16:14:47 -04:00
|
|
|
return src_a & src_b;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::AndNot:
|
2018-03-28 16:14:47 -04:00
|
|
|
return src_a & ~src_b;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ALUOperation::Nand:
|
2018-03-28 16:14:47 -04:00
|
|
|
return ~(src_a & src_b);
|
|
|
|
|
|
|
|
default:
|
2018-04-27 07:54:05 -04:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented ALU operation {}", static_cast<u32>(operation));
|
2018-12-18 20:52:32 -05:00
|
|
|
return 0;
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
void MacroInterpreterImpl::ProcessResult(Macro::ResultOperation operation, u32 reg, u32 result) {
|
2018-03-28 16:14:47 -04:00
|
|
|
switch (operation) {
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::IgnoreAndFetch:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Fetch parameter and ignore result.
|
|
|
|
SetRegister(reg, FetchParameter());
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::Move:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Move result.
|
|
|
|
SetRegister(reg, result);
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::MoveAndSetMethod:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Move result and use as Method Address.
|
|
|
|
SetRegister(reg, result);
|
|
|
|
SetMethodAddress(result);
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::FetchAndSend:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Fetch parameter and send result.
|
|
|
|
SetRegister(reg, FetchParameter());
|
|
|
|
Send(result);
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::MoveAndSend:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Move and send result.
|
|
|
|
SetRegister(reg, result);
|
|
|
|
Send(result);
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::FetchAndSetMethod:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Fetch parameter and use result as Method Address.
|
|
|
|
SetRegister(reg, FetchParameter());
|
|
|
|
SetMethodAddress(result);
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::MoveAndSetMethodFetchAndSend:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Move result and use as Method Address, then fetch and send parameter.
|
|
|
|
SetRegister(reg, result);
|
|
|
|
SetMethodAddress(result);
|
|
|
|
Send(FetchParameter());
|
|
|
|
break;
|
2020-05-29 00:53:27 -04:00
|
|
|
case Macro::ResultOperation::MoveAndSetMethodSend:
|
2018-03-28 16:14:47 -04:00
|
|
|
// Move result and use as Method Address, then send bits 12:17 of result.
|
|
|
|
SetRegister(reg, result);
|
|
|
|
SetMethodAddress(result);
|
|
|
|
Send((result >> 12) & 0b111111);
|
|
|
|
break;
|
|
|
|
default:
|
2018-04-27 07:54:05 -04:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented result operation {}", static_cast<u32>(operation));
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
bool MacroInterpreterImpl::EvaluateBranchCondition(Macro::BranchCondition cond, u32 value) const {
|
|
|
|
switch (cond) {
|
|
|
|
case Macro::BranchCondition::Zero:
|
|
|
|
return value == 0;
|
|
|
|
case Macro::BranchCondition::NotZero:
|
|
|
|
return value != 0;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return true;
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
Macro::Opcode MacroInterpreterImpl::GetOpcode() const {
|
|
|
|
ASSERT((pc % sizeof(u32)) == 0);
|
|
|
|
ASSERT(pc < code.size() * sizeof(u32));
|
|
|
|
return {code[pc / sizeof(u32)]};
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 MacroInterpreterImpl::GetRegister(u32 register_id) const {
|
2019-04-05 22:51:22 -04:00
|
|
|
return registers.at(register_id);
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
void MacroInterpreterImpl::SetRegister(u32 register_id, u32 value) {
|
2019-04-05 22:51:22 -04:00
|
|
|
// Register 0 is hardwired as the zero register.
|
|
|
|
// Ensure no writes to it actually occur.
|
|
|
|
if (register_id == 0) {
|
2018-03-28 16:14:47 -04:00
|
|
|
return;
|
2019-04-05 22:51:22 -04:00
|
|
|
}
|
2018-03-28 16:14:47 -04:00
|
|
|
|
2019-04-05 22:51:22 -04:00
|
|
|
registers.at(register_id) = value;
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
void MacroInterpreterImpl::SetMethodAddress(u32 address) {
|
2018-03-28 16:14:47 -04:00
|
|
|
method_address.raw = address;
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
void MacroInterpreterImpl::Send(u32 value) {
|
2020-04-27 21:47:58 -04:00
|
|
|
maxwell3d.CallMethodFromMME(method_address.address, value);
|
2018-03-28 16:14:47 -04:00
|
|
|
// Increment the method address by the method increment.
|
|
|
|
method_address.address.Assign(method_address.address.Value() +
|
|
|
|
method_address.increment.Value());
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
u32 MacroInterpreterImpl::Read(u32 method) const {
|
2018-03-28 16:14:47 -04:00
|
|
|
return maxwell3d.GetRegisterValue(method);
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:53:27 -04:00
|
|
|
u32 MacroInterpreterImpl::FetchParameter() {
|
|
|
|
ASSERT(next_parameter_index < num_parameters);
|
|
|
|
return parameters[next_parameter_index++];
|
2018-03-28 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Tegra
|