diff --git a/CPU.cs b/CPU.cs index 64b461d..4984b41 100644 --- a/CPU.cs +++ b/CPU.cs @@ -4,21 +4,36 @@ namespace CPUSimulation { public class CPU { + static string _program = "D8;58;A0;7F;8C;12;D0;A9;A7;8D;11;D0;8D;13;D0;C9;DF;F0;13;C9;9B;F0;03;C8;10;0F;A9;DC;20;EF;FF;A9;8D;20;EF;FF;A0;01;88;30;F6;AD;11;D0;10;FB;AD;10;D0;99;00;02;20;EF;FF;C9;8D;D0;D4;A0;FF;A9;00;AA;0A;85;2B;C8;B9;00;02;C9;8D;F0;D4;C9;AE;90;F4;F0;F0;C9;BA;F0;EB;C9;D2;F0;3B;86;28;86;29;84;2A;B9;00;02;49;B0;C9;0A;90;06;69;88;C9;FA;90;11;0A;0A;0A;0A;A2;04;0A;26;28;26;29;CA;D0;F8;C8;D0;E0;C4;2A;F0;97;24;2B;50;10;A5;28;81;26;E6;26;D0;B5;E6;27;4C;44;FF;6C;24;00;30;2B;A2;02;B5;27;95;25;95;23;CA;D0;F7;D0;14;A9;8D;20;EF;FF;A5;25;20;DC;FF;A5;24;20;DC;FF;A9;BA;20;EF;FF;A9;A0;20;EF;FF;A1;24;20;DC;FF;86;2B;A5;24;C5;28;A5;25;E5;29;B0;C1;E6;24;D0;02;E6;25;A5;24;29;07;10;C8;48;4A;4A;4A;4A;20;E5;FF;68;29;0F;09;B0;C9;BA;90;02;69;06;2C;12;D0;30;FB;8D;12;D0;60;00;00;00;0F;00;FF;00;00"; public static int numberOfMemoryBanks = 2; - static int memoryBankSize = 1024; + static int memoryBankSize = 65536; static MC _memoryController; public static void Main(string[] args) { InitilizeMemoryController(); - + string[] strings = _program.Split(';'); + Int16 startAddress = Utils.HexStringToInt16("7F00"); + for (int i = 0; i < strings.Length; i++) + { + Int16 currentAddress = (Int16)(startAddress + i); + string byteString = strings[i]; + if (byteString.Length != 2) + { + throw new ArgumentException($"Byte string length must be 2. Invalid byte string: {byteString}"); + } + Byte byteToWrite = new Byte(Utils.HexStringToBinaryString(byteString)); + Word wordToWrite = new Word(new Byte[2] { new Byte("00000000"), byteToWrite }); + _memoryController.Write(currentAddress, wordToWrite); + } _memoryController.Write("0000000000000001","0000000011110000"); _memoryController.Write("0000000000000010","1111000000001111"); for (int i = 0; i < 16; i++) { - string dataRead = _memoryController.Read((Int16)i); - Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead}"); + Int16 currentAddress = (Int16)(startAddress + i); + string dataRead = Utils.BinaryStringToHexString(_memoryController.Read(currentAddress)); + Console.WriteLine($"Data read from address {Utils.BinaryStringToHexString(new Address(currentAddress).ToString())}: {dataRead}"); } } diff --git a/MC.cs b/MC.cs index 00d52e0..af83c9f 100644 --- a/MC.cs +++ b/MC.cs @@ -113,7 +113,7 @@ namespace CPUSimulation { if (address < 0 || address >= _memoryBankSize) { - throw new ArgumentOutOfRangeException(nameof(address), $"Address must be between 0 and {_memoryBankSize - 1}."); + throw new ArgumentOutOfRangeException(nameof(address), $"Received {address}. Address must be between 0 and {_memoryBankSize - 1}."); } } diff --git a/Utils.cs b/Utils.cs index 00da09d..f31f4e5 100644 --- a/Utils.cs +++ b/Utils.cs @@ -48,5 +48,146 @@ namespace CPUSimulation { return Convert.ToString(value & 0xFFFF, 2).PadLeft(16, '0'); } + + + public static string BinaryStringToHexString(string binaryString) + { + if (binaryString.Length % 4 != 0) + { + throw new ArgumentException("Binary string length must be a multiple of 4."); + } + string hexString = ""; + for (int i = 0; i < binaryString.Length; i += 4) + { + string fourBits = binaryString.Substring(i, 4); + switch (fourBits) + { + case "0000": + hexString += "0"; + break; + case "0001": + hexString += "1"; + break; + case "0010": + hexString += "2"; + break; + case "0011": + hexString += "3"; + break; + case "0100": + hexString += "4"; + break; + case "0101": + hexString += "5"; + break; + case "0110": + hexString += "6"; + break; + case "0111": + hexString += "7"; + break; + case "1000": + hexString += "8"; + break; + case "1001": + hexString += "9"; + break; + case "1010": + hexString += "A"; + break; + case "1011": + hexString += "B"; + break; + case "1100": + hexString += "C"; + break; + case "1101": + hexString += "D"; + break; + case "1110": + hexString += "E"; + break; + case "1111": + hexString += "F"; + break; + } + } + return hexString; + } + + public static string HexStringToBinaryString(string hexString) + { + string binaryString = ""; + foreach (char hexChar in hexString) + { + switch (hexChar) + { + case '0': + binaryString += "0000"; + break; + case '1': + binaryString += "0001"; + break; + case '2': + binaryString += "0010"; + break; + case '3': + binaryString += "0011"; + break; + case '4': + binaryString += "0100"; + break; + case '5': + binaryString += "0101"; + break; + case '6': + binaryString += "0110"; + break; + case '7': + binaryString += "0111"; + break; + case '8': + binaryString += "1000"; + break; + case '9': + binaryString += "1001"; + break; + case 'A': + case 'a': + binaryString += "1010"; + break; + case 'B': + case 'b': + binaryString += "1011"; + break; + case 'C': + case 'c': + binaryString += "1100"; + break; + case 'D': + case 'd': + binaryString += "1101"; + break; + case 'E': + case 'e': + binaryString += "1110"; + break; + case 'F': + case 'f': + binaryString += "1111"; + break; + } + } + return binaryString; + } + + public static Int16 HexStringToInt16(string hexString) + { + if (hexString.Length > 4) + { + throw new ArgumentException("Hex string length must be 4 or less."); + } + return Convert.ToInt16(hexString, 16); + } } } \ No newline at end of file diff --git a/bin/Debug/net10.0/CPUSimulation.dll b/bin/Debug/net10.0/CPUSimulation.dll index a299f51..2cb1015 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 e5a074b..aae7d0d 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 2fd964e..8fef99e 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 bcf6372..00577ee 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+faa7801376d988a2b7b81332c9df3db882f08224")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+eae5314e3b7572a343c4caa2d16fb0b1c62c28a0")] [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 f3f5d59..7e1b0bd 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache @@ -1 +1 @@ -0093ec249d1fba7433aea9a209b4614dc995df5d3b8e509911d9d959d0697898 +798170df39efaf27b92d72c4185cce11e4e39a47cca7daccb922366ffee2735d diff --git a/obj/Debug/net10.0/CPUSimulation.dll b/obj/Debug/net10.0/CPUSimulation.dll index a299f51..2cb1015 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 2fd964e..8fef99e 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 e5a074b..aae7d0d 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 c9afc7b..5359233 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 c9afc7b..5359233 100644 Binary files a/obj/Debug/net10.0/refint/CPUSimulation.dll and b/obj/Debug/net10.0/refint/CPUSimulation.dll differ