diff options
| author | 2015-05-12 22:38:29 -0300 | |
|---|---|---|
| committer | 2015-05-15 00:04:38 -0300 | |
| commit | 7ada357b2d12cf616672425a8961804b865354d6 (patch) | |
| tree | 983003c1efc0950605eca3aa7d135b7c8fe9edcf /src/core/mem_map.cpp | |
| parent | Memmap: Remove unused declarations (diff) | |
| download | yuzu-7ada357b2d12cf616672425a8961804b865354d6.tar.gz yuzu-7ada357b2d12cf616672425a8961804b865354d6.tar.xz yuzu-7ada357b2d12cf616672425a8961804b865354d6.zip | |
Memmap: Re-organize memory function in two files
memory.cpp/h contains definitions related to acessing memory and
configuring the address space
mem_map.cpp/h contains higher-level definitions related to configuring
the address space accoording to the kernel and allocating memory.
Diffstat (limited to 'src/core/mem_map.cpp')
| -rw-r--r-- | src/core/mem_map.cpp | 97 |
1 files changed, 95 insertions, 2 deletions
diff --git a/src/core/mem_map.cpp b/src/core/mem_map.cpp index f99520464..66c2f77c0 100644 --- a/src/core/mem_map.cpp +++ b/src/core/mem_map.cpp | |||
| @@ -2,10 +2,13 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <map> | ||
| 6 | |||
| 5 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 6 | #include "common/logging/log.h" | 8 | #include "common/logging/log.h" |
| 7 | 9 | ||
| 8 | #include "core/mem_map.h" | 10 | #include "core/mem_map.h" |
| 11 | #include "core/memory.h" | ||
| 9 | 12 | ||
| 10 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 13 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 | 14 | ||
| @@ -37,19 +40,109 @@ static MemoryArea memory_areas[] = { | |||
| 37 | {&g_tls_mem, TLS_AREA_SIZE }, | 40 | {&g_tls_mem, TLS_AREA_SIZE }, |
| 38 | }; | 41 | }; |
| 39 | 42 | ||
| 43 | /// Represents a block of memory mapped by ControlMemory/MapMemoryBlock | ||
| 44 | struct MemoryBlock { | ||
| 45 | MemoryBlock() : handle(0), base_address(0), address(0), size(0), operation(0), permissions(0) { | ||
| 46 | } | ||
| 47 | u32 handle; | ||
| 48 | u32 base_address; | ||
| 49 | u32 address; | ||
| 50 | u32 size; | ||
| 51 | u32 operation; | ||
| 52 | u32 permissions; | ||
| 53 | |||
| 54 | const u32 GetVirtualAddress() const{ | ||
| 55 | return base_address + address; | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | |||
| 59 | static std::map<u32, MemoryBlock> heap_map; | ||
| 60 | static std::map<u32, MemoryBlock> heap_linear_map; | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | u32 MapBlock_Heap(u32 size, u32 operation, u32 permissions) { | ||
| 65 | MemoryBlock block; | ||
| 66 | |||
| 67 | block.base_address = HEAP_VADDR; | ||
| 68 | block.size = size; | ||
| 69 | block.operation = operation; | ||
| 70 | block.permissions = permissions; | ||
| 71 | |||
| 72 | if (heap_map.size() > 0) { | ||
| 73 | const MemoryBlock last_block = heap_map.rbegin()->second; | ||
| 74 | block.address = last_block.address + last_block.size; | ||
| 75 | } | ||
| 76 | heap_map[block.GetVirtualAddress()] = block; | ||
| 77 | |||
| 78 | return block.GetVirtualAddress(); | ||
| 79 | } | ||
| 80 | |||
| 81 | u32 MapBlock_HeapLinear(u32 size, u32 operation, u32 permissions) { | ||
| 82 | MemoryBlock block; | ||
| 83 | |||
| 84 | block.base_address = LINEAR_HEAP_VADDR; | ||
| 85 | block.size = size; | ||
| 86 | block.operation = operation; | ||
| 87 | block.permissions = permissions; | ||
| 88 | |||
| 89 | if (heap_linear_map.size() > 0) { | ||
| 90 | const MemoryBlock last_block = heap_linear_map.rbegin()->second; | ||
| 91 | block.address = last_block.address + last_block.size; | ||
| 92 | } | ||
| 93 | heap_linear_map[block.GetVirtualAddress()] = block; | ||
| 94 | |||
| 95 | return block.GetVirtualAddress(); | ||
| 96 | } | ||
| 97 | |||
| 98 | PAddr VirtualToPhysicalAddress(const VAddr addr) { | ||
| 99 | if (addr == 0) { | ||
| 100 | return 0; | ||
| 101 | } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) { | ||
| 102 | return addr - VRAM_VADDR + VRAM_PADDR; | ||
| 103 | } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) { | ||
| 104 | return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR; | ||
| 105 | } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) { | ||
| 106 | return addr - DSP_RAM_VADDR + DSP_RAM_PADDR; | ||
| 107 | } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) { | ||
| 108 | return addr - IO_AREA_VADDR + IO_AREA_PADDR; | ||
| 109 | } | ||
| 110 | |||
| 111 | LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08x", addr); | ||
| 112 | // To help with debugging, set bit on address so that it's obviously invalid. | ||
| 113 | return addr | 0x80000000; | ||
| 114 | } | ||
| 115 | |||
| 116 | VAddr PhysicalToVirtualAddress(const PAddr addr) { | ||
| 117 | if (addr == 0) { | ||
| 118 | return 0; | ||
| 119 | } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) { | ||
| 120 | return addr - VRAM_PADDR + VRAM_VADDR; | ||
| 121 | } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) { | ||
| 122 | return addr - FCRAM_PADDR + LINEAR_HEAP_VADDR; | ||
| 123 | } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) { | ||
| 124 | return addr - DSP_RAM_PADDR + DSP_RAM_VADDR; | ||
| 125 | } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) { | ||
| 126 | return addr - IO_AREA_PADDR + IO_AREA_VADDR; | ||
| 127 | } | ||
| 128 | |||
| 129 | LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08x", addr); | ||
| 130 | // To help with debugging, set bit on address so that it's obviously invalid. | ||
| 131 | return addr | 0x80000000; | ||
| 40 | } | 132 | } |
| 41 | 133 | ||
| 42 | void Init() { | 134 | void Init() { |
| 43 | for (MemoryArea& area : memory_areas) { | 135 | for (MemoryArea& area : memory_areas) { |
| 44 | *area.ptr = new u8[area.size]; | 136 | *area.ptr = new u8[area.size]; |
| 45 | } | 137 | } |
| 46 | MemBlock_Init(); | ||
| 47 | 138 | ||
| 48 | LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p", g_heap); | 139 | LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p", g_heap); |
| 49 | } | 140 | } |
| 50 | 141 | ||
| 51 | void Shutdown() { | 142 | void Shutdown() { |
| 52 | MemBlock_Shutdown(); | 143 | heap_map.clear(); |
| 144 | heap_linear_map.clear(); | ||
| 145 | |||
| 53 | for (MemoryArea& area : memory_areas) { | 146 | for (MemoryArea& area : memory_areas) { |
| 54 | delete[] *area.ptr; | 147 | delete[] *area.ptr; |
| 55 | *area.ptr = nullptr; | 148 | *area.ptr = nullptr; |