diff --git a/CPU.cs b/CPU.cs index 040d97f..5fd43a5 100644 --- a/CPU.cs +++ b/CPU.cs @@ -4,9 +4,38 @@ namespace CPUSimulation { public class CPU { - public void Main(string[] args) + static Random _random = new Random(); + static MC _memoryController; + public static void Main(string[] args) { - Console.WriteLine("Hello, CPU Simulation!"); + _memoryController = new MC(8, 2048); + for (int i = 0; i < 2049; i++) + { + Word dataToWrite = GenerateRandomWord(_random.Next(1, 9)); + _memoryController.Write(i, dataToWrite); + Word dataRead = _memoryController.Read(i); + Console.WriteLine($"Data read from address {i}: {dataRead.ToString()}"); + } + } + + private static Word GenerateRandomWord(int byteCount) + { + //Console.WriteLine($"Generating random word with {byteCount} bytes."); + Byte[] bytes = new Byte[byteCount]; + for (int i = 0; i < byteCount; i++) + { + bytes[i] = GenerateRandomByte(); + } + return new Word(bytes); + } + private static Byte GenerateRandomByte() + { + Bit[] bits = new Bit[8]; + for (int i = 0; i < 8; i++) + { + bits[i] = new Bit(_random.Next(2) == 1); + } + return new Byte(bits); } } } diff --git a/CU.cs b/CU.cs index 53945e8..33a7cca 100644 --- a/CU.cs +++ b/CU.cs @@ -1,4 +1,7 @@ namespace CPUSimulation { - + public class CU // Control Unit + { + + } } \ No newline at end of file diff --git a/MC.cs b/MC.cs index e2125b7..afbc1d7 100644 --- a/MC.cs +++ b/MC.cs @@ -2,10 +2,86 @@ namespace CPUSimulation { public class MC { - string _name; - public MC(string name) + + MemoryBank[] _memoryBanks; + int _numberOfMemoryBanks; + int _memoryBankSize; + + public MC(int numberOfMemoryBanks, int memoryBankSize = 1024) { - _name = name; + _numberOfMemoryBanks = numberOfMemoryBanks; + _memoryBankSize = memoryBankSize; + Bit bit = new Bit(); + Byte thisByte = new Byte(new Bit[8] { bit, bit, bit, bit, bit, bit, bit, bit }); + + _memoryBanks = new MemoryBank[numberOfMemoryBanks]; + + for(int i = 0; i < numberOfMemoryBanks; i++) + { + Byte[] newMemoryBank = new Byte[memoryBankSize]; + for(int j = 0; j < memoryBankSize; j++) + { + newMemoryBank[j] = thisByte; + } + _memoryBanks[i] = new MemoryBank(newMemoryBank); + } + } + + public Word Read(int address) + { + AddressOkay(address); + Byte[] byteData = new Byte[_numberOfMemoryBanks]; + for (int i = 0; i < _numberOfMemoryBanks; i++) + { + byteData[i] = _memoryBanks[i].Read((byte)address); + } + return new Word(byteData); + } + + public void Write(int address, Word data) + { + AddressOkay(address); + Byte[] byteData = data.GetBytes(); + int byteCount = data.GetByteCount(); + 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]; + } + } + } + else if (data.GetByteCount() > _numberOfMemoryBanks) + { + throw new ArgumentException($"Data must consist of {_numberOfMemoryBanks} bytes or less."); + } + else + { + bytesToWrite = byteData; + } + + + for (int i = 0; i < _numberOfMemoryBanks; i++) + { + _memoryBanks[i].Write((byte)address, bytesToWrite[i]); + } + } + + private void AddressOkay(int address) + { + if (address < 0 || address >= _memoryBankSize) + { + throw new ArgumentOutOfRangeException(nameof(address), $"Address must be between 0 and {_memoryBankSize - 1}."); + } } } } \ No newline at end of file diff --git a/Models.cs b/Models.cs index 53945e8..cc830c5 100644 --- a/Models.cs +++ b/Models.cs @@ -1,4 +1,85 @@ +using System; + namespace CPUSimulation { - + public class Bit + { + bool _value; + public Bit(bool value = false) + { + _value = value; + } + public bool GetValue() + { + return _value; + } + } + public class Byte + { + Bit[] _bits; + public Byte(Bit[] bits) + { + if (bits.Length != 8) + { + throw new ArgumentException("A byte must consist of exactly 8 bits."); + } + _bits = bits; + } + + public Bit[] GetBits() + { + return _bits; + } + } + + public class MemoryBank + { + Byte[] _bytes; + public MemoryBank(Byte[] bytes) + { + _bytes = bytes; + } + + public Byte Read(byte address) + { + return _bytes[address]; + } + + public void Write(byte address, Byte data) + { + _bytes[address] = data; + } + } + + public class Word + { + Byte[] _bytes; + public Word(Byte[] bytes) + { + _bytes = bytes; + } + + public int GetByteCount() + { + return _bytes.Length; + } + + public Byte[] GetBytes() + { + return _bytes; + } + + public override string ToString() + { + string wordString = ""; + foreach (Byte b in _bytes) + { + foreach (Bit bit in b.GetBits()) + { + wordString += bit.GetValue() ? "1" : "0"; + } + } + return wordString; + } + } } \ No newline at end of file diff --git a/Utils.cs b/Utils.cs new file mode 100644 index 0000000..d530f84 --- /dev/null +++ b/Utils.cs @@ -0,0 +1,7 @@ +namespace CPUSimulation +{ + public class Utils + { + + } +} \ No newline at end of file diff --git a/bin/Debug/net10.0/CPUSimulation.deps.json b/bin/Debug/net10.0/CPUSimulation.deps.json new file mode 100644 index 0000000..91b71cf --- /dev/null +++ b/bin/Debug/net10.0/CPUSimulation.deps.json @@ -0,0 +1,23 @@ +{ + "runtimeTarget": { + "name": ".NETCoreApp,Version=v10.0", + "signature": "" + }, + "compilationOptions": {}, + "targets": { + ".NETCoreApp,Version=v10.0": { + "CPUSimulation/1.0.0": { + "runtime": { + "CPUSimulation.dll": {} + } + } + } + }, + "libraries": { + "CPUSimulation/1.0.0": { + "type": "project", + "serviceable": false, + "sha512": "" + } + } +} \ No newline at end of file diff --git a/bin/Debug/net10.0/CPUSimulation.dll b/bin/Debug/net10.0/CPUSimulation.dll new file mode 100644 index 0000000..fc3b6a2 Binary files /dev/null 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 new file mode 100644 index 0000000..98da06d Binary files /dev/null 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 new file mode 100644 index 0000000..0791f4d Binary files /dev/null and b/bin/Debug/net10.0/CPUSimulation.pdb differ diff --git a/bin/Debug/net10.0/CPUSimulation.runtimeconfig.json b/bin/Debug/net10.0/CPUSimulation.runtimeconfig.json new file mode 100644 index 0000000..01e4519 --- /dev/null +++ b/bin/Debug/net10.0/CPUSimulation.runtimeconfig.json @@ -0,0 +1,12 @@ +{ + "runtimeOptions": { + "tfm": "net10.0", + "framework": { + "name": "Microsoft.NETCore.App", + "version": "10.0.0" + }, + "configProperties": { + "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false + } + } +} \ No newline at end of file diff --git a/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs b/obj/Debug/net10.0/CPUSimulation.AssemblyInfo.cs index 8165b81..a26071a 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")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0be1f139ef3c991f2a1945aef3e923154744c8a7")] [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 bbef616..d1e680a 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache @@ -1 +1 @@ -8cc2d01c0c88fb895e41bdcc39b7cca3750604405cfd8368af957ff013a48636 +06ad3fa32c2a2b0302e0bbe519d5f080612e42cca4243f7546cf9621bf5b5d3c diff --git a/obj/Debug/net10.0/CPUSimulation.csproj.CoreCompileInputs.cache b/obj/Debug/net10.0/CPUSimulation.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..1caf156 --- /dev/null +++ b/obj/Debug/net10.0/CPUSimulation.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +ee6baa5db2772c0e2c5d383021abedaeebee16b3198cd6e6a57be209e6f61557 diff --git a/obj/Debug/net10.0/CPUSimulation.csproj.FileListAbsolute.txt b/obj/Debug/net10.0/CPUSimulation.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..24524dd --- /dev/null +++ b/obj/Debug/net10.0/CPUSimulation.csproj.FileListAbsolute.txt @@ -0,0 +1,14 @@ +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.GeneratedMSBuildEditorConfig.editorconfig +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.AssemblyInfoInputs.cache +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.AssemblyInfo.cs +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.csproj.CoreCompileInputs.cache +C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.exe +C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.deps.json +C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.runtimeconfig.json +C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.dll +C:\Users\David\Documents\CPUSimulation\CPUSimulation\bin\Debug\net10.0\CPUSimulation.pdb +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.dll +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\refint\CPUSimulation.dll +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.pdb +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\CPUSimulation.genruntimeconfig.cache +C:\Users\David\Documents\CPUSimulation\CPUSimulation\obj\Debug\net10.0\ref\CPUSimulation.dll diff --git a/obj/Debug/net10.0/CPUSimulation.dll b/obj/Debug/net10.0/CPUSimulation.dll new file mode 100644 index 0000000..fc3b6a2 Binary files /dev/null and b/obj/Debug/net10.0/CPUSimulation.dll differ diff --git a/obj/Debug/net10.0/CPUSimulation.genruntimeconfig.cache b/obj/Debug/net10.0/CPUSimulation.genruntimeconfig.cache new file mode 100644 index 0000000..adcf44b --- /dev/null +++ b/obj/Debug/net10.0/CPUSimulation.genruntimeconfig.cache @@ -0,0 +1 @@ +c421d8a3d43a99866e74ff534ad991e6de3bb29db540eb835e6bc3b54b358471 diff --git a/obj/Debug/net10.0/CPUSimulation.pdb b/obj/Debug/net10.0/CPUSimulation.pdb new file mode 100644 index 0000000..0791f4d Binary files /dev/null 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 new file mode 100644 index 0000000..98da06d Binary files /dev/null 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 new file mode 100644 index 0000000..6fff91d Binary files /dev/null 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 new file mode 100644 index 0000000..6fff91d Binary files /dev/null and b/obj/Debug/net10.0/refint/CPUSimulation.dll differ