2023-08-16 23:29:22 +00:00
|
|
|
#include <Common/MemoryManagement.h>
|
|
|
|
#include <Project64-rsp-core/RSPInfo.h>
|
2023-09-28 02:22:06 +00:00
|
|
|
#include <Project64-rsp-core/cpu/RSPCpu.h>
|
2023-08-16 23:29:22 +00:00
|
|
|
#include <Project64-rsp-core/cpu/RSPRegisters.h>
|
|
|
|
#include <Settings/Settings.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-06-27 06:55:13 +00:00
|
|
|
#include <zlib/zlib.h>
|
2023-08-16 23:29:22 +00:00
|
|
|
|
2023-06-01 11:46:23 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MaxMaps = 32
|
|
|
|
};
|
2016-01-27 09:11:59 +00:00
|
|
|
|
2023-08-10 04:46:57 +00:00
|
|
|
uint32_t NoOfMaps, MapsCRC[MaxMaps];
|
2023-06-01 07:41:26 +00:00
|
|
|
uint32_t Table;
|
2023-08-16 23:29:22 +00:00
|
|
|
uint8_t *RecompCode, *RecompCodeSecondary, *RecompPos, *JumpTables;
|
2016-01-27 09:11:59 +00:00
|
|
|
void ** JumpTable;
|
|
|
|
|
2024-07-20 09:40:36 +00:00
|
|
|
extern uint8_t *pLastSecondary, *pLastPrimary;
|
|
|
|
|
2023-06-01 11:46:23 +00:00
|
|
|
int AllocateMemory(void)
|
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
if (RecompCode == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2024-07-12 05:29:16 +00:00
|
|
|
RecompCode = (uint8_t *)AllocateAddressSpace(0x00800004);
|
|
|
|
RecompCode = (uint8_t *)CommitMemory(RecompCode, 0x00800000, MEM_EXECUTE_READWRITE);
|
2023-06-01 11:46:23 +00:00
|
|
|
|
2023-08-16 23:29:22 +00:00
|
|
|
if (RecompCode == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
g_Notify->DisplayError("Not enough memory for RSP RecompCode!");
|
2023-06-29 02:59:07 +00:00
|
|
|
return false;
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:29:22 +00:00
|
|
|
if (RecompCodeSecondary == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
RecompCodeSecondary = (uint8_t *)AllocateAddressSpace(0x00200004);
|
2023-08-17 02:07:03 +00:00
|
|
|
RecompCodeSecondary = (uint8_t *)CommitMemory(RecompCodeSecondary, 0x00200000, MEM_EXECUTE_READWRITE);
|
2023-08-16 23:29:22 +00:00
|
|
|
if (RecompCodeSecondary == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
g_Notify->DisplayError("Not enough memory for RSP RecompCode Secondary!");
|
2023-06-29 02:59:07 +00:00
|
|
|
return false;
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:29:22 +00:00
|
|
|
if (JumpTables == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
JumpTables = (uint8_t *)AllocateAddressSpace(0x1000 * MaxMaps);
|
2023-08-17 02:07:03 +00:00
|
|
|
JumpTables = (uint8_t *)CommitMemory(JumpTables, 0x1000 * MaxMaps, MEM_READWRITE);
|
2023-08-16 23:29:22 +00:00
|
|
|
if (JumpTables == nullptr)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
g_Notify->DisplayError("Not enough memory for jump table!");
|
2023-06-29 02:59:07 +00:00
|
|
|
return false;
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JumpTable = (void **)JumpTables;
|
|
|
|
RecompPos = RecompCode;
|
|
|
|
NoOfMaps = 0;
|
2023-06-29 02:59:07 +00:00
|
|
|
return true;
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeMemory(void)
|
|
|
|
{
|
2024-07-12 05:29:16 +00:00
|
|
|
FreeAddressSpace(RecompCode, 0x00800004);
|
2023-08-16 23:29:22 +00:00
|
|
|
FreeAddressSpace(JumpTable, 0x1000 * MaxMaps);
|
|
|
|
FreeAddressSpace(RecompCodeSecondary, 0x00200004);
|
2023-06-01 11:46:23 +00:00
|
|
|
|
2023-08-16 23:29:22 +00:00
|
|
|
RecompCode = nullptr;
|
|
|
|
JumpTables = nullptr;
|
|
|
|
RecompCodeSecondary = nullptr;
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResetJumpTables(void)
|
|
|
|
{
|
|
|
|
memset(JumpTables, 0, 0x1000 * MaxMaps);
|
|
|
|
RecompPos = RecompCode;
|
2024-07-12 05:29:16 +00:00
|
|
|
pLastPrimary = nullptr;
|
|
|
|
pLastSecondary = nullptr;
|
2023-06-01 11:46:23 +00:00
|
|
|
NoOfMaps = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetJumpTable(uint32_t End)
|
|
|
|
{
|
2024-06-27 06:55:13 +00:00
|
|
|
uint32_t CRC = crc32(0L, Z_NULL, 0);
|
2023-06-01 11:46:23 +00:00
|
|
|
if (End < 0x800)
|
|
|
|
{
|
|
|
|
End = 0x800;
|
|
|
|
}
|
|
|
|
|
2023-09-28 02:22:06 +00:00
|
|
|
if (End == 0x1000 && ((g_RSPRegisterHandler->PendingSPMemAddr() & 0x0FFF) & ~7) == 0x80)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
|
|
|
End = 0x800;
|
|
|
|
}
|
|
|
|
|
2024-06-27 06:55:13 +00:00
|
|
|
CRC = crc32(CRC, RSPInfo.IMEM, End);
|
|
|
|
for (uint32_t i = 0; i < NoOfMaps; i++)
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2024-06-27 06:55:13 +00:00
|
|
|
if (CRC == MapsCRC[i])
|
2023-06-01 11:46:23 +00:00
|
|
|
{
|
2024-06-27 06:55:13 +00:00
|
|
|
JumpTable = (void **)(JumpTables + i * 0x1000);
|
|
|
|
Table = i;
|
2023-06-01 11:46:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//DisplayError("%X %X",NoOfMaps,CRC);
|
|
|
|
if (NoOfMaps == MaxMaps)
|
|
|
|
{
|
|
|
|
ResetJumpTables();
|
|
|
|
}
|
|
|
|
MapsCRC[NoOfMaps] = CRC;
|
|
|
|
JumpTable = (void **)(JumpTables + NoOfMaps * 0x1000);
|
|
|
|
Table = NoOfMaps;
|
|
|
|
NoOfMaps += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSP_LW_IMEM(uint32_t Addr, uint32_t * Value)
|
|
|
|
{
|
|
|
|
if ((Addr & 0x3) != 0)
|
|
|
|
{
|
2023-08-16 23:29:22 +00:00
|
|
|
g_Notify->DisplayError("Unaligned RSP_LW_IMEM");
|
2023-06-01 11:46:23 +00:00
|
|
|
}
|
|
|
|
*Value = *(uint32_t *)(RSPInfo.IMEM + (Addr & 0xFFF));
|
2023-07-27 03:53:53 +00:00
|
|
|
}
|