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 *((byte*)(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. *((byte*)(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. }