Play-/Source/iop/Iop_Dynamic.cpp

76 lines
2 KiB
C++
Raw Normal View History

2019-08-17 13:51:31 -04:00
#include <cstring>
#include "Iop_Dynamic.h"
2024-07-22 12:54:35 -04:00
#include "Log.h"
#define LOG_NAME ("iop_dynamic")
using namespace Iop;
2024-07-22 12:54:35 -04:00
static constexpr uint32 g_exportModuleNameOffset = 3;
static constexpr uint32 g_exportFctTableOffset = 5;
CDynamic::CDynamic(const uint32* exportTable)
2018-04-30 21:01:23 +01:00
: m_exportTable(exportTable)
2021-01-25 20:50:57 -05:00
{
m_name = GetDynamicModuleName(exportTable);
2024-07-22 12:54:35 -04:00
m_functionCount = GetDynamicModuleExportCount(exportTable);
2021-01-25 20:50:57 -05:00
}
2024-07-22 12:54:35 -04:00
std::string CDynamic::GetDynamicModuleName(const uint32* exportTable)
{
//Name is 8 bytes long without zero, so we need to make sure it's properly null-terminated
const unsigned int nameLength = 8;
char name[nameLength + 1];
memset(name, 0, nameLength + 1);
2024-07-22 12:54:35 -04:00
memcpy(name, reinterpret_cast<const char*>(exportTable + g_exportModuleNameOffset), nameLength);
2021-01-25 20:50:57 -05:00
return name;
}
2024-07-22 12:54:35 -04:00
uint32 CDynamic::GetDynamicModuleExportCount(const uint32* exportTable)
{
//Export tables are supposed to finish with a 0 value.
uint32 functionCount = 0;
while(exportTable[g_exportFctTableOffset + functionCount] != 0)
{
functionCount++;
if(functionCount >= 1000)
{
CLog::GetInstance().Warn(LOG_NAME, "Export count exceeded threshold of %d functions. Bailing.\r\n", functionCount);
break;
}
}
return functionCount;
}
std::string CDynamic::GetId() const
{
return m_name;
}
std::string CDynamic::GetFunctionName(unsigned int functionId) const
{
char functionName[256];
sprintf(functionName, "unknown_%04X", functionId);
return functionName;
}
void CDynamic::Invoke(CMIPS& context, unsigned int functionId)
{
2024-07-22 12:54:35 -04:00
if(functionId < m_functionCount)
{
uint32 functionAddress = m_exportTable[g_exportFctTableOffset + functionId];
context.m_State.nGPR[CMIPS::RA].nD0 = context.m_State.nPC;
context.m_State.nPC = functionAddress;
}
else
{
CLog::GetInstance().Warn(LOG_NAME, "Failed to find export %d for module '%s'.\r\n",
functionId, m_name.c_str());
}
}
2024-07-22 12:54:35 -04:00
const uint32* CDynamic::GetExportTable() const
{
return m_exportTable;
}