diff --git a/CPU.cs b/CPU.cs index 5fd43a5..28917ec 100644 --- a/CPU.cs +++ b/CPU.cs @@ -8,13 +8,13 @@ namespace CPUSimulation static MC _memoryController; public static void Main(string[] args) { - _memoryController = new MC(8, 2048); - for (int i = 0; i < 2049; i++) + _memoryController = new MC(4, 32768); + for (int i = 0; i < 32768; 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()}"); + 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()}"); } } diff --git a/MC.cs b/MC.cs index afbc1d7..1dd2f4a 100644 --- a/MC.cs +++ b/MC.cs @@ -27,18 +27,18 @@ namespace CPUSimulation } } - public Word Read(int address) + public Word Read(Int16 address) { AddressOkay(address); Byte[] byteData = new Byte[_numberOfMemoryBanks]; for (int i = 0; i < _numberOfMemoryBanks; i++) { - byteData[i] = _memoryBanks[i].Read((byte)address); + byteData[i] = _memoryBanks[i].Read(new Address(address).ToString()); } return new Word(byteData); } - public void Write(int address, Word data) + public void Write(Int16 address, Word data) { AddressOkay(address); Byte[] byteData = data.GetBytes(); @@ -72,11 +72,11 @@ namespace CPUSimulation for (int i = 0; i < _numberOfMemoryBanks; i++) { - _memoryBanks[i].Write((byte)address, bytesToWrite[i]); + _memoryBanks[i].Write(new Address(address).ToString(), bytesToWrite[i]); } } - private void AddressOkay(int address) + private void AddressOkay(Int16 address) { if (address < 0 || address >= _memoryBankSize) { diff --git a/Models.cs b/Models.cs index cc830c5..f75ae6f 100644 --- a/Models.cs +++ b/Models.cs @@ -34,23 +34,87 @@ namespace CPUSimulation public class MemoryBank { - Byte[] _bytes; + Dictionary _bytes; public MemoryBank(Byte[] bytes) { - _bytes = bytes; + _bytes = new Dictionary(); + for (int i = 0; i < bytes.Length; i++) + { + _bytes[new Address((Int16)i).ToString()] = bytes[i]; + } } - public Byte Read(byte address) + public Byte Read(string address) { return _bytes[address]; } - public void Write(byte address, Byte data) + public void Write(string address, Byte data) { _bytes[address] = data; } } + public class Address + { + Byte _highByte; + Byte _lowByte; + public Address(Byte highByte, Byte lowByte) + { + _highByte = highByte; + _lowByte = lowByte; + } + + public Address (Int16 address) + { + byte highByteValue = (byte)(address / 256); + byte lowByteValue = (byte)(address % 256); + _highByte = new Byte(GetBitsFromByte(highByteValue)); + _lowByte = new Byte(GetBitsFromByte(lowByteValue)); + } + + public Address(string address) + { + if (address.Length != 16) + { + throw new ArgumentException("Address string must be exactly 16 characters long."); + } + Bit[] highByteBits = new Bit[8]; + Bit[] lowByteBits = new Bit[8]; + for (int i = 0; i < 8; i++) + { + highByteBits[i] = new Bit(address[i] == '1'); + lowByteBits[i] = new Bit(address[i + 8] == '1'); + } + _highByte = new Byte(highByteBits); + _lowByte = new Byte(lowByteBits); + } + + private Bit[] GetBitsFromByte(byte value) + { + Bit[] bits = new Bit[8]; + for (int i = 0; i < 8; i++) + { + bits[7 - i] = new Bit((value & (1 << i)) != 0); + } + return bits; + } + + public override string ToString() + { + string addressString = ""; + foreach (Bit bit in _highByte.GetBits()) + { + addressString += bit.GetValue() ? "1" : "0"; + } + foreach (Bit bit in _lowByte.GetBits()) + { + addressString += bit.GetValue() ? "1" : "0"; + } + return addressString; + } + } + public class Word { Byte[] _bytes; diff --git a/bin/Debug/net10.0/CPUSimulation.dll b/bin/Debug/net10.0/CPUSimulation.dll index fc3b6a2..daf8142 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 98da06d..9543b23 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 0791f4d..c66b74f 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 a26071a..0e68f54 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+0be1f139ef3c991f2a1945aef3e923154744c8a7")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8cf9e2c5f5ac52e9ad7b40d2f046e9d08850a297")] [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 d1e680a..71f1110 100644 --- a/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/CPUSimulation.AssemblyInfoInputs.cache @@ -1 +1 @@ -06ad3fa32c2a2b0302e0bbe519d5f080612e42cca4243f7546cf9621bf5b5d3c +f5b5f51936326916e2fb5d1d37c6294b5a3a803b598c90e6e30bc1bd1c0e6dc7 diff --git a/obj/Debug/net10.0/CPUSimulation.dll b/obj/Debug/net10.0/CPUSimulation.dll index fc3b6a2..daf8142 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 0791f4d..c66b74f 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 98da06d..9543b23 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 6fff91d..4121ef5 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 6fff91d..4121ef5 100644 Binary files a/obj/Debug/net10.0/refint/CPUSimulation.dll and b/obj/Debug/net10.0/refint/CPUSimulation.dll differ