-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.java
81 lines (71 loc) · 1.48 KB
/
ALU.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
/**
* The mathematical/logical unit of themips processor
*/
public class ALU {
public static final String AND = "100100";
public static final String OR = "100101";
public static final String ADD = "100000";
public static final String SUBTRACT = "100010";
public static final String NOR = "100111";
public static final String SLT = "101010";
public static final String MULTIPLY = "011000";
public static final String DIVIDE= "011010";
public static final String SHIFT_RIGHT= "000010";
public static final String SHIFT_LEFT= "000000";
private int out;
/**
* @return the out value
*/
public int getOut() {
return out;
}
/**
* @return the zero line control
*/
public boolean isZero() {
return zero;
}
private boolean zero;
public void setOperation( String control, int srcv, int rsv) {
zero = false;
out = 0;
switch(control) {
case ADD:
out = rsv + srcv;
break;
case SUBTRACT:
out = rsv - srcv;
if(out == 0) {
zero = true;
}
break;
case AND:
out = rsv & srcv;
break;
case OR:
out = rsv | srcv;
break;
case NOR:
out = ~(rsv | srcv);
break;
case MULTIPLY:
out = rsv * srcv;
break;
case DIVIDE:
out = rsv / srcv;
break;
case "100110":
out = rsv ^ srcv;
break;
case SLT:
out = rsv < srcv ? 1 : 0;
break;
case SHIFT_RIGHT:
out = rsv >> srcv;
break;
case SHIFT_LEFT:
out = rsv << srcv;
break;
}
}
}