aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Memory/AMemoryHelper.cs
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-03-15 21:06:24 -0300
committerGitHub <noreply@github.com>2018-03-15 21:06:24 -0300
commit79a59397349b40758fc75cd2e19c67726a77e975 (patch)
tree4597fcdf8d8d6886df88c00650b7d924c7fc16fe /ChocolArm64/Memory/AMemoryHelper.cs
parent92f47d535e7c3b350c25499dec5e91d8be5544bc (diff)
Improvements to audout (#58)
* Some audout refactoring and improvements * More audio improvements * Change ReadAsciiString to use long for the Size, avoids some casting
Diffstat (limited to 'ChocolArm64/Memory/AMemoryHelper.cs')
-rw-r--r--ChocolArm64/Memory/AMemoryHelper.cs38
1 files changed, 34 insertions, 4 deletions
diff --git a/ChocolArm64/Memory/AMemoryHelper.cs b/ChocolArm64/Memory/AMemoryHelper.cs
index 219aeebf..1e346298 100644
--- a/ChocolArm64/Memory/AMemoryHelper.cs
+++ b/ChocolArm64/Memory/AMemoryHelper.cs
@@ -1,4 +1,6 @@
+using System;
using System.IO;
+using System.Runtime.InteropServices;
using System.Text;
namespace ChocolArm64.Memory
@@ -20,11 +22,11 @@ namespace ChocolArm64.Memory
}
}
- public static byte[] ReadBytes(AMemory Memory, long Position, int Size)
+ public static byte[] ReadBytes(AMemory Memory, long Position, long Size)
{
byte[] Data = new byte[Size];
- for (int Offs = 0; Offs < Size; Offs++)
+ for (long Offs = 0; Offs < Size; Offs++)
{
Data[Offs] = (byte)Memory.ReadByte(Position + Offs);
}
@@ -40,11 +42,39 @@ namespace ChocolArm64.Memory
}
}
- public static string ReadAsciiString(AMemory Memory, long Position, int MaxSize = -1)
+ public unsafe static T Read<T>(AMemory Memory, long Position) where T : struct
+ {
+ long Size = Marshal.SizeOf<T>();
+
+ if ((ulong)(Position + Size) > AMemoryMgr.AddrSize)
+ {
+ throw new ArgumentOutOfRangeException(nameof(Position));
+ }
+
+ IntPtr Ptr = new IntPtr((byte*)Memory.Ram + Position);
+
+ return Marshal.PtrToStructure<T>(Ptr);
+ }
+
+ public unsafe static void Write<T>(AMemory Memory, long Position, T Value) where T : struct
+ {
+ long Size = Marshal.SizeOf<T>();
+
+ if ((ulong)(Position + Size) > AMemoryMgr.AddrSize)
+ {
+ throw new ArgumentOutOfRangeException(nameof(Position));
+ }
+
+ IntPtr Ptr = new IntPtr((byte*)Memory.Ram + Position);
+
+ Marshal.StructureToPtr<T>(Value, Ptr, false);
+ }
+
+ public static string ReadAsciiString(AMemory Memory, long Position, long MaxSize = -1)
{
using (MemoryStream MS = new MemoryStream())
{
- for (int Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++)
+ for (long Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++)
{
byte Value = (byte)Memory.ReadByte(Position + Offs);