-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChipElm.java
293 lines (287 loc) · 7.39 KB
/
ChipElm.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.awt.*;
import java.util.StringTokenizer;
abstract class ChipElm extends CircuitElm {
int csize, cspc, cspc2;
int bits;
final int FLAG_SMALL = 1;
final int FLAG_FLIP_X = 1024;
final int FLAG_FLIP_Y = 2048;
public ChipElm(int xx, int yy) {
super(xx, yy);
if (needsBits())
bits = (this instanceof DecadeElm) ? 10 : 4;
noDiagonal = true;
setupPins();
setSize(sim.smallGridCheckItem.getState() ? 1 : 2);
}
public ChipElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
if (needsBits())
bits = new Integer(st.nextToken()).intValue();
noDiagonal = true;
setupPins();
setSize((f & FLAG_SMALL) != 0 ? 1 : 2);
int i;
for (i = 0; i != getPostCount(); i++) {
if (pins[i].state) {
volts[i] = new Double(st.nextToken()).doubleValue();
pins[i].value = volts[i] > 2.5;
}
}
}
boolean needsBits() { return false; }
void setSize(int s) {
csize = s;
cspc = 8*s;
cspc2 = cspc*2;
flags &= ~FLAG_SMALL;
flags |= (s == 1) ? FLAG_SMALL : 0;
}
abstract void setupPins();
void draw(Graphics g) {
drawChip(g);
}
void drawChip(Graphics g) {
int i;
Font f = new Font("SansSerif", 0, 10*csize);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
setVoltageColor(g, volts[i]);
Point a = p.post;
Point b = p.stub;
drawThickLine(g, a, b);
p.curcount = updateDotCount(p.current, p.curcount);
drawDots(g, b, a, p.curcount);
if (p.bubble) {
g.setColor(sim.printableCheckItem.getState() ?
Color.white : Color.black);
drawThickCircle(g, p.bubbleX, p.bubbleY, 1);
g.setColor(lightGrayColor);
drawThickCircle(g, p.bubbleX, p.bubbleY, 3);
}
g.setColor(whiteColor);
int sw = fm.stringWidth(p.text);
g.drawString(p.text, p.textloc.x-sw/2,
p.textloc.y+fm.getAscent()/2);
if (p.lineOver) {
int ya = p.textloc.y-fm.getAscent()/2;
g.drawLine(p.textloc.x-sw/2, ya, p.textloc.x+sw/2, ya);
}
}
g.setColor(needsHighlight() ? selectColor : lightGrayColor);
drawThickPolygon(g, rectPointsX, rectPointsY, 4);
if (clockPointsX != null)
g.drawPolyline(clockPointsX, clockPointsY, 3);
for (i = 0; i != getPostCount(); i++)
drawPost(g, pins[i].post.x, pins[i].post.y, nodes[i]);
}
int rectPointsX[], rectPointsY[];
int clockPointsX[], clockPointsY[];
Pin pins[];
int sizeX, sizeY;
boolean lastClock;
void drag(int xx, int yy) {
yy = sim.snapGrid(yy);
if (xx < x) {
xx = x; yy = y;
} else {
y = y2 = yy;
x2 = sim.snapGrid(xx);
}
setPoints();
}
void setPoints() {
if (x2-x > sizeX*cspc2 && this == sim.dragElm)
setSize(2);
int hs = cspc;
int x0 = x+cspc2; int y0 = y;
int xr = x0-cspc;
int yr = y0-cspc;
int xs = sizeX*cspc2;
int ys = sizeY*cspc2;
rectPointsX = new int[] { xr, xr+xs, xr+xs, xr };
rectPointsY = new int[] { yr, yr, yr+ys, yr+ys };
setBbox(xr, yr, rectPointsX[2], rectPointsY[2]);
int i;
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
switch (p.side) {
case SIDE_N: p.setPoint(x0, y0, 1, 0, 0, -1, 0, 0); break;
case SIDE_S: p.setPoint(x0, y0, 1, 0, 0, 1, 0, ys-cspc2);break;
case SIDE_W: p.setPoint(x0, y0, 0, 1, -1, 0, 0, 0); break;
case SIDE_E: p.setPoint(x0, y0, 0, 1, 1, 0, xs-cspc2, 0);break;
}
}
}
Point getPost(int n) {
return pins[n].post;
}
abstract int getVoltageSourceCount(); // output count
void setVoltageSource(int j, int vs) {
int i;
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
if (p.output && j-- == 0) {
p.voltSource = vs;
return;
}
}
System.out.println("setVoltageSource failed for " + this);
}
void stamp() {
int i;
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
if (p.output)
sim.stampVoltageSource(0, nodes[i], p.voltSource);
}
}
void execute() {}
void doStep() {
int i;
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
if (!p.output)
p.value = volts[i] > 2.5;
}
execute();
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
if (p.output)
sim.updateVoltageSource(0, nodes[i], p.voltSource,
p.value ? 5 : 0);
}
}
void reset() {
int i;
for (i = 0; i != getPostCount(); i++) {
pins[i].value = false;
pins[i].curcount = 0;
volts[i] = 0;
}
lastClock = false;
}
String dump() {
int t = getDumpType();
String s = super.dump();
if (needsBits())
s += " " + bits;
int i;
for (i = 0; i != getPostCount(); i++) {
if (pins[i].state)
s += " " + volts[i];
}
return s;
}
void getInfo(String arr[]) {
arr[0] = getChipName();
int i, a = 1;
for (i = 0; i != getPostCount(); i++) {
Pin p = pins[i];
if (arr[a] != null)
arr[a] += "; ";
else
arr[a] = "";
String t = p.text;
if (p.lineOver)
t += '\'';
if (p.clock)
t = "Clk";
arr[a] += t + " = " + getVoltageText(volts[i]);
if (i % 2 == 1)
a++;
}
}
void setCurrent(int x, double c) {
int i;
for (i = 0; i != getPostCount(); i++)
if (pins[i].output && pins[i].voltSource == x)
pins[i].current = c;
}
String getChipName() { return "chip"; }
boolean getConnection(int n1, int n2) { return false; }
boolean hasGroundConnection(int n1) {
return pins[n1].output;
}
public EditInfo getEditInfo(int n) {
if (n == 0) {
EditInfo ei = new EditInfo("", 0, -1, -1);
ei.checkbox = new Checkbox("Flip X", (flags & FLAG_FLIP_X) != 0);
return ei;
}
if (n == 1) {
EditInfo ei = new EditInfo("", 0, -1, -1);
ei.checkbox = new Checkbox("Flip Y", (flags & FLAG_FLIP_Y) != 0);
return ei;
}
return null;
}
public void setEditValue(int n, EditInfo ei) {
if (n == 0) {
if (ei.checkbox.getState())
flags |= FLAG_FLIP_X;
else
flags &= ~FLAG_FLIP_X;
setPoints();
}
if (n == 1) {
if (ei.checkbox.getState())
flags |= FLAG_FLIP_Y;
else
flags &= ~FLAG_FLIP_Y;
setPoints();
}
}
final int SIDE_N = 0;
final int SIDE_S = 1;
final int SIDE_W = 2;
final int SIDE_E = 3;
class Pin {
Pin(int p, int s, String t) {
pos = p; side = s; text = t;
}
Point post, stub;
Point textloc;
int pos, side, voltSource, bubbleX, bubbleY;
String text;
boolean lineOver, bubble, clock, output, value, state;
double curcount, current;
void setPoint(int px, int py, int dx, int dy, int dax, int day,
int sx, int sy) {
if ((flags & FLAG_FLIP_X) != 0) {
dx = -dx;
dax = -dax;
px += cspc2*(sizeX-1);
sx = -sx;
}
if ((flags & FLAG_FLIP_Y) != 0) {
dy = -dy;
day = -day;
py += cspc2*(sizeY-1);
sy = -sy;
}
int xa = px+cspc2*dx*pos+sx;
int ya = py+cspc2*dy*pos+sy;
post = new Point(xa+dax*cspc2, ya+day*cspc2);
stub = new Point(xa+dax*cspc , ya+day*cspc );
textloc = new Point(xa , ya );
if (bubble) {
bubbleX = xa+dax*10*csize;
bubbleY = ya+day*10*csize;
}
if (clock) {
clockPointsX = new int[3];
clockPointsY = new int[3];
clockPointsX[0] = xa+dax*cspc-dx*cspc/2;
clockPointsY[0] = ya+day*cspc-dy*cspc/2;
clockPointsX[1] = xa;
clockPointsY[1] = ya;
clockPointsX[2] = xa+dax*cspc+dx*cspc/2;
clockPointsY[2] = ya+day*cspc+dy*cspc/2;
}
}
}
}