Beginning to add Program Counter and Stack Pointer
Beginning to add initil Instruction Set
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,55 @@ namespace CPUSimulation
|
||||
{
|
||||
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,6 +1,6 @@
|
||||
namespace CPUSimulation
|
||||
{
|
||||
public class MC
|
||||
public class MC // Memory Controller
|
||||
{
|
||||
|
||||
MemoryBank[] _memoryBanks;
|
||||
|
||||
@@ -52,7 +52,6 @@ namespace CPUSimulation
|
||||
return _bits;
|
||||
}
|
||||
}
|
||||
|
||||
public class MemoryBank
|
||||
{
|
||||
Dictionary<string, Byte> _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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user