-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALU.java
More file actions
128 lines (127 loc) · 6.1 KB
/
ALU.java
File metadata and controls
128 lines (127 loc) · 6.1 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public class ALU {
// Final variables paired different values for the ALU instruction to perform
private static final Bit TRUE = new Bit(true);
private static final Bit FALSE = new Bit(false);
private static final Word16 ADD_NUM = new Word16(new Bit[]{FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 AND = new Word16(new Bit[]{FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 MULTIPLY_NUM = new Word16(new Bit[]{FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 LEFT_SHIFT = new Word16(new Bit[]{FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 SUBTRACT_NUM = new Word16(new Bit[]{FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 OR = new Word16(new Bit[]{FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
private static final Word16 RIGHT_SHIFT = new Word16(new Bit[]{FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
public static final Word16 COMPARE = new Word16(new Bit[]{FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE});
// Built vals for ALU.
public Word16 instruction = new Word16();
public Word32 op1 = new Word32();
public Word32 op2 = new Word32();
public Word32 result = new Word32();
public Bit less = new Bit(false);
public Bit equal = new Bit(false);
// doInstruction handles any arithmetic involving variables instruction, op1, and op2
public void doInstruction(){
// Handle adding
if(instruction.equals(ADD_NUM,5)){
Word32 fNum = new Word32();
Word32 sNum = new Word32();
op1.copy(fNum);
op2.copy(sNum);
Adder.add(fNum, sNum, result);
}
// Handle and
else if(instruction.equals(AND,5))
op1.and(op2, result);
// Handle multiplication
else if(instruction.equals(MULTIPLY_NUM,5)){
Word32 fNum = new Word32();
Word32 sNum = new Word32();
op1.copy(fNum);
op2.copy(sNum);
Multiplier.multiply(fNum, sNum, result);
}
// Handle left shifting
else if(instruction.equals(LEFT_SHIFT,5)){
// Create copies of op1 and op2, subtract from op2 by 1 and leftshift by 1
// the op1Fin. After op2 amount of times of being leftshifted, set it to result.
Word32 one = new Word32();
one.setBitN(31, new Bit(true));
Word32 op1Fin = new Word32();
op1.copy(op1Fin);
Word32 iterator = new Word32();
op2.copy(iterator);
while(!iterator.equals(new Word32())){
Shifter.LeftShift(op1Fin, 1, op1Fin);
Word32 cp = new Word32();
iterator.copy(cp);
Adder.subtract(cp, one, iterator);
}
op1Fin.copy(result);
}
// Handle subtracting
else if(instruction.equals(SUBTRACT_NUM,5))
Adder.subtract(op1, op2, result);
// Handle OR
else if(instruction.equals(OR,5))
op1.or(op2, result);
// Handle right shifting
else if(instruction.equals(RIGHT_SHIFT,5)){
// Create copies of op1 and op2, subtract from op2 by 1 and rightshift by 1
// the op1Fin. After op2 amount of times of being rightshifted, set it to result.
Word32 one = new Word32();
one.setBitN(31, new Bit(true));
Word32 op1Fin = new Word32();
op1.copy(op1Fin);
Word32 iterator = new Word32();
op2.copy(iterator);
while(!iterator.equals(new Word32())){
Shifter.RightShift(op1Fin, 1, op1Fin);
Word32 cp = new Word32();
iterator.copy(cp);
Adder.subtract(cp, one, iterator);
}
op1Fin.copy(result);
}
// Handle comparing
else if(instruction.equals(COMPARE,5)) {
// Account for negative numbers
Bit a = new Bit(false);
Bit b = new Bit(false);
op1.getBitN(0, a);
op2.getBitN(0, b);
if (a.getValue() == Bit.boolValues.TRUE && b.getValue() == Bit.boolValues.FALSE){
less.assign(Bit.boolValues.TRUE);
equal.assign(Bit.boolValues.FALSE);
}
else if (a.getValue() == Bit.boolValues.FALSE && b.getValue() == Bit.boolValues.TRUE) {
less.assign(Bit.boolValues.FALSE);
equal.assign(Bit.boolValues.FALSE);
}
else{
// Subtract op1 and op2 and set to res
Word32 res = new Word32();
Adder.subtract(op1, op2, res);
// If res is 0 then set equal to true and less to false
if (res.equals(new Word32())) {
less.assign(Bit.boolValues.FALSE);
equal.assign(Bit.boolValues.TRUE);
}
// Else, check if res is positive or negative. If negative then
// set equal to false and less to true, else set equal to false and
// res to false.
else {
Bit checkNegBit = new Bit(false);
res.getBitN(0, checkNegBit);
if (checkNegBit.getValue() == Bit.boolValues.TRUE) {
less.assign(Bit.boolValues.TRUE);
equal.assign(Bit.boolValues.FALSE);
} else {
less.assign(Bit.boolValues.FALSE);
equal.assign(Bit.boolValues.FALSE);
}
}
}
}
else{
System.out.println("Error!");
}
}
}