mem_map.h 6.8 KB

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