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.
52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
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)
|
|
{
|
|
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()
|
|
{
|
|
Bit[] bits = new Bit[8];
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
bits[i] = new Bit(_random.Next(2) == 1);
|
|
}
|
|
return new Byte(bits);
|
|
}
|
|
|
|
public static string Int16ToBinaryString(short value)
|
|
{
|
|
return Convert.ToString(value & 0xFFFF, 2).PadLeft(16, '0');
|
|
}
|
|
}
|
|
} |