mirror of
https://github.com/jpd002/Play-.git
synced 2025-04-28 13:47:57 +03:00

Some checks failed
Build macOS / build_macos (push) Has been cancelled
Build Android / build_android (apk) (push) Has been cancelled
Build Android / build_android (libretro) (push) Has been cancelled
Build Linux ARM32 / build_linux_arm32 (push) Has been cancelled
Build Linux ARM64 / build_linux_arm64 (push) Has been cancelled
Build Windows Psf / build_windows_psf (off, x86_64, Visual Studio 16 2019, installer64.nsi, x64) (push) Has been cancelled
Build Windows Psf / build_windows_psf (on, x86_64, Visual Studio 16 2019, installer64.nsi, x64) (push) Has been cancelled
Build Windows / build_windows (x86_32, Visual Studio 16 2019, installer32.nsi, win32_msvc2019, Win32) (push) Has been cancelled
Build Windows / build_windows (x86_64, Visual Studio 16 2019, installer64.nsi, win64_msvc2019_64, x64) (push) Has been cancelled
Check Format / run_clangformat (push) Has been cancelled
Build iOS / build_ios (push) Has been cancelled
Build JavaScript / build_js (push) Has been cancelled
Build Linux / build_linux (push) Has been cancelled
175 lines
5.3 KiB
C++
175 lines
5.3 KiB
C++
#include "Iop_SifMan.h"
|
|
#include <cstring>
|
|
#include "Iop_Sysmem.h"
|
|
#include "../MIPSAssembler.h"
|
|
#include "Log.h"
|
|
|
|
#define LOG_NAME ("iop_sifman")
|
|
|
|
#define FUNCTION_SIFINIT "SifInit"
|
|
#define FUNCTION_SIFSETDMA "SifSetDma"
|
|
#define FUNCTION_SIFDMASTAT "SifDmaStat"
|
|
#define FUNCTION_SIFCHECKINIT "SifCheckInit"
|
|
#define FUNCTION_SIFSETDMACALLBACK "SifSetDmaCallback"
|
|
|
|
using namespace Iop;
|
|
|
|
std::string CSifMan::GetId() const
|
|
{
|
|
return "sifman";
|
|
}
|
|
|
|
std::string CSifMan::GetFunctionName(unsigned int functionId) const
|
|
{
|
|
switch(functionId)
|
|
{
|
|
case 5:
|
|
return FUNCTION_SIFINIT;
|
|
break;
|
|
case 7:
|
|
return FUNCTION_SIFSETDMA;
|
|
break;
|
|
case 8:
|
|
return FUNCTION_SIFDMASTAT;
|
|
break;
|
|
case 29:
|
|
return FUNCTION_SIFCHECKINIT;
|
|
break;
|
|
case 32:
|
|
return FUNCTION_SIFSETDMACALLBACK;
|
|
break;
|
|
}
|
|
return "unknown";
|
|
}
|
|
|
|
void CSifMan::Invoke(CMIPS& context, unsigned int functionId)
|
|
{
|
|
switch(functionId)
|
|
{
|
|
case 7:
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(SifSetDma(
|
|
context.m_State.nGPR[CMIPS::A0].nV0,
|
|
context.m_State.nGPR[CMIPS::A1].nV0));
|
|
break;
|
|
case 8:
|
|
context.m_State.nGPR[CMIPS::V0].nV0 = SifDmaStat(context.m_State.nGPR[CMIPS::A0].nV0);
|
|
break;
|
|
case 29:
|
|
context.m_State.nGPR[CMIPS::V0].nV0 = SifCheckInit();
|
|
break;
|
|
case 32:
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(SifSetDmaCallback(
|
|
context,
|
|
context.m_State.nGPR[CMIPS::A0].nV0,
|
|
context.m_State.nGPR[CMIPS::A1].nV0,
|
|
context.m_State.nGPR[CMIPS::A2].nV0,
|
|
context.m_State.nGPR[CMIPS::A3].nV0));
|
|
break;
|
|
default:
|
|
CLog::GetInstance().Warn(LOG_NAME, "%08X: Unknown function (%d) called.\r\n", context.m_State.nPC, functionId);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CSifMan::PrepareModuleData(uint8* ram, CSysmem& sysMem)
|
|
{
|
|
assert(m_moduleData == nullptr);
|
|
assert(m_sifSetDmaCallbackHandlerAddr == 0);
|
|
|
|
uint32 moduleDataAddr = sysMem.AllocateMemory(sizeof(MODULEDATA), 0, 0);
|
|
|
|
m_moduleData = reinterpret_cast<MODULEDATA*>(ram + moduleDataAddr);
|
|
m_sifSetDmaCallbackHandlerAddr = moduleDataAddr + offsetof(MODULEDATA, sifSetDmaCallbackHandler);
|
|
|
|
//Assemble handler
|
|
{
|
|
CMIPSAssembler assembler(reinterpret_cast<uint32*>(ram + m_sifSetDmaCallbackHandlerAddr));
|
|
|
|
assembler.ADDIU(CMIPS::SP, CMIPS::SP, 0xFFF0);
|
|
//SP + 0x00 is the space to store A0 by handler
|
|
assembler.SW(CMIPS::RA, 0x04, CMIPS::SP);
|
|
assembler.SW(CMIPS::S0, 0x08, CMIPS::SP);
|
|
|
|
assembler.JALR(CMIPS::A1);
|
|
assembler.ADDU(CMIPS::S0, CMIPS::V0, CMIPS::R0);
|
|
|
|
assembler.ADDU(CMIPS::V0, CMIPS::S0, CMIPS::R0);
|
|
|
|
assembler.LW(CMIPS::S0, 0x08, CMIPS::SP);
|
|
assembler.LW(CMIPS::RA, 0x04, CMIPS::SP);
|
|
assembler.JR(CMIPS::RA);
|
|
assembler.ADDIU(CMIPS::SP, CMIPS::SP, 0x0010);
|
|
|
|
assert((assembler.GetProgramSize() * 4) <= SIFSETDMACALLBACK_HANDLER_SIZE);
|
|
}
|
|
|
|
memset(m_moduleData->dmaTransferTimes, 0, sizeof(m_moduleData->dmaTransferTimes));
|
|
m_moduleData->nextDmaTransferIdx = 0;
|
|
}
|
|
|
|
void CSifMan::CountTicks(int32 ticks)
|
|
{
|
|
for(int i = 0; i < DMA_TRANSFER_TIMES_SIZE; i++)
|
|
{
|
|
m_moduleData->dmaTransferTimes[i] = std::max(0, m_moduleData->dmaTransferTimes[i] - ticks);
|
|
}
|
|
}
|
|
|
|
uint32 CSifMan::SifSetDma(uint32 structAddr, uint32 count)
|
|
{
|
|
CLog::GetInstance().Print(LOG_NAME, FUNCTION_SIFSETDMA "(structAddr = 0x%08X, count = %d);\r\n",
|
|
structAddr, count);
|
|
|
|
//Give our transfer some delay time before we report it's complete
|
|
//This is needed because RenderWare FS code doesn't expect SIF CMD callbacks to
|
|
//arrive so quickly
|
|
//Call of Cthulhu: Destiny's End is also sensitive to timings.
|
|
uint32 transferIdx = m_moduleData->nextDmaTransferIdx;
|
|
assert(m_moduleData->dmaTransferTimes[transferIdx] == 0);
|
|
m_moduleData->dmaTransferTimes[transferIdx] = 0x400;
|
|
m_moduleData->nextDmaTransferIdx = (m_moduleData->nextDmaTransferIdx + 1) % DMA_TRANSFER_TIMES_SIZE;
|
|
|
|
ExecuteSifDma(structAddr, count);
|
|
return SIFDMA_XFER_ID + transferIdx;
|
|
}
|
|
|
|
uint32 CSifMan::SifDmaStat(uint32 transferId)
|
|
{
|
|
CLog::GetInstance().Print(LOG_NAME, FUNCTION_SIFDMASTAT "(transferId = %X);\r\n",
|
|
transferId);
|
|
int32 transferIdx = transferId - SIFDMA_XFER_ID;
|
|
bool isValidTransferId = (transferId != 0) && (transferIdx >= 0) && (transferIdx < DMA_TRANSFER_TIMES_SIZE);
|
|
if(!isValidTransferId)
|
|
{
|
|
CLog::GetInstance().Warn(LOG_NAME, "SifDmaStat: Provided invalid transfer id %d.\r\n", transferId);
|
|
}
|
|
if(isValidTransferId && m_moduleData->dmaTransferTimes[transferIdx] != 0)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
uint32 CSifMan::SifCheckInit()
|
|
{
|
|
CLog::GetInstance().Print(LOG_NAME, FUNCTION_SIFCHECKINIT "();\r\n");
|
|
// Since we don't handle the init call, always return true for check init.
|
|
return 1;
|
|
}
|
|
|
|
uint32 CSifMan::SifSetDmaCallback(CMIPS& context, uint32 structAddr, uint32 count, uint32 callbackPtr, uint32 callbackParam)
|
|
{
|
|
CLog::GetInstance().Print(LOG_NAME, FUNCTION_SIFSETDMACALLBACK "(structAddr = 0x%08X, count = %d, callbackPtr = 0x%08X, callbackParam = 0x%08X);\r\n",
|
|
structAddr, count, callbackPtr, callbackParam);
|
|
|
|
//Modify context so we can execute the callback function
|
|
context.m_State.nPC = m_sifSetDmaCallbackHandlerAddr;
|
|
context.m_State.nGPR[CMIPS::A0].nV0 = callbackParam;
|
|
context.m_State.nGPR[CMIPS::A1].nV0 = callbackPtr;
|
|
|
|
ExecuteSifDma(structAddr, count);
|
|
return SIFDMA_XFER_ID;
|
|
}
|