2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project / 2012 PPSSPP Project
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2022-01-30 23:49:02 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2022-12-27 22:58:20 +00:00
|
|
|
#include "Common/LogReporting.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-29 23:11:29 +00:00
|
|
|
#include "Core/Core.h"
|
2013-08-21 15:34:35 +00:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/Config.h"
|
2018-06-17 01:42:31 +00:00
|
|
|
#include "Core/ConfigValues.h"
|
2012-11-04 10:54:45 +00:00
|
|
|
|
2013-08-21 15:34:35 +00:00
|
|
|
#include "Core/MIPS/MIPS.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-07-04 18:30:05 +00:00
|
|
|
namespace Memory {
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2022-07-20 10:40:22 +00:00
|
|
|
u8 *GetPointerWrite(const u32 address) {
|
2022-07-24 11:33:03 +00:00
|
|
|
if ((address & 0x3E000000) == 0x08000000 || // RAM
|
|
|
|
(address & 0x3F800000) == 0x04000000 || // VRAM
|
|
|
|
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
|
|
|
|
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
|
2022-07-20 10:40:22 +00:00
|
|
|
return GetPointerWriteUnchecked(address);
|
2014-05-06 07:41:49 +00:00
|
|
|
} else {
|
2013-08-21 15:34:35 +00:00
|
|
|
static bool reported = false;
|
|
|
|
if (!reported) {
|
2022-07-24 11:33:03 +00:00
|
|
|
Reporting::ReportMessage("Unknown GetPointerWrite %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
|
2013-08-21 15:34:35 +00:00
|
|
|
reported = true;
|
|
|
|
}
|
2023-01-01 18:22:41 +00:00
|
|
|
|
|
|
|
// Size is not known, we pass 0 to signal that.
|
|
|
|
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
|
2015-12-27 22:29:12 +00:00
|
|
|
return nullptr;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:40:22 +00:00
|
|
|
const u8 *GetPointer(const u32 address) {
|
2022-07-24 11:33:03 +00:00
|
|
|
if ((address & 0x3E000000) == 0x08000000 || // RAM
|
|
|
|
(address & 0x3F800000) == 0x04000000 || // VRAM
|
|
|
|
(address & 0xBFFFC000) == 0x00010000 || // Scratchpad
|
|
|
|
((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize)) { // More RAM (remasters, etc.)
|
|
|
|
return GetPointerUnchecked(address);
|
|
|
|
} else {
|
|
|
|
static bool reported = false;
|
|
|
|
if (!reported) {
|
|
|
|
Reporting::ReportMessage("Unknown GetPointer %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
|
|
|
|
reported = true;
|
|
|
|
}
|
2023-01-01 18:22:41 +00:00
|
|
|
// Size is not known, we pass 0 to signal that.
|
|
|
|
Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
|
2022-07-24 11:33:03 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-07-20 10:40:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 18:25:40 +00:00
|
|
|
u8 *GetPointerWriteRange(const u32 address, const u32 size) {
|
|
|
|
u8 *ptr = GetPointerWrite(address);
|
|
|
|
if (ptr) {
|
|
|
|
if (ValidSize(address, size) != size) {
|
|
|
|
// That's a memory exception! TODO: Adjust reported address to the end of the range?
|
2023-01-01 18:22:41 +00:00
|
|
|
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK);
|
2023-01-01 18:25:40 +00:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error was reported in GetPointerWrite already, if we're not ignoring errors.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8 *GetPointerRange(const u32 address, const u32 size) {
|
|
|
|
const u8 *ptr = GetPointer(address);
|
|
|
|
if (ptr) {
|
|
|
|
if (ValidSize(address, size) != size) {
|
|
|
|
// That's a memory exception! TODO: Adjust reported address to the end of the range?
|
2023-01-01 18:22:41 +00:00
|
|
|
Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::READ_BLOCK);
|
2023-01-01 18:25:40 +00:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error was reported in GetPointer already, if we're not ignoring errors.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
template <typename T>
|
2015-12-27 22:29:12 +00:00
|
|
|
inline void ReadFromHardware(T &var, const u32 address) {
|
2012-11-01 15:19:01 +00:00
|
|
|
// TODO: Figure out the fastest order of tests for both read and write (they are probably different).
|
2013-06-21 06:10:36 +00:00
|
|
|
if ((address & 0x3E000000) == 0x08000000) {
|
2014-05-06 07:41:49 +00:00
|
|
|
// RAM
|
2013-12-10 16:25:38 +00:00
|
|
|
var = *((const T*)GetPointerUnchecked(address));
|
2014-05-06 07:41:49 +00:00
|
|
|
} else if ((address & 0x3F800000) == 0x04000000) {
|
|
|
|
// VRAM
|
|
|
|
var = *((const T*)GetPointerUnchecked(address));
|
2021-04-22 02:32:22 +00:00
|
|
|
} else if ((address & 0xBFFFC000) == 0x00010000) {
|
2012-11-01 15:19:01 +00:00
|
|
|
// Scratchpad
|
2013-12-10 16:25:38 +00:00
|
|
|
var = *((const T*)GetPointerUnchecked(address));
|
2014-05-06 07:41:49 +00:00
|
|
|
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
|
|
|
|
// More RAM (remasters, etc.)
|
|
|
|
var = *((const T*)GetPointerUnchecked(address));
|
|
|
|
} else {
|
2013-08-21 15:34:35 +00:00
|
|
|
static bool reported = false;
|
|
|
|
if (!reported) {
|
|
|
|
Reporting::ReportMessage("ReadFromHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
|
|
|
|
reported = true;
|
|
|
|
}
|
2023-01-01 18:22:41 +00:00
|
|
|
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::READ_WORD);
|
2012-11-01 15:19:01 +00:00
|
|
|
var = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-12-27 22:29:12 +00:00
|
|
|
inline void WriteToHardware(u32 address, const T data) {
|
2013-06-21 06:10:36 +00:00
|
|
|
if ((address & 0x3E000000) == 0x08000000) {
|
2014-05-06 07:41:49 +00:00
|
|
|
// RAM
|
2013-12-10 16:25:38 +00:00
|
|
|
*(T*)GetPointerUnchecked(address) = data;
|
2014-05-06 07:41:49 +00:00
|
|
|
} else if ((address & 0x3F800000) == 0x04000000) {
|
|
|
|
// VRAM
|
2013-12-10 16:25:38 +00:00
|
|
|
*(T*)GetPointerUnchecked(address) = data;
|
2021-04-22 02:32:22 +00:00
|
|
|
} else if ((address & 0xBFFFC000) == 0x00010000) {
|
2014-05-06 07:41:49 +00:00
|
|
|
// Scratchpad
|
|
|
|
*(T*)GetPointerUnchecked(address) = data;
|
|
|
|
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
|
|
|
|
// More RAM (remasters, etc.)
|
|
|
|
*(T*)GetPointerUnchecked(address) = data;
|
|
|
|
} else {
|
2013-08-21 15:34:35 +00:00
|
|
|
static bool reported = false;
|
|
|
|
if (!reported) {
|
|
|
|
Reporting::ReportMessage("WriteToHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
|
|
|
|
reported = true;
|
|
|
|
}
|
2023-01-01 18:22:41 +00:00
|
|
|
Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::WRITE_WORD);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 16:52:30 +00:00
|
|
|
bool IsRAMAddress(const u32 address) {
|
|
|
|
if ((address & 0x3E000000) == 0x08000000) {
|
|
|
|
return true;
|
2020-07-04 18:30:33 +00:00
|
|
|
} else if ((address & 0x3F000000) >= 0x08000000 && (address & 0x3F000000) < 0x08000000 + g_MemorySize) {
|
2013-09-21 16:52:30 +00:00
|
|
|
return true;
|
2020-07-04 18:30:33 +00:00
|
|
|
} else {
|
2013-09-21 16:52:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 23:25:16 +00:00
|
|
|
bool IsScratchpadAddress(const u32 address) {
|
2021-04-22 02:32:22 +00:00
|
|
|
return (address & 0xBFFFC000) == 0x00010000;
|
2014-07-05 23:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u8 Read_U8(const u32 address) {
|
|
|
|
u8 value = 0;
|
|
|
|
ReadFromHardware<u8>(value, address);
|
|
|
|
return (u8)value;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u16 Read_U16(const u32 address) {
|
|
|
|
u16_le value = 0;
|
|
|
|
ReadFromHardware<u16_le>(value, address);
|
|
|
|
return (u16)value;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u32 Read_U32(const u32 address) {
|
|
|
|
u32_le value = 0;
|
|
|
|
ReadFromHardware<u32_le>(value, address);
|
|
|
|
return value;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u64 Read_U64(const u32 address) {
|
|
|
|
u64_le value = 0;
|
|
|
|
ReadFromHardware<u64_le>(value, address);
|
|
|
|
return value;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u32 Read_U8_ZX(const u32 address) {
|
|
|
|
return (u32)Read_U8(address);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
u32 Read_U16_ZX(const u32 address) {
|
|
|
|
return (u32)Read_U16(address);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
void Write_U8(const u8 _Data, const u32 address) {
|
|
|
|
WriteToHardware<u8>(address, _Data);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
void Write_U16(const u16 _Data, const u32 address) {
|
|
|
|
WriteToHardware<u16_le>(address, _Data);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
void Write_U32(const u32 _Data, const u32 address) {
|
|
|
|
WriteToHardware<u32_le>(address, _Data);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:30:33 +00:00
|
|
|
void Write_U64(const u64 _Data, const u32 address) {
|
|
|
|
WriteToHardware<u64_le>(address, _Data);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Memory
|