mem_map.h 7.7 KB

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