DeviceMemory.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.HLE
  4. {
  5. class DeviceMemory : IDisposable
  6. {
  7. public const long RamSize = 4L * 1024 * 1024 * 1024;
  8. public IntPtr RamPointer { get; private set; }
  9. private unsafe byte* _ramPtr;
  10. public unsafe DeviceMemory()
  11. {
  12. RamPointer = Marshal.AllocHGlobal(new IntPtr(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 Dispose()
  92. {
  93. Dispose(true);
  94. }
  95. protected virtual void Dispose(bool disposing)
  96. {
  97. Marshal.FreeHGlobal(RamPointer);
  98. }
  99. }
  100. }