AMemoryHelper.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.IO;
  2. using System.Text;
  3. namespace ChocolArm64.Memory
  4. {
  5. public static class AMemoryHelper
  6. {
  7. public static void FillWithZeros(AMemory Memory, long Position, int Size)
  8. {
  9. int Size8 = Size & ~(8 - 1);
  10. for (int Offs = 0; Offs < Size8; Offs += 8)
  11. {
  12. Memory.WriteInt64(Position + Offs, 0);
  13. }
  14. for (int Offs = Size8; Offs < (Size - Size8); Offs++)
  15. {
  16. Memory.WriteByte(Position + Offs, 0);
  17. }
  18. }
  19. public static byte[] ReadBytes(AMemory Memory, long Position, int Size)
  20. {
  21. byte[] Data = new byte[Size];
  22. for (int Offs = 0; Offs < Size; Offs++)
  23. {
  24. Data[Offs] = (byte)Memory.ReadByte(Position + Offs);
  25. }
  26. return Data;
  27. }
  28. public static void WriteBytes(AMemory Memory, long Position, byte[] Data)
  29. {
  30. for (int Offs = 0; Offs < Data.Length; Offs++)
  31. {
  32. Memory.WriteByte(Position + Offs, Data[Offs]);
  33. }
  34. }
  35. public static string ReadAsciiString(AMemory Memory, long Position, int MaxSize = -1)
  36. {
  37. using (MemoryStream MS = new MemoryStream())
  38. {
  39. for (int Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++)
  40. {
  41. byte Value = (byte)Memory.ReadByte(Position + Offs);
  42. if (Value == 0)
  43. {
  44. break;
  45. }
  46. MS.WriteByte(Value);
  47. }
  48. return Encoding.ASCII.GetString(MS.ToArray());
  49. }
  50. }
  51. public static long PageRoundUp(long Value)
  52. {
  53. return (Value + AMemoryMgr.PageMask) & ~AMemoryMgr.PageMask;
  54. }
  55. public static long PageRoundDown(long Value)
  56. {
  57. return Value & ~AMemoryMgr.PageMask;
  58. }
  59. }
  60. }