memory.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <array>
  6. #include <cstddef>
  7. #include <string>
  8. #include "common/common_types.h"
  9. namespace Memory {
  10. /**
  11. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  12. * be mapped.
  13. */
  14. const u32 PAGE_SIZE = 0x1000;
  15. const u32 PAGE_MASK = PAGE_SIZE - 1;
  16. const int PAGE_BITS = 12;
  17. const size_t PAGE_TABLE_NUM_ENTRIES = 1 << (32 - PAGE_BITS);
  18. /// Physical memory regions as seen from the ARM11
  19. enum : PAddr {
  20. /// IO register area
  21. IO_AREA_PADDR = 0x10100000,
  22. IO_AREA_SIZE = 0x01000000, ///< IO area size (16MB)
  23. IO_AREA_PADDR_END = IO_AREA_PADDR + IO_AREA_SIZE,
  24. /// MPCore internal memory region
  25. MPCORE_RAM_PADDR = 0x17E00000,
  26. MPCORE_RAM_SIZE = 0x00002000, ///< MPCore internal memory size (8KB)
  27. MPCORE_RAM_PADDR_END = MPCORE_RAM_PADDR + MPCORE_RAM_SIZE,
  28. /// Video memory
  29. VRAM_PADDR = 0x18000000,
  30. VRAM_SIZE = 0x00600000, ///< VRAM size (6MB)
  31. VRAM_PADDR_END = VRAM_PADDR + VRAM_SIZE,
  32. /// New 3DS additional memory. Supposedly faster than regular FCRAM. Part of it can be used by
  33. /// applications and system modules if mapped via the ExHeader.
  34. N3DS_EXTRA_RAM_PADDR = 0x1F000000,
  35. N3DS_EXTRA_RAM_SIZE = 0x00400000, ///< New 3DS additional memory size (4MB)
  36. N3DS_EXTRA_RAM_PADDR_END = N3DS_EXTRA_RAM_PADDR + N3DS_EXTRA_RAM_SIZE,
  37. /// DSP memory
  38. DSP_RAM_PADDR = 0x1FF00000,
  39. DSP_RAM_SIZE = 0x00080000, ///< DSP memory size (512KB)
  40. DSP_RAM_PADDR_END = DSP_RAM_PADDR + DSP_RAM_SIZE,
  41. /// AXI WRAM
  42. AXI_WRAM_PADDR = 0x1FF80000,
  43. AXI_WRAM_SIZE = 0x00080000, ///< AXI WRAM size (512KB)
  44. AXI_WRAM_PADDR_END = AXI_WRAM_PADDR + AXI_WRAM_SIZE,
  45. /// Main FCRAM
  46. FCRAM_PADDR = 0x20000000,
  47. FCRAM_SIZE = 0x08000000, ///< FCRAM size (128MB)
  48. FCRAM_PADDR_END = FCRAM_PADDR + FCRAM_SIZE,
  49. };
  50. /// Virtual user-space memory regions
  51. enum : VAddr {
  52. /// Where the application text, data and bss reside.
  53. PROCESS_IMAGE_VADDR = 0x00100000,
  54. PROCESS_IMAGE_MAX_SIZE = 0x03F00000,
  55. PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
  56. /// Area where IPC buffers are mapped onto.
  57. IPC_MAPPING_VADDR = 0x04000000,
  58. IPC_MAPPING_SIZE = 0x04000000,
  59. IPC_MAPPING_VADDR_END = IPC_MAPPING_VADDR + IPC_MAPPING_SIZE,
  60. /// Application heap (includes stack).
  61. HEAP_VADDR = 0x08000000,
  62. HEAP_SIZE = 0x08000000,
  63. HEAP_VADDR_END = HEAP_VADDR + HEAP_SIZE,
  64. /// Area where shared memory buffers are mapped onto.
  65. SHARED_MEMORY_VADDR = 0x10000000,
  66. SHARED_MEMORY_SIZE = 0x04000000,
  67. SHARED_MEMORY_VADDR_END = SHARED_MEMORY_VADDR + SHARED_MEMORY_SIZE,
  68. /// Maps 1:1 to an offset in FCRAM. Used for HW allocations that need to be linear in physical
  69. /// memory.
  70. LINEAR_HEAP_VADDR = 0x14000000,
  71. LINEAR_HEAP_SIZE = 0x08000000,
  72. LINEAR_HEAP_VADDR_END = LINEAR_HEAP_VADDR + LINEAR_HEAP_SIZE,
  73. /// Maps 1:1 to New 3DS additional memory
  74. N3DS_EXTRA_RAM_VADDR = 0x1E800000,
  75. N3DS_EXTRA_RAM_VADDR_END = N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_SIZE,
  76. /// Maps 1:1 to the IO register area.
  77. IO_AREA_VADDR = 0x1EC00000,
  78. IO_AREA_VADDR_END = IO_AREA_VADDR + IO_AREA_SIZE,
  79. /// Maps 1:1 to VRAM.
  80. VRAM_VADDR = 0x1F000000,
  81. VRAM_VADDR_END = VRAM_VADDR + VRAM_SIZE,
  82. /// Maps 1:1 to DSP memory.
  83. DSP_RAM_VADDR = 0x1FF00000,
  84. DSP_RAM_VADDR_END = DSP_RAM_VADDR + DSP_RAM_SIZE,
  85. /// Read-only page containing kernel and system configuration values.
  86. CONFIG_MEMORY_VADDR = 0x1FF80000,
  87. CONFIG_MEMORY_SIZE = 0x00001000,
  88. CONFIG_MEMORY_VADDR_END = CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE,
  89. /// Usually read-only page containing mostly values read from hardware.
  90. SHARED_PAGE_VADDR = 0x1FF81000,
  91. SHARED_PAGE_SIZE = 0x00001000,
  92. SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
  93. /// Area where TLS (Thread-Local Storage) buffers are allocated.
  94. TLS_AREA_VADDR = 0x1FF82000,
  95. TLS_ENTRY_SIZE = 0x200,
  96. /// Equivalent to LINEAR_HEAP_VADDR, but expanded to cover the extra memory in the New 3DS.
  97. NEW_LINEAR_HEAP_VADDR = 0x30000000,
  98. NEW_LINEAR_HEAP_SIZE = 0x10000000,
  99. NEW_LINEAR_HEAP_VADDR_END = NEW_LINEAR_HEAP_VADDR + NEW_LINEAR_HEAP_SIZE,
  100. };
  101. bool IsValidVirtualAddress(const VAddr addr);
  102. bool IsValidPhysicalAddress(const PAddr addr);
  103. u8 Read8(VAddr addr);
  104. u16 Read16(VAddr addr);
  105. u32 Read32(VAddr addr);
  106. u64 Read64(VAddr addr);
  107. void Write8(VAddr addr, u8 data);
  108. void Write16(VAddr addr, u16 data);
  109. void Write32(VAddr addr, u32 data);
  110. void Write64(VAddr addr, u64 data);
  111. void ReadBlock(const VAddr src_addr, void* dest_buffer, size_t size);
  112. void WriteBlock(const VAddr dest_addr, const void* src_buffer, size_t size);
  113. void ZeroBlock(const VAddr dest_addr, const size_t size);
  114. void CopyBlock(VAddr dest_addr, VAddr src_addr, size_t size);
  115. u8* GetPointer(VAddr virtual_address);
  116. std::string ReadCString(VAddr virtual_address, std::size_t max_length);
  117. /**
  118. * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical
  119. * address. This should be used by services to translate addresses for use by the hardware.
  120. */
  121. PAddr VirtualToPhysicalAddress(VAddr addr);
  122. /**
  123. * Undoes a mapping performed by VirtualToPhysicalAddress().
  124. */
  125. VAddr PhysicalToVirtualAddress(PAddr addr);
  126. /**
  127. * Gets a pointer to the memory region beginning at the specified physical address.
  128. *
  129. * @note This is currently implemented using PhysicalToVirtualAddress().
  130. */
  131. u8* GetPhysicalPointer(PAddr address);
  132. /**
  133. * Adds the supplied value to the rasterizer resource cache counter of each
  134. * page touching the region.
  135. */
  136. void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta);
  137. /**
  138. * Flushes any externally cached rasterizer resources touching the given region.
  139. */
  140. void RasterizerFlushRegion(PAddr start, u32 size);
  141. /**
  142. * Flushes and invalidates any externally cached rasterizer resources touching the given region.
  143. */
  144. void RasterizerFlushAndInvalidateRegion(PAddr start, u32 size);
  145. /**
  146. * Dynarmic has an optimization to memory accesses when the pointer to the page exists that
  147. * can be used by setting up the current page table as a callback. This function is used to
  148. * retrieve the current page table for that purpose.
  149. */
  150. std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers();
  151. }