Created functions to allow for data to be written to and read from memory using bianry strings.
This commit is contained in:
@@ -46,19 +46,7 @@ namespace CPUSimulation
|
||||
Byte[] bytesToWrite;
|
||||
if (byteCount < _numberOfMemoryBanks)
|
||||
{
|
||||
int numberOfPaddingBytes = _numberOfMemoryBanks - byteCount;
|
||||
bytesToWrite = new Byte[_numberOfMemoryBanks];
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
if (i < numberOfPaddingBytes)
|
||||
{
|
||||
bytesToWrite[i] = new Byte(new Bit[8]{ new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false) });
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite[i] = byteData[i - numberOfPaddingBytes];
|
||||
}
|
||||
}
|
||||
bytesToWrite = GeneratePaddingBytes(byteData, byteCount);
|
||||
}
|
||||
else if (data.GetByteCount() > _numberOfMemoryBanks)
|
||||
{
|
||||
@@ -76,6 +64,36 @@ namespace CPUSimulation
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Write(string address, string data)
|
||||
{
|
||||
if(address.Length != 16)
|
||||
{
|
||||
throw new ArgumentException("Address must be 16 bits long.");
|
||||
}
|
||||
if (data.Length % 8 != 0)
|
||||
{
|
||||
throw new ArgumentException("Data must be a multiple of 8 bits long.");
|
||||
}
|
||||
int byteCount = data.Length / 8;
|
||||
if (byteCount > _numberOfMemoryBanks)
|
||||
{
|
||||
throw new ArgumentException($"Data must consist of {_numberOfMemoryBanks} bytes or less.");
|
||||
}
|
||||
if (byteCount < _numberOfMemoryBanks)
|
||||
{
|
||||
data = data.PadLeft(_numberOfMemoryBanks * 8, '0');
|
||||
}
|
||||
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
string byteString = data.Substring(i * 8, 8);
|
||||
Byte byteData = new Byte(byteString);
|
||||
_memoryBanks[i].Write(address, byteData);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddressOkay(Int16 address)
|
||||
{
|
||||
if (address < 0 || address >= _memoryBankSize)
|
||||
@@ -83,5 +101,26 @@ namespace CPUSimulation
|
||||
throw new ArgumentOutOfRangeException(nameof(address), $"Address must be between 0 and {_memoryBankSize - 1}.");
|
||||
}
|
||||
}
|
||||
|
||||
private Byte[] GeneratePaddingBytes(Byte[] byteData, int byteCount)
|
||||
{
|
||||
Byte[] bytesToWrite;
|
||||
int numberOfPaddingBytes = _numberOfMemoryBanks - byteCount;
|
||||
bytesToWrite = new Byte[_numberOfMemoryBanks];
|
||||
for (int i = 0; i < _numberOfMemoryBanks; i++)
|
||||
{
|
||||
if (i < numberOfPaddingBytes)
|
||||
{
|
||||
Bit bit = new Bit();
|
||||
bytesToWrite[i] = new Byte(new Bit[8] { bit, bit, bit, bit, bit, bit, bit, bit });
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToWrite[i] = byteData[i - numberOfPaddingBytes];
|
||||
}
|
||||
}
|
||||
|
||||
return bytesToWrite;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user