memory.h 5.8 KB

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