summaryrefslogtreecommitdiff
path: root/src/core/mem_map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/mem_map.cpp')
-rw-r--r--src/core/mem_map.cpp122
1 files changed, 112 insertions, 10 deletions
diff --git a/src/core/mem_map.cpp b/src/core/mem_map.cpp
index f99520464..5ecec9566 100644
--- a/src/core/mem_map.cpp
+++ b/src/core/mem_map.cpp
@@ -2,10 +2,16 @@
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
10#include "core/hle/config_mem.h"
11#include "core/hle/shared_page.h"
8#include "core/mem_map.h" 12#include "core/mem_map.h"
13#include "core/memory.h"
14#include "core/memory_setup.h"
9 15
10//////////////////////////////////////////////////////////////////////////////////////////////////// 16////////////////////////////////////////////////////////////////////////////////////////////////////
11 17
@@ -23,33 +29,129 @@ namespace {
23 29
24struct MemoryArea { 30struct MemoryArea {
25 u8** ptr; 31 u8** ptr;
26 size_t size; 32 u32 base;
33 u32 size;
27}; 34};
28 35
29// We don't declare the IO regions in here since its handled by other means. 36// We don't declare the IO regions in here since its handled by other means.
30static MemoryArea memory_areas[] = { 37static MemoryArea memory_areas[] = {
31 {&g_exefs_code, PROCESS_IMAGE_MAX_SIZE}, 38 {&g_exefs_code, PROCESS_IMAGE_VADDR, PROCESS_IMAGE_MAX_SIZE},
32 {&g_heap, HEAP_SIZE }, 39 {&g_heap, HEAP_VADDR, HEAP_SIZE },
33 {&g_shared_mem, SHARED_MEMORY_SIZE }, 40 {&g_shared_mem, SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE },
34 {&g_heap_linear, LINEAR_HEAP_SIZE }, 41 {&g_heap_linear, LINEAR_HEAP_VADDR, LINEAR_HEAP_SIZE },
35 {&g_vram, VRAM_SIZE }, 42 {&g_vram, VRAM_VADDR, VRAM_SIZE },
36 {&g_dsp_mem, DSP_RAM_SIZE }, 43 {&g_dsp_mem, DSP_RAM_VADDR, DSP_RAM_SIZE },
37 {&g_tls_mem, TLS_AREA_SIZE }, 44 {&g_tls_mem, TLS_AREA_VADDR, TLS_AREA_SIZE },
45};
46
47/// Represents a block of memory mapped by ControlMemory/MapMemoryBlock
48struct MemoryBlock {
49 MemoryBlock() : handle(0), base_address(0), address(0), size(0), operation(0), permissions(0) {
50 }
51 u32 handle;
52 u32 base_address;
53 u32 address;
54 u32 size;
55 u32 operation;
56 u32 permissions;
57
58 const u32 GetVirtualAddress() const{
59 return base_address + address;
60 }
38}; 61};
39 62
63static std::map<u32, MemoryBlock> heap_map;
64static std::map<u32, MemoryBlock> heap_linear_map;
65
66}
67
68u32 MapBlock_Heap(u32 size, u32 operation, u32 permissions) {
69 MemoryBlock block;
70
71 block.base_address = HEAP_VADDR;
72 block.size = size;
73 block.operation = operation;
74 block.permissions = permissions;
75
76 if (heap_map.size() > 0) {
77 const MemoryBlock last_block = heap_map.rbegin()->second;
78 block.address = last_block.address + last_block.size;
79 }
80 heap_map[block.GetVirtualAddress()] = block;
81
82 return block.GetVirtualAddress();
83}
84
85u32 MapBlock_HeapLinear(u32 size, u32 operation, u32 permissions) {
86 MemoryBlock block;
87
88 block.base_address = LINEAR_HEAP_VADDR;
89 block.size = size;
90 block.operation = operation;
91 block.permissions = permissions;
92
93 if (heap_linear_map.size() > 0) {
94 const MemoryBlock last_block = heap_linear_map.rbegin()->second;
95 block.address = last_block.address + last_block.size;
96 }
97 heap_linear_map[block.GetVirtualAddress()] = block;
98
99 return block.GetVirtualAddress();
100}
101
102PAddr VirtualToPhysicalAddress(const VAddr addr) {
103 if (addr == 0) {
104 return 0;
105 } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
106 return addr - VRAM_VADDR + VRAM_PADDR;
107 } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
108 return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
109 } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
110 return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
111 } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
112 return addr - IO_AREA_VADDR + IO_AREA_PADDR;
113 }
114
115 LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08x", addr);
116 // To help with debugging, set bit on address so that it's obviously invalid.
117 return addr | 0x80000000;
118}
119
120VAddr PhysicalToVirtualAddress(const PAddr addr) {
121 if (addr == 0) {
122 return 0;
123 } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
124 return addr - VRAM_PADDR + VRAM_VADDR;
125 } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
126 return addr - FCRAM_PADDR + LINEAR_HEAP_VADDR;
127 } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
128 return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
129 } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
130 return addr - IO_AREA_PADDR + IO_AREA_VADDR;
131 }
132
133 LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08x", addr);
134 // To help with debugging, set bit on address so that it's obviously invalid.
135 return addr | 0x80000000;
40} 136}
41 137
42void Init() { 138void Init() {
139 InitMemoryMap();
140
43 for (MemoryArea& area : memory_areas) { 141 for (MemoryArea& area : memory_areas) {
44 *area.ptr = new u8[area.size]; 142 *area.ptr = new u8[area.size];
143 MapMemoryRegion(area.base, area.size, *area.ptr);
45 } 144 }
46 MemBlock_Init(); 145 MapMemoryRegion(CONFIG_MEMORY_VADDR, CONFIG_MEMORY_SIZE, (u8*)&ConfigMem::config_mem);
146 MapMemoryRegion(SHARED_PAGE_VADDR, SHARED_PAGE_SIZE, (u8*)&SharedPage::shared_page);
47 147
48 LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p", g_heap); 148 LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p", g_heap);
49} 149}
50 150
51void Shutdown() { 151void Shutdown() {
52 MemBlock_Shutdown(); 152 heap_map.clear();
153 heap_linear_map.clear();
154
53 for (MemoryArea& area : memory_areas) { 155 for (MemoryArea& area : memory_areas) {
54 delete[] *area.ptr; 156 delete[] *area.ptr;
55 *area.ptr = nullptr; 157 *area.ptr = nullptr;