Done MemoryBlock API

This commit is contained in:
Elia Sulimanov 2020-08-19 18:30:16 +03:00
parent b996a83fc3
commit 38c3f36972

View file

@ -4,17 +4,35 @@ using IL2CPU.API.Attribs;
namespace Cosmos.Core
{
/// <summary>
/// MemoryBlock class. Used to read and write to memory blocks.
/// </summary>
public class MemoryBlock
{
/// <summary>
/// Memory block base address.
/// </summary>
public readonly uint Base;
/// <summary>
/// Memory block size.
/// </summary>
public readonly uint Size;
/// <summary>
/// Bytes memory block.
/// </summary>
public readonly MemoryBlock08 Bytes;
/// <summary>
/// Words memory block.
/// </summary>
public readonly MemoryBlock16 Words;
/// <summary>
/// DWords memory block.
/// </summary>
public readonly MemoryBlock32 DWords;
/// <summary>
/// Create new inctanse of the <see cref="MemoryBlock"/> class.
/// Create new instance of the <see cref="MemoryBlock"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aSize">A size.</param>
@ -141,12 +159,24 @@ namespace Cosmos.Core
MemoryOperations.Fill(xDest, aData, (int)aCount);
}
/// <summary>
/// Copy data from the buffer to the start of the memory block.
/// </summary>
/// <param name="aData">A data buffer array.</param>
/// <exception cref="OverflowException">Thrown if aData length in greater then Int32.MaxValue.</exception>
[DebugStub(Off = true)]
public unsafe void Copy(uint[] aData)
{
Copy(0, aData, 0, aData.Length);
}
/// <summary>
/// Copy data from the buffer array to the memory block.
/// </summary>
/// <param name="aStart">A data starting position inside the memory block.</param>
/// <param name="aData">A data buffer array.</param>
/// <param name="aIndex">A staring index in the source data buffer array.</param>
/// <param name="aCount">Number of bytes to copy.</param>
unsafe public void Copy(uint aStart, uint[] aData, int aIndex, int aCount)
{
// TODO thow exception if aStart and aCount are not in bound. I've tried to do this but Bochs dies :-(
@ -158,11 +188,23 @@ namespace Cosmos.Core
}
}
/// <summary>
/// Copy data from the buffer to the start of the memory block.
/// </summary>
/// <param name="aData">A data buffer array.</param>
/// <exception cref="OverflowException">Thrown if aData length in greater then Int32.MaxValue.</exception>
public void Copy(byte[] aData)
{
Copy(0, aData, 0, aData.Length);
}
/// <summary>
/// Copy data from the buffer array to the memory block.
/// </summary>
/// <param name="aStart">A data starting position inside the memory block.</param>
/// <param name="aData">A data buffer array.</param>
/// <param name="aIndex">A staring index in the source data buffer array.</param>
/// <param name="aCount">Number of bytes to copy.</param>
public unsafe void Copy(int aStart, byte[] aData, int aIndex, int aCount)
{
// TODO thow exception if aStart and aCount are not in bound. I've tried to do this but Bochs dies :-(
@ -173,16 +215,35 @@ namespace Cosmos.Core
}
}
/// <summary>
/// Copy data from the buffer to the start of the memory block.
/// </summary>
/// <param name="aData">A data buffer array.</param>
/// <exception cref="OverflowException">Thrown if aData length in greater then Int32.MaxValue.</exception>
public void Copy(int[] aData)
{
Copy(0, aData, 0, aData.Length);
}
/// <summary>
/// Copy data from the buffer array to the start of the memory block.
/// </summary>
/// <param name="aData">A data buffer array.</param>
/// <param name="aIndex">A staring index in the source data buffer array.</param>
/// <param name="aCount">Number of bytes to copy.</param>
/// <exception cref="OverflowException">Thrown if aData length in greater then Int32.MaxValue.</exception>
public void Copy(int []aData, int aIndex, int aCount)
{
Copy(0, aData, aIndex, aData.Length);
}
/// <summary>
/// Copy data from the buffer array to the memory block.
/// </summary>
/// <param name="aStart">A data starting position inside the memory block.</param>
/// <param name="aData">A data buffer array.</param>
/// <param name="aIndex">A staring index in the source data buffer array.</param>
/// <param name="aCount">Number of bytes to copy.</param>
public unsafe void Copy(int aStart, int[] aData, int aIndex, int aCount)
{
// TODO thow exception if aStart and aCount are not in bound. I've tried to do this but Bochs dies :-(
@ -219,12 +280,26 @@ namespace Cosmos.Core
MemoryOperations.Copy(xDest, xSrc, (int)aCount);
}
/// <summary>
/// Move data inside the block. Undone.
/// </summary>
/// <remarks>Always throw. Yet to be done.</remarks>
/// <param name="aDest">A destination address.</param>
/// <param name="aSrc">A source address.</param>
/// <param name="aCount">Number of bytes to move.</param>
/// <exception cref="Exception">Thrown always.</exception>
public void MoveUp(uint aDest, uint aSrc, uint aCount)
{
throw new Exception("TODO");
}
#region ReadWrite
/// <summary>
/// Read 8-bit from the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to write the data to.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read8(Byte[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -235,6 +310,12 @@ namespace Cosmos.Core
aBuffer[i] = (*(Byte*)(Base + i));
}
/// <summary>
/// Write 8-bit to the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to be written to the memory block.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write8(Byte[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -245,6 +326,12 @@ namespace Cosmos.Core
(*(Byte*)(Base + i)) = aBuffer[i];
}
/// <summary>
/// Read 16-bit from the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to write the data to.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read16(ushort[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -257,6 +344,12 @@ namespace Cosmos.Core
}
}
/// <summary>
/// Write 16-bit to the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to be written to the memory block.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write16(ushort[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -269,6 +362,12 @@ namespace Cosmos.Core
}
}
/// <summary>
/// Read 32-bit from the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to write the data to.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read32(uint[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -279,6 +378,12 @@ namespace Cosmos.Core
aBuffer[i] = (*(uint*)(Base + i));
}
/// <summary>
/// Write 32-bit to the memory block.
/// </summary>
/// <param name="aBuffer">A buffer to be written to the memory block.</param>
/// <exception cref="OverflowException">Thrown if aBuffer length in greater then Int32.MaxValue.</exception>
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write32(uint[] aBuffer)
{
if(aBuffer.Length >= Size)
@ -289,7 +394,14 @@ namespace Cosmos.Core
(*(uint*)(Base + i)) = aBuffer[i];
}
#endregion ReadWrite
/// <summary>
/// Convert part for the memory block to array.
/// </summary>
/// <param name="aStart">A starting position of the data at the source memory block.</param>
/// <param name="aIndex">A index to be the staring index at the destination array.</param>
/// <param name="aCount">Number of bytes to get.</param>
/// <returns>uint array.</returns>
public unsafe uint[] ToArray(int aStart, int aIndex, int aCount)
{
uint* xDest = (uint*)(Base + aStart);
@ -301,6 +413,10 @@ namespace Cosmos.Core
return array;
}
/// <summary>
/// Convert the memory block to array.
/// </summary>
/// <returns>uint array.</returns>
public uint[] ToArray()
{
return ToArray(0, 0, (int)Size);
@ -323,7 +439,7 @@ namespace Cosmos.Core
public readonly uint Size;
/// <summary>
/// Create new inctanse of the <see cref="MemoryBlock08"/> class.
/// Create new instance of the <see cref="MemoryBlock08"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aSize">A size.</param>
@ -376,7 +492,7 @@ namespace Cosmos.Core
public readonly uint Size;
/// <summary>
/// Create new inctanse of the <see cref="MemoryBlock16"/> class.
/// Create new instance of the <see cref="MemoryBlock16"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aSize">A size.</param>
@ -429,7 +545,7 @@ namespace Cosmos.Core
public readonly uint Size;
/// <summary>
/// Create new inctanse of the <see cref="MemoryBlock32"/> class.
/// Create new instance of the <see cref="MemoryBlock32"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aSize">A size.</param>