Beginning to add Program Counter and Stack Pointer

Beginning to add initil Instruction Set
This commit is contained in:
2026-05-31 21:29:23 -04:00
parent a958d11c4e
commit faa7801376
5 changed files with 84 additions and 30 deletions
+12 -24
View File
@@ -4,18 +4,14 @@ namespace CPUSimulation
{ {
public class CPU public class CPU
{ {
static Random _random = new Random(); static int numberOfMemoryBanks = 4;
static int memoryBankSize = 1024;
static MC _memoryController; static MC _memoryController;
public static void Main(string[] args) public static void Main(string[] args)
{ {
_memoryController = new MC(4, 32768); InitilizeMemoryController();
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()}");
}
_memoryController.Write("0000000000000001","0000000011110000"); _memoryController.Write("0000000000000001","0000000011110000");
_memoryController.Write("0000000000000010","1111000000001111"); _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."); _memoryController = new MC(numberOfMemoryBanks, memoryBankSize);
Byte[] bytes = new Byte[byteCount]; for (int i = 0; i < memoryBankSize; i++)
for (int i = 0; i < byteCount; 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);
} }
} }
} }
+49
View File
@@ -2,6 +2,55 @@ namespace CPUSimulation
{ {
public class CU // Control Unit public class CU // Control Unit
{ {
Dictionary<string, string> _instructionSet;
Dictionary<string, Word> _registers;
public CU(int numberOfMemoryBanks)
{
_instructionSet = new Dictionary<string, string>();
_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<string, Word>();
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;
}
}
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
namespace CPUSimulation namespace CPUSimulation
{ {
public class MC public class MC // Memory Controller
{ {
MemoryBank[] _memoryBanks; MemoryBank[] _memoryBanks;
-3
View File
@@ -52,7 +52,6 @@ namespace CPUSimulation
return _bits; return _bits;
} }
} }
public class MemoryBank public class MemoryBank
{ {
Dictionary<string, Byte> _bytes; Dictionary<string, Byte> _bytes;
@@ -75,7 +74,6 @@ namespace CPUSimulation
_bytes[address] = data; _bytes[address] = data;
} }
} }
public class Address public class Address
{ {
Byte _highByte; Byte _highByte;
@@ -135,7 +133,6 @@ namespace CPUSimulation
return addressString; return addressString;
} }
} }
public class Word public class Word
{ {
Byte[] _bytes; Byte[] _bytes;
+21 -1
View File
@@ -2,6 +2,26 @@ namespace CPUSimulation
{ {
public class Utils 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);
}
} }
} }