DeviceMemory.cs 4.6 KB

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