diff --git a/CPU.cs b/CPU.cs index e9fe17b..3284fd1 100644 --- a/CPU.cs +++ b/CPU.cs @@ -4,18 +4,14 @@ namespace CPUSimulation { public class CPU { - static Random _random = new Random(); + static int numberOfMemoryBanks = 4; + static int memoryBankSize = 1024; static MC _memoryController; + + public static void Main(string[] args) { - _memoryController = new MC(4, 32768); - for (int i = 0; i < 32768; i++) - { - 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()}"); - } + InitilizeMemoryController(); _memoryController.Write("0000000000000001","0000000011110000"); _memoryController.Write("0000000000000010","1111000000001111"); @@ -26,24 +22,16 @@ namespace CPUSimulation } } - private static Word GenerateRandomWord(int byteCount) + private static void InitilizeMemoryController() { - //Console.WriteLine($"Generating random word with {byteCount} bytes."); - Byte[] bytes = new Byte[byteCount]; - for (int i = 0; i < byteCount; i++) + _memoryController = new MC(numberOfMemoryBanks, memoryBankSize); + for (int i = 0; i < memoryBankSize; i++) { - bytes[i] = GenerateRandomByte(); + Word dataToWrite = Utils.GenerateRandomWord(numberOfMemoryBanks); + _memoryController.Write((Int16)i, dataToWrite); + //Word dataRead = _memoryController.Read((Int16)i); + //Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}"); } - 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 33a7cca..5a59f89 100644 --- a/CU.cs +++ b/CU.cs @@ -2,6 +2,55 @@ namespace CPUSimulation { public class CU // Control Unit { - + Dictionary _instructionSet; + Dictionary _registers; + public CU(int numberOfMemoryBanks) + { + _instructionSet = new Dictionary(); + _instructionSet.Add("00000", "NOP"); + _instructionSet.Add("00001", "LOAD"); + _instructionSet.Add("00010", "STORE"); + _instructionSet.Add("00011", "ADD"); + _instructionSet.Add("00100", "SUB"); + _instructionSet.Add("00101", "JUMP"); + _instructionSet.Add("00110", "JUMPIFZERO"); + _instructionSet.Add("00111", "JUMPIFNEG"); + _instructionSet.Add("01000", "AND"); + _instructionSet.Add("01001", "OR"); + _instructionSet.Add("01010", "XOR"); + _instructionSet.Add("01011", "NOT"); + _instructionSet.Add("01100", "SHL"); + _instructionSet.Add("01101", "SHR"); + _instructionSet.Add("01110", "INC"); + _instructionSet.Add("01111", "DEC"); + + _registers = new Dictionary(); + for (int i = 0; i < numberOfMemoryBanks; i++) + { + _registers.Add($"R{i}", new Word(new Byte[0])); + } + + } + + internal class ProgramCounter + { + Int16 _value; + public ProgramCounter() + { + _value = 0; + } + public void Increment() + { + _value++; + } + public void SetValue(Int16 value) + { + _value = value; + } + public Int16 GetValue() + { + return _value; + } + } } } \ No newline at end of file diff --git a/MC.cs b/MC.cs index 87fb552..7525fbf 100644 --- a/MC.cs +++ b/MC.cs @@ -1,6 +1,6 @@ namespace CPUSimulation { - public class MC + public class MC // Memory Controller { MemoryBank[] _memoryBanks; diff --git a/Models.cs b/Models.cs index e13d61e..4c5f97b 100644 --- a/Models.cs +++ b/Models.cs @@ -52,7 +52,6 @@ namespace CPUSimulation return _bits; } } - public class MemoryBank { Dictionary _bytes; @@ -75,7 +74,6 @@ namespace CPUSimulation _bytes[address] = data; } } - public class Address { Byte _highByte; @@ -135,7 +133,6 @@ namespace CPUSimulation return addressString; } } - public class Word { Byte[] _bytes; diff --git a/Utils.cs b/Utils.cs index d530f84..cb3a6df 100644 --- a/Utils.cs +++ b/Utils.cs @@ -2,6 +2,26 @@ namespace CPUSimulation { public class Utils { - + static Random _random = new Random(); + public static Word GenerateRandomWord(int byteCount) + { + byteCount = _random.Next(1, byteCount + 1); + //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); + } + public 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); + } } } \ No newline at end of file