diff --git a/CPU.cs b/CPU.cs index 3284fd1..64b461d 100644 --- a/CPU.cs +++ b/CPU.cs @@ -4,7 +4,7 @@ namespace CPUSimulation { public class CPU { - static int numberOfMemoryBanks = 4; + public static int numberOfMemoryBanks = 2; static int memoryBankSize = 1024; static MC _memoryController; @@ -17,8 +17,8 @@ namespace CPUSimulation _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()}"); + string dataRead = _memoryController.Read((Int16)i); + Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead}"); } } @@ -27,8 +27,27 @@ namespace CPUSimulation _memoryController = new MC(numberOfMemoryBanks, memoryBankSize); for (int i = 0; i < memoryBankSize; i++) { - Word dataToWrite = Utils.GenerateRandomWord(numberOfMemoryBanks); - _memoryController.Write((Int16)i, dataToWrite); + if (numberOfMemoryBanks == 2) + { + Word dataToWrite = Utils.GenerateRandomWord(numberOfMemoryBanks); + _memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString()); + } + else if (numberOfMemoryBanks == 4) + { + DWord dataToWrite = new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2)); + _memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString()); + } + else if (numberOfMemoryBanks == 8) + { + QWord dataToWrite = new QWord(new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2)), new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2))); + _memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString()); + } + else + { + throw new ArgumentException($"Number of memory banks must be between 1 and 8 (inclusive)."); + } + + //Word dataRead = _memoryController.Read((Int16)i); //Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}"); } diff --git a/CU.cs b/CU.cs index 5a59f89..9d58c9c 100644 --- a/CU.cs +++ b/CU.cs @@ -2,10 +2,12 @@ namespace CPUSimulation { public class CU // Control Unit { + int numberOfRegisters = 16; Dictionary _instructionSet; Dictionary _registers; public CU(int numberOfMemoryBanks) { + _instructionSet = new Dictionary(); _instructionSet.Add("00000", "NOP"); _instructionSet.Add("00001", "LOAD"); @@ -25,9 +27,11 @@ namespace CPUSimulation _instructionSet.Add("01111", "DEC"); _registers = new Dictionary(); - for (int i = 0; i < numberOfMemoryBanks; i++) - { - _registers.Add($"R{i}", new Word(new Byte[0])); + Word zeroWord = new Word(new Byte[2] { new Byte("00000000"), new Byte("00000000") }); + for (int i = 0; i < numberOfRegisters; i++) + { + string registerName = ((byte)i).ToString().Substring(4, 4); + _registers.Add(registerName, zeroWord); } } @@ -43,13 +47,13 @@ namespace CPUSimulation { _value++; } - public void SetValue(Int16 value) + public void SetValue(string value) { - _value = value; + _value = Convert.ToInt16(value, 2); } - public Int16 GetValue() + public string GetValue() { - return _value; + return Utils.Int16ToBinaryString(_value); } } } diff --git a/MC.cs b/MC.cs index 7525fbf..00d52e0 100644 --- a/MC.cs +++ b/MC.cs @@ -27,7 +27,7 @@ namespace CPUSimulation } } - public Word Read(Int16 address) + public string Read(Int16 address) { AddressOkay(address); Byte[] byteData = new Byte[_numberOfMemoryBanks]; @@ -35,7 +35,22 @@ namespace CPUSimulation { byteData[i] = _memoryBanks[i].Read(new Address(address).ToString()); } - return new Word(byteData); + if (byteData.Length == 2) + { + return new Word(byteData).ToString(); + } + else if (byteData.Length == 4) + { + return new DWord(new Word(byteData.Take(2).ToArray()), new Word(byteData.Skip(2).Take(2).ToArray())).ToString(); + } + else if (byteData.Length == 8) + { + return new QWord(new DWord(new Word(byteData.Take(2).ToArray()), new Word(byteData.Skip(2).Take(2).ToArray())), new DWord(new Word(byteData.Skip(4).Take(2).ToArray()), new Word(byteData.Skip(6).Take(2).ToArray()))).ToString(); + } + else + { + throw new ArgumentException($"Number of memory banks must be between 1 and 8 (inclusive)."); + } } public void Write(Int16 address, Word data) diff --git a/Models.cs b/Models.cs index 4c5f97b..9fcca57 100644 --- a/Models.cs +++ b/Models.cs @@ -133,11 +133,24 @@ namespace CPUSimulation return addressString; } } + + + /// + /// A word is a collection of 2 Bytes + /// public class Word { Byte[] _bytes; public Word(Byte[] bytes) { + if (bytes.Length == 0 || bytes.Length > 2) + { + throw new ArgumentException("A word must consist of 1 or 2 bytes."); + } + else if (bytes.Length == 1) + { + _bytes = new Byte[2] { new Byte("00000000"), bytes[0] }; + } _bytes = bytes; } @@ -164,4 +177,42 @@ namespace CPUSimulation return wordString; } } + + /// + /// A double word is a collection of 2 Words, or 4 Bytes + /// + public class DWord + { + Word _highWord; + Word _lowWord; + public DWord(Word highWord, Word lowWord) + { + _highWord = highWord; + _lowWord = lowWord; + } + + public override string ToString() + { + return _highWord.ToString() + _lowWord.ToString(); + } + } + + /// + /// A quad word is a collection of 2 Double Words, or 8 Bytes + /// + public class QWord + { + DWord _highDWord; + DWord _lowDWord; + public QWord(DWord highDWord, DWord lowDWord) + { + _highDWord = highDWord; + _lowDWord = lowDWord; + } + + override public string ToString() + { + return _highDWord.ToString() + _lowDWord.ToString(); + } + } } \ No newline at end of file diff --git a/Utils.cs b/Utils.cs index cb3a6df..00da09d 100644 --- a/Utils.cs +++ b/Utils.cs @@ -3,16 +3,36 @@ namespace CPUSimulation public class Utils { static Random _random = new Random(); + + /// + /// Generates a random word + /// + /// + /// input must be between 0 and 2 (inclusive) + /// + /// public static Word GenerateRandomWord(int byteCount) { - byteCount = _random.Next(1, byteCount + 1); - //Console.WriteLine($"Generating random word with {byteCount} bytes."); + if (byteCount < 0) + { + byteCount = 0; + } Byte[] bytes = new Byte[byteCount]; for (int i = 0; i < byteCount; i++) { bytes[i] = GenerateRandomByte(); } + if (byteCount == 1) + { + bytes = new Byte[2] { new Byte("00000000"), bytes[0] }; + } + else if (byteCount == 0) + { + bytes = new Byte[2] { new Byte("00000000"), new Byte("00000000") }; + } return new Word(bytes); + + } public static Byte GenerateRandomByte() { @@ -23,5 +43,10 @@ namespace CPUSimulation } return new Byte(bits); } + + public static string Int16ToBinaryString(short value) + { + return Convert.ToString(value & 0xFFFF, 2).PadLeft(16, '0'); + } } } \ No newline at end of file diff --git a/bin/Debug/net10.0/CPUSimulation.dll b/bin/Debug/net10.0/CPUSimulation.dll index 6fa2e28..a299f51 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 a590070..e5a074b 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 18f5091..2fd964e 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 008d7c9..bcf6372 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+9efadc3b4a2f776b2177e2873a3b46b0d1af54c3")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+faa7801376d988a2b7b81332c9df3db882f08224")] [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 3e09ce0..f3f5d59 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache @@ -1 +1 @@ -c2e5aae0cecf3bbefacc496747f7594154fd639e9a9c0b2086d3b9acd9b437a5 +0093ec249d1fba7433aea9a209b4614dc995df5d3b8e509911d9d959d0697898 diff --git a/obj/Debug/net10.0/CPUSimulation.dll b/obj/Debug/net10.0/CPUSimulation.dll index 6fa2e28..a299f51 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 18f5091..2fd964e 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 a590070..e5a074b 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 7631741..c9afc7b 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 7631741..c9afc7b 100644 Binary files a/obj/Debug/net10.0/refint/CPUSimulation.dll and b/obj/Debug/net10.0/refint/CPUSimulation.dll differ