AMemory.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace ChocolArm64.Memory
  5. {
  6. public unsafe class AMemory
  7. {
  8. private const long ErgMask = (4 << ARegisters.ErgSizeLog2) - 1;
  9. public AMemoryMgr Manager { get; private set; }
  10. private struct ExMonitor
  11. {
  12. public long Position { get; private set; }
  13. private bool ExState;
  14. public ExMonitor(long Position, bool ExState)
  15. {
  16. this.Position = Position;
  17. this.ExState = ExState;
  18. }
  19. public bool HasExclusiveAccess(long Position)
  20. {
  21. return this.Position == Position && ExState;
  22. }
  23. public void Reset()
  24. {
  25. ExState = false;
  26. }
  27. }
  28. private Dictionary<int, ExMonitor> Monitors;
  29. private HashSet<long> ExAddrs;
  30. private byte* RamPtr;
  31. public AMemory(IntPtr Ram, AMemoryAlloc Allocator)
  32. {
  33. Manager = new AMemoryMgr(Allocator);
  34. Monitors = new Dictionary<int, ExMonitor>();
  35. ExAddrs = new HashSet<long>();
  36. RamPtr = (byte*)Ram;
  37. }
  38. public void RemoveMonitor(int ThreadId)
  39. {
  40. lock (Monitors)
  41. {
  42. if (Monitors.TryGetValue(ThreadId, out ExMonitor Monitor))
  43. {
  44. ExAddrs.Remove(Monitor.Position);
  45. }
  46. Monitors.Remove(ThreadId);
  47. }
  48. }
  49. public void SetExclusive(ARegisters Registers, long Position)
  50. {
  51. lock (Monitors)
  52. {
  53. Position &= ~ErgMask;
  54. if (Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  55. {
  56. ExAddrs.Remove(Monitor.Position);
  57. }
  58. bool ExState = ExAddrs.Add(Position);
  59. Monitor = new ExMonitor(Position, ExState);
  60. if (!Monitors.TryAdd(Registers.ThreadId, Monitor))
  61. {
  62. Monitors[Registers.ThreadId] = Monitor;
  63. }
  64. }
  65. }
  66. public bool TestExclusive(ARegisters Registers, long Position)
  67. {
  68. lock (Monitors)
  69. {
  70. Position &= ~ErgMask;
  71. if (!Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  72. {
  73. return false;
  74. }
  75. return Monitor.HasExclusiveAccess(Position);
  76. }
  77. }
  78. public void ClearExclusive(ARegisters Registers)
  79. {
  80. lock (Monitors)
  81. {
  82. if (Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  83. {
  84. Monitor.Reset();
  85. ExAddrs.Remove(Monitor.Position);
  86. }
  87. }
  88. }
  89. public sbyte ReadSByte(long Position) => (sbyte)ReadByte (Position);
  90. public short ReadInt16(long Position) => (short)ReadUInt16(Position);
  91. public int ReadInt32(long Position) => (int)ReadUInt32(Position);
  92. public long ReadInt64(long Position) => (long)ReadUInt64(Position);
  93. public byte ReadByte(long Position)
  94. {
  95. return *((byte*)(RamPtr + (uint)Position));
  96. }
  97. public ushort ReadUInt16(long Position)
  98. {
  99. return *((ushort*)(RamPtr + (uint)Position));
  100. }
  101. public uint ReadUInt32(long Position)
  102. {
  103. return *((uint*)(RamPtr + (uint)Position));
  104. }
  105. public ulong ReadUInt64(long Position)
  106. {
  107. return *((ulong*)(RamPtr + (uint)Position));
  108. }
  109. public AVec ReadVector128(long Position)
  110. {
  111. return new AVec()
  112. {
  113. X0 = ReadUInt64(Position + 0),
  114. X1 = ReadUInt64(Position + 8)
  115. };
  116. }
  117. public void WriteSByte(long Position, sbyte Value) => WriteByte (Position, (byte)Value);
  118. public void WriteInt16(long Position, short Value) => WriteUInt16(Position, (ushort)Value);
  119. public void WriteInt32(long Position, int Value) => WriteUInt32(Position, (uint)Value);
  120. public void WriteInt64(long Position, long Value) => WriteUInt64(Position, (ulong)Value);
  121. public void WriteByte(long Position, byte Value)
  122. {
  123. *((byte*)(RamPtr + (uint)Position)) = Value;
  124. }
  125. public void WriteUInt16(long Position, ushort Value)
  126. {
  127. *((ushort*)(RamPtr + (uint)Position)) = Value;
  128. }
  129. public void WriteUInt32(long Position, uint Value)
  130. {
  131. *((uint*)(RamPtr + (uint)Position)) = Value;
  132. }
  133. public void WriteUInt64(long Position, ulong Value)
  134. {
  135. *((ulong*)(RamPtr + (uint)Position)) = Value;
  136. }
  137. public void WriteVector128(long Position, AVec Value)
  138. {
  139. WriteUInt64(Position + 0, Value.X0);
  140. WriteUInt64(Position + 8, Value.X1);
  141. }
  142. private bool IsPageCrossed(long Position, int Size)
  143. {
  144. return (Position & AMemoryMgr.PageMask) + Size > AMemoryMgr.PageSize;
  145. }
  146. }
  147. }