mem_map.h 7.3 KB

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