Skip to content

Commit

Permalink
Minor refactoring & Implement swap for memoryblock
Browse files Browse the repository at this point in the history
  • Loading branch information
terminal-cs committed Mar 5, 2023
1 parent fdcd23b commit f68ce81
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions source/Cosmos.Core/MemoryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,48 @@ namespace Cosmos.Core
/// </summary>
public class MemoryBlock
{
/// <summary>
/// Create new instance of the <see cref="MemoryBlock"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aByteSize">A size.</param>
public MemoryBlock(uint aBase, uint aByteSize)
{
Base = aBase;
Size = aByteSize;
Bytes = new MemoryBlock08(aBase, aByteSize);
Words = new MemoryBlock16(aBase, aByteSize);
DWords = new MemoryBlock32(aBase, aByteSize);
}

#region Properties

/// <summary>
/// Memory block base address.
/// </summary>
public readonly uint Base;
public uint Base { get; internal set; }

/// <summary>
/// Memory block size.
/// </summary>
public readonly uint Size;
public uint Size { get; internal set; }

/// <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 instance of the <see cref="MemoryBlock"/> class.
/// </summary>
/// <param name="aBase">A base.</param>
/// <param name="aByteSize">A size.</param>
public MemoryBlock(uint aBase, uint aByteSize)
{
Base = aBase;
Size = aByteSize;
Bytes = new MemoryBlock08(aBase, aByteSize);
Words = new MemoryBlock16(aBase, aByteSize);
DWords = new MemoryBlock32(aBase, aByteSize);
}
#endregion

//TODO: Fill all these methods with fast ASM
//TODO: Make an attribute that can be applied to methods to tell the copmiler to inline them to save
Expand Down Expand Up @@ -76,6 +83,8 @@ public unsafe uint this[uint aByteOffset]
}
}

#region Methods

/// <summary>
/// Fill memory block.
/// </summary>
Expand Down Expand Up @@ -183,7 +192,8 @@ unsafe public void Copy(uint aByteOffset, 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 :-(
uint* xDest = (uint*)(Base + aByteOffset);

fixed (uint* aDataPtr = aData) {
fixed (uint* aDataPtr = aData)
{
MemoryOperations.Copy(xDest, aDataPtr + aIndex, aCount);
}
}
Expand Down Expand Up @@ -234,7 +244,7 @@ public void Copy(int[] aData)
/// <exception cref="OverflowException">Thrown if aData length in greater then Int32.MaxValue.</exception>
public void Copy(int[] aData, int aIndex, int aCount)
{
if(aData.Length < aCount)
if (aData.Length < aCount)
{
throw new IndexOutOfRangeException();
}
Expand Down Expand Up @@ -270,6 +280,20 @@ public unsafe void Copy(ManagedMemoryBlock block)
MemoryOperations.Copy(xDest, aDataPtr, (int)block.Size);
}

/// <summary>
/// Swaps the internal pointer for a replacement and the replacement becomes the current pointer.
/// </summary>
/// <param name="toSwap">The pointer value to swap with.</param>
/// <param name="newSize">The new size that the memory block should have.</param>
public unsafe void Swap(ref uint toSwap, uint newSize, out uint oldSize)
{
// Assign the old size to the output size and set the new block size.
(oldSize, Size) = (Size, newSize);

// Swap both pointer values.
(Base, toSwap) = (toSwap, Base);
}

/// <summary>
/// Move bytes array down the memory block.
/// </summary>
Expand Down Expand Up @@ -306,7 +330,7 @@ public void MoveUp(uint aDest, uint aSrc, uint aCount)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read8(byte[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand All @@ -324,7 +348,7 @@ public unsafe void Read8(byte[] aBuffer)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write8(byte[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand All @@ -342,7 +366,7 @@ public unsafe void Write8(byte[] aBuffer)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read16(ushort[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand All @@ -360,7 +384,7 @@ public unsafe void Read16(ushort[] aBuffer)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write16(ushort[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand All @@ -378,7 +402,7 @@ public unsafe void Write16(ushort[] aBuffer)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Read32(uint[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand All @@ -396,7 +420,7 @@ public unsafe void Read32(uint[] aBuffer)
/// <exception cref="Exception">Thrown on memory access violation.</exception>
public unsafe void Write32(uint[] aBuffer)
{
if(aBuffer.Length >= Size)
if (aBuffer.Length >= Size)
{
throw new Exception("Memory access violation");
}
Expand Down Expand Up @@ -434,6 +458,7 @@ public uint[] ToArray()
return ToArray(0, 0, (int)Size);
}

#endregion
}

/// <summary>
Expand Down

0 comments on commit f68ce81

Please sign in to comment.