2023-09-03 04:30:26 +00:00
|
|
|
// SPDX-FileCopyrightText: 2016 iCatButler, 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
#include "cpu_pgxp.h"
|
2021-05-25 10:01:00 +00:00
|
|
|
#include "bus.h"
|
|
|
|
#include "cpu_core.h"
|
2020-08-01 14:25:07 +00:00
|
|
|
#include "settings.h"
|
2023-09-03 04:30:26 +00:00
|
|
|
|
2023-11-28 15:55:54 +00:00
|
|
|
#include "common/assert.h"
|
2023-09-03 04:30:26 +00:00
|
|
|
#include "common/log.h"
|
|
|
|
|
2020-08-19 15:21:36 +00:00
|
|
|
#include <climits>
|
2020-09-02 12:44:52 +00:00
|
|
|
#include <cmath>
|
2023-09-03 04:30:26 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Log_SetChannel(CPU::PGXP);
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
namespace CPU::PGXP {
|
2023-11-18 06:21:51 +00:00
|
|
|
namespace {
|
2021-06-12 12:02:28 +00:00
|
|
|
|
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
VERTEX_CACHE_WIDTH = 0x800 * 2,
|
|
|
|
VERTEX_CACHE_HEIGHT = 0x800 * 2,
|
|
|
|
VERTEX_CACHE_SIZE = VERTEX_CACHE_WIDTH * VERTEX_CACHE_HEIGHT,
|
2023-11-06 08:09:27 +00:00
|
|
|
PGXP_MEM_SIZE = (static_cast<u32>(Bus::RAM_8MB_SIZE) + static_cast<u32>(CPU::SCRATCHPAD_SIZE)) / 4,
|
2021-08-22 14:17:51 +00:00
|
|
|
PGXP_MEM_SCRATCH_OFFSET = Bus::RAM_8MB_SIZE / 4
|
2021-06-12 12:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NONE 0
|
|
|
|
#define ALL 0xFFFFFFFF
|
|
|
|
#define VALID 1
|
|
|
|
#define VALID_0 (VALID << 0)
|
|
|
|
#define VALID_1 (VALID << 8)
|
|
|
|
#define VALID_2 (VALID << 16)
|
|
|
|
#define VALID_3 (VALID << 24)
|
|
|
|
#define VALID_01 (VALID_0 | VALID_1)
|
|
|
|
#define VALID_012 (VALID_0 | VALID_1 | VALID_2)
|
|
|
|
#define VALID_ALL (VALID_0 | VALID_1 | VALID_2 | VALID_3)
|
|
|
|
#define INV_VALID_ALL (ALL ^ VALID_ALL)
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
union psx_value
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u8 l, h, h2, h3;
|
|
|
|
} b;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
u16 l, h;
|
|
|
|
} w;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
s8 l, h, h2, h3;
|
|
|
|
} sb;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
s16 l, h;
|
|
|
|
} sw;
|
|
|
|
u32 d;
|
|
|
|
s32 sd;
|
2023-12-06 06:45:17 +00:00
|
|
|
};
|
2023-11-18 06:21:51 +00:00
|
|
|
} // namespace
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2021-06-12 12:02:28 +00:00
|
|
|
static void PGXP_CacheVertex(s16 sx, s16 sy, const PGXP_value& vertex);
|
2023-11-18 06:21:51 +00:00
|
|
|
static PGXP_value* PGXP_GetCachedVertex(short sx, short sy);
|
|
|
|
|
|
|
|
static float TruncateVertexPosition(float p);
|
|
|
|
static bool IsWithinTolerance(float precise_x, float precise_y, int int_x, int int_y);
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2020-08-19 13:26:57 +00:00
|
|
|
static void MakeValid(PGXP_value* pV, u32 psxV);
|
2023-12-06 06:45:17 +00:00
|
|
|
|
2020-08-01 14:25:07 +00:00
|
|
|
static void Validate(PGXP_value* pV, u32 psxV);
|
|
|
|
static void MaskValidate(PGXP_value* pV, u32 psxV, u32 mask, u32 validMask);
|
|
|
|
|
2020-08-19 13:26:57 +00:00
|
|
|
static double f16Sign(double in);
|
|
|
|
static double f16Unsign(double in);
|
|
|
|
static double f16Overflow(double in);
|
|
|
|
|
2020-08-01 14:25:07 +00:00
|
|
|
static PGXP_value* GetPtr(u32 addr);
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
static void ValidateAndCopyMem(PGXP_value* dest, u32 addr, u32 value);
|
|
|
|
static void ValidateAndCopyMem16(PGXP_value* dest, u32 addr, u32 value, bool sign);
|
|
|
|
|
|
|
|
static void CPU_MTC2_int(const PGXP_value& value, u32 reg);
|
2023-11-18 06:21:51 +00:00
|
|
|
static void CPU_BITWISE(u32 instr, u32 rdVal, u32 rsVal, u32 rtVal);
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
static void WriteMem(const PGXP_value* value, u32 addr);
|
|
|
|
static void WriteMem16(const PGXP_value* src, u32 addr);
|
|
|
|
|
2021-06-12 12:02:28 +00:00
|
|
|
static const PGXP_value PGXP_value_invalid = {0.f, 0.f, 0.f, {0}, 0};
|
|
|
|
static const PGXP_value PGXP_value_zero = {0.f, 0.f, 0.f, {VALID_ALL}, 0};
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2020-09-02 12:44:52 +00:00
|
|
|
static PGXP_value* Mem = nullptr;
|
|
|
|
static PGXP_value* vertexCache = nullptr;
|
2023-12-06 06:45:17 +00:00
|
|
|
} // namespace CPU::PGXP
|
2020-09-02 12:44:52 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::Initialize()
|
|
|
|
{
|
|
|
|
std::memset(g_state.pgxp_gpr, 0, sizeof(g_state.pgxp_gpr));
|
|
|
|
std::memset(g_state.pgxp_cop0, 0, sizeof(g_state.pgxp_cop0));
|
|
|
|
|
|
|
|
std::memset(g_state.pgxp_gte, 0, sizeof(g_state.pgxp_gte));
|
|
|
|
|
|
|
|
if (!Mem)
|
|
|
|
{
|
|
|
|
Mem = static_cast<PGXP_value*>(std::calloc(PGXP_MEM_SIZE, sizeof(PGXP_value)));
|
|
|
|
if (!Mem)
|
|
|
|
Panic("Failed to allocate PGXP memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.gpu_pgxp_vertex_cache && !vertexCache)
|
|
|
|
{
|
|
|
|
vertexCache = static_cast<PGXP_value*>(std::calloc(VERTEX_CACHE_SIZE, sizeof(PGXP_value)));
|
|
|
|
if (!vertexCache)
|
|
|
|
{
|
|
|
|
Log_ErrorPrint("Failed to allocate memory for vertex cache, disabling.");
|
|
|
|
g_settings.gpu_pgxp_vertex_cache = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vertexCache)
|
|
|
|
std::memset(vertexCache, 0, sizeof(PGXP_value) * VERTEX_CACHE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPU::PGXP::Reset()
|
|
|
|
{
|
|
|
|
std::memset(g_state.pgxp_gpr, 0, sizeof(g_state.pgxp_gpr));
|
|
|
|
std::memset(g_state.pgxp_cop0, 0, sizeof(g_state.pgxp_cop0));
|
|
|
|
std::memset(g_state.pgxp_gte, 0, sizeof(g_state.pgxp_gte));
|
|
|
|
|
|
|
|
if (Mem)
|
|
|
|
std::memset(Mem, 0, sizeof(PGXP_value) * PGXP_MEM_SIZE);
|
|
|
|
|
|
|
|
if (vertexCache)
|
|
|
|
std::memset(vertexCache, 0, sizeof(PGXP_value) * VERTEX_CACHE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPU::PGXP::Shutdown()
|
|
|
|
{
|
|
|
|
if (vertexCache)
|
|
|
|
{
|
|
|
|
std::free(vertexCache);
|
|
|
|
vertexCache = nullptr;
|
|
|
|
}
|
|
|
|
if (Mem)
|
|
|
|
{
|
|
|
|
std::free(Mem);
|
|
|
|
Mem = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memset(g_state.pgxp_gte, 0, sizeof(g_state.pgxp_gte));
|
|
|
|
|
|
|
|
std::memset(g_state.pgxp_gpr, 0, sizeof(g_state.pgxp_gpr));
|
|
|
|
std::memset(g_state.pgxp_cop0, 0, sizeof(g_state.pgxp_cop0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instruction register decoding
|
|
|
|
#define op(_instr) (_instr >> 26) // The op part of the instruction register
|
|
|
|
#define func(_instr) ((_instr)&0x3F) // The funct part of the instruction register
|
|
|
|
#define sa(_instr) ((_instr >> 6) & 0x1F) // The sa part of the instruction register
|
|
|
|
#define rd(_instr) ((_instr >> 11) & 0x1F) // The rd part of the instruction register
|
|
|
|
#define rt(_instr) ((_instr >> 16) & 0x1F) // The rt part of the instruction register
|
|
|
|
#define rs(_instr) ((_instr >> 21) & 0x1F) // The rs part of the instruction register
|
|
|
|
#define imm(_instr) (_instr & 0xFFFF) // The immediate part of the instruction register
|
|
|
|
#define cop2idx(_instr) (((_instr >> 11) & 0x1F) | ((_instr >> 17) & 0x20))
|
|
|
|
|
|
|
|
#define SX0 (g_state.pgxp_gte[12].x)
|
|
|
|
#define SY0 (g_state.pgxp_gte[12].y)
|
|
|
|
#define SX1 (g_state.pgxp_gte[13].x)
|
|
|
|
#define SY1 (g_state.pgxp_gte[13].y)
|
|
|
|
#define SX2 (g_state.pgxp_gte[14].x)
|
|
|
|
#define SY2 (g_state.pgxp_gte[14].y)
|
|
|
|
|
|
|
|
#define SXY0 (g_state.pgxp_gte[12])
|
|
|
|
#define SXY1 (g_state.pgxp_gte[13])
|
|
|
|
#define SXY2 (g_state.pgxp_gte[14])
|
|
|
|
#define SXYP (g_state.pgxp_gte[15])
|
|
|
|
|
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::MakeValid(PGXP_value* pV, u32 psxV)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
if (VALID_01 != (pV->flags & VALID_01))
|
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
pV->x = static_cast<float>(static_cast<s16>(Truncate16(psxV)));
|
|
|
|
pV->y = static_cast<float>(static_cast<s16>(Truncate16(psxV >> 16)));
|
2020-08-19 13:26:57 +00:00
|
|
|
pV->z = 0.f;
|
|
|
|
pV->flags |= VALID_01;
|
2021-06-12 12:02:28 +00:00
|
|
|
pV->value = psxV;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::Validate(PGXP_value* pV, u32 psxV)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// assume pV is not NULL
|
|
|
|
pV->flags &= (pV->value == psxV) ? ALL : INV_VALID_ALL;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::MaskValidate(PGXP_value* pV, u32 psxV, u32 mask, u32 validMask)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// assume pV is not NULL
|
|
|
|
pV->flags &= ((pV->value & mask) == (psxV & mask)) ? ALL : (ALL ^ (validMask));
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE double CPU::PGXP::f16Sign(double in)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-11-20 10:56:21 +00:00
|
|
|
const s32 s = static_cast<s32>(static_cast<s64>(in * (USHRT_MAX + 1)));
|
2023-10-31 15:32:29 +00:00
|
|
|
return static_cast<double>(s) / static_cast<double>(USHRT_MAX + 1);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE double CPU::PGXP::f16Unsign(double in)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-10-31 15:32:29 +00:00
|
|
|
return (in >= 0) ? in : (in + (USHRT_MAX + 1));
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE double CPU::PGXP::f16Overflow(double in)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
double out = 0;
|
|
|
|
s64 v = ((s64)in) >> 16;
|
|
|
|
out = (double)v;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE CPU::PGXP_value* CPU::PGXP::GetPtr(u32 addr)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
if ((addr & SCRATCHPAD_ADDR_MASK) == SCRATCHPAD_ADDR)
|
|
|
|
return &Mem[PGXP_MEM_SCRATCH_OFFSET + ((addr & SCRATCHPAD_OFFSET_MASK) >> 2)];
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
const u32 paddr = (addr & PHYSICAL_MEMORY_ADDRESS_MASK);
|
2021-05-25 10:01:00 +00:00
|
|
|
if (paddr < Bus::RAM_MIRROR_END)
|
2021-08-22 14:17:51 +00:00
|
|
|
return &Mem[(paddr & Bus::g_ram_mask) >> 2];
|
2021-05-25 10:01:00 +00:00
|
|
|
else
|
|
|
|
return nullptr;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::ValidateAndCopyMem(PGXP_value* dest, u32 addr, u32 value)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
PGXP_value* pMem = GetPtr(addr);
|
2023-11-04 10:03:45 +00:00
|
|
|
if (pMem)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
Validate(pMem, value);
|
|
|
|
*dest = *pMem;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-12 12:02:28 +00:00
|
|
|
*dest = PGXP_value_invalid;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::ValidateAndCopyMem16(PGXP_value* dest, u32 addr, u32 value, bool sign)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
u32 validMask = 0;
|
|
|
|
psx_value val, mask;
|
|
|
|
PGXP_value* pMem = GetPtr(addr);
|
2023-11-04 10:03:45 +00:00
|
|
|
if (pMem)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
mask.d = val.d = 0;
|
|
|
|
// determine if high or low word
|
|
|
|
if ((addr % 4) == 2)
|
|
|
|
{
|
|
|
|
val.w.h = static_cast<u16>(value);
|
|
|
|
mask.w.h = 0xFFFF;
|
|
|
|
validMask = VALID_1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val.w.l = static_cast<u16>(value);
|
|
|
|
mask.w.l = 0xFFFF;
|
|
|
|
validMask = VALID_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate and copy whole value
|
|
|
|
MaskValidate(pMem, val.d, mask.d, validMask);
|
|
|
|
*dest = *pMem;
|
|
|
|
|
|
|
|
// if high word then shift
|
|
|
|
if ((addr % 4) == 2)
|
|
|
|
{
|
|
|
|
dest->x = dest->y;
|
|
|
|
dest->compFlags[0] = dest->compFlags[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncate value
|
|
|
|
dest->y = (dest->x < 0) ? -1.f * sign : 0.f; // 0.f;
|
|
|
|
dest->value = value;
|
|
|
|
dest->compFlags[1] = VALID; // iCB: High word is valid, just 0
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-12 12:02:28 +00:00
|
|
|
*dest = PGXP_value_invalid;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::WriteMem(const PGXP_value* value, u32 addr)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
PGXP_value* pMem = GetPtr(addr);
|
|
|
|
|
|
|
|
if (pMem)
|
|
|
|
*pMem = *value;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::WriteMem16(const PGXP_value* src, u32 addr)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
PGXP_value* dest = GetPtr(addr);
|
2023-11-04 10:03:45 +00:00
|
|
|
psx_value* pVal = nullptr;
|
2020-08-01 14:25:07 +00:00
|
|
|
|
|
|
|
if (dest)
|
|
|
|
{
|
|
|
|
pVal = (psx_value*)&dest->value;
|
|
|
|
// determine if high or low word
|
|
|
|
if ((addr % 4) == 2)
|
|
|
|
{
|
|
|
|
dest->y = src->x;
|
|
|
|
dest->compFlags[1] = src->compFlags[0];
|
|
|
|
pVal->w.h = (u16)src->value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dest->x = src->x;
|
|
|
|
dest->compFlags[0] = src->compFlags[0];
|
|
|
|
pVal->w.l = (u16)src->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite z/w if valid
|
|
|
|
if (src->compFlags[2] == VALID)
|
|
|
|
{
|
|
|
|
dest->z = src->z;
|
|
|
|
dest->compFlags[2] = src->compFlags[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// dest->valid = dest->valid && src->valid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::GTE_PushSXYZ2f(float x, float y, float z, u32 v)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// push values down FIFO
|
|
|
|
SXY0 = SXY1;
|
|
|
|
SXY1 = SXY2;
|
|
|
|
|
2021-06-12 12:02:28 +00:00
|
|
|
SXY2.x = x;
|
|
|
|
SXY2.y = y;
|
|
|
|
SXY2.z = z;
|
|
|
|
SXY2.value = v;
|
2020-08-01 14:25:07 +00:00
|
|
|
SXY2.flags = VALID_ALL;
|
|
|
|
|
|
|
|
if (g_settings.gpu_pgxp_vertex_cache)
|
2021-06-12 12:02:28 +00:00
|
|
|
PGXP_CacheVertex(static_cast<s16>(Truncate16(v)), static_cast<s16>(Truncate16(v >> 16)), SXY2);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define VX(n) (psxRegs.CP2D.p[n << 1].sw.l)
|
|
|
|
#define VY(n) (psxRegs.CP2D.p[n << 1].sw.h)
|
|
|
|
#define VZ(n) (psxRegs.CP2D.p[(n << 1) + 1].sw.l)
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
int CPU::PGXP::GTE_NCLIP_valid(u32 sxy0, u32 sxy1, u32 sxy2)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
Validate(&SXY0, sxy0);
|
|
|
|
Validate(&SXY1, sxy1);
|
|
|
|
Validate(&SXY2, sxy2);
|
2023-11-28 16:00:22 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
// Don't use accurate clipping for game-constructed values, which don't have a valid Z.
|
|
|
|
return (((SXY0.flags & SXY1.flags & SXY2.flags & VALID_012) == VALID_012));
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
float CPU::PGXP::GTE_NCLIP()
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
float nclip = ((SX0 * SY1) + (SX1 * SY2) + (SX2 * SY0) - (SX0 * SY2) - (SX1 * SY0) - (SX2 * SY1));
|
|
|
|
|
|
|
|
// ensure fractional values are not incorrectly rounded to 0
|
|
|
|
float nclipAbs = std::abs(nclip);
|
|
|
|
if ((0.1f < nclipAbs) && (nclipAbs < 1.f))
|
|
|
|
nclip += (nclip < 0.f ? -1 : 1);
|
|
|
|
|
|
|
|
// float AX = SX1 - SX0;
|
|
|
|
// float AY = SY1 - SY0;
|
|
|
|
|
|
|
|
// float BX = SX2 - SX0;
|
|
|
|
// float BY = SY2 - SY0;
|
|
|
|
|
|
|
|
//// normalise A and B
|
|
|
|
// float mA = sqrt((AX*AX) + (AY*AY));
|
|
|
|
// float mB = sqrt((BX*BX) + (BY*BY));
|
|
|
|
|
|
|
|
//// calculate AxB to get Z component of C
|
|
|
|
// float CZ = ((AX * BY) - (AY * BX)) * (1 << 12);
|
|
|
|
|
|
|
|
return nclip;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::CPU_MTC2_int(const PGXP_value& value, u32 reg)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
switch (reg)
|
|
|
|
{
|
|
|
|
case 15:
|
|
|
|
// push FIFO
|
|
|
|
SXY0 = SXY1;
|
|
|
|
SXY1 = SXY2;
|
|
|
|
SXY2 = value;
|
|
|
|
SXYP = SXY2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 31:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gte[reg] = value;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Data transfer tracking
|
|
|
|
////////////////////////////////////
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MFC2(u32 instr, u32 rdVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// CPU[Rt] = GTE_D[Rd]
|
2023-08-15 13:12:21 +00:00
|
|
|
const u32 idx = cop2idx(instr);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gte[idx], rdVal);
|
|
|
|
g_state.pgxp_gpr[rt(instr)] = g_state.pgxp_gte[idx];
|
|
|
|
g_state.pgxp_gpr[rt(instr)].value = rdVal;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MTC2(u32 instr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// GTE_D[Rd] = CPU[Rt]
|
2023-08-15 13:12:21 +00:00
|
|
|
const u32 idx = cop2idx(instr);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
CPU_MTC2_int(g_state.pgxp_gpr[rt(instr)], idx);
|
|
|
|
g_state.pgxp_gte[idx].value = rtVal;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Memory Access
|
|
|
|
////////////////////////////////////
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LWC2(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// GTE_D[Rt] = Mem[addr]
|
|
|
|
PGXP_value val;
|
|
|
|
ValidateAndCopyMem(&val, addr, rtVal);
|
2023-12-06 06:45:17 +00:00
|
|
|
CPU_MTC2_int(val, rt(instr));
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SWC2(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// Mem[addr] = GTE_D[Rt]
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gte[rt(instr)], rtVal);
|
|
|
|
WriteMem(&g_state.pgxp_gte[rt(instr)], addr);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::PGXP_CacheVertex(s16 sx, s16 sy, const PGXP_value& vertex)
|
2020-09-02 12:44:52 +00:00
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
if (sx >= -0x800 && sx <= 0x7ff && sy >= -0x800 && sy <= 0x7ff)
|
2020-09-02 12:44:52 +00:00
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
// Write vertex into cache
|
|
|
|
vertexCache[(sy + 0x800) * VERTEX_CACHE_WIDTH + (sx + 0x800)] = vertex;
|
2020-09-02 12:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE CPU::PGXP_value* CPU::PGXP::PGXP_GetCachedVertex(short sx, short sy)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
if (sx >= -0x800 && sx <= 0x7ff && sy >= -0x800 && sy <= 0x7ff)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
// Return pointer to cache entry
|
|
|
|
return &vertexCache[(sy + 0x800) * VERTEX_CACHE_WIDTH + (sx + 0x800)];
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 10:44:09 +00:00
|
|
|
return nullptr;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE float CPU::PGXP::TruncateVertexPosition(float p)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
const s32 int_part = static_cast<s32>(p);
|
|
|
|
const float int_part_f = static_cast<float>(int_part);
|
|
|
|
return static_cast<float>(static_cast<s16>(int_part << 5) >> 5) + (p - int_part_f);
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE bool CPU::PGXP::IsWithinTolerance(float precise_x, float precise_y, int int_x, int int_y)
|
2020-11-26 14:02:13 +00:00
|
|
|
{
|
|
|
|
const float tolerance = g_settings.gpu_pgxp_tolerance;
|
|
|
|
if (tolerance < 0.0f)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return (std::abs(precise_x - static_cast<float>(int_x)) <= tolerance &&
|
|
|
|
std::abs(precise_y - static_cast<float>(int_y)) <= tolerance);
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
bool CPU::PGXP::GetPreciseVertex(u32 addr, u32 value, int x, int y, int xOffs, int yOffs, float* out_x, float* out_y,
|
|
|
|
float* out_w)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
const PGXP_value* vert = GetPtr(addr);
|
2020-08-01 14:25:07 +00:00
|
|
|
if (vert && ((vert->flags & VALID_01) == VALID_01) && (vert->value == value))
|
|
|
|
{
|
|
|
|
// There is a value here with valid X and Y coordinates
|
|
|
|
*out_x = TruncateVertexPosition(vert->x) + static_cast<float>(xOffs);
|
|
|
|
*out_y = TruncateVertexPosition(vert->y) + static_cast<float>(yOffs);
|
|
|
|
*out_w = vert->z / 32768.0f;
|
2021-06-12 12:02:28 +00:00
|
|
|
|
2020-11-26 14:02:13 +00:00
|
|
|
if (IsWithinTolerance(*out_x, *out_y, x, y))
|
|
|
|
{
|
|
|
|
// check validity of z component
|
|
|
|
return ((vert->flags & VALID_2) == VALID_2);
|
|
|
|
}
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
2021-06-12 12:02:28 +00:00
|
|
|
|
|
|
|
if (g_settings.gpu_pgxp_vertex_cache)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
const short psx_x = (short)(value & 0xFFFFu);
|
|
|
|
const short psx_y = (short)(value >> 16);
|
|
|
|
|
|
|
|
// Look in cache for valid vertex
|
|
|
|
vert = PGXP_GetCachedVertex(psx_x, psx_y);
|
2021-06-12 12:02:28 +00:00
|
|
|
if (vert && (vert->flags & VALID_01) == VALID_01)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
*out_x = TruncateVertexPosition(vert->x) + static_cast<float>(xOffs);
|
|
|
|
*out_y = TruncateVertexPosition(vert->y) + static_cast<float>(yOffs);
|
|
|
|
*out_w = vert->z / 32768.0f;
|
2020-11-26 14:02:13 +00:00
|
|
|
|
|
|
|
if (IsWithinTolerance(*out_x, *out_y, x, y))
|
2021-06-12 12:02:28 +00:00
|
|
|
return false;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 14:02:13 +00:00
|
|
|
|
|
|
|
// no valid value can be found anywhere, use the native PSX data
|
|
|
|
*out_x = static_cast<float>(x);
|
|
|
|
*out_y = static_cast<float>(y);
|
|
|
|
*out_w = 1.0f;
|
|
|
|
return false;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instruction register decoding
|
2023-09-03 04:30:26 +00:00
|
|
|
#define op(_instr) (_instr >> 26) // The op part of the instruction register
|
|
|
|
#define func(_instr) ((_instr)&0x3F) // The funct part of the instruction register
|
|
|
|
#define sa(_instr) ((_instr >> 6) & 0x1F) // The sa part of the instruction register
|
2020-08-01 14:25:07 +00:00
|
|
|
#define rd(_instr) ((_instr >> 11) & 0x1F) // The rd part of the instruction register
|
|
|
|
#define rt(_instr) ((_instr >> 16) & 0x1F) // The rt part of the instruction register
|
|
|
|
#define rs(_instr) ((_instr >> 21) & 0x1F) // The rs part of the instruction register
|
2023-09-03 04:30:26 +00:00
|
|
|
#define imm(_instr) (_instr & 0xFFFF) // The immediate part of the instruction register
|
2021-02-17 15:45:32 +00:00
|
|
|
#define imm_sext(_instr) \
|
|
|
|
static_cast<s32>(static_cast<s16>(_instr & 0xFFFF)) // The immediate part of the instruction register
|
2020-08-01 14:25:07 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LW(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// Rt = Mem[Rs + Im]
|
2023-12-06 06:45:17 +00:00
|
|
|
ValidateAndCopyMem(&g_state.pgxp_gpr[rt(instr)], addr, rtVal);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LBx(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = PGXP_value_invalid;
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LH(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2023-09-03 04:30:26 +00:00
|
|
|
// Rt = Mem[Rs + Im] (sign extended)
|
2023-12-06 06:45:17 +00:00
|
|
|
ValidateAndCopyMem16(&g_state.pgxp_gpr[rt(instr)], addr, rtVal, true);
|
2023-09-03 04:30:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LHU(u32 instr, u32 addr, u32 rtVal)
|
2023-09-03 04:30:26 +00:00
|
|
|
{
|
|
|
|
// Rt = Mem[Rs + Im] (zero extended)
|
2023-12-06 06:45:17 +00:00
|
|
|
ValidateAndCopyMem16(&g_state.pgxp_gpr[rt(instr)], addr, rtVal, false);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SB(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2021-06-12 12:02:28 +00:00
|
|
|
WriteMem(&PGXP_value_invalid, addr);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SH(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
PGXP_value* val = &g_state.pgxp_gpr[rt(instr)];
|
2021-06-24 06:52:38 +00:00
|
|
|
|
2020-08-01 14:25:07 +00:00
|
|
|
// validate and copy half value
|
2021-06-24 06:52:38 +00:00
|
|
|
MaskValidate(val, rtVal, 0xFFFF, VALID_0);
|
|
|
|
WriteMem16(val, addr);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SW(u32 instr, u32 addr, u32 rtVal)
|
2020-08-01 14:25:07 +00:00
|
|
|
{
|
|
|
|
// Mem[Rs + Im] = Rt
|
2023-12-06 06:45:17 +00:00
|
|
|
PGXP_value* val = &g_state.pgxp_gpr[rt(instr)];
|
2021-06-24 06:52:38 +00:00
|
|
|
Validate(val, rtVal);
|
|
|
|
WriteMem(val, addr);
|
2020-08-01 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MOVE(u32 rd_and_rs, u32 rsVal)
|
2020-10-04 12:13:33 +00:00
|
|
|
{
|
|
|
|
const u32 Rs = (rd_and_rs & 0xFFu);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[Rs], rsVal);
|
|
|
|
g_state.pgxp_gpr[(rd_and_rs >> 8)] = g_state.pgxp_gpr[Rs];
|
2020-10-04 12:13:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_ADDI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs + Imm (signed)
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
PGXP_value ret = g_state.pgxp_gpr[rs(instr)];
|
2023-10-31 15:32:29 +00:00
|
|
|
|
|
|
|
psx_value tempImm;
|
|
|
|
tempImm.d = SignExtend32(static_cast<u16>(imm(instr)));
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
if (tempImm.d != 0)
|
|
|
|
{
|
|
|
|
ret.x = (float)f16Unsign(ret.x);
|
|
|
|
ret.x += (float)tempImm.w.l;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
// carry on over/underflow
|
|
|
|
float of = (ret.x > USHRT_MAX) ? 1.f : (ret.x < 0) ? -1.f : 0.f;
|
|
|
|
ret.x = (float)f16Sign(ret.x);
|
|
|
|
// ret.x -= of * (USHRT_MAX + 1);
|
|
|
|
ret.y += tempImm.sw.h + of;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
// truncate on overflow/underflow
|
|
|
|
ret.y += (ret.y > SHRT_MAX) ? -(USHRT_MAX + 1) : (ret.y < SHRT_MIN) ? USHRT_MAX + 1 : 0.f;
|
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
|
|
|
g_state.pgxp_gpr[rt(instr)].value = rsVal + imm_sext(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_ANDI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs & Imm
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rtVal = rsVal & imm(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
psx_value vRt;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
vRt.d = rtVal;
|
|
|
|
|
|
|
|
ret.y = 0.f; // remove upper 16-bits
|
|
|
|
|
|
|
|
switch (imm(instr))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// if 0 then x == 0
|
|
|
|
ret.x = 0.f;
|
|
|
|
break;
|
|
|
|
case 0xFFFF:
|
|
|
|
// if saturated then x == x
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// otherwise x is low precision value
|
|
|
|
ret.x = vRt.sw.l;
|
|
|
|
ret.flags |= VALID_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.flags |= VALID_1;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
|
|
|
g_state.pgxp_gpr[rt(instr)].value = rtVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_ORI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs | Imm
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rtVal = rsVal | imm(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
psx_value vRt;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
vRt.d = rtVal;
|
|
|
|
|
|
|
|
switch (imm(instr))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// if 0 then x == x
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// otherwise x is low precision value
|
|
|
|
ret.x = vRt.sw.l;
|
|
|
|
ret.flags |= VALID_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.value = rtVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_XORI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs ^ Imm
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rtVal = rsVal ^ imm(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
psx_value vRt;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
vRt.d = rtVal;
|
|
|
|
|
|
|
|
switch (imm(instr))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// if 0 then x == x
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// otherwise x is low precision value
|
|
|
|
ret.x = vRt.sw.l;
|
|
|
|
ret.flags |= VALID_0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.value = rtVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLTI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs < Imm (signed)
|
|
|
|
psx_value tempImm;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
tempImm.w.h = imm(instr);
|
|
|
|
ret.y = 0.f;
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = (g_state.pgxp_gpr[rs(instr)].x < tempImm.sw.h) ? 1.f : 0.f;
|
2020-08-19 13:26:57 +00:00
|
|
|
ret.flags |= VALID_1;
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = BoolToUInt32(static_cast<s32>(rsVal) < imm_sext(instr));
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLTIU(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Rs < Imm (Unsigned)
|
|
|
|
psx_value tempImm;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
tempImm.w.h = imm(instr);
|
|
|
|
ret.y = 0.f;
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = (f16Unsign(g_state.pgxp_gpr[rs(instr)].x) < tempImm.w.h) ? 1.f : 0.f;
|
2020-08-19 13:26:57 +00:00
|
|
|
ret.flags |= VALID_1;
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = BoolToUInt32(rsVal < imm(instr));
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Load Upper
|
|
|
|
////////////////////////////////////
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_LUI(u32 instr)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rt = Imm << 16
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rt(instr)] = PGXP_value_zero;
|
|
|
|
g_state.pgxp_gpr[rt(instr)].y = (float)(s16)imm(instr);
|
|
|
|
g_state.pgxp_gpr[rt(instr)].value = static_cast<u32>(imm(instr)) << 16;
|
|
|
|
g_state.pgxp_gpr[rt(instr)].flags = VALID_01;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Register Arithmetic
|
|
|
|
////////////////////////////////////
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_ADD(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs + Rt (signed)
|
|
|
|
PGXP_value ret;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-09-03 04:30:26 +00:00
|
|
|
if (rtVal == 0)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2023-09-03 04:30:26 +00:00
|
|
|
}
|
|
|
|
else if (rsVal == 0)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2023-09-03 04:30:26 +00:00
|
|
|
}
|
|
|
|
else
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2020-12-03 15:07:34 +00:00
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-12-03 15:07:34 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-12-03 15:07:34 +00:00
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
ret.x = (float)f16Unsign(ret.x);
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x += (float)f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
// carry on over/underflow
|
|
|
|
float of = (ret.x > USHRT_MAX) ? 1.f : (ret.x < 0) ? -1.f : 0.f;
|
|
|
|
ret.x = (float)f16Sign(ret.x);
|
|
|
|
// ret.x -= of * (USHRT_MAX + 1);
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.y += g_state.pgxp_gpr[rt(instr)].y + of;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
// truncate on overflow/underflow
|
|
|
|
ret.y += (ret.y > SHRT_MAX) ? -(USHRT_MAX + 1) : (ret.y < SHRT_MIN) ? USHRT_MAX + 1 : 0.f;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2020-12-03 15:07:34 +00:00
|
|
|
// TODO: decide which "z/w" component to use
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.halfFlags[0] &= g_state.pgxp_gpr[rt(instr)].halfFlags[0];
|
2020-12-03 15:07:34 +00:00
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
if (!(ret.flags & VALID_2) && (g_state.pgxp_gpr[rt(instr)].flags & VALID_2))
|
2023-11-28 16:00:22 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.z = g_state.pgxp_gpr[rt(instr)].z;
|
2023-11-28 16:00:22 +00:00
|
|
|
ret.flags |= VALID_2;
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = rsVal + rtVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SUB(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs - Rt (signed)
|
|
|
|
PGXP_value ret;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
ret.x = (float)f16Unsign(ret.x);
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x -= (float)f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// carry on over/underflow
|
|
|
|
float of = (ret.x > USHRT_MAX) ? 1.f : (ret.x < 0) ? -1.f : 0.f;
|
|
|
|
ret.x = (float)f16Sign(ret.x);
|
|
|
|
// ret.x -= of * (USHRT_MAX + 1);
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.y -= g_state.pgxp_gpr[rt(instr)].y - of;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// truncate on overflow/underflow
|
|
|
|
ret.y += (ret.y > SHRT_MAX) ? -(USHRT_MAX + 1) : (ret.y < SHRT_MIN) ? USHRT_MAX + 1 : 0.f;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.halfFlags[0] &= g_state.pgxp_gpr[rt(instr)].halfFlags[0];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = rsVal - rtVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
if (!(ret.flags & VALID_2) && (g_state.pgxp_gpr[rt(instr)].flags & VALID_2))
|
2023-11-28 16:00:22 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.z = g_state.pgxp_gpr[rt(instr)].z;
|
2023-11-28 16:00:22 +00:00
|
|
|
ret.flags |= VALID_2;
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ALWAYS_INLINE_RELEASE void CPU::PGXP::CPU_BITWISE(u32 instr, u32 rdVal, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs & Rt
|
|
|
|
psx_value vald, vals, valt;
|
|
|
|
PGXP_value ret;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vald.d = rdVal;
|
|
|
|
vals.d = rsVal;
|
|
|
|
valt.d = rtVal;
|
|
|
|
|
|
|
|
// CPU_reg[rd(instr)].valid = CPU_reg[rs(instr)].valid && CPU_reg[rt(instr)].valid;
|
|
|
|
ret.flags = VALID_01;
|
|
|
|
|
|
|
|
if (vald.w.l == 0)
|
|
|
|
{
|
|
|
|
ret.x = 0.f;
|
|
|
|
}
|
|
|
|
else if (vald.w.l == vals.w.l)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = g_state.pgxp_gpr[rs(instr)].x;
|
|
|
|
ret.compFlags[0] = g_state.pgxp_gpr[rs(instr)].compFlags[0];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
else if (vald.w.l == valt.w.l)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = g_state.pgxp_gpr[rt(instr)].x;
|
|
|
|
ret.compFlags[0] = g_state.pgxp_gpr[rt(instr)].compFlags[0];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.x = (float)vald.sw.l;
|
|
|
|
ret.compFlags[0] = VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vald.w.h == 0)
|
|
|
|
{
|
|
|
|
ret.y = 0.f;
|
|
|
|
}
|
|
|
|
else if (vald.w.h == vals.w.h)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.y = g_state.pgxp_gpr[rs(instr)].y;
|
|
|
|
ret.compFlags[1] &= g_state.pgxp_gpr[rs(instr)].compFlags[1];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
else if (vald.w.h == valt.w.h)
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.y = g_state.pgxp_gpr[rt(instr)].y;
|
|
|
|
ret.compFlags[1] &= g_state.pgxp_gpr[rt(instr)].compFlags[1];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.y = (float)vald.sw.h;
|
|
|
|
ret.compFlags[1] = VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iCB Hack: Force validity if even one half is valid
|
|
|
|
// if ((ret.hFlags & VALID_HALF) || (ret.lFlags & VALID_HALF))
|
|
|
|
// ret.valid = 1;
|
|
|
|
// /iCB Hack
|
|
|
|
|
|
|
|
// Get a valid W
|
2023-12-06 06:45:17 +00:00
|
|
|
if ((g_state.pgxp_gpr[rs(instr)].flags & VALID_2) == VALID_2)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.z = g_state.pgxp_gpr[rs(instr)].z;
|
|
|
|
ret.compFlags[2] = g_state.pgxp_gpr[rs(instr)].compFlags[2];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
2023-12-06 06:45:17 +00:00
|
|
|
else if ((g_state.pgxp_gpr[rt(instr)].flags & VALID_2) == VALID_2)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.z = g_state.pgxp_gpr[rt(instr)].z;
|
|
|
|
ret.compFlags[2] = g_state.pgxp_gpr[rt(instr)].compFlags[2];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
2023-10-31 15:32:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.z = 0.0f;
|
|
|
|
ret.compFlags[2] = 0;
|
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_AND_(u32 instr, u32 rsVal, u32 rtVal)
|
2021-02-17 15:45:32 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs & Rt
|
|
|
|
const u32 rdVal = rsVal & rtVal;
|
|
|
|
CPU_BITWISE(instr, rdVal, rsVal, rtVal);
|
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_OR_(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs | Rt
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rsVal | rtVal;
|
|
|
|
CPU_BITWISE(instr, rdVal, rsVal, rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_XOR_(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs ^ Rt
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rsVal ^ rtVal;
|
|
|
|
CPU_BITWISE(instr, rdVal, rsVal, rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_NOR(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs NOR Rt
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = ~(rsVal | rtVal);
|
|
|
|
CPU_BITWISE(instr, rdVal, rsVal, rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLT(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs < Rt (signed)
|
|
|
|
PGXP_value ret;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
ret.y = 0.f;
|
|
|
|
ret.compFlags[1] = VALID;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = (g_state.pgxp_gpr[rs(instr)].y < g_state.pgxp_gpr[rt(instr)].y) ? 1.f :
|
|
|
|
(f16Unsign(g_state.pgxp_gpr[rs(instr)].x) < f16Unsign(g_state.pgxp_gpr[rt(instr)].x)) ? 1.f :
|
|
|
|
0.f;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = BoolToUInt32(static_cast<s32>(rsVal) < static_cast<s32>(rtVal));
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLTU(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rs < Rt (unsigned)
|
|
|
|
PGXP_value ret;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
ret.y = 0.f;
|
|
|
|
ret.compFlags[1] = VALID;
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret.x = (f16Unsign(g_state.pgxp_gpr[rs(instr)].y) < f16Unsign(g_state.pgxp_gpr[rt(instr)].y)) ? 1.f :
|
|
|
|
(f16Unsign(g_state.pgxp_gpr[rs(instr)].x) < f16Unsign(g_state.pgxp_gpr[rt(instr)].x)) ? 1.f :
|
|
|
|
0.f;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 15:45:32 +00:00
|
|
|
ret.value = BoolToUInt32(rsVal < rtVal);
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Register mult/div
|
|
|
|
////////////////////////////////////
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MULT(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Hi/Lo = Rs * Rt (signed)
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo = g_state.pgxp_hi = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.halfFlags[0] = g_state.pgxp_hi.halfFlags[0] =
|
|
|
|
(g_state.pgxp_gpr[rs(instr)].halfFlags[0] & g_state.pgxp_gpr[rt(instr)].halfFlags[0]);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double xx, xy, yx, yy;
|
|
|
|
double lx = 0, ly = 0, hx = 0, hy = 0;
|
|
|
|
|
|
|
|
// Multiply out components
|
2023-12-06 06:45:17 +00:00
|
|
|
xx = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) * f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
xy = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) * (g_state.pgxp_gpr[rt(instr)].y);
|
|
|
|
yx = (g_state.pgxp_gpr[rs(instr)].y) * f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
yy = (g_state.pgxp_gpr[rs(instr)].y) * (g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// Split values into outputs
|
|
|
|
lx = xx;
|
|
|
|
|
|
|
|
ly = f16Overflow(xx);
|
|
|
|
ly += xy + yx;
|
|
|
|
|
|
|
|
hx = f16Overflow(ly);
|
|
|
|
hx += yy;
|
|
|
|
|
|
|
|
hy = f16Overflow(hx);
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.x = (float)f16Sign(lx);
|
|
|
|
g_state.pgxp_lo.y = (float)f16Sign(ly);
|
|
|
|
g_state.pgxp_hi.x = (float)f16Sign(hx);
|
|
|
|
g_state.pgxp_hi.y = (float)f16Sign(hy);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 14:37:17 +00:00
|
|
|
// compute PSX value
|
|
|
|
const u64 result = static_cast<u64>(static_cast<s64>(SignExtend64(rsVal)) * static_cast<s64>(SignExtend64(rtVal)));
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_hi.value = Truncate32(result >> 32);
|
|
|
|
g_state.pgxp_lo.value = Truncate32(result);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MULTU(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Hi/Lo = Rs * Rt (unsigned)
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo = g_state.pgxp_hi = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.halfFlags[0] = g_state.pgxp_hi.halfFlags[0] =
|
|
|
|
(g_state.pgxp_gpr[rs(instr)].halfFlags[0] & g_state.pgxp_gpr[rt(instr)].halfFlags[0]);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double xx, xy, yx, yy;
|
|
|
|
double lx = 0, ly = 0, hx = 0, hy = 0;
|
|
|
|
|
|
|
|
// Multiply out components
|
2023-12-06 06:45:17 +00:00
|
|
|
xx = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) * f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
xy = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) * f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
|
|
|
yx = f16Unsign(g_state.pgxp_gpr[rs(instr)].y) * f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
yy = f16Unsign(g_state.pgxp_gpr[rs(instr)].y) * f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// Split values into outputs
|
|
|
|
lx = xx;
|
|
|
|
|
|
|
|
ly = f16Overflow(xx);
|
|
|
|
ly += xy + yx;
|
|
|
|
|
|
|
|
hx = f16Overflow(ly);
|
|
|
|
hx += yy;
|
|
|
|
|
|
|
|
hy = f16Overflow(hx);
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.x = (float)f16Sign(lx);
|
|
|
|
g_state.pgxp_lo.y = (float)f16Sign(ly);
|
|
|
|
g_state.pgxp_hi.x = (float)f16Sign(hx);
|
|
|
|
g_state.pgxp_hi.y = (float)f16Sign(hy);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 14:37:17 +00:00
|
|
|
// compute PSX value
|
|
|
|
const u64 result = ZeroExtend64(rsVal) * ZeroExtend64(rtVal);
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_hi.value = Truncate32(result >> 32);
|
|
|
|
g_state.pgxp_lo.value = Truncate32(result);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_DIV(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Lo = Rs / Rt (signed)
|
|
|
|
// Hi = Rs % Rt (signed)
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
//// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo = g_state.pgxp_hi = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.halfFlags[0] = g_state.pgxp_hi.halfFlags[0] =
|
|
|
|
(g_state.pgxp_gpr[rs(instr)].halfFlags[0] & g_state.pgxp_gpr[rt(instr)].halfFlags[0]);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double vs = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) + (g_state.pgxp_gpr[rs(instr)].y) * (double)(1 << 16);
|
|
|
|
double vt = f16Unsign(g_state.pgxp_gpr[rt(instr)].x) + (g_state.pgxp_gpr[rt(instr)].y) * (double)(1 << 16);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double lo = vs / vt;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.y = (float)f16Sign(f16Overflow(lo));
|
|
|
|
g_state.pgxp_lo.x = (float)f16Sign(lo);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double hi = fmod(vs, vt);
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_hi.y = (float)f16Sign(f16Overflow(hi));
|
|
|
|
g_state.pgxp_hi.x = (float)f16Sign(hi);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 14:37:17 +00:00
|
|
|
// compute PSX value
|
|
|
|
if (static_cast<s32>(rtVal) == 0)
|
|
|
|
{
|
|
|
|
// divide by zero
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.value = (static_cast<s32>(rsVal) >= 0) ? UINT32_C(0xFFFFFFFF) : UINT32_C(1);
|
|
|
|
g_state.pgxp_hi.value = static_cast<u32>(static_cast<s32>(rsVal));
|
2021-02-17 14:37:17 +00:00
|
|
|
}
|
|
|
|
else if (rsVal == UINT32_C(0x80000000) && static_cast<s32>(rtVal) == -1)
|
|
|
|
{
|
|
|
|
// unrepresentable
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.value = UINT32_C(0x80000000);
|
|
|
|
g_state.pgxp_hi.value = 0;
|
2021-02-17 14:37:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.value = static_cast<u32>(static_cast<s32>(rsVal) / static_cast<s32>(rtVal));
|
|
|
|
g_state.pgxp_hi.value = static_cast<u32>(static_cast<s32>(rsVal) % static_cast<s32>(rtVal));
|
2021-02-17 14:37:17 +00:00
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_DIVU(u32 instr, u32 rsVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Lo = Rs / Rt (unsigned)
|
|
|
|
// Hi = Rs % Rt (unsigned)
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
//// iCB: Only require one valid input
|
2023-12-06 06:45:17 +00:00
|
|
|
if (((g_state.pgxp_gpr[rt(instr)].flags & VALID_01) != VALID_01) !=
|
|
|
|
((g_state.pgxp_gpr[rs(instr)].flags & VALID_01) != VALID_01))
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
MakeValid(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
|
|
|
MakeValid(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo = g_state.pgxp_hi = g_state.pgxp_gpr[rs(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.halfFlags[0] = g_state.pgxp_hi.halfFlags[0] =
|
|
|
|
(g_state.pgxp_gpr[rs(instr)].halfFlags[0] & g_state.pgxp_gpr[rt(instr)].halfFlags[0]);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double vs = f16Unsign(g_state.pgxp_gpr[rs(instr)].x) + f16Unsign(g_state.pgxp_gpr[rs(instr)].y) * (double)(1 << 16);
|
|
|
|
double vt = f16Unsign(g_state.pgxp_gpr[rt(instr)].x) + f16Unsign(g_state.pgxp_gpr[rt(instr)].y) * (double)(1 << 16);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double lo = vs / vt;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.y = (float)f16Sign(f16Overflow(lo));
|
|
|
|
g_state.pgxp_lo.x = (float)f16Sign(lo);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
double hi = fmod(vs, vt);
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_hi.y = (float)f16Sign(f16Overflow(hi));
|
|
|
|
g_state.pgxp_hi.x = (float)f16Sign(hi);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2021-02-17 14:37:17 +00:00
|
|
|
if (rtVal == 0)
|
|
|
|
{
|
|
|
|
// divide by zero
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.value = UINT32_C(0xFFFFFFFF);
|
|
|
|
g_state.pgxp_hi.value = rsVal;
|
2021-02-17 14:37:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo.value = rsVal / rtVal;
|
|
|
|
g_state.pgxp_hi.value = rsVal % rtVal;
|
2021-02-17 14:37:17 +00:00
|
|
|
}
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Shift operations (sa)
|
|
|
|
////////////////////////////////////
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLL(u32 instr, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt << Sa
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rtVal << sa(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = sa(instr);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
// TODO: Shift flags
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
double y = f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
if (sh >= 32)
|
|
|
|
{
|
|
|
|
x = 0.f;
|
|
|
|
y = 0.f;
|
|
|
|
}
|
|
|
|
else if (sh == 16)
|
|
|
|
{
|
|
|
|
y = f16Sign(x);
|
|
|
|
x = 0.f;
|
|
|
|
}
|
|
|
|
else if (sh >= 16)
|
|
|
|
{
|
|
|
|
y = x * (1 << (sh - 16));
|
|
|
|
y = f16Sign(y);
|
|
|
|
x = 0.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = x * (1 << sh);
|
|
|
|
y = y * (1 << sh);
|
|
|
|
y += f16Overflow(x);
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SRL(u32 instr, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt >> Sa
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rtVal >> sa(instr);
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = sa(instr);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = g_state.pgxp_gpr[rt(instr)].x, y = f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
psx_value iX;
|
|
|
|
iX.d = rtVal;
|
|
|
|
psx_value iY;
|
|
|
|
iY.d = rtVal;
|
|
|
|
|
|
|
|
iX.sd = (iX.sd << 16) >> 16; // remove Y
|
|
|
|
iY.sw.l = iX.sw.h; // overwrite x with sign(x)
|
|
|
|
|
|
|
|
// Shift test values
|
|
|
|
psx_value dX;
|
|
|
|
dX.sd = iX.sd >> sh;
|
|
|
|
psx_value dY;
|
|
|
|
dY.d = iY.d >> sh;
|
|
|
|
|
|
|
|
if (dX.sw.l != iX.sw.h)
|
|
|
|
x = x / (1 << sh);
|
|
|
|
else
|
|
|
|
x = dX.sw.l; // only sign bits left
|
|
|
|
|
|
|
|
if (dY.sw.l != iX.sw.h)
|
|
|
|
{
|
|
|
|
if (sh == 16)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
else if (sh < 16)
|
|
|
|
{
|
|
|
|
x += y * (1 << (16 - sh));
|
2023-12-06 06:45:17 +00:00
|
|
|
if (g_state.pgxp_gpr[rt(instr)].x < 0)
|
2020-08-19 13:26:57 +00:00
|
|
|
x += 1 << (16 - sh);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x += y / (1 << (sh - 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dY.sw.h == 0) || (dY.sw.h == -1))
|
|
|
|
y = dY.sw.h;
|
|
|
|
else
|
|
|
|
y = y / (1 << sh);
|
|
|
|
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SRA(u32 instr, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt >> Sa
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = static_cast<u32>(static_cast<s32>(rtVal) >> sa(instr));
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = sa(instr);
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = g_state.pgxp_gpr[rt(instr)].x, y = g_state.pgxp_gpr[rt(instr)].y;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
psx_value iX;
|
|
|
|
iX.d = rtVal;
|
|
|
|
psx_value iY;
|
|
|
|
iY.d = rtVal;
|
|
|
|
|
|
|
|
iX.sd = (iX.sd << 16) >> 16; // remove Y
|
|
|
|
iY.sw.l = iX.sw.h; // overwrite x with sign(x)
|
|
|
|
|
|
|
|
// Shift test values
|
|
|
|
psx_value dX;
|
|
|
|
dX.sd = iX.sd >> sh;
|
|
|
|
psx_value dY;
|
|
|
|
dY.sd = iY.sd >> sh;
|
|
|
|
|
|
|
|
if (dX.sw.l != iX.sw.h)
|
|
|
|
x = x / (1 << sh);
|
|
|
|
else
|
|
|
|
x = dX.sw.l; // only sign bits left
|
|
|
|
|
|
|
|
if (dY.sw.l != iX.sw.h)
|
|
|
|
{
|
|
|
|
if (sh == 16)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
else if (sh < 16)
|
|
|
|
{
|
|
|
|
x += y * (1 << (16 - sh));
|
2023-12-06 06:45:17 +00:00
|
|
|
if (g_state.pgxp_gpr[rt(instr)].x < 0)
|
2020-08-19 13:26:57 +00:00
|
|
|
x += 1 << (16 - sh);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x += y / (1 << (sh - 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dY.sw.h == 0) || (dY.sw.h == -1))
|
|
|
|
y = dY.sw.h;
|
|
|
|
else
|
|
|
|
y = y / (1 << sh);
|
|
|
|
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
// Shift operations variable
|
|
|
|
////////////////////////////////////
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SLLV(u32 instr, u32 rtVal, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt << Rs
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rtVal << rsVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = rsVal & 0x1F;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = f16Unsign(g_state.pgxp_gpr[rt(instr)].x);
|
|
|
|
double y = f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
if (sh >= 32)
|
|
|
|
{
|
|
|
|
x = 0.f;
|
|
|
|
y = 0.f;
|
|
|
|
}
|
|
|
|
else if (sh == 16)
|
|
|
|
{
|
|
|
|
y = f16Sign(x);
|
|
|
|
x = 0.f;
|
|
|
|
}
|
|
|
|
else if (sh >= 16)
|
|
|
|
{
|
|
|
|
y = x * (1 << (sh - 16));
|
|
|
|
y = f16Sign(y);
|
|
|
|
x = 0.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = x * (1 << sh);
|
|
|
|
y = y * (1 << sh);
|
|
|
|
y += f16Overflow(x);
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SRLV(u32 instr, u32 rtVal, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt >> Sa
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = rtVal >> rsVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = rsVal & 0x1F;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = g_state.pgxp_gpr[rt(instr)].x, y = f16Unsign(g_state.pgxp_gpr[rt(instr)].y);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
psx_value iX;
|
|
|
|
iX.d = rtVal;
|
|
|
|
psx_value iY;
|
|
|
|
iY.d = rtVal;
|
|
|
|
|
|
|
|
iX.sd = (iX.sd << 16) >> 16; // remove Y
|
|
|
|
iY.sw.l = iX.sw.h; // overwrite x with sign(x)
|
|
|
|
|
|
|
|
// Shift test values
|
|
|
|
psx_value dX;
|
|
|
|
dX.sd = iX.sd >> sh;
|
|
|
|
psx_value dY;
|
|
|
|
dY.d = iY.d >> sh;
|
|
|
|
|
|
|
|
if (dX.sw.l != iX.sw.h)
|
|
|
|
x = x / (1 << sh);
|
|
|
|
else
|
|
|
|
x = dX.sw.l; // only sign bits left
|
|
|
|
|
|
|
|
if (dY.sw.l != iX.sw.h)
|
|
|
|
{
|
|
|
|
if (sh == 16)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
else if (sh < 16)
|
|
|
|
{
|
|
|
|
x += y * (1 << (16 - sh));
|
2023-12-06 06:45:17 +00:00
|
|
|
if (g_state.pgxp_gpr[rt(instr)].x < 0)
|
2020-08-19 13:26:57 +00:00
|
|
|
x += 1 << (16 - sh);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x += y / (1 << (sh - 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dY.sw.h == 0) || (dY.sw.h == -1))
|
|
|
|
y = dY.sw.h;
|
|
|
|
else
|
|
|
|
y = y / (1 << sh);
|
|
|
|
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_SRAV(u32 instr, u32 rtVal, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Rt >> Sa
|
2021-02-17 15:45:32 +00:00
|
|
|
const u32 rdVal = static_cast<u32>(static_cast<s32>(rtVal) >> rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
PGXP_value ret;
|
|
|
|
u32 sh = rsVal & 0x1F;
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
ret = g_state.pgxp_gpr[rt(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
double x = g_state.pgxp_gpr[rt(instr)].x, y = g_state.pgxp_gpr[rt(instr)].y;
|
2020-08-19 13:26:57 +00:00
|
|
|
|
|
|
|
psx_value iX;
|
|
|
|
iX.d = rtVal;
|
|
|
|
psx_value iY;
|
|
|
|
iY.d = rtVal;
|
|
|
|
|
|
|
|
iX.sd = (iX.sd << 16) >> 16; // remove Y
|
|
|
|
iY.sw.l = iX.sw.h; // overwrite x with sign(x)
|
|
|
|
|
|
|
|
// Shift test values
|
|
|
|
psx_value dX;
|
|
|
|
dX.sd = iX.sd >> sh;
|
|
|
|
psx_value dY;
|
|
|
|
dY.sd = iY.sd >> sh;
|
|
|
|
|
|
|
|
if (dX.sw.l != iX.sw.h)
|
|
|
|
x = x / (1 << sh);
|
|
|
|
else
|
|
|
|
x = dX.sw.l; // only sign bits left
|
|
|
|
|
|
|
|
if (dY.sw.l != iX.sw.h)
|
|
|
|
{
|
|
|
|
if (sh == 16)
|
|
|
|
{
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
else if (sh < 16)
|
|
|
|
{
|
|
|
|
x += y * (1 << (16 - sh));
|
2023-12-06 06:45:17 +00:00
|
|
|
if (g_state.pgxp_gpr[rt(instr)].x < 0)
|
2020-08-19 13:26:57 +00:00
|
|
|
x += 1 << (16 - sh);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x += y / (1 << (sh - 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dY.sw.h == 0) || (dY.sw.h == -1))
|
|
|
|
y = dY.sw.h;
|
|
|
|
else
|
|
|
|
y = y / (1 << sh);
|
|
|
|
|
|
|
|
x = f16Sign(x);
|
|
|
|
y = f16Sign(y);
|
|
|
|
|
|
|
|
ret.x = (float)x;
|
|
|
|
ret.y = (float)y;
|
|
|
|
|
|
|
|
ret.value = rdVal;
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = ret;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MFHI(u32 instr, u32 hiVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Hi
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_hi, hiVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = g_state.pgxp_hi;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MTHI(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Hi = Rd
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_hi = g_state.pgxp_gpr[rd(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MFLO(u32 instr, u32 loVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Rd = Lo
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_lo, loVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_gpr[rd(instr)] = g_state.pgxp_lo;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MTLO(u32 instr, u32 rsVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// Lo = Rd
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rs(instr)], rsVal);
|
2020-08-19 13:26:57 +00:00
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
g_state.pgxp_lo = g_state.pgxp_gpr[rd(instr)];
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MFC0(u32 instr, u32 rdVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// CPU[Rt] = CP0[Rd]
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_cop0[rd(instr)], rdVal);
|
|
|
|
g_state.pgxp_gpr[rt(instr)] = g_state.pgxp_cop0[rd(instr)];
|
|
|
|
g_state.pgxp_gpr[rt(instr)].value = rdVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 06:45:17 +00:00
|
|
|
void CPU::PGXP::CPU_MTC0(u32 instr, u32 rdVal, u32 rtVal)
|
2020-08-19 13:26:57 +00:00
|
|
|
{
|
|
|
|
// CP0[Rd] = CPU[Rt]
|
2023-12-06 06:45:17 +00:00
|
|
|
Validate(&g_state.pgxp_gpr[rt(instr)], rtVal);
|
|
|
|
g_state.pgxp_cop0[rd(instr)] = g_state.pgxp_gpr[rt(instr)];
|
|
|
|
g_state.pgxp_cop0[rd(instr)].value = rdVal;
|
2020-08-19 13:26:57 +00:00
|
|
|
}
|