-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGateElm.java
More file actions
130 lines (127 loc) · 3.67 KB
/
GateElm.java
File metadata and controls
130 lines (127 loc) · 3.67 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
129
import java.awt.*;
import java.util.StringTokenizer;
abstract class GateElm extends CircuitElm {
final int FLAG_SMALL = 1;
int inputCount = 2;
boolean lastOutput;
public GateElm(int xx, int yy) {
super(xx, yy);
noDiagonal = true;
inputCount = 2;
setSize(sim.smallGridCheckItem.getState() ? 1 : 2);
}
public GateElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
inputCount = new Integer(st.nextToken()).intValue();
lastOutput = new Double (st.nextToken()).doubleValue() > 2.5;
noDiagonal = true;
setSize((f & FLAG_SMALL) != 0 ? 1 : 2);
}
boolean isInverting() { return false; }
int gsize, gwidth, gwidth2, gheight, hs2;
void setSize(int s) {
gsize = s;
gwidth = 7*s;
gwidth2 = 14*s;
gheight = 8*s;
flags = (s == 1) ? FLAG_SMALL : 0;
}
String dump() {
return super.dump() + " " + inputCount + " " + volts[inputCount];
}
Point inPosts[], inGates[];
int ww;
void setPoints() {
super.setPoints();
if (dn > 150 && this == sim.dragElm)
setSize(2);
int hs = gheight;
int i;
ww = gwidth2; // was 24
if (ww > dn/2)
ww = (int) (dn/2);
if (isInverting() && ww+8 > dn/2)
ww = (int) (dn/2-8);
calcLeads(ww*2);
inPosts = new Point[inputCount];
inGates = new Point[inputCount];
allocNodes();
int i0 = -inputCount/2;
for (i = 0; i != inputCount; i++, i0++) {
if (i0 == 0 && (inputCount & 1) == 0)
i0++;
inPosts[i] = interpPoint(point1, point2, 0, hs*i0);
inGates[i] = interpPoint(lead1, lead2, 0, hs*i0);
volts[i] = (lastOutput ^ isInverting()) ? 5 : 0;
}
hs2 = gwidth*(inputCount/2+1);
setBbox(point1, point2, hs2);
}
void draw(Graphics g) {
int i;
for (i = 0; i != inputCount; i++) {
setVoltageColor(g, volts[i]);
drawThickLine(g, inPosts[i], inGates[i]);
}
setVoltageColor(g, volts[inputCount]);
drawThickLine(g, lead2, point2);
g.setColor(needsHighlight() ? selectColor : lightGrayColor);
drawThickPolygon(g, gatePoly);
if (linePoints != null)
for (i = 0; i != linePoints.length-1; i++)
drawThickLine(g, linePoints[i], linePoints[i+1]);
if (isInverting())
drawThickCircle(g, pcircle.x, pcircle.y, 3);
curcount = updateDotCount(current, curcount);
drawDots(g, lead2, point2, curcount);
drawPosts(g);
}
Polygon gatePoly;
Point pcircle, linePoints[];
int getPostCount() { return inputCount+1; }
Point getPost(int n) {
if (n == inputCount)
return point2;
return inPosts[n];
}
int getVoltageSourceCount() { return 1; }
abstract String getGateName();
void getInfo(String arr[]) {
arr[0] = getGateName();
arr[1] = "Vout = " + getVoltageText(volts[inputCount]);
arr[2] = "Iout = " + getCurrentText(getCurrent());
}
void stamp() {
sim.stampVoltageSource(0, nodes[inputCount], voltSource);
}
boolean getInput(int x) {
return volts[x] > 2.5;
}
abstract boolean calcFunction();
void doStep() {
int i;
boolean f = calcFunction();
if (isInverting())
f = !f;
lastOutput = f;
double res = f ? 5 : 0;
sim.updateVoltageSource(0, nodes[inputCount], voltSource, res);
}
public EditInfo getEditInfo(int n) {
if (n == 0)
return new EditInfo("# of Inputs", inputCount, 1, 8).
setDimensionless();
return null;
}
public void setEditValue(int n, EditInfo ei) {
inputCount = (int) ei.value;
setPoints();
}
// there is no current path through the gate inputs, but there
// is an indirect path through the output to ground.
boolean getConnection(int n1, int n2) { return false; }
boolean hasGroundConnection(int n1) {
return (n1 == inputCount);
}
}