diff --git a/CPU.cs b/CPU.cs index 28917ec..e9fe17b 100644 --- a/CPU.cs +++ b/CPU.cs @@ -13,6 +13,14 @@ namespace CPUSimulation { Word dataToWrite = GenerateRandomWord(_random.Next(1, 5)); _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); Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}"); } diff --git a/MC.cs b/MC.cs index 1dd2f4a..87fb552 100644 --- a/MC.cs +++ b/MC.cs @@ -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; + } } } \ No newline at end of file diff --git a/Models.cs b/Models.cs index f75ae6f..e13d61e 100644 --- a/Models.cs +++ b/Models.cs @@ -9,6 +9,14 @@ namespace CPUSimulation { _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() { return _value; @@ -26,6 +34,19 @@ namespace CPUSimulation _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() { return _bits; diff --git a/bin/Debug/net10.0/CPUSimulation.dll b/bin/Debug/net10.0/CPUSimulation.dll index daf8142..6fa2e28 100644 Binary files a/bin/Debug/net10.0/CPUSimulation.dll and b/bin/Debug/net10.0/CPUSimulation.dll differ diff --git a/bin/Debug/net10.0/CPUSimulation.exe b/bin/Debug/net10.0/CPUSimulation.exe index 9543b23..a590070 100644 Binary files a/bin/Debug/net10.0/CPUSimulation.exe and b/bin/Debug/net10.0/CPUSimulation.exe differ diff --git a/bin/Debug/net10.0/CPUSimulation.pdb b/bin/Debug/net10.0/CPUSimulation.pdb index c66b74f..18f5091 100644 Binary files a/bin/Debug/net10.0/CPUSimulation.pdb and b/bin/Debug/net10.0/CPUSimulation.pdb differ diff --git a/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs b/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs index 0e68f54..008d7c9 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [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.AssemblyTitleAttribute("CPUSimulation")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache index 71f1110..3e09ce0 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache @@ -1 +1 @@ -f5b5f51936326916e2fb5d1d37c6294b5a3a803b598c90e6e30bc1bd1c0e6dc7 +c2e5aae0cecf3bbefacc496747f7594154fd639e9a9c0b2086d3b9acd9b437a5 diff --git a/obj/Debug/net10.0/CPUSimulation.dll b/obj/Debug/net10.0/CPUSimulation.dll index daf8142..6fa2e28 100644 Binary files a/obj/Debug/net10.0/CPUSimulation.dll and b/obj/Debug/net10.0/CPUSimulation.dll differ diff --git a/obj/Debug/net10.0/CPUSimulation.pdb b/obj/Debug/net10.0/CPUSimulation.pdb index c66b74f..18f5091 100644 Binary files a/obj/Debug/net10.0/CPUSimulation.pdb and b/obj/Debug/net10.0/CPUSimulation.pdb differ diff --git a/obj/Debug/net10.0/apphost.exe b/obj/Debug/net10.0/apphost.exe index 9543b23..a590070 100644 Binary files a/obj/Debug/net10.0/apphost.exe and b/obj/Debug/net10.0/apphost.exe differ diff --git a/obj/Debug/net10.0/ref/CPUSimulation.dll b/obj/Debug/net10.0/ref/CPUSimulation.dll index 4121ef5..7631741 100644 Binary files a/obj/Debug/net10.0/ref/CPUSimulation.dll and b/obj/Debug/net10.0/ref/CPUSimulation.dll differ diff --git a/obj/Debug/net10.0/refint/CPUSimulation.dll b/obj/Debug/net10.0/refint/CPUSimulation.dll index 4121ef5..7631741 100644 Binary files a/obj/Debug/net10.0/refint/CPUSimulation.dll and b/obj/Debug/net10.0/refint/CPUSimulation.dll differ