diff --git a/libctru/include/3ds/os.h b/libctru/include/3ds/os.h index 690ce7a..e94feb1 100644 --- a/libctru/include/3ds/os.h +++ b/libctru/include/3ds/os.h @@ -26,6 +26,36 @@ /// Retrieves the revision version from a packed system version. #define GET_VERSION_REVISION(version) (((version)>> 8)&0xFF) +#define OS_HEAP_AREA_BEGIN 0x08000000 ///< Start of the heap area in the virtual address space +#define OS_HEAP_AREA_END 0x0E000000 ///< End of the heap area in the virtual address space + +#define OS_MAP_AREA_BEGIN 0x10000000 ///< Start of the mappable area in the virtual address space +#define OS_MAP_AREA_END 0x14000000 ///< End of the mappable area in the virtual address space + +#define OS_OLD_FCRAM_VADDR 0x14000000 ///< Old pre-8.x linear FCRAM mapping virtual address +#define OS_OLD_FCRAM_PADDR 0x20000000 ///< Old pre-8.x linear FCRAM mapping physical address +#define OS_OLD_FCRAM_SIZE 0x8000000 ///< Old pre-8.x linear FCRAM mapping size (128 MiB) + +#define OS_QTMRAM_VADDR 0x1E800000 ///< New3DS QTM memory virtual address +#define OS_QTMRAM_PADDR 0x1F000000 ///< New3DS QTM memory physical address +#define OS_QTMRAM_SIZE 0x400000 ///< New3DS QTM memory size (4 MiB; last 128 KiB reserved by kernel) + +#define OS_MMIO_VADDR 0x1EC00000 ///< Memory mapped IO range virtual address +#define OS_MMIO_PADDR 0x10100000 ///< Memory mapped IO range physical address +#define OS_MMIO_SIZE 0x400000 ///< Memory mapped IO range size (4 MiB) + +#define OS_VRAM_VADDR 0x1F000000 ///< VRAM virtual address +#define OS_VRAM_PADDR 0x18000000 ///< VRAM physical address +#define OS_VRAM_SIZE 0x600000 ///< VRAM size (6 MiB) + +#define OS_DSPRAM_VADDR 0x1FF00000 ///< DSP memory virtual address +#define OS_DSPRAM_PADDR 0x1FF00000 ///< DSP memory physical address +#define OS_DSPRAM_SIZE 0x80000 ///< DSP memory size (512 KiB) + +#define OS_FCRAM_VADDR 0x30000000 ///< Linear FCRAM mapping virtual address +#define OS_FCRAM_PADDR 0x20000000 ///< Linear FCRAM mapping physical address +#define OS_FCRAM_SIZE 0x10000000 ///< Linear FCRAM mapping size (256 MiB) + /// Tick counter. typedef struct { diff --git a/libctru/source/allocator/vram.cpp b/libctru/source/allocator/vram.cpp index 30498a5..14838fa 100644 --- a/libctru/source/allocator/vram.cpp +++ b/libctru/source/allocator/vram.cpp @@ -1,6 +1,7 @@ extern "C" { #include <3ds/types.h> + #include <3ds/os.h> #include <3ds/allocator/vram.h> #include <3ds/util/rbtree.h> } @@ -12,7 +13,7 @@ static MemPool sVramPool; static bool vramInit() { - auto blk = MemBlock::Create((u8*)0x1F000000, 0x00600000); + auto blk = MemBlock::Create((u8*)OS_VRAM_VADDR, OS_VRAM_SIZE); if (blk) { sVramPool.AddBlock(blk); diff --git a/libctru/source/os.c b/libctru/source/os.c index 0eabfd4..3b95a17 100644 --- a/libctru/source/os.c +++ b/libctru/source/os.c @@ -29,14 +29,18 @@ __attribute__((weak)) bool __ctru_speedup = false; u32 osConvertVirtToPhys(const void* addr) { //--------------------------------------------------------------------------------- u32 vaddr = (u32)addr; - if(vaddr >= 0x14000000 && vaddr < 0x1c000000) - return vaddr + 0x0c000000; // LINEAR heap - if(vaddr >= 0x1F000000 && vaddr < 0x1F600000) - return vaddr - 0x07000000; // VRAM - if(vaddr >= 0x1FF00000 && vaddr < 0x1FF80000) - return vaddr + 0x00000000; // DSP memory - if(vaddr >= 0x30000000 && vaddr < 0x40000000) - return vaddr - 0x10000000; // Only available under FIRM v8+ for certain processes. +#define CONVERT_REGION(_name) \ + if (vaddr >= OS_##_name##_VADDR && vaddr < (OS_##_name##_VADDR + OS_##_name##_SIZE)) \ + return vaddr + (OS_##_name##_PADDR - OS_##_name##_VADDR); + + CONVERT_REGION(FCRAM); + CONVERT_REGION(VRAM); + CONVERT_REGION(OLD_FCRAM); + CONVERT_REGION(DSPRAM); + CONVERT_REGION(QTMRAM); + CONVERT_REGION(MMIO); + +#undef CONVERT_REGION return 0; } @@ -44,9 +48,11 @@ u32 osConvertVirtToPhys(const void* addr) { void* osConvertOldLINEARMemToNew(const void* addr) { //--------------------------------------------------------------------------------- u32 vaddr = (u32)addr; - if(vaddr >= 0x30000000 && vaddr < 0x40000000)return (void*)vaddr; - if(vaddr >= 0x14000000 && vaddr < 0x1c000000)return (void*)(vaddr+0x1c000000); - return 0; + if (vaddr >= OS_FCRAM_VADDR && vaddr < (OS_FCRAM_VADDR+OS_FCRAM_SIZE)) + return (void*)vaddr; + if (vaddr >= OS_OLD_FCRAM_VADDR && vaddr < (OS_FCRAM_VADDR+OS_OLD_FCRAM_SIZE)) + return (void*)(vaddr + (OS_FCRAM_VADDR-OS_OLD_FCRAM_VADDR)); + return NULL; } //--------------------------------------------------------------------------------- diff --git a/libctru/source/system/allocateHeaps.c b/libctru/source/system/allocateHeaps.c index d35b4d6..0c8478d 100644 --- a/libctru/source/system/allocateHeaps.c +++ b/libctru/source/system/allocateHeaps.c @@ -33,14 +33,14 @@ void __attribute__((weak)) __system_allocateHeaps(void) { } // Allocate the application heap - __ctru_heap = 0x08000000; + __ctru_heap = OS_HEAP_AREA_BEGIN; svcControlMemory(&tmp, __ctru_heap, 0x0, __ctru_heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE); // Allocate the linear heap svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE); // Mappable allocator init - mappableInit(0x10000000, 0x14000000); + mappableInit(OS_MAP_AREA_BEGIN, OS_MAP_AREA_END); // Set up newlib heap fake_heap_start = (char*)__ctru_heap;