aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/DeviceMemory.cs
blob: edc70911972dd653d07544a50b2245d96e6a3bc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Runtime.InteropServices;

namespace Ryujinx.HLE
{
    class DeviceMemory : IDisposable
    {
        public const long RamSize = 4L * 1024 * 1024 * 1024;

        public IntPtr RamPointer { get; private set; }

        private unsafe byte* RamPtr;

        public unsafe DeviceMemory()
        {
            RamPointer = Marshal.AllocHGlobal(new IntPtr(RamSize));

            RamPtr = (byte*)RamPointer;
        }

        public sbyte ReadSByte(long Position)
        {
            return (sbyte)ReadByte(Position);
        }

        public short ReadInt16(long Position)
        {
            return (short)ReadUInt16(Position);
        }

        public int ReadInt32(long Position)
        {
            return (int)ReadUInt32(Position);
        }

        public long ReadInt64(long Position)
        {
            return (long)ReadUInt64(Position);
        }

        public unsafe byte ReadByte(long Position)
        {
            return *((byte*)(RamPtr + Position));
        }

        public unsafe ushort ReadUInt16(long Position)
        {
            return *((ushort*)(RamPtr + Position));
        }

        public unsafe uint ReadUInt32(long Position)
        {
            return *((uint*)(RamPtr + Position));
        }

        public unsafe ulong ReadUInt64(long Position)
        {
            return *((ulong*)(RamPtr + Position));
        }

        public void WriteSByte(long Position, sbyte Value)
        {
            WriteByte(Position, (byte)Value);
        }

        public void WriteInt16(long Position, short Value)
        {
            WriteUInt16(Position, (ushort)Value);
        }

        public void WriteInt32(long Position, int Value)
        {
            WriteUInt32(Position, (uint)Value);
        }

        public void WriteInt64(long Position, long Value)
        {
            WriteUInt64(Position, (ulong)Value);
        }

        public unsafe void WriteByte(long Position, byte Value)
        {
            *((byte*)(RamPtr + Position)) = Value;
        }

        public unsafe void WriteUInt16(long Position, ushort Value)
        {
            *((ushort*)(RamPtr + Position)) = Value;
        }

        public unsafe void WriteUInt32(long Position, uint Value)
        {
            *((uint*)(RamPtr + Position)) = Value;
        }

        public unsafe void WriteUInt64(long Position, ulong Value)
        {
            *((ulong*)(RamPtr + Position)) = Value;
        }

        public void FillWithZeros(long Position, int Size)
        {
            int Size8 = Size & ~(8 - 1);

            for (int Offs = 0; Offs < Size8; Offs += 8)
            {
                WriteInt64(Position + Offs, 0);
            }

            for (int Offs = Size8; Offs < (Size - Size8); Offs++)
            {
                WriteByte(Position + Offs, 0);
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool Disposing)
        {
            Marshal.FreeHGlobal(RamPointer);
        }
    }
}