DeviceMemory.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.HLE.Memory
  4. {
  5. class DeviceMemory : IDisposable
  6. {
  7. public const long RamSize = 4L * 1024 * 1024 * 1024;
  8. public ArenaAllocator Allocator { get; private set; }
  9. public IntPtr RamPointer { get; private set; }
  10. private unsafe byte* RamPtr;
  11. public unsafe DeviceMemory()
  12. {
  13. Allocator = new ArenaAllocator(RamSize);
  14. RamPointer = Marshal.AllocHGlobal(new IntPtr(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 *((byte*)(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 void WriteSByte(long Position, sbyte Value)
  50. {
  51. WriteByte(Position, (byte)Value);
  52. }
  53. public void WriteInt16(long Position, short Value)
  54. {
  55. WriteUInt16(Position, (ushort)Value);
  56. }
  57. public void WriteInt32(long Position, int Value)
  58. {
  59. WriteUInt32(Position, (uint)Value);
  60. }
  61. public void WriteInt64(long Position, long Value)
  62. {
  63. WriteUInt64(Position, (ulong)Value);
  64. }
  65. public unsafe void WriteByte(long Position, byte Value)
  66. {
  67. *((byte*)(RamPtr + Position)) = Value;
  68. }
  69. public unsafe void WriteUInt16(long Position, ushort Value)
  70. {
  71. *((ushort*)(RamPtr + Position)) = Value;
  72. }
  73. public unsafe void WriteUInt32(long Position, uint Value)
  74. {
  75. *((uint*)(RamPtr + Position)) = Value;
  76. }
  77. public unsafe void WriteUInt64(long Position, ulong Value)
  78. {
  79. *((ulong*)(RamPtr + Position)) = Value;
  80. }
  81. public void FillWithZeros(long Position, int Size)
  82. {
  83. int Size8 = Size & ~(8 - 1);
  84. for (int Offs = 0; Offs < Size8; Offs += 8)
  85. {
  86. WriteInt64(Position + Offs, 0);
  87. }
  88. for (int Offs = Size8; Offs < (Size - Size8); Offs++)
  89. {
  90. WriteByte(Position + Offs, 0);
  91. }
  92. }
  93. public void Dispose()
  94. {
  95. Dispose(true);
  96. }
  97. protected virtual void Dispose(bool Disposing)
  98. {
  99. Marshal.FreeHGlobal(RamPointer);
  100. }
  101. }
  102. }