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:
@@ -3,16 +3,36 @@ namespace CPUSimulation
|
||||
public class Utils
|
||||
{
|
||||
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)
|
||||
{
|
||||
byteCount = _random.Next(1, byteCount + 1);
|
||||
//Console.WriteLine($"Generating random word with {byteCount} bytes.");
|
||||
if (byteCount < 0)
|
||||
{
|
||||
byteCount = 0;
|
||||
}
|
||||
Byte[] bytes = new Byte[byteCount];
|
||||
for (int i = 0; i < byteCount; i++)
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
public static Byte GenerateRandomByte()
|
||||
{
|
||||
@@ -23,5 +43,10 @@ namespace CPUSimulation
|
||||
}
|
||||
return new Byte(bits);
|
||||
}
|
||||
|
||||
public static string Int16ToBinaryString(short value)
|
||||
{
|
||||
return Convert.ToString(value & 0xFFFF, 2).PadLeft(16, '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user