Skip to content

Commit

Permalink
[SAVEVERSION+] EE: Expose advanced option for extra memory
Browse files Browse the repository at this point in the history
  • Loading branch information
DaZombieKiller committed Apr 30, 2024
1 parent e862e68 commit 4602cce
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 48 deletions.
4 changes: 4 additions & 0 deletions pcsx2-qt/Settings/AdvancedSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AdvancedSettingsWidget::AdvancedSettingsWidget(SettingsWindow* dialog, QWidget*
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.eeWaitLoopDetection, "EmuCore/Speedhacks", "WaitLoop", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.eeFastmem, "EmuCore/CPU/Recompiler", "EnableFastmem", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.pauseOnTLBMiss, "EmuCore/CPU/Recompiler", "PauseOnTLBMiss", false);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.extraMemory, "EmuCore/CPU", "ExtraMemory", false);

SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.vu0Recompiler, "EmuCore/CPU/Recompiler", "EnableVU0", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.vu1Recompiler, "EmuCore/CPU/Recompiler", "EnableVU1", true);
Expand Down Expand Up @@ -86,6 +87,9 @@ AdvancedSettingsWidget::AdvancedSettingsWidget(SettingsWindow* dialog, QWidget*
"end of the block, not on the instruction which caused the exception. Refer to the console to see the address where the invalid "
"access occurred."));

dialog->registerWidgetHelp(m_ui.extraMemory, tr("Enable 128MB RAM (Dev Console)"), tr("Unchecked"),
tr("Exposes an additional 96 MB of memory to the virtual machine."));

dialog->registerWidgetHelp(m_ui.vu0RoundingMode, tr("VU0 Rounding Mode"), tr("Chop/Zero (Default)"), tr("Changes how PCSX2 handles rounding while emulating the Emotion Engine's Vector Unit 0 (EE VU0). "
"The default value handles the vast majority of games; <b>modifying this setting when a game is not having a visible problem will cause stability issues and/or crashes.</b>"));

Expand Down
7 changes: 7 additions & 0 deletions pcsx2-qt/Settings/AdvancedSettingsWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="extraMemory">
<property name="text">
<string>Enable 128MB RAM (Dev Console)</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down
18 changes: 9 additions & 9 deletions pcsx2/Achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
#include "RA_Interface.h"
#endif

// Size of the EE physical memory exposed to RetroAchievements.
#define EXPOSED_EE_MEMORY_SIZE (Ps2MemSize::ExposedRam + Ps2MemSize::Scratch)

namespace Achievements
{
// Size of the EE physical memory exposed to RetroAchievements.
static constexpr u32 EXPOSED_EE_MEMORY_SIZE = Ps2MemSize::MainRam + Ps2MemSize::Scratch;

static constexpr u32 LEADERBOARD_NEARBY_ENTRIES_TO_FETCH = 10;
static constexpr u32 LEADERBOARD_ALL_FETCH_SIZE = 20;

Expand Down Expand Up @@ -608,7 +608,7 @@ uint32_t Achievements::ClientReadMemory(uint32_t address, uint8_t* buffer, uint3
// RA uses a fake memory map with the scratchpad directly above physical memory.
// The scratchpad is not meant to be accessible via physical addressing, only virtual.
// This also means that the upper 96MB of memory will never be accessible to achievements.
const u8* ptr = (address < Ps2MemSize::MainRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::MainRam];
const u8* ptr = (address < Ps2MemSize::ExposedRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::ExposedRam];

// Fast paths for known data sizes.
switch (num_bytes)
Expand Down Expand Up @@ -3134,7 +3134,7 @@ unsigned char Achievements::RAIntegration::RACallbackReadMemory(unsigned int add
}

unsigned char value;
const u8* ptr = (address < Ps2MemSize::MainRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::MainRam];
const u8* ptr = (address < Ps2MemSize::ExposedRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::ExposedRam];
std::memcpy(&value, ptr, sizeof(value));
return value;
}
Expand All @@ -3147,17 +3147,17 @@ unsigned int Achievements::RAIntegration::RACallbackReadBlock(unsigned int addre
return 0u;
}

if (address < Ps2MemSize::MainRam && (address + bytes) > Ps2MemSize::MainRam) [[unlikely]]
if (address < Ps2MemSize::ExposedRam && (address + bytes) > Ps2MemSize::ExposedRam) [[unlikely]]
{
// Split across RAM+Scratch.
const unsigned int bytes_from_ram = Ps2MemSize::MainRam - address;
const unsigned int bytes_from_ram = Ps2MemSize::ExposedRam - address;
const unsigned int bytes_from_scratch = bytes - bytes_from_ram;
return (RACallbackReadBlock(address, buffer, bytes_from_ram) +
RACallbackReadBlock(address + bytes_from_ram, buffer + bytes_from_ram, bytes_from_scratch));
}

const unsigned int read_byte_count = std::min<unsigned int>(EXPOSED_EE_MEMORY_SIZE - address, bytes);
const u8* ptr = (address < Ps2MemSize::MainRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::MainRam];
const u8* ptr = (address < Ps2MemSize::ExposedRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::ExposedRam];
std::memcpy(buffer, ptr, read_byte_count);
return read_byte_count;
}
Expand All @@ -3170,7 +3170,7 @@ void Achievements::RAIntegration::RACallbackWriteMemory(unsigned int address, un
return;
}

u8* ptr = (address < Ps2MemSize::MainRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::MainRam];
u8* ptr = (address < Ps2MemSize::ExposedRam) ? &eeMem->Main[address] : &eeMem->Scratch[address - Ps2MemSize::ExposedRam];
std::memcpy(ptr, &value, sizeof(value));
}

Expand Down
6 changes: 6 additions & 0 deletions pcsx2/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ struct Pcsx2Config
// ------------------------------------------------------------------------
struct CpuOptions
{
BITFIELD32()
bool
ExtraMemory : 1;
BITFIELD_END

RecompilerOptions Recompiler;

FPControlRegister FPUFPCR;
Expand Down Expand Up @@ -1256,6 +1261,7 @@ namespace EmuFolders
#define CHECK_CACHE (EmuConfig.Cpu.Recompiler.EnableEECache)
#define CHECK_IOPREC (EmuConfig.Cpu.Recompiler.EnableIOP)
#define CHECK_FASTMEM (EmuConfig.Cpu.Recompiler.EnableEE && EmuConfig.Cpu.Recompiler.EnableFastmem)
#define CHECK_EXTRAMEM (memGetExtraMemMode())

//------------ SPECIAL GAME FIXES!!! ---------------
#define CHECK_VUADDSUBHACK (EmuConfig.Gamefixes.VuAddSubHack) // Special Fix for Tri-ace games, they use an encryption algorithm that requires VU addi opcode to be bit-accurate.
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/DebugTools/DebugInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ bool R5900DebugInterface::isValidAddress(u32 addr)
// [ 0000_8000 - 01FF_FFFF ] RAM
// [ 2000_8000 - 21FF_FFFF ] RAM MIRROR
// [ 3000_8000 - 31FF_FFFF ] RAM MIRROR
if (lopart >= 0x80000 && lopart <= 0x1ffFFff)
if (lopart >= 0x80000 && lopart < Ps2MemSize::ExposedRam)
return !!vtlb_GetPhyPtr(lopart);
break;
case 1:
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/Dmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ __fi tDMA_TAG* SPRdmaGetAddr(u32 addr, bool write)
// FIXME: Why??? DMA uses physical addresses
addr &= 0x1ffffff0;

if (addr < Ps2MemSize::MainRam)
if (addr < Ps2MemSize::ExposedRam)
{
return (tDMA_TAG*)&eeMem->Main[addr];
}
Expand Down Expand Up @@ -154,7 +154,7 @@ __ri tDMA_TAG *dmaGetAddr(u32 addr, bool write)
// FIXME: Why??? DMA uses physical addresses
addr &= 0x1ffffff0;

if (addr < Ps2MemSize::MainRam)
if (addr < Ps2MemSize::ExposedRam)
{
return (tDMA_TAG*)&eeMem->Main[addr];
}
Expand Down
28 changes: 26 additions & 2 deletions pcsx2/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ BIOS
#include "Cache.h"
#endif

namespace Ps2MemSize
{
u32 ExposedRam = MainRam;
} // namespace Ps2MemSize

namespace SysMemory
{
static u8* TryAllocateVirtualMemory(const char* name, void* file_handle, uptr base, size_t size);
Expand All @@ -66,6 +71,7 @@ static u16 s_dve_regs[0xff];
static bool s_ba_command_executing = false;
static bool s_ba_error_detected = false;
static u16 s_ba_current_reg = 0;
static bool s_extra_memory = false;

namespace HostMemoryMap
{
Expand Down Expand Up @@ -282,6 +288,19 @@ void* SysMemory::GetDataFileHandle()
return s_data_memory_file_handle;
}

bool memGetExtraMemMode()
{
return s_extra_memory;
}

void memSetExtraMemMode(bool mode)
{
s_extra_memory = mode;

// update the amount of RAM exposed to the VM
Ps2MemSize::ExposedRam = mode ? Ps2MemSize::TotalRam : Ps2MemSize::MainRam;
}

void memSetKernelMode() {
//Do something here
MemMode = 0;
Expand Down Expand Up @@ -442,9 +461,10 @@ void memMapVUmicro()
void memMapPhy()
{
// Main memory
vtlb_MapBlock(eeMem->Main, 0x00000000,Ps2MemSize::MainRam);//mirrored on first 256 mb ?
vtlb_MapBlock(eeMem->Main, 0x00000000,Ps2MemSize::ExposedRam);//mirrored on first 256 mb ?

// High memory, uninstalled on the configuration we emulate
vtlb_MapHandler(null_handler, Ps2MemSize::MainRam, 0x10000000 - Ps2MemSize::MainRam);
vtlb_MapHandler(null_handler, Ps2MemSize::ExposedRam, 0x10000000 - Ps2MemSize::ExposedRam);

// Various ROMs (all read-only)
vtlb_MapBlock(eeMem->ROM, 0x1fc00000, Ps2MemSize::Rom);
Expand Down Expand Up @@ -1152,6 +1172,10 @@ bool SaveStateBase::memFreeze()
Freeze(s_ba_command_executing);
Freeze(s_ba_error_detected);
Freeze(s_ba_current_reg);
Freeze(s_extra_memory);

if (IsLoading())
memSetExtraMemMode(s_extra_memory);

return IsOkay();
}
3 changes: 2 additions & 1 deletion pcsx2/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ extern void memSetUserMode();
extern void memSetPageAddr(u32 vaddr, u32 paddr);
extern void memClearPageAddr(u32 vaddr);
extern void memBindConditionalHandlers();

extern bool memGetExtraMemMode();
extern void memSetExtraMemMode(bool mode);
extern void memMapVUmicro();

#define memRead8 vtlb_memRead<mem8_t>
Expand Down
3 changes: 3 additions & 0 deletions pcsx2/MemoryTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Ps2MemSize
{
static constexpr u32 MainRam = _32mb; // 32 MB main memory.
static constexpr u32 ExtraRam = _1mb * 96; // 32+96 MB devkit memory.
static constexpr u32 TotalRam = _1mb * 128;// 128 MB total memory.
static constexpr u32 Rom = _1mb * 4; // 4 MB main rom
static constexpr u32 Rom1 = _1mb * 4; // DVD player
static constexpr u32 Rom2 = 0x00080000; // Chinese rom extension
Expand All @@ -18,6 +19,8 @@ namespace Ps2MemSize
static constexpr u32 IopHardware = _64kb;

static constexpr u32 GSregs = 0x00002000; // 8k for the GS registers and stuff.

extern u32 ExposedRam;
} // namespace Ps2MemSize

typedef u8 mem8_t;
Expand Down
2 changes: 2 additions & 0 deletions pcsx2/Pcsx2Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ Pcsx2Config::CpuOptions::CpuOptions()
VU0FPCR = DEFAULT_VU_FP_CONTROL_REGISTER;
VU1FPCR = DEFAULT_VU_FP_CONTROL_REGISTER;
AffinityControlMode = 0;
ExtraMemory = false;
}

void Pcsx2Config::CpuOptions::ApplySanityCheck()
Expand Down Expand Up @@ -543,6 +544,7 @@ void Pcsx2Config::CpuOptions::LoadSave(SettingsWrapper& wrap)
read_fpcr(VU1FPCR, "VU1");

SettingsWrapEntry(AffinityControlMode);
SettingsWrapBitBool(ExtraMemory);

Recompiler.LoadSave(wrap);
}
Expand Down
10 changes: 10 additions & 0 deletions pcsx2/R5900.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,16 @@ void eeloadHook()
}

VMManager::Internal::ELFLoadingOnCPUThread(std::move(elfname));

if (CHECK_EXTRAMEM)
{
// Map extra memory.
vtlb_VMap(Ps2MemSize::MainRam, Ps2MemSize::MainRam, Ps2MemSize::ExtraRam);

// Map RAM mirrors for extra memory.
vtlb_VMap(0x20000000 | Ps2MemSize::MainRam, Ps2MemSize::MainRam, Ps2MemSize::ExtraRam);
vtlb_VMap(0x30000000 | Ps2MemSize::MainRam, Ps2MemSize::MainRam, Ps2MemSize::ExtraRam);
}
}

// Called from recompilers; define is mandatory.
Expand Down
13 changes: 13 additions & 0 deletions pcsx2/R5900OpcodeImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,12 @@ void SYSCALL()
}
}
break;
case Syscall::RFU060:
if (CHECK_EXTRAMEM && cpuRegs.GPR.n.a1.UL[0] == 0xFFFFFFFF)
{
cpuRegs.GPR.n.a1.UL[0] = Ps2MemSize::ExposedRam - cpuRegs.GPR.n.a2.SL[0];
}
break;
case Syscall::SetOsdConfigParam:
// The whole thing gets written back to BIOS memory, so it'll be in the right place, no need to continue HLEing
AllowParams1 = true;
Expand Down Expand Up @@ -1148,6 +1154,13 @@ void SYSCALL()
}
break;
}
case Syscall::GetMemorySize:
if (CHECK_EXTRAMEM)
{
cpuRegs.GPR.n.v0.UL[0] = Ps2MemSize::ExposedRam;
return;
}
break;


default:
Expand Down
4 changes: 3 additions & 1 deletion pcsx2/R5900OpcodeTables.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ enum Syscall : u8
SetVTLBRefillHandler = 13,
StartThread = 34,
ChangeThreadPriority = 41,
RFU060 = 60,
SetOsdConfigParam = 74,
GetOsdConfigParam = 75,
SetOsdConfigParam2 = 110,
GetOsdConfigParam2 = 111,
sysPrintOut = 117,
sceSifSetDma = 119,
Deci2Call = 124
Deci2Call = 124,
GetMemorySize = 127
};

// TODO : Move these into the OpcodeTables namespace
Expand Down
11 changes: 10 additions & 1 deletion pcsx2/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ class SavestateEntry_EmotionMemory final : public MemorySavestateEntry

const char* GetFilename() const override { return "eeMemory.bin"; }
u8* GetDataPtr() const override { return eeMem->Main; }
uint GetDataSize() const override { return sizeof(eeMem->Main); }
uint GetDataSize() const override { return Ps2MemSize::ExposedRam; }

virtual bool FreezeIn(zip_file_t* zf) const override
{
Expand Down Expand Up @@ -1144,13 +1144,22 @@ bool SaveState_UnzipFromDisk(const std::string& filename, Error* error)
}

PreLoadPrep();
const bool extraMemory = CHECK_EXTRAMEM;

if (!LoadInternalStructuresState(zf.get(), internal_index))
{
Error::SetString(error, "Save state corruption in internal structures.");
return false;
}

// A save state is not allowed to change the extra memory mode.
if (extraMemory != CHECK_EXTRAMEM)
{
memSetExtraMemMode(extraMemory);
Error::SetString(error, fmt::format("Save state requires 128MB RAM to be {}.", extraMemory ? "disabled" : "enabled"));
return false;
}

for (u32 i = 0; i < std::size(SavestateEntries); ++i)
{
if (entryIndices[i] < 0)
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SaveState.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum class FreezeAction
// [SAVEVERSION+]
// This informs the auto updater that the users savestates will be invalidated.

static const u32 g_SaveVersion = (0x9A4C << 16) | 0x0000;
static const u32 g_SaveVersion = (0x9A4D << 16) | 0x0000;


// the freezing data between submodules and core
Expand Down
7 changes: 7 additions & 0 deletions pcsx2/VMManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ bool VMManager::Initialize(VMBootParameters boot_params)

s_cpu_implementation_changed = false;
UpdateCPUImplementations();
memSetExtraMemMode(EmuConfig.Cpu.ExtraMemory);
Internal::ClearCPUExecutionCaches();
FPControlRegister::SetCurrent(EmuConfig.Cpu.FPUFPCR);
memBindConditionalHandlers();
Expand Down Expand Up @@ -1616,6 +1617,7 @@ void VMManager::Reset()
if (elf_was_changed)
HandleELFChange(false);

memSetExtraMemMode(EmuConfig.Cpu.ExtraMemory);
Internal::ClearCPUExecutionCaches();
memBindConditionalHandlers();
SysMemory::Reset();
Expand Down Expand Up @@ -3036,6 +3038,11 @@ void VMManager::WarnAboutUnsafeSettings()
append(ICON_PF_MICROCHIP,
TRANSLATE_SV("VMManager", "VU Clamp Mode is not set to default, this may break some games."));
}
if (EmuConfig.Cpu.ExtraMemory)
{
append(ICON_PF_MICROCHIP,
TRANSLATE_SV("VMManager", "128MB RAM is enabled. Compatibility with some games may be affected."));
}
if (!EmuConfig.EnableGameFixes)
{
append(ICON_FA_GAMEPAD,
Expand Down
Loading

0 comments on commit 4602cce

Please sign in to comment.