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/.
|
|
|
|
|
2013-12-11 12:55:57 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-03-15 17:38:46 +00:00
|
|
|
#include "Common/Common.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
|
|
|
#include "Common/MemArena.h"
|
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/HDRemaster.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
|
|
|
#include "Core/MIPS/JitCommon/JitCommon.h"
|
|
|
|
#include "Core/HLE/HLE.h"
|
2013-12-18 10:42:19 +00:00
|
|
|
|
|
|
|
#include "Core/Core.h"
|
2013-12-14 22:54:05 +00:00
|
|
|
#include "Core/Debugger/SymbolMap.h"
|
|
|
|
#include "Core/Debugger/Breakpoints.h"
|
2013-11-28 19:58:59 +00:00
|
|
|
#include "Core/Config.h"
|
2013-12-18 10:42:19 +00:00
|
|
|
#include "Core/HLE/ReplaceTables.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-18 10:42:19 +00:00
|
|
|
namespace Memory {
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// The base pointer to the auto-mirrored arena.
|
|
|
|
u8* base = NULL;
|
|
|
|
|
|
|
|
// The MemArena class
|
|
|
|
MemArena g_arena;
|
|
|
|
// ==============
|
|
|
|
|
|
|
|
// 64-bit: Pointers to low-mem (sub-0x10000000) mirror
|
|
|
|
// 32-bit: Same as the corresponding physical/virtual pointers.
|
|
|
|
u8 *m_pRAM;
|
2013-12-10 16:25:38 +00:00
|
|
|
u8 *m_pRAM2;
|
|
|
|
u8 *m_pRAM3;
|
2012-11-01 15:19:01 +00:00
|
|
|
u8 *m_pScratchPad;
|
|
|
|
u8 *m_pVRAM;
|
|
|
|
|
|
|
|
u8 *m_pPhysicalScratchPad;
|
2012-11-06 15:01:41 +00:00
|
|
|
u8 *m_pUncachedScratchPad;
|
2012-11-01 15:19:01 +00:00
|
|
|
// 64-bit: Pointers to high-mem mirrors
|
|
|
|
// 32-bit: Same as above
|
|
|
|
u8 *m_pPhysicalRAM;
|
|
|
|
u8 *m_pUncachedRAM;
|
|
|
|
u8 *m_pKernelRAM; // RAM mirrored up to "kernel space". Fully accessible at all times currently.
|
2013-12-10 16:25:38 +00:00
|
|
|
u8 *m_pPhysicalRAM2;
|
|
|
|
u8 *m_pUncachedRAM2;
|
|
|
|
u8 *m_pKernelRAM2;
|
|
|
|
u8 *m_pPhysicalRAM3;
|
|
|
|
u8 *m_pUncachedRAM3;
|
|
|
|
u8 *m_pKernelRAM3;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
u8 *m_pPhysicalVRAM;
|
|
|
|
u8 *m_pUncachedVRAM;
|
|
|
|
|
2013-06-21 17:34:57 +00:00
|
|
|
// Holds the ending address of the PSP's user space.
|
|
|
|
// Required for HD Remasters to work properly.
|
2013-09-09 07:19:26 +00:00
|
|
|
// These replace RAM_NORMAL_SIZE and RAM_NORMAL_MASK, respectively.
|
2013-06-21 17:34:57 +00:00
|
|
|
u32 g_MemorySize;
|
2013-09-09 07:19:26 +00:00
|
|
|
u32 g_MemoryMask;
|
2013-11-28 20:34:11 +00:00
|
|
|
// Used to store the PSP model on game startup.
|
|
|
|
u32 g_PSPModel;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// We don't declare the IO region in here since its handled by other means.
|
2013-06-22 09:14:01 +00:00
|
|
|
static MemoryView views[] =
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
{&m_pScratchPad, &m_pPhysicalScratchPad, 0x00010000, SCRATCHPAD_SIZE, 0},
|
2012-11-06 15:01:41 +00:00
|
|
|
{NULL, &m_pUncachedScratchPad, 0x40010000, SCRATCHPAD_SIZE, MV_MIRROR_PREVIOUS},
|
2012-11-01 15:19:01 +00:00
|
|
|
{&m_pVRAM, &m_pPhysicalVRAM, 0x04000000, 0x00800000, 0},
|
|
|
|
{NULL, &m_pUncachedVRAM, 0x44000000, 0x00800000, MV_MIRROR_PREVIOUS},
|
2013-07-08 03:28:47 +00:00
|
|
|
{&m_pRAM, &m_pPhysicalRAM, 0x08000000, g_MemorySize, MV_IS_PRIMARY_RAM}, // only from 0x08800000 is it usable (last 24 megs)
|
|
|
|
{NULL, &m_pUncachedRAM, 0x48000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM},
|
|
|
|
{NULL, &m_pKernelRAM, 0x88000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM},
|
2013-12-10 16:25:38 +00:00
|
|
|
// Starts at memory + 31 MB.
|
|
|
|
{&m_pRAM2, &m_pPhysicalRAM2, 0x09F00000, g_MemorySize, MV_IS_EXTRA1_RAM},
|
|
|
|
{NULL, &m_pUncachedRAM2, 0x49F00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA1_RAM},
|
|
|
|
{NULL, &m_pKernelRAM2, 0x89F00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA1_RAM},
|
|
|
|
// Starts at memory + 31 * 2 MB.
|
|
|
|
{&m_pRAM3, &m_pPhysicalRAM3, 0x0BE00000, g_MemorySize, MV_IS_EXTRA2_RAM},
|
|
|
|
{NULL, &m_pUncachedRAM3, 0x4BE00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA2_RAM},
|
|
|
|
{NULL, &m_pKernelRAM3, 0x8BE00000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_EXTRA2_RAM},
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// TODO: There are a few swizzled mirrors of VRAM, not sure about the best way to
|
|
|
|
// implement those.
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int num_views = sizeof(views) / sizeof(MemoryView);
|
|
|
|
|
2013-06-22 09:14:01 +00:00
|
|
|
void Init()
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
int flags = 0;
|
2013-09-09 07:19:26 +00:00
|
|
|
// This mask is used ONLY after validating the address is in the correct range.
|
2013-09-09 07:35:31 +00:00
|
|
|
// So let's just use a fixed mask to remove the uncached/user memory bits.
|
2013-09-09 07:19:26 +00:00
|
|
|
// Using (Memory::g_MemorySize - 1) won't work for e.g. 0x04C00000.
|
|
|
|
Memory::g_MemoryMask = 0x07FFFFFF;
|
2013-06-21 17:34:57 +00:00
|
|
|
|
2013-12-10 16:25:38 +00:00
|
|
|
// On some 32 bit platforms, you can only map < 32 megs at a time.
|
|
|
|
const static int MAX_MMAP_SIZE = 31 * 1024 * 1024;
|
2013-12-11 10:00:01 +00:00
|
|
|
_dbg_assert_msg_(MEMMAP, g_MemorySize < MAX_MMAP_SIZE * 3, "ACK - too much memory for three mmap views.");
|
2013-07-08 03:28:47 +00:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(views); i++) {
|
|
|
|
if (views[i].flags & MV_IS_PRIMARY_RAM)
|
2013-12-10 16:25:38 +00:00
|
|
|
views[i].size = std::min((int)g_MemorySize, MAX_MMAP_SIZE);
|
|
|
|
if (views[i].flags & MV_IS_EXTRA1_RAM)
|
|
|
|
views[i].size = std::min(std::max((int)g_MemorySize - MAX_MMAP_SIZE, 0), MAX_MMAP_SIZE);
|
|
|
|
if (views[i].flags & MV_IS_EXTRA2_RAM)
|
|
|
|
views[i].size = std::min(std::max((int)g_MemorySize - MAX_MMAP_SIZE * 2, 0), MAX_MMAP_SIZE);
|
2013-06-22 08:10:42 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
base = MemoryMap_Setup(views, num_views, flags, &g_arena);
|
|
|
|
|
|
|
|
INFO_LOG(MEMMAP, "Memory system initialized. RAM at %p (mirror at 0 @ %p, uncached @ %p)",
|
|
|
|
m_pRAM, m_pPhysicalRAM, m_pUncachedRAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoState(PointerWrap &p)
|
|
|
|
{
|
2013-11-28 22:46:33 +00:00
|
|
|
auto s = p.Section("Memory", 1, 2);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2013-11-28 20:34:11 +00:00
|
|
|
if (s < 2) {
|
2013-11-28 21:28:25 +00:00
|
|
|
if (!g_RemasterMode)
|
|
|
|
g_MemorySize = RAM_NORMAL_SIZE;
|
2013-11-28 20:34:11 +00:00
|
|
|
g_PSPModel = PSP_MODEL_FAT;
|
2013-11-29 17:51:24 +00:00
|
|
|
} else {
|
2014-01-08 08:06:27 +00:00
|
|
|
u32 oldMemorySize = g_MemorySize;
|
2013-11-28 20:34:11 +00:00
|
|
|
p.Do(g_PSPModel);
|
|
|
|
p.DoMarker("PSPModel");
|
2014-01-08 08:06:27 +00:00
|
|
|
if (!g_RemasterMode) {
|
2013-11-28 21:28:25 +00:00
|
|
|
g_MemorySize = g_PSPModel == PSP_MODEL_FAT ? RAM_NORMAL_SIZE : RAM_DOUBLE_SIZE;
|
2014-01-08 08:06:27 +00:00
|
|
|
if (oldMemorySize < g_MemorySize) {
|
|
|
|
Shutdown();
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
}
|
2013-11-28 20:34:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 16:25:38 +00:00
|
|
|
p.DoArray(GetPointer(PSP_GetKernelMemoryBase()), g_MemorySize);
|
2012-11-01 15:19:01 +00:00
|
|
|
p.DoMarker("RAM");
|
2013-11-28 20:34:11 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
p.DoArray(m_pVRAM, VRAM_SIZE);
|
|
|
|
p.DoMarker("VRAM");
|
|
|
|
p.DoArray(m_pScratchPad, SCRATCHPAD_SIZE);
|
|
|
|
p.DoMarker("ScratchPad");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
u32 flags = 0;
|
|
|
|
MemoryMap_Shutdown(views, num_views, flags, &g_arena);
|
|
|
|
g_arena.ReleaseSpace();
|
|
|
|
base = NULL;
|
2013-12-09 12:45:17 +00:00
|
|
|
DEBUG_LOG(MEMMAP, "Memory system shut down.");
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
if (m_pRAM)
|
2013-12-10 16:25:38 +00:00
|
|
|
memset(GetPointerUnchecked(PSP_GetKernelMemoryBase()), 0, g_MemorySize);
|
2012-11-01 15:19:01 +00:00
|
|
|
if (m_pScratchPad)
|
|
|
|
memset(m_pScratchPad, 0, SCRATCHPAD_SIZE);
|
|
|
|
if (m_pVRAM)
|
|
|
|
memset(m_pVRAM, 0, VRAM_SIZE);
|
|
|
|
}
|
|
|
|
|
2014-03-02 05:15:17 +00:00
|
|
|
static Opcode Read_Instruction(u32 address, bool resolveReplacements, Opcode inst)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-03-02 05:15:17 +00:00
|
|
|
if (!MIPS_IS_EMUHACK(inst.encoding)) {
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:27:23 +00:00
|
|
|
if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) {
|
2013-04-26 22:22:18 +00:00
|
|
|
JitBlockCache *bc = MIPSComp::jit->GetBlockCache();
|
2013-12-10 12:06:57 +00:00
|
|
|
int block_num = bc->GetBlockNumberFromEmuHackOp(inst, true);
|
2013-04-29 03:59:13 +00:00
|
|
|
if (block_num >= 0) {
|
2013-12-18 15:27:23 +00:00
|
|
|
inst = bc->GetOriginalFirstOp(block_num);
|
2013-12-20 12:51:26 +00:00
|
|
|
if (resolveReplacements && MIPS_IS_REPLACEMENT(inst)) {
|
|
|
|
u32 op;
|
|
|
|
if (GetReplacedOpAt(address, &op)) {
|
|
|
|
if (MIPS_IS_EMUHACK(op)) {
|
|
|
|
ERROR_LOG(HLE,"WTF 1");
|
|
|
|
return Opcode(op);
|
|
|
|
} else {
|
|
|
|
return Opcode(op);
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-30 00:08:06 +00:00
|
|
|
ERROR_LOG(HLE, "Replacement, but no replacement op? %08x", inst.encoding);
|
2013-12-20 12:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 15:27:23 +00:00
|
|
|
return inst;
|
2013-04-29 03:59:13 +00:00
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
2013-12-18 10:42:19 +00:00
|
|
|
} else if (MIPS_IS_REPLACEMENT(inst.encoding)) {
|
|
|
|
u32 op;
|
|
|
|
if (GetReplacedOpAt(address, &op)) {
|
2013-12-20 12:51:26 +00:00
|
|
|
if (MIPS_IS_EMUHACK(op)) {
|
|
|
|
ERROR_LOG(HLE,"WTF 2");
|
|
|
|
return Opcode(op);
|
|
|
|
} else {
|
|
|
|
return Opcode(op);
|
|
|
|
}
|
2013-12-18 10:42:19 +00:00
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
2013-04-26 22:22:18 +00:00
|
|
|
} else {
|
2012-11-01 15:19:01 +00:00
|
|
|
return inst;
|
2013-04-26 22:22:18 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 05:15:17 +00:00
|
|
|
Opcode Read_Instruction(u32 address, bool resolveReplacements)
|
|
|
|
{
|
|
|
|
Opcode inst = Opcode(Read_U32(address));
|
|
|
|
return Read_Instruction(address, resolveReplacements, inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Opcode ReadUnchecked_Instruction(u32 address, bool resolveReplacements)
|
|
|
|
{
|
|
|
|
Opcode inst = Opcode(ReadUnchecked_U32(address));
|
|
|
|
return Read_Instruction(address, resolveReplacements, inst);
|
|
|
|
}
|
|
|
|
|
2013-08-24 21:43:49 +00:00
|
|
|
Opcode Read_Opcode_JIT(u32 address)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-12-18 15:27:23 +00:00
|
|
|
Opcode inst = Opcode(Read_U32(address));
|
|
|
|
if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) {
|
|
|
|
JitBlockCache *bc = MIPSComp::jit->GetBlockCache();
|
|
|
|
int block_num = bc->GetBlockNumberFromEmuHackOp(inst, true);
|
|
|
|
if (block_num >= 0) {
|
|
|
|
return bc->GetOriginalFirstOp(block_num);
|
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return inst;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING! No checks!
|
|
|
|
// We assume that _Address is cached
|
2013-08-24 21:43:49 +00:00
|
|
|
void Write_Opcode_JIT(const u32 _Address, const Opcode _Value)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-08-24 21:43:49 +00:00
|
|
|
Memory::WriteUnchecked_U32(_Value.encoding, _Address);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Memset(const u32 _Address, const u8 _iValue, const u32 _iLength)
|
2013-12-30 09:49:05 +00:00
|
|
|
{
|
2012-11-01 15:19:01 +00:00
|
|
|
u8 *ptr = GetPointer(_Address);
|
2013-12-18 10:42:19 +00:00
|
|
|
if (ptr != NULL) {
|
|
|
|
memset(ptr, _iValue, _iLength);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _iLength; i++)
|
2012-11-05 14:41:37 +00:00
|
|
|
Write_U8(_iValue, (u32)(_Address + i));
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2014-02-08 18:29:22 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
2013-12-14 22:54:05 +00:00
|
|
|
CBreakPoints::ExecMemCheck(_Address, true, _iLength, currentMIPS->pc);
|
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetAddressName(u32 address)
|
|
|
|
{
|
|
|
|
// TODO, follow GetPointer
|
|
|
|
return "[mem]";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|