Created functions to allow for data to be written to and read from memory using bianry strings.

This commit is contained in:
2026-05-31 20:36:00 -04:00
parent 9efadc3b4a
commit a958d11c4e
13 changed files with 83 additions and 15 deletions
+8
View File
@@ -13,6 +13,14 @@ namespace CPUSimulation
{ {
Word dataToWrite = GenerateRandomWord(_random.Next(1, 5)); Word dataToWrite = GenerateRandomWord(_random.Next(1, 5));
_memoryController.Write((Int16)i, dataToWrite); _memoryController.Write((Int16)i, dataToWrite);
//Word dataRead = _memoryController.Read((Int16)i);
//Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
}
_memoryController.Write("0000000000000001","0000000011110000");
_memoryController.Write("0000000000000010","1111000000001111");
for (int i = 0; i < 16; i++)
{
Word dataRead = _memoryController.Read((Int16)i); Word dataRead = _memoryController.Read((Int16)i);
Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}"); Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
} }
+52 -13
View File
@@ -46,19 +46,7 @@ namespace CPUSimulation
Byte[] bytesToWrite; Byte[] bytesToWrite;
if (byteCount < _numberOfMemoryBanks) if (byteCount < _numberOfMemoryBanks)
{ {
int numberOfPaddingBytes = _numberOfMemoryBanks - byteCount; bytesToWrite = GeneratePaddingBytes(byteData, 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];
}
}
} }
else if (data.GetByteCount() > _numberOfMemoryBanks) 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) private void AddressOkay(Int16 address)
{ {
if (address < 0 || address >= _memoryBankSize) if (address < 0 || address >= _memoryBankSize)
@@ -83,5 +101,26 @@ namespace CPUSimulation
throw new ArgumentOutOfRangeException(nameof(address), $"Address must be between 0 and {_memoryBankSize - 1}."); 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;
}
} }
} }
+21
View File
@@ -9,6 +9,14 @@ namespace CPUSimulation
{ {
_value = value; _value = value;
} }
public Bit(string bitString)
{
if (bitString.Length != 1 || (bitString != "0" && bitString != "1"))
{
throw new ArgumentException("Bit string must be '0' or '1'.");
}
_value = bitString == "1";
}
public bool GetValue() public bool GetValue()
{ {
return _value; return _value;
@@ -26,6 +34,19 @@ namespace CPUSimulation
_bits = bits; _bits = bits;
} }
public Byte(string bitString)
{
if (bitString.Length != 8)
{
throw new ArgumentException("Bit string must be exactly 8 characters long.");
}
_bits = new Bit[8];
for (int i = 0; i < 8; i++)
{
_bits[i] = new Bit(bitString[i] == '1');
}
}
public Bit[] GetBits() public Bit[] GetBits()
{ {
return _bits; return _bits;
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8cf9e2c5f5ac52e9ad7b40d2f046e9d08850a297")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9efadc3b4a2f776b2177e2873a3b46b0d1af54c3")]
[assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
f5b5f51936326916e2fb5d1d37c6294b5a3a803b598c90e6e30bc1bd1c0e6dc7 c2e5aae0cecf3bbefacc496747f7594154fd639e9a9c0b2086d3b9acd9b437a5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.