From bacd834840a12b7df19df175bb5cbf286c93d43f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 15 Sep 2021 12:23:15 +1000 Subject: [PATCH] Cheevos: Use safe memory access routines --- src/frontend-common/cheevos.cpp | 52 ++++++++++++--------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/src/frontend-common/cheevos.cpp b/src/frontend-common/cheevos.cpp index 0164f803d..ba15685fd 100644 --- a/src/frontend-common/cheevos.cpp +++ b/src/frontend-common/cheevos.cpp @@ -1399,47 +1399,31 @@ void CheevosEventHandler(const rc_runtime_event_t* runtime_event) SubmitLeaderboard(runtime_event->id, runtime_event->value); } -// from cheats.cpp - do we want to move this somewhere else? -template -static T DoMemoryRead(PhysicalMemoryAddress address) -{ - T result; - - if ((address & CPU::DCACHE_LOCATION_MASK) == CPU::DCACHE_LOCATION && - (address & CPU::DCACHE_OFFSET_MASK) < CPU::DCACHE_SIZE) - { - std::memcpy(&result, &CPU::g_state.dcache[address & CPU::DCACHE_OFFSET_MASK], sizeof(result)); - return result; - } - - address &= CPU::PHYSICAL_MEMORY_ADDRESS_MASK; - - if (address < Bus::RAM_MIRROR_END) - { - std::memcpy(&result, &Bus::g_ram[address & Bus::g_ram_mask], sizeof(result)); - return result; - } - - if (address >= Bus::BIOS_BASE && address < (Bus::BIOS_BASE + Bus::BIOS_SIZE)) - { - std::memcpy(&result, &Bus::g_bios[address & Bus::BIOS_MASK], sizeof(result)); - return result; - } - - result = static_cast(0); - return result; -} - unsigned CheevosPeek(unsigned address, unsigned num_bytes, void* ud) { switch (num_bytes) { case 1: - return ZeroExtend32(DoMemoryRead(address)); + { + u8 value = 0; + CPU::SafeReadMemoryByte(address, &value); + return value; + } + case 2: - return ZeroExtend32(DoMemoryRead(address)); + { + u16 value; + CPU::SafeReadMemoryHalfWord(address, &value); + return value; + } + case 4: - return ZeroExtend32(DoMemoryRead(address)); + { + u32 value; + CPU::SafeReadMemoryWord(address, &value); + return value; + } + default: return 0; }