forked from FanOfWolves/ISA-Decoder_16Bit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
84 lines (76 loc) · 3.33 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ------------------------------------------------------------------------------------------------------------------------
// File name: Program.cs
// Project name: ISA
// Project description: Decoder for our awesome Detached-Toe 16-bit RISC ISA.
// ------------------------------------------------------------------------------------------------------------------------
// Creation Date: 03/31/2021
// ------------------------------------------------------------------------------------------------------------------------
using System;
namespace ISA_Decoder_16Bit {
class Program {
static void Main() {
// Read Input
String hexinput = "0000";
int binaryInput;
while (true) {
Console.WriteLine("Input a 4-digit hex string or \"XXXX\" to exit.\n");
// Read input
hexinput = Console.ReadLine();
// Convert Input to unsigned 16-bit integer
try {
// Exit on EXIT CODE
if (hexinput.ToUpper() == "XXXX") {
Console.WriteLine("Program exited.");
break;
}
binaryInput = Convert.ToInt32(hexinput, 16);
//Check if larger than Uint16... (i.e. not a 16bit)
if (binaryInput > ushort.MaxValue) {
throw new Exception("Input cannot be greater than an unsigned short.");
}
// Else decode input
string output = DecodeInput(binaryInput);
// Display output
Console.WriteLine(output);
}
catch (Exception e) {
Console.WriteLine($"Could not read input '{hexinput}': {e.Message}");
}
}
}
/// <summary>
/// Decode the input from the user.
/// </summary>
/// <param name="input">from user</param>
/// <returns>a string representing a human-readable text translation of the machine code read and translated by the ISA architecture.</returns>
private static string DecodeInput(int input) {
InstructionType ourInstruction = null;
// I Check instruction type (Bits 14-15)
// I.A SHOW bits 14-15
int bitmask = BitUtilities.CreateBitMask(14, 15); //1100 0000 0000
int maskedinput = input & bitmask;
switch (maskedinput) {
case 0x0000: //00 - data transfer
ourInstruction = new DataTransferType();
break;
case 0x4000: //01 - arithmetic/logical
ourInstruction = new ArithmeticType();
break;
case 0x8000: //10 - flow control
ourInstruction = new FlowControlType();
break;
default:
return "Invalid Instruction Type!";
}
try {
// Decode input
string output = ourInstruction.DecodeInstruction(input);
return output;
}
// Oh no! Something went a wrong!
catch (Exception e) {
return $"{e.Message} You dun messed up.";
}
}
}
}