Core: Refactor fastmem arena construction.

This shall be called by the Jit, not by the emulation start itself.
This commit is contained in:
degasus 2019-11-27 09:59:00 +01:00
parent 9d88180df7
commit ccbadf6e72
2 changed files with 77 additions and 16 deletions

View File

@ -42,6 +42,7 @@ namespace Memory
// Store the MemArena here // Store the MemArena here
u8* physical_base = nullptr; u8* physical_base = nullptr;
u8* logical_base = nullptr; u8* logical_base = nullptr;
static bool is_fastmem_arena_initialized = false;
// The MemArena class // The MemArena class
static Common::MemArena g_arena; static Common::MemArena g_arena;
@ -131,7 +132,7 @@ struct LogicalMemoryView
// other devices, like the GPU, use other rules, approximated by // other devices, like the GPU, use other rules, approximated by
// Memory::GetPointer.) This memory is laid out as follows: // Memory::GetPointer.) This memory is laid out as follows:
// [0x00000000, 0x02000000) - 32MB RAM // [0x00000000, 0x02000000) - 32MB RAM
// [0x02000000, 0x08000000) - Mirrors of 32MB RAM // [0x02000000, 0x08000000) - Mirrors of 32MB RAM (not handled here)
// [0x08000000, 0x0C000000) - EFB "mapping" (not handled here) // [0x08000000, 0x0C000000) - EFB "mapping" (not handled here)
// [0x0C000000, 0x0E000000) - MMIO etc. (not handled here) // [0x0C000000, 0x0E000000) - MMIO etc. (not handled here)
// [0x10000000, 0x14000000) - 64MB RAM (Wii-only; slightly slower) // [0x10000000, 0x14000000) - 64MB RAM (Wii-only; slightly slower)
@ -155,6 +156,7 @@ struct LogicalMemoryView
// //
// TODO: The actual size of RAM is REALRAM_SIZE (24MB); the other 8MB shouldn't // TODO: The actual size of RAM is REALRAM_SIZE (24MB); the other 8MB shouldn't
// be backed by actual memory. // be backed by actual memory.
// TODO: Do we want to handle the mirrors of the GC RAM?
static PhysicalMemoryRegion physical_regions[] = { static PhysicalMemoryRegion physical_regions[] = {
{&m_pRAM, 0x00000000, RAM_SIZE, PhysicalMemoryRegion::ALWAYS}, {&m_pRAM, 0x00000000, RAM_SIZE, PhysicalMemoryRegion::ALWAYS},
{&m_pL1Cache, 0xE0000000, L1_CACHE_SIZE, PhysicalMemoryRegion::ALWAYS}, {&m_pL1Cache, 0xE0000000, L1_CACHE_SIZE, PhysicalMemoryRegion::ALWAYS},
@ -164,7 +166,7 @@ static PhysicalMemoryRegion physical_regions[] = {
static std::vector<LogicalMemoryView> logical_mapped_entries; static std::vector<LogicalMemoryView> logical_mapped_entries;
void Init() static u32 GetFlags()
{ {
bool wii = SConfig::GetInstance().bWii; bool wii = SConfig::GetInstance().bWii;
bool bMMU = SConfig::GetInstance().bMMU; bool bMMU = SConfig::GetInstance().bMMU;
@ -181,6 +183,14 @@ void Init()
flags |= PhysicalMemoryRegion::WII_ONLY; flags |= PhysicalMemoryRegion::WII_ONLY;
if (bFakeVMEM) if (bFakeVMEM)
flags |= PhysicalMemoryRegion::FAKE_VMEM; flags |= PhysicalMemoryRegion::FAKE_VMEM;
return flags;
}
void Init()
{
bool wii = SConfig::GetInstance().bWii;
u32 flags = GetFlags();
u32 mem_size = 0; u32 mem_size = 0;
for (PhysicalMemoryRegion& region : physical_regions) for (PhysicalMemoryRegion& region : physical_regions)
{ {
@ -190,15 +200,14 @@ void Init()
mem_size += region.size; mem_size += region.size;
} }
g_arena.GrabSHMSegment(mem_size); g_arena.GrabSHMSegment(mem_size);
physical_base = Common::MemArena::FindMemoryBase();
// Create an anonymous view of the physical memory
for (PhysicalMemoryRegion& region : physical_regions) for (PhysicalMemoryRegion& region : physical_regions)
{ {
if ((flags & region.flags) != region.flags) if ((flags & region.flags) != region.flags)
continue; continue;
u8* base = physical_base + region.physical_address; *region.out_pointer = (u8*)g_arena.CreateView(region.shm_position, region.size);
*region.out_pointer = (u8*)g_arena.CreateView(region.shm_position, region.size, base);
if (!*region.out_pointer) if (!*region.out_pointer)
{ {
@ -207,9 +216,8 @@ void Init()
} }
} }
#ifndef _ARCH_32 // TODO: Move this to the Jit
logical_base = physical_base + 0x200000000; InitFastmemArena();
#endif
if (wii) if (wii)
mmio_mapping = InitMMIOWii(); mmio_mapping = InitMMIOWii();
@ -222,8 +230,41 @@ void Init()
m_IsInitialized = true; m_IsInitialized = true;
} }
bool InitFastmemArena()
{
u32 flags = GetFlags();
physical_base = Common::MemArena::FindMemoryBase();
if (!physical_base)
return false;
for (PhysicalMemoryRegion& region : physical_regions)
{
if ((flags & region.flags) != region.flags)
continue;
u8* base = physical_base + region.physical_address;
u8* view = (u8*)g_arena.CreateView(region.shm_position, region.size, base);
if (base != view)
{
return false;
}
}
#ifndef _ARCH_32
logical_base = physical_base + 0x200000000;
#endif
is_fastmem_arena_initialized = true;
return true;
}
void UpdateLogicalMemory(const PowerPC::BatTable& dbat_table) void UpdateLogicalMemory(const PowerPC::BatTable& dbat_table)
{ {
if (!is_fastmem_arena_initialized)
return;
for (auto& entry : logical_mapped_entries) for (auto& entry : logical_mapped_entries)
{ {
g_arena.ReleaseView(entry.mapped_pointer, entry.mapped_size); g_arena.ReleaseView(entry.mapped_pointer, entry.mapped_size);
@ -279,12 +320,10 @@ void DoState(PointerWrap& p)
void Shutdown() void Shutdown()
{ {
ShutdownFastmemArena();
m_IsInitialized = false; m_IsInitialized = false;
u32 flags = 0; u32 flags = GetFlags();
if (SConfig::GetInstance().bWii)
flags |= PhysicalMemoryRegion::WII_ONLY;
if (m_pFakeVMEM)
flags |= PhysicalMemoryRegion::FAKE_VMEM;
for (PhysicalMemoryRegion& region : physical_regions) for (PhysicalMemoryRegion& region : physical_regions)
{ {
if ((flags & region.flags) != region.flags) if ((flags & region.flags) != region.flags)
@ -292,16 +331,36 @@ void Shutdown()
g_arena.ReleaseView(*region.out_pointer, region.size); g_arena.ReleaseView(*region.out_pointer, region.size);
*region.out_pointer = nullptr; *region.out_pointer = nullptr;
} }
g_arena.ReleaseSHMSegment();
mmio_mapping.reset();
INFO_LOG(MEMMAP, "Memory system shut down.");
}
void ShutdownFastmemArena()
{
if (!is_fastmem_arena_initialized)
return;
u32 flags = GetFlags();
for (PhysicalMemoryRegion& region : physical_regions)
{
if ((flags & region.flags) != region.flags)
continue;
u8* base = physical_base + region.physical_address;
g_arena.ReleaseView(base, region.size);
}
for (auto& entry : logical_mapped_entries) for (auto& entry : logical_mapped_entries)
{ {
g_arena.ReleaseView(entry.mapped_pointer, entry.mapped_size); g_arena.ReleaseView(entry.mapped_pointer, entry.mapped_size);
} }
logical_mapped_entries.clear(); logical_mapped_entries.clear();
g_arena.ReleaseSHMSegment();
physical_base = nullptr; physical_base = nullptr;
logical_base = nullptr; logical_base = nullptr;
mmio_mapping.reset();
INFO_LOG(MEMMAP, "Memory system shut down."); is_fastmem_arena_initialized = false;
} }
void Clear() void Clear()

View File

@ -62,6 +62,8 @@ extern std::unique_ptr<MMIO::Mapping> mmio_mapping;
bool IsInitialized(); bool IsInitialized();
void Init(); void Init();
void Shutdown(); void Shutdown();
bool InitFastmemArena();
void ShutdownFastmemArena();
void DoState(PointerWrap& p); void DoState(PointerWrap& p);
void UpdateLogicalMemory(const PowerPC::BatTable& dbat_table); void UpdateLogicalMemory(const PowerPC::BatTable& dbat_table);