namespace CPUSimulation
{
public class Utils
{
static Random _random = new Random();
///
/// Generates a random word
///
///
/// input must be between 0 and 2 (inclusive)
///
///
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');
}
}
}