forked from FanOfWolves/ISA-Decoder_16Bit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUBoperation.cs
107 lines (90 loc) · 3.84 KB
/
SUBoperation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// ------------------------------------------------------------------------------------------------------------------------
// File name: SUBoperation.cs
// Project name: ISA
// Project description: Decoder for our awesome Detached-Toe 16-bit RISC ISA.
// ------------------------------------------------------------------------------------------------------------------------
// Creation Date: 03/31/2021
// ------------------------------------------------------------------------------------------------------------------------
namespace ISA_Decoder_16Bit {
/// <summary>
/// two operand operation that subtracts operand 2 from operand 1 and stores it in operand 1
/// </summary>
class SUBoperation : Operation {
string verb = "Subtract";
int operandOneEndBit = 8;
int operandOneStartBit = 5;
int operandOneValue;
string operandOneMeaning;
int operandTwoEndBit = 4;
int operandTwoStartBit = 1;
int operandTwoValue;
string operandTwoMeaning;
int immediateSwitchStartBit = 9;
int immediateSwitchEndBit = 9;
int immediateSwitchValue;
/// <summary>
/// default constructor
/// </summary>
public SUBoperation() {
}
/// <summary>
/// used to decode the operation and provide a human readable string.
/// </summary>
/// <param name="inputBits"></param>
/// <returns></returns>
public override string DecodeOperation(int inputBits) {
// Decode Immediate
DecodeImmediateSwitch(inputBits);
// Decode Operand One
DecodeFirstOperand(inputBits);
// Decode Operand Two
DecodeSecondOperand(inputBits);
// Get readable text
return GetHumanText();
}
/// <summary>
/// used to decode the first operand and get its meaning and value
/// </summary>
/// <param name="inputBits"></param>
private void DecodeFirstOperand(int inputBits) {
operandOneValue = BitUtilities.MaskInput(inputBits, operandOneStartBit, operandOneEndBit);
if (operandOneValue < 0 || operandOneValue > 15) {
operandTwoMeaning = $"OP1: Ya messed up";
}
else {
operandOneMeaning = $"r{operandOneValue}";
}
}
/// <summary>
/// used to decode the second operand and get its meaning and value
/// </summary>
/// <param name="inputBits">instruction bits</param>
private void DecodeSecondOperand(int inputBits) {
operandTwoValue = BitUtilities.MaskInput(inputBits, operandTwoStartBit, operandTwoEndBit);
// Immediate or Register?
if (immediateSwitchValue == 0) { // if register
if (operandTwoValue < 0 || operandTwoValue > 15)
{ //used to check if valid register
operandTwoMeaning = $"OP2: Ya messed up";
}
operandTwoMeaning = $"r{operandTwoValue}";
}
else { // else is an immediate
operandTwoMeaning = $"#{operandTwoValue}";
}
}
/// <summary>
/// Decode value of the immediateSwitch
/// </summary>
/// <param name="inputBits">our instruction</param>
private void DecodeImmediateSwitch(int inputBits) {
immediateSwitchValue = BitUtilities.MaskInput(inputBits, immediateSwitchStartBit, immediateSwitchEndBit);
}
/// <summary>
/// This badboy looks through ALL our available fields and constructs an English sentence.
/// </summary>
private string GetHumanText() {
return $"{verb} {operandOneMeaning} from {operandTwoMeaning}, store result in {operandOneMeaning}";
}
}
}