mem_map.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common.h"
  6. #include "common/common_types.h"
  7. namespace Memory {
  8. // TODO: It would be nice to eventually replace these with strong types that prevent accidental
  9. // conversion between each other.
  10. typedef u32 VAddr; ///< Represents a pointer in the ARM11 virtual address space.
  11. typedef u32 PAddr; ///< Represents a pointer in the physical address space.
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. enum : u32 {
  14. BOOTROM_SIZE = 0x00010000, ///< Bootrom (super secret code/data @ 0x8000) size
  15. BOOTROM_PADDR = 0x00000000, ///< Bootrom physical address
  16. BOOTROM_PADDR_END = (BOOTROM_PADDR + BOOTROM_SIZE),
  17. BOOTROM_MIRROR_SIZE = 0x00010000, ///< Bootrom Mirror size
  18. BOOTROM_MIRROR_PADDR = 0x00010000, ///< Bootrom Mirror physical address
  19. BOOTROM_MIRROR_PADDR_END = (BOOTROM_MIRROR_PADDR + BOOTROM_MIRROR_SIZE),
  20. MPCORE_PRIV_SIZE = 0x00002000, ///< MPCore private memory region size
  21. MPCORE_PRIV_PADDR = 0x17E00000, ///< MPCore private memory region physical address
  22. MPCORE_PRIV_PADDR_END = (MPCORE_PRIV_PADDR + MPCORE_PRIV_SIZE),
  23. FCRAM_SIZE = 0x08000000, ///< FCRAM size
  24. FCRAM_PADDR = 0x20000000, ///< FCRAM physical address
  25. FCRAM_PADDR_END = (FCRAM_PADDR + FCRAM_SIZE), ///< FCRAM end of physical space
  26. FCRAM_VADDR = 0x08000000, ///< FCRAM virtual address
  27. FCRAM_VADDR_END = (FCRAM_VADDR + FCRAM_SIZE), ///< FCRAM end of virtual space
  28. AXI_WRAM_SIZE = 0x00080000, ///< AXI WRAM size
  29. AXI_WRAM_PADDR = 0x1FF80000, ///< AXI WRAM physical address
  30. AXI_WRAM_PADDR_END = (AXI_WRAM_PADDR + AXI_WRAM_SIZE),
  31. SHARED_MEMORY_SIZE = 0x04000000, ///< Shared memory size
  32. SHARED_MEMORY_VADDR = 0x10000000, ///< Shared memory
  33. SHARED_MEMORY_VADDR_END = (SHARED_MEMORY_VADDR + SHARED_MEMORY_SIZE),
  34. DSP_MEMORY_SIZE = 0x00080000, ///< DSP memory size
  35. DSP_MEMORY_VADDR = 0x1FF00000, ///< DSP memory virtual address
  36. DSP_MEMORY_VADDR_END = (DSP_MEMORY_VADDR + DSP_MEMORY_SIZE),
  37. CONFIG_MEMORY_SIZE = 0x00001000, ///< Configuration memory size
  38. CONFIG_MEMORY_VADDR = 0x1FF80000, ///< Configuration memory virtual address
  39. CONFIG_MEMORY_VADDR_END = (CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE),
  40. SHARED_PAGE_SIZE = 0x00001000, ///< Shared page size
  41. SHARED_PAGE_VADDR = 0x1FF81000, ///< Shared page virtual address
  42. SHARED_PAGE_VADDR_END = (SHARED_PAGE_VADDR + SHARED_PAGE_SIZE),
  43. KERNEL_MEMORY_SIZE = 0x00001000, ///< Kernel memory size
  44. KERNEL_MEMORY_VADDR = 0xFFFF0000, ///< Kernel memory where the kthread objects etc are
  45. KERNEL_MEMORY_VADDR_END = (KERNEL_MEMORY_VADDR + KERNEL_MEMORY_SIZE),
  46. EXEFS_CODE_SIZE = 0x03F00000,
  47. EXEFS_CODE_VADDR = 0x00100000, ///< ExeFS:/.code is loaded here
  48. EXEFS_CODE_VADDR_END = (EXEFS_CODE_VADDR + EXEFS_CODE_SIZE),
  49. // Region of FCRAM used by system
  50. SYSTEM_MEMORY_SIZE = 0x02C00000, ///< 44MB
  51. SYSTEM_MEMORY_VADDR = 0x04000000,
  52. SYSTEM_MEMORY_VADDR_END = (SYSTEM_MEMORY_VADDR + SYSTEM_MEMORY_SIZE),
  53. HEAP_SIZE = FCRAM_SIZE, ///< Application heap size
  54. //HEAP_PADDR = HEAP_GSP_SIZE,
  55. //HEAP_PADDR_END = (HEAP_PADDR + HEAP_SIZE),
  56. HEAP_VADDR = 0x08000000,
  57. HEAP_VADDR_END = (HEAP_VADDR + HEAP_SIZE),
  58. HEAP_LINEAR_SIZE = 0x08000000, ///< Linear heap size... TODO: Define correctly?
  59. HEAP_LINEAR_VADDR = 0x14000000,
  60. HEAP_LINEAR_VADDR_END = (HEAP_LINEAR_VADDR + HEAP_LINEAR_SIZE),
  61. HEAP_LINEAR_PADDR = 0x00000000,
  62. HEAP_LINEAR_PADDR_END = (HEAP_LINEAR_PADDR + HEAP_LINEAR_SIZE),
  63. HARDWARE_IO_SIZE = 0x01000000,
  64. HARDWARE_IO_PADDR = 0x10000000, ///< IO physical address start
  65. HARDWARE_IO_VADDR = 0x1EC00000, ///< IO virtual address start
  66. HARDWARE_IO_PADDR_END = (HARDWARE_IO_PADDR + HARDWARE_IO_SIZE),
  67. HARDWARE_IO_VADDR_END = (HARDWARE_IO_VADDR + HARDWARE_IO_SIZE),
  68. VRAM_SIZE = 0x00600000,
  69. VRAM_PADDR = 0x18000000,
  70. VRAM_VADDR = 0x1F000000,
  71. VRAM_PADDR_END = (VRAM_PADDR + VRAM_SIZE),
  72. VRAM_VADDR_END = (VRAM_VADDR + VRAM_SIZE),
  73. SCRATCHPAD_SIZE = 0x00004000, ///< Typical stack size - TODO: Read from exheader
  74. SCRATCHPAD_VADDR_END = 0x10000000,
  75. SCRATCHPAD_VADDR = (SCRATCHPAD_VADDR_END - SCRATCHPAD_SIZE), ///< Stack space
  76. };
  77. ////////////////////////////////////////////////////////////////////////////////////////////////////
  78. /// Represents a block of memory mapped by ControlMemory/MapMemoryBlock
  79. struct MemoryBlock {
  80. MemoryBlock() : handle(0), base_address(0), address(0), size(0), operation(0), permissions(0) {
  81. }
  82. u32 handle;
  83. u32 base_address;
  84. u32 address;
  85. u32 size;
  86. u32 operation;
  87. u32 permissions;
  88. const u32 GetVirtualAddress() const{
  89. return base_address + address;
  90. }
  91. };
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////
  93. // Base is a pointer to the base of the memory map. Yes, some MMU tricks
  94. // are used to set up a full GC or Wii memory map in process memory. on
  95. // 32-bit, you have to mask your offsets with 0x3FFFFFFF. This means that
  96. // some things are mirrored too many times, but eh... it works.
  97. // In 64-bit, this might point to "high memory" (above the 32-bit limit),
  98. // so be sure to load it into a 64-bit register.
  99. extern u8 *g_base;
  100. // These are guaranteed to point to "low memory" addresses (sub-32-bit).
  101. // 64-bit: Pointers to low-mem (sub-0x10000000) mirror
  102. // 32-bit: Same as the corresponding physical/virtual pointers.
  103. extern u8* g_heap_linear; ///< Linear heap (main memory)
  104. extern u8* g_heap; ///< Application heap (main memory)
  105. extern u8* g_vram; ///< Video memory (VRAM)
  106. extern u8* g_shared_mem; ///< Shared memory
  107. extern u8* g_kernel_mem; ///< Kernel memory
  108. extern u8* g_dsp_mem; ///< DSP memory
  109. extern u8* g_system_mem; ///< System memory
  110. extern u8* g_exefs_code; ///< ExeFS:/.code is loaded here
  111. void Init();
  112. void Shutdown();
  113. template <typename T>
  114. inline void Read(T &var, VAddr addr);
  115. template <typename T>
  116. inline void Write(VAddr addr, T data);
  117. u8 Read8(VAddr addr);
  118. u16 Read16(VAddr addr);
  119. u32 Read32(VAddr addr);
  120. u32 Read8_ZX(VAddr addr);
  121. u32 Read16_ZX(VAddr addr);
  122. void Write8(VAddr addr, u8 data);
  123. void Write16(VAddr addr, u16 data);
  124. void Write32(VAddr addr, u32 data);
  125. void Write64(VAddr addr, u64 data);
  126. void WriteBlock(VAddr addr, const u8* data, size_t size);
  127. u8* GetPointer(VAddr virtual_address);
  128. /**
  129. * Maps a block of memory on the heap
  130. * @param size Size of block in bytes
  131. * @param operation Memory map operation type
  132. * @param permissions Memory allocation permissions
  133. */
  134. u32 MapBlock_Heap(u32 size, u32 operation, u32 permissions);
  135. /**
  136. * Maps a block of memory on the GSP heap
  137. * @param size Size of block in bytes
  138. * @param operation Memory map operation type
  139. * @param permissions Control memory permissions
  140. */
  141. u32 MapBlock_HeapLinear(u32 size, u32 operation, u32 permissions);
  142. inline const char* GetCharPointer(const VAddr address) {
  143. return (const char *)GetPointer(address);
  144. }
  145. /// Converts a physical address to virtual address
  146. VAddr PhysicalToVirtualAddress(PAddr addr);
  147. /// Converts a virtual address to physical address
  148. PAddr VirtualToPhysicalAddress(VAddr addr);
  149. } // namespace
  150. // These are used often, so re-export then on the root namespace
  151. using Memory::VAddr;
  152. using Memory::PAddr;