-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALU_test.java
129 lines (122 loc) · 6.56 KB
/
ALU_test.java
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
129
package ComputerArchitecture;
/*
Jan Karl Galia
Comp. Architecture ******************NOTE TO USERS: TESTS ARE AUTOMATED, VALUES(BINARY BIT ARRAYS AND INTEGERS) ARE RANDOMLY GENERATED************
*************TO CONVERT TO T,F RATHER THAN 1,0, REPLACE toStringConvert() to toString()**********
HW 4: The ALU
*/
public class ALU_test {
public static void main(String[] args) {
runTests();
}
public static void runTests() {
testALU();
}
public static void testALU() {
final Bit[] test = new Bit[4];
Longword otherInput = new Longword();
//Random BitArray Generator for Automatic testing---------------------------------------
Longword randomInput = new Longword();
Bit randomBit = new Bit();
Bit bitTrue = new Bit();
bitTrue.set(true);
boolean randomBool = false;
for (int i = 0; i < 32; i++) {//Creates Random binary string and sets it to randomInput bitArray
randomBool = randomBit.randomBool();
if (randomBool) {
randomInput.setBit(i, bitTrue);
}
}
for (int i = 0; i < 32; i++) {//Creates Random binary string and sets it to randomInput bitArray
randomBool = randomBit.randomBool();
if (randomBool) {
otherInput.setBit(i, bitTrue);
}
}
//--------------------------------------------------------------------------------------------
System.out.println("-------------------------------------------");
System.out.println("AND");
test[0] = new Bit(1);
test[1] = new Bit(0);
test[2] = new Bit(0);
test[3] = new Bit(0);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUand(t,f):" + ALU.doOp(test, randomInput, otherInput));
System.out.println("TestALUand(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
//--------------------------------------------------------------------------------------------
System.out.println("-------------------------------------------");
System.out.println("OR");
test[0] = new Bit(1);
test[1] = new Bit(0);
test[2] = new Bit(0);
test[3] = new Bit(1);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUor(t,f):" + ALU.doOp(test, randomInput, otherInput));
System.out.println("TestALUor(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
//--------------------------------------------------------------------------------------------
System.out.println("-------------------------------------------");
System.out.println("XOR");
test[0] = new Bit(1);
test[1] = new Bit(0);
test[2] = new Bit(1);
test[3] = new Bit(0);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUxor(t,f):" + ALU.doOp(test, randomInput, otherInput));
System.out.println("TestALUxor(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("NOT");
test[0] = new Bit(1);
test[1] = new Bit(0);
test[2] = new Bit(1);
test[3] = new Bit(1);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestALUnot(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("LEFT SHIFT");
test[0] = new Bit(1);
test[1] = new Bit(1);
test[2] = new Bit(0);
test[3] = new Bit(0);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestALULeftShift(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("RIGHT SHIFT");
test[0] = new Bit(1);
test[1] = new Bit(1);
test[2] = new Bit(0);
test[3] = new Bit(1);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestALURightShift(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("ADDING 2s Complement Inputs, OUTPUT SIGNS ARE BIT[33]");
test[0] = new Bit(1);
test[1] = new Bit(1);
test[2] = new Bit(1);
test[3] = new Bit(0);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUAdd(1,0)(2sComplement-Sign Bit NOT INCLUDED):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("SUBTRACTING 2s Complement Inputs, OUTPUT SIGNS ARE BIT[33]");
test[0] = new Bit(1);
test[1] = new Bit(1);
test[2] = new Bit(1);
test[3] = new Bit(1);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUSub(1,0)(2sComplement-Sign Bit NOT INCLUDED):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
System.out.println("-------------------------------------------");
System.out.println("MULTIPLYING 2s COMPLEMENT, OUTPUTTING LOWER 32 BITS ONLY");
test[0] = new Bit(0);
test[1] = new Bit(1);
test[2] = new Bit(1);
test[3] = new Bit(1);
System.out.println("TestFirstInput:" + randomInput.toStringConvert());
System.out.println("TestOtherInput:" + otherInput.toStringConvert());
System.out.println("TestALUMultiply(t,f):" + ALU.doOp(test, randomInput, otherInput));
System.out.println("TestALUMultiply(1,0):" + ALU.doOp(test, randomInput, otherInput).toStringConvert());
}
}