mem_map.h 6.9 KB

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