CPU: Add Safe{Read,Write}MemoryBytes

This commit is contained in:
Stenzek 2024-07-21 13:33:54 +10:00
parent f1f89d3f0f
commit 79644bfb2a
No known key found for this signature in database
2 changed files with 50 additions and 0 deletions

View File

@ -3076,6 +3076,54 @@ bool CPU::SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value)
return SafeWriteMemoryHalfWord(addr, Truncate16(value)) && SafeWriteMemoryHalfWord(addr + 2, Truncate16(value >> 16)); return SafeWriteMemoryHalfWord(addr, Truncate16(value)) && SafeWriteMemoryHalfWord(addr + 2, Truncate16(value >> 16));
} }
bool CPU::SafeReadMemoryBytes(VirtualMemoryAddress addr, void* data, u32 length)
{
using namespace Bus;
const u32 seg = (addr >> 29);
if ((seg != 0 && seg != 4 && seg != 5) || (((addr + length) & PHYSICAL_MEMORY_ADDRESS_MASK) >= RAM_MIRROR_END) ||
(((addr & g_ram_mask) + length) > g_ram_size))
{
u8* ptr = static_cast<u8*>(data);
u8* const ptr_end = ptr + length;
while (ptr != ptr_end)
{
if (!SafeReadMemoryByte(addr++, ptr++))
return false;
}
return true;
}
// Fast path: all in RAM, no wraparound.
std::memcpy(data, &g_ram[addr & g_ram_mask], length);
return true;
}
bool CPU::SafeWriteMemoryBytes(VirtualMemoryAddress addr, const void* data, u32 length)
{
using namespace Bus;
const u32 seg = (addr >> 29);
if ((seg != 0 && seg != 4 && seg != 5) || (((addr + length) & PHYSICAL_MEMORY_ADDRESS_MASK) >= RAM_MIRROR_END) ||
(((addr & g_ram_mask) + length) > g_ram_size))
{
const u8* ptr = static_cast<const u8*>(data);
const u8* const ptr_end = ptr + length;
while (ptr != ptr_end)
{
if (!SafeWriteMemoryByte(addr++, *(ptr++)))
return false;
}
return true;
}
// Fast path: all in RAM, no wraparound.
std::memcpy(&g_ram[addr & g_ram_mask], data, length);
return true;
}
void* CPU::GetDirectReadMemoryPointer(VirtualMemoryAddress address, MemoryAccessSize size, TickCount* read_ticks) void* CPU::GetDirectReadMemoryPointer(VirtualMemoryAddress address, MemoryAccessSize size, TickCount* read_ticks)
{ {
using namespace Bus; using namespace Bus;

View File

@ -183,9 +183,11 @@ bool SafeReadMemoryByte(VirtualMemoryAddress addr, u8* value);
bool SafeReadMemoryHalfWord(VirtualMemoryAddress addr, u16* value); bool SafeReadMemoryHalfWord(VirtualMemoryAddress addr, u16* value);
bool SafeReadMemoryWord(VirtualMemoryAddress addr, u32* value); bool SafeReadMemoryWord(VirtualMemoryAddress addr, u32* value);
bool SafeReadMemoryCString(VirtualMemoryAddress addr, std::string* value, u32 max_length = 1024); bool SafeReadMemoryCString(VirtualMemoryAddress addr, std::string* value, u32 max_length = 1024);
bool SafeReadMemoryBytes(VirtualMemoryAddress addr, void* data, u32 length);
bool SafeWriteMemoryByte(VirtualMemoryAddress addr, u8 value); bool SafeWriteMemoryByte(VirtualMemoryAddress addr, u8 value);
bool SafeWriteMemoryHalfWord(VirtualMemoryAddress addr, u16 value); bool SafeWriteMemoryHalfWord(VirtualMemoryAddress addr, u16 value);
bool SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value); bool SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value);
bool SafeWriteMemoryBytes(VirtualMemoryAddress addr, const void* data, u32 length);
// External IRQs // External IRQs
void SetIRQRequest(bool state); void SetIRQRequest(bool state);