2018-11-16 23:01:31 -05:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-11-21 21:46:14 -05:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
2018-04-08 15:17:35 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
namespace Ryujinx.Graphics.Gpu
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Macro code interpreter.
|
|
|
|
/// </summary>
|
2018-04-08 15:17:35 -04:00
|
|
|
class MacroInterpreter
|
|
|
|
{
|
|
|
|
private enum AssignmentOperation
|
|
|
|
{
|
|
|
|
IgnoreAndFetch = 0,
|
|
|
|
Move = 1,
|
|
|
|
MoveAndSetMaddr = 2,
|
|
|
|
FetchAndSend = 3,
|
|
|
|
MoveAndSend = 4,
|
|
|
|
FetchAndSetMaddr = 5,
|
|
|
|
MoveAndSetMaddrThenFetchAndSend = 6,
|
|
|
|
MoveAndSetMaddrThenSendHigh = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum AluOperation
|
|
|
|
{
|
|
|
|
AluReg = 0,
|
|
|
|
AddImmediate = 1,
|
|
|
|
BitfieldReplace = 2,
|
|
|
|
BitfieldExtractLslImm = 3,
|
|
|
|
BitfieldExtractLslReg = 4,
|
|
|
|
ReadImmediate = 5
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum AluRegOperation
|
|
|
|
{
|
|
|
|
Add = 0,
|
|
|
|
AddWithCarry = 1,
|
|
|
|
Subtract = 2,
|
|
|
|
SubtractWithBorrow = 3,
|
|
|
|
BitwiseExclusiveOr = 8,
|
|
|
|
BitwiseOr = 9,
|
|
|
|
BitwiseAnd = 10,
|
|
|
|
BitwiseAndNot = 11,
|
|
|
|
BitwiseNotAnd = 12
|
|
|
|
}
|
|
|
|
|
|
|
|
public Queue<int> Fifo { get; private set; }
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private int[] _gprs;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private int _methAddr;
|
|
|
|
private int _methIncr;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private bool _carry;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private int _opCode;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private int _pipeOp;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
private int _pc;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the macro code interpreter.
|
|
|
|
/// </summary>
|
|
|
|
public MacroInterpreter()
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
Fifo = new Queue<int>();
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_gprs = new int[8];
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Executes a macro program until it exits.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mme">Code of the program to execute</param>
|
|
|
|
/// <param name="position">Start position to execute</param>
|
|
|
|
/// <param name="param">Optional argument passed to the program, 0 if not used</param>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
public void Execute(int[] mme, int position, int param, GpuState state)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_gprs[1] = param;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_pc = position;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
FetchOpCode(mme);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
while (Step(mme, state));
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Due to the delay slot, we still need to execute
|
|
|
|
// one more instruction before we actually exit.
|
2019-11-21 21:46:14 -05:00
|
|
|
Step(mme, state);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Resets the internal interpreter state.
|
|
|
|
/// Call each time you run a new program.
|
|
|
|
/// </summary>
|
2018-04-08 15:17:35 -04:00
|
|
|
private void Reset()
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
for (int index = 0; index < _gprs.Length; index++)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
_gprs[index] = 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_methAddr = 0;
|
|
|
|
_methIncr = 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_carry = false;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Executes a single instruction of the program.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mme">Program code to execute</param>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <returns>True to continue execution, false if the program exited</returns>
|
2019-11-21 21:46:14 -05:00
|
|
|
private bool Step(int[] mme, GpuState state)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
int baseAddr = _pc - 1;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
FetchOpCode(mme);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
if ((_opCode & 7) < 7)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Operation produces a value.
|
2019-03-03 20:45:25 -05:00
|
|
|
AssignmentOperation asgOp = (AssignmentOperation)((_opCode >> 4) & 7);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
int result = GetAluResult(state);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
switch (asgOp)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Fetch parameter and ignore result.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.IgnoreAndFetch:
|
|
|
|
{
|
|
|
|
SetDstGpr(FetchParam());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Move result.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.Move:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
SetDstGpr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Move result and use as Method Address.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.MoveAndSetMaddr:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
SetDstGpr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
SetMethAddr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Fetch parameter and send result.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.FetchAndSend:
|
|
|
|
{
|
|
|
|
SetDstGpr(FetchParam());
|
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
Send(state, result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Move and send result.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.MoveAndSend:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
SetDstGpr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
Send(state, result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Fetch parameter and use result as Method Address.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.FetchAndSetMaddr:
|
|
|
|
{
|
|
|
|
SetDstGpr(FetchParam());
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
SetMethAddr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Move result and use as Method Address, then fetch and send parameter.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
SetDstGpr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
SetMethAddr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
Send(state, FetchParam());
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Move result and use as Method Address, then send bits 17:12 of result.
|
2018-04-08 15:17:35 -04:00
|
|
|
case AssignmentOperation.MoveAndSetMaddrThenSendHigh:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
SetDstGpr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
SetMethAddr(result);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
Send(state, (result >> 12) & 0x3f);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Branch.
|
2019-03-03 20:45:25 -05:00
|
|
|
bool onNotZero = ((_opCode >> 4) & 1) != 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
bool taken = onNotZero
|
2018-04-08 15:17:35 -04:00
|
|
|
? GetGprA() != 0
|
|
|
|
: GetGprA() == 0;
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
if (taken)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
_pc = baseAddr + GetImm();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
bool noDelays = (_opCode & 0x20) != 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
if (noDelays)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
FetchOpCode(mme);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
bool exit = (_opCode & 0x80) != 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return !exit;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches a single operation code from the program code.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mme">Program code</param>
|
2019-03-03 20:45:25 -05:00
|
|
|
private void FetchOpCode(int[] mme)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
_opCode = _pipeOp;
|
|
|
|
_pipeOp = mme[_pc++];
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the result of the current Arithmetic and Logic unit operation.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <returns>Operation result</returns>
|
2019-11-21 21:46:14 -05:00
|
|
|
private int GetAluResult(GpuState state)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
AluOperation op = (AluOperation)(_opCode & 7);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
switch (op)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
case AluOperation.AluReg:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
AluRegOperation aluOp = (AluRegOperation)((_opCode >> 17) & 0x1f);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return GetAluResult(aluOp, GetGprA(), GetGprB());
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluOperation.AddImmediate:
|
|
|
|
{
|
|
|
|
return GetGprA() + GetImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
case AluOperation.BitfieldReplace:
|
|
|
|
case AluOperation.BitfieldExtractLslImm:
|
|
|
|
case AluOperation.BitfieldExtractLslReg:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
int bfSrcBit = (_opCode >> 17) & 0x1f;
|
|
|
|
int bfSize = (_opCode >> 22) & 0x1f;
|
|
|
|
int bfDstBit = (_opCode >> 27) & 0x1f;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
int bfMask = (1 << bfSize) - 1;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
int dst = GetGprA();
|
|
|
|
int src = GetGprB();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
switch (op)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
case AluOperation.BitfieldReplace:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
src = (int)((uint)src >> bfSrcBit) & bfMask;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
dst &= ~(bfMask << bfDstBit);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
dst |= src << bfDstBit;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return dst;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluOperation.BitfieldExtractLslImm:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
src = (int)((uint)src >> dst) & bfMask;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return src << bfDstBit;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluOperation.BitfieldExtractLslReg:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
src = (int)((uint)src >> bfSrcBit) & bfMask;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return src << dst;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AluOperation.ReadImmediate:
|
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
return Read(state, GetGprA() + GetImm());
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
throw new ArgumentException(nameof(_opCode));
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
2020-01-01 18:14:18 -05:00
|
|
|
/// Gets the result of an Arithmetic and Logic operation using registers.
|
2019-12-31 15:08:20 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="aluOp">Arithmetic and Logic unit operation with registers</param>
|
|
|
|
/// <param name="a">First operand value</param>
|
|
|
|
/// <param name="b">Second operand value</param>
|
|
|
|
/// <returns>Operation result</returns>
|
2019-03-03 20:45:25 -05:00
|
|
|
private int GetAluResult(AluRegOperation aluOp, int a, int b)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
switch (aluOp)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
case AluRegOperation.Add:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
ulong result = (ulong)a + (ulong)b;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_carry = result > 0xffffffff;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return (int)result;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluRegOperation.AddWithCarry:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
ulong result = (ulong)a + (ulong)b + (_carry ? 1UL : 0UL);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_carry = result > 0xffffffff;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return (int)result;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluRegOperation.Subtract:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
ulong result = (ulong)a - (ulong)b;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_carry = result < 0x100000000;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return (int)result;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case AluRegOperation.SubtractWithBorrow:
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
ulong result = (ulong)a - (ulong)b - (_carry ? 0UL : 1UL);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_carry = result < 0x100000000;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return (int)result;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
case AluRegOperation.BitwiseExclusiveOr: return a ^ b;
|
|
|
|
case AluRegOperation.BitwiseOr: return a | b;
|
|
|
|
case AluRegOperation.BitwiseAnd: return a & b;
|
|
|
|
case AluRegOperation.BitwiseAndNot: return a & ~b;
|
|
|
|
case AluRegOperation.BitwiseNotAnd: return ~(a & b);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(aluOp));
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Extracts a 32-bits signed integer constant from the current operation code.
|
|
|
|
/// </summary>
|
2020-01-01 10:39:09 -05:00
|
|
|
/// <returns>The 32-bits immediate value encoded at the current operation code</returns>
|
2018-04-08 15:17:35 -04:00
|
|
|
private int GetImm()
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Note: The immediate is signed, the sign-extension is intended here.
|
2019-03-03 20:45:25 -05:00
|
|
|
return _opCode >> 14;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the current method address, for method calls.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">Packed address and increment value</param>
|
2019-03-03 20:45:25 -05:00
|
|
|
private void SetMethAddr(int value)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
_methAddr = (value >> 0) & 0xfff;
|
|
|
|
_methIncr = (value >> 12) & 0x3f;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the destination register value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">Value to set (usually the operation result)</param>
|
2019-03-03 20:45:25 -05:00
|
|
|
private void SetDstGpr(int value)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
_gprs[(_opCode >> 8) & 7] = value;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets first operand value from the respective register.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Operand value</returns>
|
2018-04-08 15:17:35 -04:00
|
|
|
private int GetGprA()
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
return GetGprValue((_opCode >> 11) & 7);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets second operand value from the respective register.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Operand value</returns>
|
2018-04-08 15:17:35 -04:00
|
|
|
private int GetGprB()
|
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
return GetGprValue((_opCode >> 14) & 7);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the value from a register, or 0 if the R0 register is specified.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">Index of the register</param>
|
|
|
|
/// <returns>Register value</returns>
|
2019-03-03 20:45:25 -05:00
|
|
|
private int GetGprValue(int index)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-03-03 20:45:25 -05:00
|
|
|
return index != 0 ? _gprs[index] : 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches a call argument from the call argument FIFO.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The call argument, or 0 if the FIFO is empty</returns>
|
2018-04-08 15:17:35 -04:00
|
|
|
private int FetchParam()
|
|
|
|
{
|
2019-12-31 15:08:20 -05:00
|
|
|
if (!Fifo.TryDequeue(out int value))
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-11-16 23:01:31 -05:00
|
|
|
Logger.PrintWarning(LogClass.Gpu, "Macro attempted to fetch an inexistent argument.");
|
|
|
|
|
|
|
|
return 0;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
return value;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Reads data from a GPU register.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="reg">Register offset to read</param>
|
|
|
|
/// <returns>GPU register value</returns>
|
2019-11-21 21:46:14 -05:00
|
|
|
private int Read(GpuState state, int reg)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-11-21 21:46:14 -05:00
|
|
|
return state.Read(reg);
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 15:08:20 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a GPU method call.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="value">Call argument</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void Send(GpuState state, int value)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2019-10-13 02:02:07 -04:00
|
|
|
MethodParams meth = new MethodParams(_methAddr, value);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
state.CallMethod(meth);
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2019-03-03 20:45:25 -05:00
|
|
|
_methAddr += _methIncr;
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|