DeviceMemory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using ARMeilleure.Memory;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.CompilerServices;
  5. namespace Ryujinx.HLE
  6. {
  7. class DeviceMemory : IDisposable
  8. {
  9. public const long RamSize = 4L * 1024 * 1024 * 1024;
  10. public IntPtr RamPointer { get; }
  11. private unsafe byte* _ramPtr;
  12. public unsafe DeviceMemory()
  13. {
  14. RamPointer = MemoryManagement.AllocateWriteTracked(RamSize);
  15. _ramPtr = (byte*)RamPointer;
  16. }
  17. public sbyte ReadSByte(long position)
  18. {
  19. return (sbyte)ReadByte(position);
  20. }
  21. public short ReadInt16(long position)
  22. {
  23. return (short)ReadUInt16(position);
  24. }
  25. public int ReadInt32(long position)
  26. {
  27. return (int)ReadUInt32(position);
  28. }
  29. public long ReadInt64(long position)
  30. {
  31. return (long)ReadUInt64(position);
  32. }
  33. public unsafe byte ReadByte(long position)
  34. {
  35. return *(_ramPtr + position);
  36. }
  37. public unsafe ushort ReadUInt16(long position)
  38. {
  39. return *((ushort*)(_ramPtr + position));
  40. }
  41. public unsafe uint ReadUInt32(long position)
  42. {
  43. return *((uint*)(_ramPtr + position));
  44. }
  45. public unsafe ulong ReadUInt64(long position)
  46. {
  47. return *((ulong*)(_ramPtr + position));
  48. }
  49. public unsafe T ReadStruct<T>(long position)
  50. {
  51. return Marshal.PtrToStructure<T>((IntPtr)(_ramPtr + position));
  52. }
  53. public unsafe ref T GetStructRef<T>(long position)
  54. {
  55. return ref Unsafe.AsRef<T>((void*)(IntPtr)(_ramPtr + position));
  56. }
  57. public void WriteSByte(long position, sbyte value)
  58. {
  59. WriteByte(position, (byte)value);
  60. }
  61. public void WriteInt16(long position, short value)
  62. {
  63. WriteUInt16(position, (ushort)value);
  64. }
  65. public void WriteInt32(long position, int value)
  66. {
  67. WriteUInt32(position, (uint)value);
  68. }
  69. public void WriteInt64(long position, long value)
  70. {
  71. WriteUInt64(position, (ulong)value);
  72. }
  73. public unsafe void WriteByte(long position, byte value)
  74. {
  75. *(_ramPtr + position) = value;
  76. }
  77. public unsafe void WriteUInt16(long position, ushort value)
  78. {
  79. *((ushort*)(_ramPtr + position)) = value;
  80. }
  81. public unsafe void WriteUInt32(long position, uint value)
  82. {
  83. *((uint*)(_ramPtr + position)) = value;
  84. }
  85. public unsafe void WriteUInt64(long position, ulong value)
  86. {
  87. *((ulong*)(_ramPtr + position)) = value;
  88. }
  89. public unsafe void WriteStruct<T>(long position, T value)
  90. {
  91. Marshal.StructureToPtr(value, (IntPtr)(_ramPtr + position), false);
  92. }
  93. public void FillWithZeros(long position, int size)
  94. {
  95. int size8 = size & ~(8 - 1);
  96. for (int offs = 0; offs < size8; offs += 8)
  97. {
  98. WriteInt64(position + offs, 0);
  99. }
  100. for (int offs = size8; offs < (size - size8); offs++)
  101. {
  102. WriteByte(position + offs, 0);
  103. }
  104. }
  105. public void Set(ulong address, byte value, ulong size)
  106. {
  107. if (address + size < address)
  108. {
  109. throw new ArgumentOutOfRangeException(nameof(size));
  110. }
  111. if (address + size > RamSize)
  112. {
  113. throw new ArgumentOutOfRangeException(nameof(address));
  114. }
  115. ulong size8 = size & ~7UL;
  116. ulong valueRep = (ulong)value * 0x0101010101010101;
  117. for (ulong offs = 0; offs < size8; offs += 8)
  118. {
  119. WriteUInt64((long)(address + offs), valueRep);
  120. }
  121. for (ulong offs = size8; offs < (size - size8); offs++)
  122. {
  123. WriteByte((long)(address + offs), value);
  124. }
  125. }
  126. public void Copy(ulong dst, ulong src, ulong size)
  127. {
  128. if (dst + size < dst || src + size < src)
  129. {
  130. throw new ArgumentOutOfRangeException(nameof(size));
  131. }
  132. if (dst + size > RamSize)
  133. {
  134. throw new ArgumentOutOfRangeException(nameof(dst));
  135. }
  136. if (src + size > RamSize)
  137. {
  138. throw new ArgumentOutOfRangeException(nameof(src));
  139. }
  140. ulong size8 = size & ~7UL;
  141. for (ulong offs = 0; offs < size8; offs += 8)
  142. {
  143. WriteUInt64((long)(dst + offs), ReadUInt64((long)(src + offs)));
  144. }
  145. for (ulong offs = size8; offs < (size - size8); offs++)
  146. {
  147. WriteByte((long)(dst + offs), ReadByte((long)(src + offs)));
  148. }
  149. }
  150. public void Dispose()
  151. {
  152. Dispose(true);
  153. }
  154. protected virtual void Dispose(bool disposing)
  155. {
  156. MemoryManagement.Free(RamPointer);
  157. }
  158. }
  159. }