public bool ReadAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness) { if (Endianness == endianness) { return(ReadFunc(address, buffer)); } else { // Not a between alignment, or aligned write if (buffer.Length >= 4 && (buffer.Length % 4 != 0)) { throw new Exception("Misaligned data"); } address = EndiannessUtilities.SwapAddressEndianness(address, buffer.Length); byte[] readBytes = new byte[buffer.Length]; if (!ReadFunc(address, readBytes)) { return(false); } readBytes = EndiannessUtilities.SwapByteEndianness(readBytes); Buffer.BlockCopy(readBytes, 0, buffer, 0, buffer.Length); return(true); } }
public bool ReadAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness) { if (Endianness == endianness) { int numOfBytes = 0; return(ReadFunc(address, buffer)); } else { // Read padded if misaligned address byte[] swapBytes; UIntPtr alignedAddress = EndiannessUtilities.AlignedAddressFloor(address); swapBytes = new byte[(buffer.Length / 4) * 4 + 8]; // Read memory if (!ReadFunc(address, swapBytes)) { return(false); } swapBytes = EndiannessUtilities.SwapByteEndianness(swapBytes); // Copy memory Buffer.BlockCopy(swapBytes, (int)(address.ToUInt64() - alignedAddress.ToUInt64()), buffer, 0, buffer.Length); return(true); } }
public byte[] ReadRam(UIntPtr address, int length, EndiannessType endianness, bool absoluteAddress = false) { byte[] readBytes = new byte[length]; // Get local address uint localAddress; if (absoluteAddress) { localAddress = _io?.GetRelativeAddress(address, length) ?? 0; } else { localAddress = address.ToUInt32(); } localAddress &= ~0x80000000; if (EndiannessUtilities.DataIsMisaligned(address, length, EndiannessType.Big)) { return(readBytes); } /// Fix endianness switch (endianness) { case EndiannessType.Little: // Address is not little endian, fix: localAddress = EndiannessUtilities.SwapAddressEndianness(localAddress, length); if (localAddress + length > _ram.Length) { break; } Buffer.BlockCopy(_ram, (int)localAddress, readBytes, 0, length); break; case EndiannessType.Big: // Read padded if misaligned address byte[] swapBytes; uint alignedAddress = EndiannessUtilities.AlignedAddressFloor(localAddress); int alignedReadByteCount = (readBytes.Length / 4) * 4 + 8; swapBytes = new byte[alignedReadByteCount]; // Read memory Buffer.BlockCopy(_ram, (int)alignedAddress, swapBytes, 0, swapBytes.Length); swapBytes = EndiannessUtilities.SwapByteEndianness(swapBytes); // Copy memory Buffer.BlockCopy(swapBytes, (int)(localAddress - alignedAddress), readBytes, 0, readBytes.Length); break; } return(readBytes); }
public bool WriteAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness) { int numOfBytes = 0; // Safety bounds check if (address.ToUInt64() < BaseOffset.ToUInt64() || address.ToUInt64() + (uint)buffer.Length >= BaseOffset.ToUInt64() + _ramSize) { return(false); } if (Endianness == endianness) { return(WriteFunc(address, buffer)); } else { bool success = true; IEnumerable <byte> bytes = buffer; int numberToWrite = Math.Min(EndiannessUtilities.NumberOfBytesToAlignment(address), buffer.Length); if (numberToWrite > 0) { byte[] toWrite = bytes.Take(numberToWrite).Reverse().ToArray(); success &= WriteFunc(EndiannessUtilities.AlignedAddressFloor(address), toWrite); bytes = bytes.Skip(toWrite.Length); address += toWrite.Length; } numberToWrite = bytes.Count(); if (bytes.Count() >= 4) { byte[] toWrite = EndiannessUtilities.SwapByteEndianness(bytes.Take(bytes.Count() & ~0x03).ToArray()); success &= WriteFunc(address, toWrite); bytes = bytes.Skip(toWrite.Length); address += toWrite.Length; } numberToWrite = bytes.Count(); if (numberToWrite > 0) { byte[] toWrite = bytes.Reverse().ToArray(); address = EndiannessUtilities.SwapAddressEndianness(address, toWrite.Length); success &= WriteFunc(address, toWrite); } return(success); } }
public bool WriteAbsolute(UIntPtr address, byte[] buffer, EndiannessType endianness) { // Safety bounds check if (address.ToUInt64() < BaseOffset.ToUInt64() || address.ToUInt64() + (uint)buffer.Length >= BaseOffset.ToUInt64() + Config.RamSize) { return(false); } if (Endianness == endianness) { return(WriteFunc(address, buffer)); } else { bool success = true; byte[] toWrite = EndiannessUtilities.SwapByteEndianness(buffer); // Between alignment writes if (buffer.Length < 4) { address = EndiannessUtilities.SwapAddressEndianness(address, toWrite.Length); success &= WriteFunc(address, toWrite); } else if (buffer.Length % 4 == 0) // Full alignment writes { success &= WriteFunc(address, toWrite); } else { throw new Exception("Misaligned data"); } return(success); } }