Skip to content

Commit

Permalink
CPU: Add Safe{Read,Write}MemoryBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 21, 2024
1 parent f1f89d3 commit 79644bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/cpu_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,54 @@ bool CPU::SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value)
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)
{
using namespace Bus;
Expand Down
2 changes: 2 additions & 0 deletions src/core/cpu_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ bool SafeReadMemoryByte(VirtualMemoryAddress addr, u8* value);
bool SafeReadMemoryHalfWord(VirtualMemoryAddress addr, u16* value);
bool SafeReadMemoryWord(VirtualMemoryAddress addr, u32* value);
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 SafeWriteMemoryHalfWord(VirtualMemoryAddress addr, u16 value);
bool SafeWriteMemoryWord(VirtualMemoryAddress addr, u32 value);
bool SafeWriteMemoryBytes(VirtualMemoryAddress addr, const void* data, u32 length);

// External IRQs
void SetIRQRequest(bool state);
Expand Down

0 comments on commit 79644bf

Please sign in to comment.