added a new ToString method to Word, DWord, and QWord classes.
The ToString method will concatenate the string representations of the high and low words (or dwords) to create a single string representation of the entire word. This can be used to more easily write and read the value from the MC and CU classes.
This commit is contained in:
@@ -4,7 +4,7 @@ namespace CPUSimulation
|
|||||||
{
|
{
|
||||||
public class CPU
|
public class CPU
|
||||||
{
|
{
|
||||||
static int numberOfMemoryBanks = 4;
|
public static int numberOfMemoryBanks = 2;
|
||||||
static int memoryBankSize = 1024;
|
static int memoryBankSize = 1024;
|
||||||
static MC _memoryController;
|
static MC _memoryController;
|
||||||
|
|
||||||
@@ -17,8 +17,8 @@ namespace CPUSimulation
|
|||||||
_memoryController.Write("0000000000000010","1111000000001111");
|
_memoryController.Write("0000000000000010","1111000000001111");
|
||||||
for (int i = 0; i < 16; i++)
|
for (int i = 0; i < 16; i++)
|
||||||
{
|
{
|
||||||
Word dataRead = _memoryController.Read((Int16)i);
|
string dataRead = _memoryController.Read((Int16)i);
|
||||||
Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
|
Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +27,27 @@ namespace CPUSimulation
|
|||||||
_memoryController = new MC(numberOfMemoryBanks, memoryBankSize);
|
_memoryController = new MC(numberOfMemoryBanks, memoryBankSize);
|
||||||
for (int i = 0; i < memoryBankSize; i++)
|
for (int i = 0; i < memoryBankSize; i++)
|
||||||
{
|
{
|
||||||
Word dataToWrite = Utils.GenerateRandomWord(numberOfMemoryBanks);
|
if (numberOfMemoryBanks == 2)
|
||||||
_memoryController.Write((Int16)i, dataToWrite);
|
{
|
||||||
|
Word dataToWrite = Utils.GenerateRandomWord(numberOfMemoryBanks);
|
||||||
|
_memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString());
|
||||||
|
}
|
||||||
|
else if (numberOfMemoryBanks == 4)
|
||||||
|
{
|
||||||
|
DWord dataToWrite = new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2));
|
||||||
|
_memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString());
|
||||||
|
}
|
||||||
|
else if (numberOfMemoryBanks == 8)
|
||||||
|
{
|
||||||
|
QWord dataToWrite = new QWord(new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2)), new DWord(Utils.GenerateRandomWord(2), Utils.GenerateRandomWord(2)));
|
||||||
|
_memoryController.Write(new Address((Int16)i).ToString(), dataToWrite.ToString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Number of memory banks must be between 1 and 8 (inclusive).");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Word dataRead = _memoryController.Read((Int16)i);
|
//Word dataRead = _memoryController.Read((Int16)i);
|
||||||
//Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
|
//Console.WriteLine($"Data read from address {new Address((Int16)i).ToString()}: {dataRead.ToString()}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ namespace CPUSimulation
|
|||||||
{
|
{
|
||||||
public class CU // Control Unit
|
public class CU // Control Unit
|
||||||
{
|
{
|
||||||
|
int numberOfRegisters = 16;
|
||||||
Dictionary<string, string> _instructionSet;
|
Dictionary<string, string> _instructionSet;
|
||||||
Dictionary<string, Word> _registers;
|
Dictionary<string, Word> _registers;
|
||||||
public CU(int numberOfMemoryBanks)
|
public CU(int numberOfMemoryBanks)
|
||||||
{
|
{
|
||||||
|
|
||||||
_instructionSet = new Dictionary<string, string>();
|
_instructionSet = new Dictionary<string, string>();
|
||||||
_instructionSet.Add("00000", "NOP");
|
_instructionSet.Add("00000", "NOP");
|
||||||
_instructionSet.Add("00001", "LOAD");
|
_instructionSet.Add("00001", "LOAD");
|
||||||
@@ -25,9 +27,11 @@ namespace CPUSimulation
|
|||||||
_instructionSet.Add("01111", "DEC");
|
_instructionSet.Add("01111", "DEC");
|
||||||
|
|
||||||
_registers = new Dictionary<string, Word>();
|
_registers = new Dictionary<string, Word>();
|
||||||
for (int i = 0; i < numberOfMemoryBanks; i++)
|
Word zeroWord = new Word(new Byte[2] { new Byte("00000000"), new Byte("00000000") });
|
||||||
|
for (int i = 0; i < numberOfRegisters; i++)
|
||||||
{
|
{
|
||||||
_registers.Add($"R{i}", new Word(new Byte[0]));
|
string registerName = ((byte)i).ToString().Substring(4, 4);
|
||||||
|
_registers.Add(registerName, zeroWord);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -43,13 +47,13 @@ namespace CPUSimulation
|
|||||||
{
|
{
|
||||||
_value++;
|
_value++;
|
||||||
}
|
}
|
||||||
public void SetValue(Int16 value)
|
public void SetValue(string value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = Convert.ToInt16(value, 2);
|
||||||
}
|
}
|
||||||
public Int16 GetValue()
|
public string GetValue()
|
||||||
{
|
{
|
||||||
return _value;
|
return Utils.Int16ToBinaryString(_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace CPUSimulation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Word Read(Int16 address)
|
public string Read(Int16 address)
|
||||||
{
|
{
|
||||||
AddressOkay(address);
|
AddressOkay(address);
|
||||||
Byte[] byteData = new Byte[_numberOfMemoryBanks];
|
Byte[] byteData = new Byte[_numberOfMemoryBanks];
|
||||||
@@ -35,7 +35,22 @@ namespace CPUSimulation
|
|||||||
{
|
{
|
||||||
byteData[i] = _memoryBanks[i].Read(new Address(address).ToString());
|
byteData[i] = _memoryBanks[i].Read(new Address(address).ToString());
|
||||||
}
|
}
|
||||||
return new Word(byteData);
|
if (byteData.Length == 2)
|
||||||
|
{
|
||||||
|
return new Word(byteData).ToString();
|
||||||
|
}
|
||||||
|
else if (byteData.Length == 4)
|
||||||
|
{
|
||||||
|
return new DWord(new Word(byteData.Take(2).ToArray()), new Word(byteData.Skip(2).Take(2).ToArray())).ToString();
|
||||||
|
}
|
||||||
|
else if (byteData.Length == 8)
|
||||||
|
{
|
||||||
|
return new QWord(new DWord(new Word(byteData.Take(2).ToArray()), new Word(byteData.Skip(2).Take(2).ToArray())), new DWord(new Word(byteData.Skip(4).Take(2).ToArray()), new Word(byteData.Skip(6).Take(2).ToArray()))).ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Number of memory banks must be between 1 and 8 (inclusive).");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(Int16 address, Word data)
|
public void Write(Int16 address, Word data)
|
||||||
|
|||||||
@@ -133,11 +133,24 @@ namespace CPUSimulation
|
|||||||
return addressString;
|
return addressString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A word is a collection of 2 Bytes
|
||||||
|
/// </summary>
|
||||||
public class Word
|
public class Word
|
||||||
{
|
{
|
||||||
Byte[] _bytes;
|
Byte[] _bytes;
|
||||||
public Word(Byte[] bytes)
|
public Word(Byte[] bytes)
|
||||||
{
|
{
|
||||||
|
if (bytes.Length == 0 || bytes.Length > 2)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("A word must consist of 1 or 2 bytes.");
|
||||||
|
}
|
||||||
|
else if (bytes.Length == 1)
|
||||||
|
{
|
||||||
|
_bytes = new Byte[2] { new Byte("00000000"), bytes[0] };
|
||||||
|
}
|
||||||
_bytes = bytes;
|
_bytes = bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,4 +177,42 @@ namespace CPUSimulation
|
|||||||
return wordString;
|
return wordString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A double word is a collection of 2 Words, or 4 Bytes
|
||||||
|
/// </summary>
|
||||||
|
public class DWord
|
||||||
|
{
|
||||||
|
Word _highWord;
|
||||||
|
Word _lowWord;
|
||||||
|
public DWord(Word highWord, Word lowWord)
|
||||||
|
{
|
||||||
|
_highWord = highWord;
|
||||||
|
_lowWord = lowWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return _highWord.ToString() + _lowWord.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A quad word is a collection of 2 Double Words, or 8 Bytes
|
||||||
|
/// </summary>
|
||||||
|
public class QWord
|
||||||
|
{
|
||||||
|
DWord _highDWord;
|
||||||
|
DWord _lowDWord;
|
||||||
|
public QWord(DWord highDWord, DWord lowDWord)
|
||||||
|
{
|
||||||
|
_highDWord = highDWord;
|
||||||
|
_lowDWord = lowDWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
override public string ToString()
|
||||||
|
{
|
||||||
|
return _highDWord.ToString() + _lowDWord.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,16 +3,36 @@ namespace CPUSimulation
|
|||||||
public class Utils
|
public class Utils
|
||||||
{
|
{
|
||||||
static Random _random = new Random();
|
static Random _random = new Random();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generates a random word
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="byteCount"></param>
|
||||||
|
/// input must be between 0 and 2 (inclusive)
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="ArgumentException"></exception>
|
||||||
public static Word GenerateRandomWord(int byteCount)
|
public static Word GenerateRandomWord(int byteCount)
|
||||||
{
|
{
|
||||||
byteCount = _random.Next(1, byteCount + 1);
|
if (byteCount < 0)
|
||||||
//Console.WriteLine($"Generating random word with {byteCount} bytes.");
|
{
|
||||||
|
byteCount = 0;
|
||||||
|
}
|
||||||
Byte[] bytes = new Byte[byteCount];
|
Byte[] bytes = new Byte[byteCount];
|
||||||
for (int i = 0; i < byteCount; i++)
|
for (int i = 0; i < byteCount; i++)
|
||||||
{
|
{
|
||||||
bytes[i] = GenerateRandomByte();
|
bytes[i] = GenerateRandomByte();
|
||||||
}
|
}
|
||||||
|
if (byteCount == 1)
|
||||||
|
{
|
||||||
|
bytes = new Byte[2] { new Byte("00000000"), bytes[0] };
|
||||||
|
}
|
||||||
|
else if (byteCount == 0)
|
||||||
|
{
|
||||||
|
bytes = new Byte[2] { new Byte("00000000"), new Byte("00000000") };
|
||||||
|
}
|
||||||
return new Word(bytes);
|
return new Word(bytes);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public static Byte GenerateRandomByte()
|
public static Byte GenerateRandomByte()
|
||||||
{
|
{
|
||||||
@@ -23,5 +43,10 @@ namespace CPUSimulation
|
|||||||
}
|
}
|
||||||
return new Byte(bits);
|
return new Byte(bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string Int16ToBinaryString(short value)
|
||||||
|
{
|
||||||
|
return Convert.ToString(value & 0xFFFF, 2).PadLeft(16, '0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("CPUSimulation")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9efadc3b4a2f776b2177e2873a3b46b0d1af54c3")]
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+faa7801376d988a2b7b81332c9df3db882f08224")]
|
||||||
[assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")]
|
[assembly: System.Reflection.AssemblyProductAttribute("CPUSimulation")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("CPUSimulation")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
c2e5aae0cecf3bbefacc496747f7594154fd639e9a9c0b2086d3b9acd9b437a5
|
0093ec249d1fba7433aea9a209b4614dc995df5d3b8e509911d9d959d0697898
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user