diff options
author | Jesse Talavera-Greenberg <jesse@jesse.tg> | 2023-11-18 10:40:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-18 16:40:54 +0100 |
commit | 544fefa27f698f3a0d799a782dc03d3eb47561db (patch) | |
tree | b4907fca30677cc4e1befb02301392f172eed543 /src/ARMJIT_Memory.h | |
parent | f2d7a290156b5aa62edc00644c55b00de73b6229 (diff) |
Refactor the JIT to be object-oriented (#1879)
* Move TinyVector to a new file
- So it's less sensitive to #include ordering
* Forgot to include assert.h
* Refactor ARMJIT_Memory into an object
* Oops, forgot a declaration
* Refactor ARMJIT to be contained in an object
* Remove an unused function declaration
* Add a missing #include
* Remove a now-unused global
* Use ARMJIT_Memory's own memory access functions
* Fix some omissions in the ARM JIT
* Move libandroid to be a member of ARMJIT_Memory instead of a global
* Default-initialize most fields in ARMJIT_Compiler.h
* Define NOOP_IF_NO_JIT
* Finish refactoring the JIT to be object-oriented
Diffstat (limited to 'src/ARMJIT_Memory.h')
-rw-r--r-- | src/ARMJIT_Memory.h | 243 |
1 files changed, 195 insertions, 48 deletions
diff --git a/src/ARMJIT_Memory.h b/src/ARMJIT_Memory.h index 6a7e13d..1cf4f81 100644 --- a/src/ARMJIT_Memory.h +++ b/src/ARMJIT_Memory.h @@ -20,62 +20,209 @@ #define ARMJIT_MEMORY #include "types.h" +#include "TinyVector.h" #include "ARM.h" +#include "DSi.h" + +#if defined(__SWITCH__) +#include <switch.h> +#elif defined(_WIN32) +#include <windows.h> +#else +#include <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <signal.h> +#endif -namespace ARMJIT_Memory -{ +#ifndef JIT_ENABLED +#include <array> +#include "NDS.h" +#endif -extern void* FastMem9Start; -extern void* FastMem7Start; +namespace ARMJIT +{ +class Compiler; +class ARMJIT; +} -void Init(); -void DeInit(); +constexpr u32 RoundUp(u32 size) noexcept +{ +#ifdef _WIN32 + return (size + 0xFFFF) & ~0xFFFF; +#else + return size; +#endif +} -void Reset(); +const u32 MemBlockMainRAMOffset = 0; +const u32 MemBlockSWRAMOffset = RoundUp(NDS::MainRAMMaxSize); +const u32 MemBlockARM7WRAMOffset = MemBlockSWRAMOffset + RoundUp(NDS::SharedWRAMSize); +const u32 MemBlockDTCMOffset = MemBlockARM7WRAMOffset + RoundUp(NDS::ARM7WRAMSize); +const u32 MemBlockNWRAM_AOffset = MemBlockDTCMOffset + RoundUp(DTCMPhysicalSize); +const u32 MemBlockNWRAM_BOffset = MemBlockNWRAM_AOffset + RoundUp(DSi::NWRAMSize); +const u32 MemBlockNWRAM_COffset = MemBlockNWRAM_BOffset + RoundUp(DSi::NWRAMSize); +const u32 MemoryTotalSize = MemBlockNWRAM_COffset + RoundUp(DSi::NWRAMSize); -enum +class ARMJIT_Memory { - memregion_Other = 0, - memregion_ITCM, - memregion_DTCM, - memregion_BIOS9, - memregion_MainRAM, - memregion_SharedWRAM, - memregion_IO9, - memregion_VRAM, - memregion_BIOS7, - memregion_WRAM7, - memregion_IO7, - memregion_Wifi, - memregion_VWRAM, - - // DSi - memregion_BIOS9DSi, - memregion_BIOS7DSi, - memregion_NewSharedWRAM_A, - memregion_NewSharedWRAM_B, - memregion_NewSharedWRAM_C, - - memregions_Count +public: + enum + { + memregion_Other = 0, + memregion_ITCM, + memregion_DTCM, + memregion_BIOS9, + memregion_MainRAM, + memregion_SharedWRAM, + memregion_IO9, + memregion_VRAM, + memregion_BIOS7, + memregion_WRAM7, + memregion_IO7, + memregion_Wifi, + memregion_VWRAM, + + // DSi + memregion_BIOS9DSi, + memregion_BIOS7DSi, + memregion_NewSharedWRAM_A, + memregion_NewSharedWRAM_B, + memregion_NewSharedWRAM_C, + + memregions_Count + }; + +#ifdef JIT_ENABLED +public: + explicit ARMJIT_Memory(ARMJIT::ARMJIT& jit) noexcept; + ~ARMJIT_Memory() noexcept; + ARMJIT_Memory(const ARMJIT_Memory&) = delete; + ARMJIT_Memory(ARMJIT_Memory&&) = delete; + ARMJIT_Memory& operator=(const ARMJIT_Memory&) = delete; + ARMJIT_Memory& operator=(ARMJIT_Memory&&) = delete; + void Reset() noexcept; + void RemapDTCM(u32 newBase, u32 newSize) noexcept; + void RemapSWRAM() noexcept; + void RemapNWRAM(int num) noexcept; + void SetCodeProtection(int region, u32 offset, bool protect) noexcept; + + [[nodiscard]] u8* GetMainRAM() noexcept { return MemoryBase + MemBlockMainRAMOffset; } + [[nodiscard]] const u8* GetMainRAM() const noexcept { return MemoryBase + MemBlockMainRAMOffset; } + + [[nodiscard]] u8* GetSharedWRAM() noexcept { return MemoryBase + MemBlockSWRAMOffset; } + [[nodiscard]] const u8* GetSharedWRAM() const noexcept { return MemoryBase + MemBlockSWRAMOffset; } + + [[nodiscard]] u8* GetARM7WRAM() noexcept { return MemoryBase + MemBlockARM7WRAMOffset; } + [[nodiscard]] const u8* GetARM7WRAM() const noexcept { return MemoryBase + MemBlockARM7WRAMOffset; } + + [[nodiscard]] u8* GetARM9DTCM() noexcept { return MemoryBase + MemBlockDTCMOffset; } + [[nodiscard]] const u8* GetARM9DTCM() const noexcept { return MemoryBase + MemBlockDTCMOffset; } + + [[nodiscard]] u8* GetNWRAM_A() noexcept { return MemoryBase + MemBlockNWRAM_AOffset; } + [[nodiscard]] const u8* GetNWRAM_A() const noexcept { return MemoryBase + MemBlockNWRAM_AOffset; } + + [[nodiscard]] u8* GetNWRAM_B() noexcept { return MemoryBase + MemBlockNWRAM_BOffset; } + [[nodiscard]] const u8* GetNWRAM_B() const noexcept { return MemoryBase + MemBlockNWRAM_BOffset; } + + [[nodiscard]] u8* GetNWRAM_C() noexcept { return MemoryBase + MemBlockNWRAM_COffset; } + [[nodiscard]] const u8* GetNWRAM_C() const noexcept { return MemoryBase + MemBlockNWRAM_COffset; } + + int ClassifyAddress9(u32 addr) const noexcept; + int ClassifyAddress7(u32 addr) const noexcept; + bool GetMirrorLocation(int region, u32 num, u32 addr, u32& memoryOffset, u32& mirrorStart, u32& mirrorSize) const noexcept; + u32 LocaliseAddress(int region, u32 num, u32 addr) const noexcept; + bool IsFastmemCompatible(int region) const noexcept; + void* GetFuncForAddr(ARM* cpu, u32 addr, bool store, int size) const noexcept; + bool MapAtAddress(u32 addr) noexcept; +private: + friend class ARMJIT::Compiler; + struct Mapping + { + u32 Addr; + u32 Size, LocalOffset; + u32 Num; + + void Unmap(int region, ARMJIT_Memory& memory) noexcept; + }; + + struct FaultDescription + { + u32 EmulatedFaultAddr; + u8* FaultPC; + }; + static bool FaultHandler(FaultDescription& faultDesc, ARMJIT::ARMJIT& jit); + bool MapIntoRange(u32 addr, u32 num, u32 offset, u32 size) noexcept; + bool UnmapFromRange(u32 addr, u32 num, u32 offset, u32 size) noexcept; + void SetCodeProtectionRange(u32 addr, u32 size, u32 num, int protection) noexcept; + + ARMJIT::ARMJIT& JIT; + void* FastMem9Start; + void* FastMem7Start; + u8* MemoryBase = nullptr; +#if defined(__SWITCH__) + VirtmemReservation* FastMem9Reservation, *FastMem7Reservation; + u8* MemoryBaseCodeMem; +#elif defined(_WIN32) + static LONG ExceptionHandler(EXCEPTION_POINTERS* exceptionInfo); + HANDLE MemoryFile = INVALID_HANDLE_VALUE; + LPVOID ExceptionHandlerHandle = nullptr; +#else + static void SigsegvHandler(int sig, siginfo_t* info, void* rawContext); + int MemoryFile = -1; +#endif +#ifdef ANDROID + Platform::DynamicLibrary* Libandroid = nullptr; +#endif + u8 MappingStatus9[1 << (32-12)] {}; + u8 MappingStatus7[1 << (32-12)] {}; + ARMJIT::TinyVector<Mapping> Mappings[memregions_Count] {}; +#else +public: + explicit ARMJIT_Memory(ARMJIT::ARMJIT&) {}; + ~ARMJIT_Memory() = default; + ARMJIT_Memory(const ARMJIT_Memory&) = delete; + ARMJIT_Memory(ARMJIT_Memory&&) = delete; + ARMJIT_Memory& operator=(const ARMJIT_Memory&) = delete; + ARMJIT_Memory& operator=(ARMJIT_Memory&&) = delete; + + void Reset() noexcept {} + void RemapDTCM(u32 newBase, u32 newSize) noexcept {} + void RemapSWRAM() noexcept {} + void RemapNWRAM(int num) noexcept {} + void SetCodeProtection(int region, u32 offset, bool protect) noexcept {} + + [[nodiscard]] u8* GetMainRAM() noexcept { return MainRAM.data(); } + [[nodiscard]] const u8* GetMainRAM() const noexcept { return MainRAM.data(); } + + [[nodiscard]] u8* GetSharedWRAM() noexcept { return SharedWRAM.data(); } + [[nodiscard]] const u8* GetSharedWRAM() const noexcept { return SharedWRAM.data(); } + + [[nodiscard]] u8* GetARM7WRAM() noexcept { return ARM7WRAM.data(); } + [[nodiscard]] const u8* GetARM7WRAM() const noexcept { return ARM7WRAM.data(); } + + [[nodiscard]] u8* GetARM9DTCM() noexcept { return DTCM.data(); } + [[nodiscard]] const u8* GetARM9DTCM() const noexcept { return DTCM.data(); } + + [[nodiscard]] u8* GetNWRAM_A() noexcept { return NWRAM_A.data(); } + [[nodiscard]] const u8* GetNWRAM_A() const noexcept { return NWRAM_A.data(); } + + [[nodiscard]] u8* GetNWRAM_B() noexcept { return NWRAM_B.data(); } + [[nodiscard]] const u8* GetNWRAM_B() const noexcept { return NWRAM_B.data(); } + + [[nodiscard]] u8* GetNWRAM_C() noexcept { return NWRAM_C.data(); } + [[nodiscard]] const u8* GetNWRAM_C() const noexcept { return NWRAM_C.data(); } +private: + std::array<u8, NDS::MainRAMMaxSize> MainRAM {}; + std::array<u8, NDS::ARM7WRAMSize> ARM7WRAM {}; + std::array<u8, NDS::SharedWRAMSize> SharedWRAM {}; + std::array<u8, DTCMPhysicalSize> DTCM {}; + std::array<u8, DSi::NWRAMSize> NWRAM_A {}; + std::array<u8, DSi::NWRAMSize> NWRAM_B {}; + std::array<u8, DSi::NWRAMSize> NWRAM_C {}; +#endif }; -int ClassifyAddress9(u32 addr); -int ClassifyAddress7(u32 addr); - -bool GetMirrorLocation(int region, u32 num, u32 addr, u32& memoryOffset, u32& mirrorStart, u32& mirrorSize); -u32 LocaliseAddress(int region, u32 num, u32 addr); - -bool IsFastmemCompatible(int region); - -void RemapDTCM(u32 newBase, u32 newSize); -void RemapSWRAM(); -void RemapNWRAM(int num); - -void SetCodeProtection(int region, u32 offset, bool protect); - -void* GetFuncForAddr(ARM* cpu, u32 addr, bool store, int size); - -} - #endif |