Files

193 lines
6.1 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');
}
public static string BinaryStringToHexString(string binaryString)
{
if (binaryString.Length % 4 != 0)
{
throw new ArgumentException("Binary string length must be a multiple of 4.");
}
string hexString = "";
for (int i = 0; i < binaryString.Length; i += 4)
{
string fourBits = binaryString.Substring(i, 4);
switch (fourBits)
{
case "0000":
hexString += "0";
break;
case "0001":
hexString += "1";
break;
case "0010":
hexString += "2";
break;
case "0011":
hexString += "3";
break;
case "0100":
hexString += "4";
break;
case "0101":
hexString += "5";
break;
case "0110":
hexString += "6";
break;
case "0111":
hexString += "7";
break;
case "1000":
hexString += "8";
break;
case "1001":
hexString += "9";
break;
case "1010":
hexString += "A";
break;
case "1011":
hexString += "B";
break;
case "1100":
hexString += "C";
break;
case "1101":
hexString += "D";
break;
case "1110":
hexString += "E";
break;
case "1111":
hexString += "F";
break;
}
}
return hexString;
}
public static string HexStringToBinaryString(string hexString)
{
string binaryString = "";
foreach (char hexChar in hexString)
{
switch (hexChar)
{
case '0':
binaryString += "0000";
break;
case '1':
binaryString += "0001";
break;
case '2':
binaryString += "0010";
break;
case '3':
binaryString += "0011";
break;
case '4':
binaryString += "0100";
break;
case '5':
binaryString += "0101";
break;
case '6':
binaryString += "0110";
break;
case '7':
binaryString += "0111";
break;
case '8':
binaryString += "1000";
break;
case '9':
binaryString += "1001";
break;
case 'A':
case 'a':
binaryString += "1010";
break;
case 'B':
case 'b':
binaryString += "1011";
break;
case 'C':
case 'c':
binaryString += "1100";
break;
case 'D':
case 'd':
binaryString += "1101";
break;
case 'E':
case 'e':
binaryString += "1110";
break;
case 'F':
case 'f':
binaryString += "1111";
break;
}
}
return binaryString;
}
public static Int16 HexStringToInt16(string hexString)
{
if (hexString.Length > 4)
{
throw new ArgumentException("Hex string length must be 4 or less.");
}
return Convert.ToInt16(hexString, 16);
}
}
}