From dcac8c1661e27d5c7dc5a8b72306646d9ad27273 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Tue, 1 Aug 2023 17:59:08 +0300 Subject: [PATCH] sceKernelAllocateDirectMemory should be ok for now --- CMakeLists.txt | 2 +- src/Core/PS4/HLE/Kernel/MemoryManagement.cpp | 41 +++++++++++++++++- src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp | 44 -------------------- src/Core/PS4/HLE/Kernel/PhysicalMemory.h | 2 +- src/main.cpp | 2 + 5 files changed, 44 insertions(+), 47 deletions(-) delete mode 100644 src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d19851e1..4200bfe5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ add_executable(shadps4 src/Lib/Threads.cpp src/Lib/Threads.h src/Core/PS4/HLE/Kernel/PhysicalMemory.h - src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceVideoOut.cpp" "src/Core/PS4/HLE/LibSceVideoOut.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/Debug.h" "src/Core/PS4/HLE/Kernel/MemoryManagement.cpp" "src/Core/PS4/HLE/Kernel/MemoryManagement.h" "src/Core/PS4/HLE/Kernel/MemMngCodes.h" "src/Util/StringUtil.cpp") find_package(OpenGL REQUIRED) diff --git a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp index 78cd3651..9ea98670 100644 --- a/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp +++ b/src/Core/PS4/HLE/Kernel/MemoryManagement.cpp @@ -8,7 +8,46 @@ #include "PhysicalMemory.h" namespace HLE::Libs::LibKernel::MemoryManagement { - + +static PhysicalMemory* g_physical_memory = nullptr; + + +void PhysicalMemoryInit() { g_physical_memory = new PhysicalMemory; } + +static u64 align_pos(u64 pos, u64 align) { return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos); } + +bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignment, u64* physAddrOut, int memoryType) { + + u64 find_free_pos = 0; + + //iterate through allocated blocked and find the next free position + if (!m_allocatedBlocks.empty()) { + for (const auto& block : m_allocatedBlocks) { + u64 n = block.start_addr + block.size; + if (n > find_free_pos) { + find_free_pos = n; + } + } + } + + //align free position + find_free_pos = align_pos(find_free_pos, alignment); + + //if the new position is between searchStart - searchEnd , allocate a new block + if (find_free_pos >= searchStart && find_free_pos + len <= searchEnd) { + AllocatedBlock block{}; + block.size = len; + block.start_addr = find_free_pos; + block.memoryType = memoryType; + + m_allocatedBlocks.push_back(block); + + *physAddrOut = find_free_pos; + return true; + } + + return false; +} bool isPowerOfTwo(u64 n) { return std::popcount(n) == 1; } bool is16KBAligned(u64 n) { return ((n % (16ull * 1024) == 0)); } diff --git a/src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp b/src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp deleted file mode 100644 index b998d118..00000000 --- a/src/Core/PS4/HLE/Kernel/PhysicalMemory.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "PhysicalMemory.h" - -namespace HLE::Libs::LibKernel::MemoryManagement { - - - -void PhysicalMemoryInit() { g_physical_memory = new PhysicalMemory; } - -static u64 align_pos(u64 pos, u64 align) { return (align != 0 ? (pos + (align - 1)) & ~(align - 1) : pos); } - -bool PhysicalMemory::Alloc(u64 searchStart, u64 searchEnd, u64 len, u64 alignment, u64* physAddrOut, int memoryType) { - - u64 find_free_pos = 0; - - //iterate through allocated blocked and find the next free position - if (!m_allocatedBlocks.empty()) { - for (const auto& b : m_allocatedBlocks) { - u64 n = b.start_addr + b.size; - if (n > find_free_pos) { - find_free_pos = n; - } - } - } - - //align free position - find_free_pos = align_pos(find_free_pos, alignment); - - //if the new position is between searchStart - searchEnd , allocate a new block - if (find_free_pos >= searchStart && find_free_pos + len <= searchEnd) { - AllocatedBlock b{}; - b.size = len; - b.start_addr = find_free_pos; - b.memoryType = memoryType; - - m_allocatedBlocks.push_back(b); - - *physAddrOut = find_free_pos; - return true; - } - - return false; -} - -} // namespace HLE::Libs::LibKernel::MemoryManagement \ No newline at end of file diff --git a/src/Core/PS4/HLE/Kernel/PhysicalMemory.h b/src/Core/PS4/HLE/Kernel/PhysicalMemory.h index bb607d3f..a71e197d 100644 --- a/src/Core/PS4/HLE/Kernel/PhysicalMemory.h +++ b/src/Core/PS4/HLE/Kernel/PhysicalMemory.h @@ -21,6 +21,6 @@ class PhysicalMemory { private: std::vector m_allocatedBlocks; }; -static PhysicalMemory* g_physical_memory = nullptr; + } // namespace HLE::Libs::LibKernel::MemoryManagement \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ab3d0c9f..e8a167d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,7 @@ #include #include "Core/PS4/HLE/Libs.h" #include "Lib/Threads.h" +#include "Core/PS4/HLE/Kernel/PhysicalMemory.h" // Main code int main(int argc, char* argv[]) @@ -41,6 +42,7 @@ int main(int argc, char* argv[]) logging::init(true);//init logging Lib::InitThreads(); + HLE::Libs::LibKernel::MemoryManagement::PhysicalMemoryInit(); const char* const path = argv[1]; //argument 1 is the path of self file to boot auto* linker = Singleton::Instance();