Files
ARMSX2/pcsx2/GS.cpp
T

344 lines
8.6 KiB
C++
Raw Permalink Normal View History

// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team
2024-07-30 13:42:36 +02:00
// SPDX-License-Identifier: GPL-3.0+
#include "Counters.h"
2023-06-24 17:46:36 +10:00
#include "Common.h"
2021-09-16 20:54:06 +10:00
#include "Config.h"
2023-06-24 17:46:36 +10:00
#include "Gif_Unit.h"
#include "MTGS.h"
#include "VMManager.h"
2023-06-24 17:46:36 +10:00
#include <list>
2021-09-03 01:23:59 -05:00
alignas(16) u8 g_RealGSMem[Ps2MemSize::GSregs];
2022-01-09 19:21:59 +10:00
static bool s_GSRegistersWritten = false;
2021-08-30 03:10:48 -04:00
void gsSetVideoMode(GS_VideoMode mode)
{
gsVideoMode = mode;
2023-04-05 20:27:45 +01:00
UpdateVSyncRate(false);
}
2021-05-14 17:05:50 +02:00
// Make sure framelimiter options are in sync with GS capabilities.
void gsReset()
{
2023-06-24 17:46:36 +10:00
MTGS::ResetGS(true);
2023-01-31 09:59:27 +00:00
gsVideoMode = GS_VideoMode::Uninitialized;
std::memset(g_RealGSMem, 0, sizeof(g_RealGSMem));
2023-04-05 20:27:45 +01:00
UpdateVSyncRate(true);
}
2010-08-09 04:10:38 +00:00
static __fi void gsCSRwrite( const tGS_CSR& csr )
{
if (csr.RESET) {
GUNIT_WARN("GUNIT_WARN: csr.RESET");
//Console.Warning( "csr.RESET" );
2011-07-24 13:02:50 +00:00
//gifUnit.Reset(true); // Don't think gif should be reset...
gifUnit.gsSIGNAL.queued = false;
2023-01-31 09:59:27 +00:00
gifUnit.gsFINISH.gsFINISHFired = true;
gifUnit.gsFINISH.gsFINISHPending = false;
// Privilage registers also reset.
std::memset(g_RealGSMem, 0, sizeof(g_RealGSMem));
GSIMR.reset();
CSRreg.Reset();
2023-06-24 17:49:30 +10:00
MTGS::ResetGS(false);
}
if(csr.FLUSH)
{
// Our emulated GS has no FIFO, but if it did, it would flush it here...
2009-10-04 08:27:27 +00:00
//Console.WriteLn("GS_CSR FLUSH GS fifo: %x (CSRr=%x)", value, GSCSRr);
}
if(csr.SIGNAL)
{
const bool resume = CSRreg.SIGNAL;
// SIGNAL : What's not known here is whether or not the SIGID register should be updated
// here or when the IMR is cleared (below).
2011-07-24 13:02:50 +00:00
GUNIT_LOG("csr.SIGNAL");
if (gifUnit.gsSIGNAL.queued) {
//DevCon.Warning("Firing pending signal");
GSSIGLBLID.SIGID = (GSSIGLBLID.SIGID & ~gifUnit.gsSIGNAL.data[1])
| (gifUnit.gsSIGNAL.data[0]&gifUnit.gsSIGNAL.data[1]);
if (!GSIMR.SIGMSK) gsIrq();
2011-07-24 22:24:10 +00:00
CSRreg.SIGNAL = true; // Just to be sure :p
2011-07-24 13:02:50 +00:00
}
else CSRreg.SIGNAL = false;
gifUnit.gsSIGNAL.queued = false;
if (resume)
gifUnit.Execute(false, true); // Resume paused transfers
}
if (csr.FINISH) {
CSRreg.FINISH = false;
gifUnit.gsFINISH.gsFINISHFired = false; //Clear the previously fired FINISH (YS, Indiecar 2005, MGS3)
gifUnit.gsFINISH.gsFINISHPending = false;
}
if(csr.HSINT) CSRreg.HSINT = false;
if(csr.VSINT) CSRreg.VSINT = false;
if(csr.EDWINT) CSRreg.EDWINT = false;
}
2010-08-09 04:10:38 +00:00
static __fi void IMRwrite(u32 value)
{
2011-07-24 22:24:10 +00:00
GUNIT_LOG("IMRwrite()");
if (CSRreg.GetInterruptMask() & (~value & GSIMR._u32) >> 8)
gsIrq();
GSIMR._u32 = (value & 0x1f00)|0x6000;
}
2010-08-09 04:10:38 +00:00
__fi void gsWrite8(u32 mem, u8 value)
{
switch (mem)
{
// CSR 8-bit write handlers.
2011-07-24 13:02:50 +00:00
// I'm quite sure these would just write the CSR portion with the other
// bits set to 0 (no action). The previous implementation masked the 8-bit
// write value against the previous CSR write value, but that really doesn't
// make any sense, given that the real hardware's CSR circuit probably has no
// real "memory" where it saves anything. (for example, you can't write to
// and change the GS revision or ID portions -- they're all hard wired.) --air
2020-12-19 02:29:24 +00:00
2009-10-06 09:08:36 +00:00
case GS_CSR: // GS_CSR
gsCSRwrite( tGS_CSR((u32)value) ); break;
2009-10-06 09:08:36 +00:00
case GS_CSR + 1: // GS_CSR
gsCSRwrite( tGS_CSR(((u32)value) << 8) ); break;
2009-10-06 09:08:36 +00:00
case GS_CSR + 2: // GS_CSR
gsCSRwrite( tGS_CSR(((u32)value) << 16) ); break;
2009-10-06 09:08:36 +00:00
case GS_CSR + 3: // GS_CSR
gsCSRwrite( tGS_CSR(((u32)value) << 24) ); break;
default:
*PS2GS_BASE(mem) = value;
break;
}
GIF_LOG("GS write 8 at %8.8lx with data %8.8lx", mem, value);
}
//////////////////////////////////////////////////////////////////////////
// GS Write 16 bit
2010-08-09 04:10:38 +00:00
__fi void gsWrite16(u32 mem, u16 value)
{
GIF_LOG("GS write 16 at %8.8lx with data %8.8lx", mem, value);
switch (mem)
{
// See note above about CSR 8 bit writes, and handling them as zero'd bits
// for all but the written parts.
case GS_CSR:
gsCSRwrite( tGS_CSR((u32)value) );
return; // do not write to MTGS memory
case GS_CSR+2:
gsCSRwrite( tGS_CSR(((u32)value) << 16) );
return; // do not write to MTGS memory
case GS_IMR:
IMRwrite(value);
return; // do not write to MTGS memory
}
*(u16*)PS2GS_BASE(mem) = value;
}
//////////////////////////////////////////////////////////////////////////
// GS Write 32 bit
2010-08-09 04:10:38 +00:00
__fi void gsWrite32(u32 mem, u32 value)
{
pxAssume( (mem & 3) == 0 );
GIF_LOG("GS write 32 at %8.8lx with data %8.8lx", mem, value);
switch (mem)
{
case GS_CSR:
gsCSRwrite(tGS_CSR(value));
return;
case GS_IMR:
IMRwrite(value);
return;
}
*(u32*)PS2GS_BASE(mem) = value;
}
//////////////////////////////////////////////////////////////////////////
// GS Write 64 bit
2022-10-12 23:57:53 +10:00
void gsWrite64_generic( u32 mem, u64 value )
{
2022-10-12 23:57:53 +10:00
GIF_LOG("GS Write64 at %8.8lx with data %8.8x_%8.8x", mem, (u32)(value >> 32), (u32)value);
2022-10-12 23:57:53 +10:00
std::memcpy(PS2GS_BASE(mem), &value, sizeof(value));
}
2022-10-12 23:57:53 +10:00
void gsWrite64_page_00( u32 mem, u64 value )
{
2022-01-09 19:21:59 +10:00
s_GSRegistersWritten |= (mem == GS_DISPFB1 || mem == GS_DISPFB2 || mem == GS_PMODE);
bool reqUpdate = false;
if (mem == GS_SMODE1 || mem == GS_SMODE2)
{
if (value != *(u64*)PS2GS_BASE(mem))
reqUpdate = true;
}
gsWrite64_generic( mem, value );
if (reqUpdate)
UpdateVSyncRate(false);
}
2022-10-12 23:57:53 +10:00
void gsWrite64_page_01( u32 mem, u64 value )
{
2022-10-12 23:57:53 +10:00
GIF_LOG("GS Write64 at %8.8lx with data %8.8x_%8.8x", mem, (u32)(value >> 32), (u32)value);
switch( mem )
{
2011-07-24 13:02:50 +00:00
case GS_BUSDIR:
2011-07-24 22:24:10 +00:00
2022-10-12 23:57:53 +10:00
gifUnit.stat.DIR = static_cast<u32>(value) & 1;
if (gifUnit.stat.DIR) { // Assume will do local->host transfer
gifUnit.stat.OPH = true; // Should we set OPH here?
gifUnit.FlushToMTGS(); // Send any pending GS Primitives to the GS
GUNIT_LOG("Busdir - GS->EE Download");
}
else {
GUNIT_LOG("Busdir - EE->GS Upload");
2011-07-24 22:24:10 +00:00
}
gsWrite64_generic( mem, value );
2010-06-22 23:10:40 +00:00
return;
case GS_CSR:
2022-10-12 23:57:53 +10:00
gsCSRwrite(tGS_CSR(value));
return;
case GS_IMR:
2022-10-12 23:57:53 +10:00
IMRwrite(static_cast<u32>(value));
return;
}
gsWrite64_generic( mem, value );
}
//////////////////////////////////////////////////////////////////////////
// GS Write 128 bit
2022-10-12 23:57:53 +10:00
void TAKES_R128 gsWrite128_page_00( u32 mem, r128 value )
{
gsWrite128_generic( mem, value );
}
2022-10-12 23:57:53 +10:00
void TAKES_R128 gsWrite128_page_01( u32 mem, r128 value )
{
switch( mem )
{
case GS_CSR:
2022-10-12 23:57:53 +10:00
gsCSRwrite(r128_to_u32(value));
return;
case GS_IMR:
2022-10-12 23:57:53 +10:00
IMRwrite(r128_to_u32(value));
return;
}
gsWrite128_generic( mem, value );
}
2022-10-12 23:57:53 +10:00
void TAKES_R128 gsWrite128_generic( u32 mem, r128 value )
{
2022-10-12 23:57:53 +10:00
alignas(16) const u128 uvalue = r128_to_u128(value);
GIF_LOG("GS Write128 at %8.8lx with data %8.8x_%8.8x_%8.8x_%8.8x", mem,
2022-10-12 23:57:53 +10:00
uvalue._u32[3], uvalue._u32[2], uvalue._u32[1], uvalue._u32[0]);
2022-10-12 23:57:53 +10:00
r128_store(PS2GS_BASE(mem), value);
}
2010-08-09 04:10:38 +00:00
__fi u8 gsRead8(u32 mem)
{
GIF_LOG("GS read 8 from %8.8lx value: %8.8lx", mem, *(u8*)PS2GS_BASE(mem));
2020-12-19 02:29:24 +00:00
switch (mem & ~0xF)
{
case GS_SIGLBLID:
return *(u8*)PS2GS_BASE(mem);
default: // Only SIGLBLID and CSR are readable, everything else mirrors CSR
return *(u8*)PS2GS_BASE(GS_CSR + (mem & 0xF));
}
}
2010-08-09 04:10:38 +00:00
__fi u16 gsRead16(u32 mem)
{
GIF_LOG("GS read 16 from %8.8lx value: %8.8lx", mem, *(u16*)PS2GS_BASE(mem));
2020-12-19 02:29:24 +00:00
switch (mem & ~0xF)
{
case GS_SIGLBLID:
return *(u16*)PS2GS_BASE(mem);
default: // Only SIGLBLID and CSR are readable, everything else mirrors CSR
return *(u16*)PS2GS_BASE(GS_CSR + (mem & 0x7));
2020-12-19 02:29:24 +00:00
}
}
2010-08-09 04:10:38 +00:00
__fi u32 gsRead32(u32 mem)
{
GIF_LOG("GS read 32 from %8.8lx value: %8.8lx", mem, *(u32*)PS2GS_BASE(mem));
2020-12-19 02:29:24 +00:00
switch (mem & ~0xF)
{
case GS_SIGLBLID:
return *(u32*)PS2GS_BASE(mem);
default: // Only SIGLBLID and CSR are readable, everything else mirrors CSR
return *(u32*)PS2GS_BASE(GS_CSR + (mem & 0xC));
2020-12-19 02:29:24 +00:00
}
}
2010-08-09 04:10:38 +00:00
__fi u64 gsRead64(u32 mem)
{
// fixme - PS2GS_BASE(mem+4) = (g_RealGSMem+(mem + 4 & 0x13ff))
GIF_LOG("GS read 64 from %8.8lx value: %8.8lx_%8.8lx", mem, *(u32*)PS2GS_BASE(mem+4), *(u32*)PS2GS_BASE(mem) );
2020-12-19 02:29:24 +00:00
switch (mem & ~0xF)
{
case GS_SIGLBLID:
return *(u64*)PS2GS_BASE(mem);
default: // Only SIGLBLID and CSR are readable, everything else mirrors CSR
return *(u64*)PS2GS_BASE(GS_CSR + (mem & 0x8));
2020-12-19 02:29:24 +00:00
}
}
__fi u128 gsNonMirroredRead(u32 mem)
{
return *(u128*)PS2GS_BASE(mem);
}
void gsIrq() {
hwIntcIrq(INTC_GS);
}
//These are done at VSync Start. Drawing is done when VSync is off, then output the screen when Vsync is on
//The GS needs to be told at the start of a vsync else it loses half of its picture (could be responsible for some halfscreen issues)
//We got away with it before i think due to our awful GS timing, but now we have it right (ish)
void gsPostVsyncStart()
{
//gifUnit.FlushToMTGS(); // Needed for some (broken?) homebrew game loaders
2022-01-09 19:21:59 +10:00
const bool registers_written = s_GSRegistersWritten;
s_GSRegistersWritten = false;
2023-06-24 17:46:36 +10:00
MTGS::PostVsyncStart(registers_written);
}
2022-12-28 21:13:38 +10:00
bool SaveStateBase::gsFreeze()
{
FreezeMem(PS2MEM_GS, 0x2000);
Freeze(gsVideoMode);
2022-12-28 21:13:38 +10:00
return IsOkay();
}
2021-12-19 00:43:50 +10:00