2018-08-15 20:59:51 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
namespace Ryujinx.HLE
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
class DeviceMemory : IDisposable
|
|
|
|
{
|
|
|
|
public const long RamSize = 4L * 1024 * 1024 * 1024;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public IntPtr RamPointer { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private unsafe byte* RamPtr;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
public unsafe DeviceMemory()
|
|
|
|
{
|
|
|
|
RamPointer = Marshal.AllocHGlobal(new IntPtr(RamSize));
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
RamPtr = (byte*)RamPointer;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public sbyte ReadSByte(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (sbyte)ReadByte(Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public short ReadInt16(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (short)ReadUInt16(Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public int ReadInt32(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (int)ReadUInt32(Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public long ReadInt64(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (long)ReadUInt64(Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe byte ReadByte(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return *((byte*)(RamPtr + Position));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe ushort ReadUInt16(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return *((ushort*)(RamPtr + Position));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe uint ReadUInt32(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return *((uint*)(RamPtr + Position));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe ulong ReadUInt64(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return *((ulong*)(RamPtr + Position));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void WriteSByte(long Position, sbyte Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteByte(Position, (byte)Value);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void WriteInt16(long Position, short Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteUInt16(Position, (ushort)Value);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void WriteInt32(long Position, int Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteUInt32(Position, (uint)Value);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void WriteInt64(long Position, long Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteUInt64(Position, (ulong)Value);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe void WriteByte(long Position, byte Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
*((byte*)(RamPtr + Position)) = Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe void WriteUInt16(long Position, ushort Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
*((ushort*)(RamPtr + Position)) = Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe void WriteUInt32(long Position, uint Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
*((uint*)(RamPtr + Position)) = Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public unsafe void WriteUInt64(long Position, ulong Value)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
*((ulong*)(RamPtr + Position)) = Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void FillWithZeros(long Position, int Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Size8 = Size & ~(8 - 1);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
for (int Offs = 0; Offs < Size8; Offs += 8)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteInt64(Position + Offs, 0);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
for (int Offs = Size8; Offs < (Size - Size8); Offs++)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
WriteByte(Position + Offs, 0);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
protected virtual void Dispose(bool Disposing)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
Marshal.FreeHGlobal(RamPointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|