1 package edu.berkeley.slipway.gui;
3 import com.atmel.fpslic.*;
4 import edu.berkeley.slipway.*;
5 import static com.atmel.fpslic.FpslicConstants.*;
6 import static com.atmel.fpslic.FpslicUtil.*;
7 import edu.berkeley.slipway.*;
9 import java.awt.geom.*;
10 import java.awt.event.*;
11 import java.awt.color.*;
12 import org.ibex.util.*;
16 import static edu.berkeley.slipway.gui.GuiConstants.*;
18 public class Gui extends ZoomingPanel implements KeyListener, MouseMotionListener {
26 private Cell[][] ca = new Cell[128][];
28 //public static final Color nonselectedcell = new Color(0x44, 0x44, 0x44);
29 public static final Color nonselectedcell = new Color(0xee, 0xee, 0xee);
30 public static final Color selectedcell = new Color(0x00, 0x00, 0x00);
32 public void writeMode4() {
34 final JFileChooser fc = new JFileChooser();
35 int returnVal = fc.showSaveDialog(this);
36 Writer pw = new OutputStreamWriter(new FileOutputStream(fc.getSelectedFile()));
37 FpslicUtil.writeMode4(pw, drone);
40 System.err.println("done writing");
41 } catch (Exception e) {
46 public void readMode4() {
48 final JFileChooser fc = new JFileChooser();
49 int returnVal = fc.showOpenDialog(this);
50 FpslicUtil.readMode4(new FileInputStream(fc.getSelectedFile()), drone);
51 System.err.println("done reading");
53 } catch (Exception e) {
58 public Gui(Fpslic at40k, FtdiBoard drone) {
59 this(at40k, drone, 24, 24);
61 public Gui(Fpslic at40k, FtdiBoard drone, int width, int height) {
65 for(int i=0; i<ca.length; i++)
66 ca[i] = new Cell[128];
67 for(int x=0; x<width; x++)
68 for(int y=0; y<height; y++)
69 new Cell(x,y, at40k.cell(x, y));
79 Fpslic.Cell c = at40k.cell(0,0);
80 for(int i=0; i<256; i++) {
82 System.out.println(c.printYLut());
90 public boolean scanme = false;
91 public boolean xon = false;
92 public boolean yon = false;
93 public boolean xknown = false;
94 public boolean yknown = false;
96 public Cell(int x, int y, Fpslic.Cell cell) {
103 public boolean scanme() {
104 return cell.relevant();
106 public void clear() {
107 gg.color(in ? selectedcell : (scanme() ? new Color(0xbb, 0xbb, 0xbb) : nonselectedcell));
108 g.fillRect(0, 0, SIZE, SIZE);
112 if (cell.relevant() || scanme()) {
116 AffineTransform t = g.getTransform();
119 g.transform(rotateInnerTransform());
122 drawInternalRouting();
129 public void drawBuffer() {
131 if (!cell.out_relevant()) return;
133 GeneralPath p = new GeneralPath();
138 gg.color(cell.oe() == NONE ? Color.white : Color.green);
140 gg.color(Color.green);
143 gg.color(Color.magenta);
144 if (cell.oe() == V4) {
145 gg.line(24, 62, 16, 63);
146 } else if (cell.oe() == H4) {
147 gg.line(24, 67, 24, 76);
151 public void drawWires() {
153 for(int i=0; i<5; i++)
155 if (cell.hwire(i).isDriven()) {
156 gg.color(cell.out(i) ? ORANGE : MAGENTA);
157 gg.line(0, SIZE-(2*(1+RINGS)+2*i), SIZE, SIZE-(2*(1+RINGS)+2*i));
159 for(int i=0; i<5; i++)
161 if (cell.vwire(i).isDriven()) {
162 gg.color(cell.out(i) ? ORANGE : MAGENTA);
163 gg.line(2*(1+RINGS)+2*i, 0, 2*(1+RINGS)+2*i, SIZE);
165 if (cell.zi_to_xlut_relevant()) wire(cell.zi(), true);
166 if (cell.zi_to_ylut_relevant()) wire(cell.zi(), false);
167 if (cell.zi_to_xlut_relevant()) wire(cell.wi(), true);
168 if (cell.zi_to_ylut_relevant()) wire(cell.wi(), false);
171 public void wire(int plane, boolean xlut) {
172 if (!(plane >= L0 && plane <= L4 && cell.vx(plane))) return;
173 if (xlut ? xlut_relevant(cell) : cell.ylut_relevant())
174 route(new P(17 - 2*(plane-L0), 8),
175 rotateInner(xlut ? new P(38, 18) : new P(64, 18)),
179 public P corner(int corner, int size, int ring) {
181 case NW: return new P(0 +2*ring, size -2*ring);
182 case SW: return new P(0 +2*ring, 0 +2*ring);
183 case NE: return new P(size -2*ring, size -2*ring);
184 case SE: return new P(size -2*ring, 0 +2*ring);
185 default: return null;
189 public void drawInternalRouting() {
191 if (cell.fb_relevant()) {
193 gg.line(51, 74, 37, 74);
194 gg.line(37, 74, 51, 12);
195 } else if (cell.c() == XLUT) {
197 gg.line(33, 52, 51, 52);
198 gg.line(51, 52, 51, 12);
199 } else if (cell.c() == YLUT) {
201 gg.line(67, 52, 51, 52);
202 gg.line(51, 52, 51, 12);
204 gg.line(51, 56, 41, 56);
205 gg.line(41, 56, 51, 12);
207 if (xlut_relevant(cell)) {
208 gg.line(52, 12, XLUT_OUTPUT_POINT.getX(), 12);
209 gg.line(XLUT_OUTPUT_POINT.getX(), 12, XLUT_OUTPUT_POINT.getX(), 32);
211 if (cell.ylut_relevant()) {
212 gg.line(52, 12, YLUT_OUTPUT_POINT.getX(), 12);
213 gg.line(YLUT_OUTPUT_POINT.getX(), 12, YLUT_OUTPUT_POINT.getX(), 32);
218 public void drawLocal() {
219 if (!cell.ylut_relevant() && !xlut_relevant(cell)) return;
221 P in = rotateOuter(new P(HOFF, 0));
222 P join = rotateInner(new P(HOFF, CORE_OFFSET));
226 join = new P(in.getX(), join.getY());
229 join = new P(join.getX(), in.getY());
235 P xi = corner(cell.xi(), SIZE, 4);
236 if (((cell.xlut_relevant() && cell.xi_to_xlut_relevant()) ||
237 (cell.ylut_relevant() && cell.xi_to_ylut_relevant()))
238 && (cell.xi() != NONE) && (cell.xi() < L0 || cell.xi() > L4)
240 P xi2 = corner(cell.xi(), SIZE + 2*BEVEL, -1).translate(-BEVEL, -BEVEL);
243 xi = translate(xi, 0, -3);
244 xi2 = translate(xi2, 0, -3);
247 xi = translate(xi, 0, 3);
248 xi2 = translate(xi2, 0, 3);
254 if (xlut_relevant(cell)) {
256 if (cell.xi_to_xlut_relevant() && xi != null)
257 route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 3, 20)), 4);
261 if (cell.yi_to_xlut_relevant())
262 route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 6, 20)), 5);
266 gg.color(cell.xo() ? ORANGE : LIGHTRED);
267 P xout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+17 - 2, CORE_OFFSET + CORE_SIZE - 3));
269 xout = rotateInner(new P(51, 74));
270 gg.line(rotateInner(new P(51, 62)), xout);
271 } else if (cell.xo_relevant()) {
272 gg.line(rotateInner(XLUT_OUTPUT_POINT), xout);
274 if (cell.xo_relevant(NE)) {
275 gg.line(corner(NE, SIZE, xring).translate(0, 3), corner(NE, SIZE, 0).translate(0, 3));
276 route(xout, corner(NE, SIZE, xring).translate(0, 3), xring);
278 if (cell.xo_relevant(NW)) {
279 gg.line(corner(NW, SIZE, xring).translate(0, 3), corner(NW, SIZE, 0).translate(0, 3));
280 route(xout, corner(NW, SIZE, xring).translate(0, 3), xring);
282 if (cell.xo_relevant(SE)) {
283 gg.line(corner(SE, SIZE, xring).translate(0, -3), corner(SE, SIZE, 0).translate(0, -3));
284 route(xout, corner(SE, SIZE, xring).translate(0, -3), xring);
286 if (cell.xo_relevant(SW)) {
287 gg.line(corner(SW, SIZE, xring).translate(0, -3), corner(SW, SIZE, 0).translate(0, -3));
288 route(xout, corner(SW, SIZE, xring).translate(0, -3), xring);
292 if (cell.ylut_relevant()) {
296 if (cell.yi_to_ylut_relevant())
297 route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 6, 20)), 5);
301 if (xi != null && cell.xi_to_ylut_relevant())
302 route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 3, 20)), 4);
304 // lines directly from the ylut output to the four neighbors
305 gg.color(cell.yo() ? ORANGE : LIGHTBLUE);
306 P yout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+51 - 2, CORE_OFFSET + CORE_SIZE - 3));
308 yout = rotateInner(new P(51, 74));
309 gg.line(rotateInner(new P(51, 62)), yout);
310 } else if (cell.yo_relevant()) {
311 gg.line(rotateInner(YLUT_OUTPUT_POINT), yout);
313 if (cell.yo_relevant(NORTH)) route(yout, new P(SIZE-40, SIZE+ 0), 2);
314 if (cell.yo_relevant(EAST)) route(yout, new P(SIZE+ 0, 40), 2);
315 if (cell.yo_relevant(SOUTH)) route(yout, new P( 40, 0), 2);
316 if (cell.yo_relevant(WEST)) route(yout, new P( 0, SIZE-40), 2);
321 private AffineTransform rotateOuterTransform() {
323 AffineTransform a = new AffineTransform();
324 a.rotate((Math.PI/2) * rot);
327 case 1: a.translate(0, -SIZE); break;
328 case 2: a.translate(-SIZE, -SIZE); break;
329 case 3: a.translate(-SIZE, 0); break;
334 private P rotateOuter(P p) { return p.transform(rotateOuterTransform()); }
335 private P rotateInner(P p) { return p.transform(rotateInnerTransform()); }
336 private P unRotateInner(P p) { return p.inverseTransform(rotateInnerTransform()); }
338 private AffineTransform rotateInnerTransform() {
340 AffineTransform a = new AffineTransform();
341 a.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
342 a.rotate((Math.PI/2) * rot);
345 case 1: a.translate(0, -CORE_SIZE); break;
346 case 2: a.translate(-CORE_SIZE, -CORE_SIZE); break;
347 case 3: a.translate(-CORE_SIZE, 0); break;
349 a.translate(-1 * (SIZE-CORE_SIZE-CORE_OFFSET), -CORE_OFFSET);
354 private P project(P p1, int ring) {
355 double north = Math.abs( (SIZE-(ring*2)) - p1.getY() );
356 double south = Math.abs( ( (ring*2)) - p1.getY() );
357 double east = Math.abs( (SIZE-(ring*2)) - p1.getX() );
358 double west = Math.abs( ( (ring*2)) - p1.getX() );
359 if (north < south && north < east && north < west) return new P(p1.x, SIZE-ring*2);
360 else if (south < east && south < west) return new P(p1.x, ring*2);
361 else if (east < west) return new P(SIZE-ring*2, p1.y);
362 else return new P(ring*2, p1.y);
366 private void route(P p1, P p2, int ring) {
367 int ringpos = ring * 2;
368 P projected = project(p1, ring);
369 gg.line(p1, projected);
372 projected = project(p2, ring);
373 gg.line(p2, projected);
376 if (p1.x==p2.x || p1.y==p2.y) {
381 if ((p1.x==SIZE-ring*2 || p1.x==ring*2) && !(p1.y==SIZE-ring*2 || p1.y==ring*2)) {
382 P p3 = new P(p1.x, p2.y > SIZE/2 ? SIZE-ring*2 : ring*2);
385 } else if ((p1.y==SIZE-ring*2 || p1.y==ring*2) && !(p1.x==SIZE-ring*2 || p1.x==ring*2)) {
386 P p3 = new P(p2.x > SIZE/2 ? SIZE-ring*2 : ring*2, p1.y);
396 case SOUTH: rot = 0; break;
397 case NORTH: rot = 2; break;
398 case EAST: rot = 1; break;
399 case WEST: rot = 3; break;
401 // FIXME: choose based on xin
402 if (cell.north() != null && cell.north().yi()==SOUTH) { rot = 0; break; }
403 if (cell.south() != null && cell.south().yi()==NORTH) { rot = 2; break; }
404 if (cell.east() != null && cell.east().yi()==WEST) { rot = 3; break; }
405 if (cell.west() != null && cell.west().yi()==EAST) { rot = 1; break; }
411 public void drawGates() {
412 AffineTransform t = g.getTransform();
414 g.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
417 g.rotate((Math.PI/2) * rot);
420 case 1: g.translate(0, -CORE_SIZE); break;
421 case 2: g.translate(-CORE_SIZE, -CORE_SIZE); break;
422 case 3: g.translate(-CORE_SIZE, 0); break;
425 //gg.color(Color.gray);
426 //g.drawRect(0, 0, CORE_SIZE, CORE_SIZE);
431 if (xlut_relevant(cell) || scanme()) {
432 Gate gate = getGate(cell.xlut(), true);
434 !xknown ? Color.gray : xon ? Color.red : Color.white,
435 (xon && xknown) ? Color.white : Color.red,
436 xon ? Color.white : Color.red
440 g.translate(34f, 0f);
441 if (cell.ylut_relevant() || scanme()) {
442 Gate gate = getGate(cell.ylut(), false);
444 !yknown ? Color.gray : yon ? Color.blue : Color.white,
445 (yon && yknown) ? Color.white : Color.blue,
446 yon ? Color.white : Color.blue
454 public void drawMux() {
455 if (!cell.c_relevant()) return;
456 gg.color(Color.black);
457 if (xlut_relevant(cell) && (cell.c() == ZMUX || cell.c() == XLUT)) {
459 gg.line(XLUT_OUTPUT_POINT, new P(XLUT_OUTPUT_POINT.getX(), 52));
460 gg.line(new P(XLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
462 if (cell.ylut_relevant() && (cell.c() == ZMUX || cell.c() == YLUT)) {
464 gg.line(YLUT_OUTPUT_POINT, new P(YLUT_OUTPUT_POINT.getX(), 52));
465 gg.line(new P(YLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
467 if (cell.c() == ZMUX)
470 gg.line(51, 52, 51, 51+23);
472 if (cell.register_relevant() && cell.f() && !cell.b()) {
473 gg.line(51, 56, 60, 56);
474 gg.line(60, 56, 60, 51+25);
476 gg.line(51, 51+23, 51, 51+25);
479 if (cell.c() == ZMUX) {
482 int plane = cell.zi()+1;
483 route(unRotateInner(new P(8 + 2*(plane-L0), 76 + 2*(plane-L0))),
486 gg.line(new P(51, 20), new P(51, 50));
488 GeneralPath p = new GeneralPath();
502 public int ccolor() {
504 case XLUT: return LIGHTRED;
505 case YLUT: return LIGHTBLUE;
506 case ZMUX: return ORANGE;
510 public void drawRegister() {
511 if (!cell.register_relevant()) return;
514 gg.color(Color.white);
515 g.fillRect(47, 58, 9, 14);
517 g.drawRect(47, 58, 9, 14);
519 GeneralPath p = new GeneralPath();
524 gg.color(cell.ff_reset_value() ? WHITE : dark);
529 public void drawBorder() {
530 gg.color(Color.gray);
531 gg.line(BEVEL, 0, SIZE-BEVEL, 0);
532 gg.line(SIZE, BEVEL, SIZE, SIZE-BEVEL);
533 gg.line(SIZE-BEVEL, SIZE, BEVEL, SIZE);
534 gg.line(0, SIZE-BEVEL, 0, BEVEL);
537 gg.line(0, BEVEL, BEVEL, 0);
538 gg.line(SIZE-BEVEL, 0, SIZE, BEVEL);
539 gg.line(SIZE, SIZE-BEVEL, SIZE-BEVEL, SIZE);
540 gg.line(BEVEL, SIZE, 0, SIZE-BEVEL);
545 public void pressed() {
548 public void released() {
549 if (dragFrom == null || oldcell == null) return;
550 if (Math.abs(dragFrom._y - oldcell._y) > 1) return;
551 if (Math.abs(dragFrom._x - oldcell._x) > 1) return;
552 if (dragFrom._x == oldcell._x && dragFrom._y == oldcell._y+1) oldcell.cell.yi(NORTH);
553 if (dragFrom._x == oldcell._x && dragFrom._y == oldcell._y-1) oldcell.cell.yi(SOUTH);
554 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y) oldcell.cell.yi(EAST);
555 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y) oldcell.cell.yi(WEST);
556 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NE);
557 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SE);
558 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NW);
559 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SW);
562 public void drawKeyboard(Image keyboardImage, Graphics2D g) {
565 int height = (keyboardImage.getHeight(null) * width) / keyboardImage.getWidth(null);
566 g.drawImage(keyboardImage,
567 0, getHeight() - height,
570 keyboardImage.getWidth(null), keyboardImage.getHeight(null),
575 public void _paint(Graphics2D g) {
579 g.setStroke(new BasicStroke((float)0.5));
581 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
582 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
584 AffineTransform t = g.getTransform();
585 for(Cell c : cells) {
587 g.translate( c._x * SIZE /*+ (10 * (c._x/4))*/, c._y * SIZE /*+ (10 * (c._y/4))*/);
590 for(Cell c : cells) {
592 g.translate( c._x * SIZE /*+ (10 * (c._x/4))*/, c._y * SIZE /*+ (10 * (c._y/4))*/);
597 g.setTransform(new AffineTransform());
599 gg.color(selectedcell);
600 g.fillRect(getWidth() - 200, 0, 200, 600);
601 gg.color(Color.white);
602 g.drawRect(getWidth() - 200, 0, 200, 600);
604 Cell newcell = whichCell(mousex, mousey);
606 g.setFont(new Font("monospaced", 0, 14));
607 if (newcell != null && newcell.cell != null) {
608 g.drawString("selected: " + newcell._x + "," + newcell._y, getWidth() - 200 + 10, (line += 15));
609 g.drawString(" xlut: " + XLUT_EQUATIONS[newcell.cell.xlut() & 0xff], getWidth() - 200 + 10, (line += 15));
610 g.drawString(" ylut: " + YLUT_EQUATIONS[newcell.cell.ylut() & 0xff], getWidth() - 200 + 10, (line += 15));
612 switch(newcell.cell.xi()) {
613 case NW : xi = "NW"; break;
614 case NE : xi = "NE"; break;
615 case SW : xi = "SW"; break;
616 case SE : xi = "SE"; break;
617 case NONE : xi = "."; break;
618 default: xi = "L"+(newcell.cell.xi()-L0); break;
620 g.drawString("x-in mux: " + xi, getWidth() - 200 + 10, (line += 15));
623 switch(newcell.cell.yi()) {
624 case NORTH : yi = "NORTH"; break;
625 case SOUTH : yi = "SOUTH"; break;
626 case EAST : yi = "EAST"; break;
627 case WEST : yi = "WEST"; break;
628 case NONE : yi = "."; break;
629 default: yi = "L"+(newcell.cell.yi()-L0); break;
631 g.drawString("y-in mux: " + yi, getWidth() - 200 + 10, (line += 15));
633 g.drawString("w-in mux: " + (newcell.cell.wi()==NONE ? "." : ("L"+(newcell.cell.wi()-L0))),
634 getWidth() - 200 + 10, (line += 15));
635 g.drawString("z-in mux: " + (newcell.cell.zi()==NONE ? "." : ("L"+(newcell.cell.zi()-L0))),
636 getWidth() - 200 + 10, (line += 15));
639 switch(newcell.cell.t()) {
640 case TMUX_FB: tm = "fb"; break;
641 case TMUX_W_AND_FB: tm = "w&fb"; break;
642 case TMUX_Z: tm = "z"; break;
643 case TMUX_W_AND_Z: tm = "w&z"; break;
644 case TMUX_W: tm = "w"; break;
646 g.drawString("t-in mux: " + tm, getWidth() - 200 + 10, (line += 15));
648 g.drawString(" set/rst: " + (newcell.cell.ff_reset_value() ? "reset=SET" : "."),
649 getWidth() - 200 + 10, (line += 15));
652 for(int i=0; i<5; i++) outs += (newcell.cell.out(L0+i) ? (i+" ") : ". ");
653 g.drawString(" out: " + outs,
654 getWidth() - 200 + 10, (line += 15));
656 for(int i=0; i<5; i++) hs += (newcell.cell.hx(L0+i) ? (i+" ") : ". ");
657 g.drawString(" h conn: " + hs,
658 getWidth() - 200 + 10, (line += 15));
660 for(int i=0; i<5; i++) vs += (newcell.cell.vx(L0+i) ? (i+" ") : ". ");
661 g.drawString(" v conn: " + vs,
662 getWidth() - 200 + 10, (line += 15));
663 g.drawString("out enab: " + (newcell.cell.oe()==H4 ? "H4" : newcell.cell.oe()==V4 ? "V4" : "."),
664 getWidth() - 200 + 10, (line += 15));
665 g.drawString(" c-mux: " + (newcell.cell.c()==ZMUX ? "zmux" : newcell.cell.c()==XLUT ? "x-lut" : "y-lut"),
666 getWidth() - 200 + 10, (line += 15));
667 g.drawString(" fb src: " + (newcell.cell.f() ? "clocked" : "."),
668 getWidth() - 200 + 10, (line += 15));
669 g.drawString(" bypass: " + (newcell.cell.b() ? "clocked" : "."),
670 getWidth() - 200 + 10, (line += 15));
671 g.drawString(" x out: " + (newcell.cell.xo() ? (newcell.cell.b() ? "register" : "center") : "."),
672 getWidth() - 200 + 10, (line += 15));
673 g.drawString(" y out: " + (newcell.cell.yo() ? (newcell.cell.b() ? "register" : "center") : "."),
674 getWidth() - 200 + 10, (line += 15));
678 drawKeyboard(keyboard2, g);
679 } else switch(lastChar) {
687 drawKeyboard(keyboard3, g);
690 drawKeyboard(keyboard1, g);
694 while (mousebutton) {
696 if (dragFrom == null || oldcell == null) break;
697 if (Math.abs(dragFrom._y - oldcell._y) > 1) break;
698 if (Math.abs(dragFrom._x - oldcell._x) > 1) break;
700 if (dragFrom._x == oldcell._x || dragFrom._y == oldcell._y)
705 gg.line( oldcell._x * SIZE + SIZE/2,
706 oldcell._y * SIZE + SIZE/2,
707 dragFrom._x * SIZE + SIZE/2,
708 dragFrom._y * SIZE + SIZE/2, 5);
715 Cell dragFrom = null;
717 public void clear() {
718 Graphics2D g = (Graphics2D)getGraphics();
719 //gg.color(Color.black);
720 //gg.color(Color.lightGray);
721 g.clearRect(0, 0, getWidth(), getHeight());
724 public static final P translate(P p, int dx, int dy) {
725 return new P(p.getX()+dx, p.getY()+dy);
728 public Cell whichCell(int x, int y) {
731 p = p.inverseTransform(transform);
732 } catch (Exception e) {
735 int col = ((int)p.getX()+0) / SIZE;
736 int row = ((int)p.getY()+0) / SIZE;
738 if (c._x == col && c._y == row)
743 public static boolean xlut_relevant(Fpslic.Cell c) {
744 return c.xlut_relevant();
747 public void mouseClicked(MouseEvent e) {
748 final Cell c = whichCell(e.getX(), e.getY());
754 for(int x=0; x<at40k.getWidth(); x++)
755 for(int y=0; y<at40k.getHeight(); y++)
756 if (ca[x][y] != null)
757 if (ca[x][y].scanme())
760 public void scan(final Gui.Cell c) {
762 final Fpslic.Cell cell = c.cell;
763 scan(at40k, cell, NONE, true);
764 boolean safe = !cell.fb_relevant();
765 if (cell.xo()) safe = false;
766 if (cell.yo()) safe = false;
767 for(int i=0; i<5; i++)
772 if (cell.xlut_relevant()) {
774 drone.readBus(new BCB(c, XLUT));
778 if (cell.ylut_relevant()) {
780 drone.readBus(new BCB(c, YLUT));
788 if (!cell.xlut_relevant()) {
791 drone.readBus(new BCB(c, XLUT));
795 for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
796 if (c2!=null && !c2.relevant()) {
797 scan(at40k, cell, NONE, false);
800 scan(at40k, c2, NONE, true);
801 drone.readBus(new BCB(c, YLUT));
802 scan(at40k, c2, NONE, false);
810 if (!cell.ylut_relevant()) {
813 drone.readBus(new BCB(c, YLUT));
817 for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
818 if (c2!=null && !c2.relevant()) {
819 scan(at40k, cell, NONE, false);
821 scan(at40k, c2, NONE, true);
823 drone.readBus(new BCB(c, XLUT));
824 scan(at40k, c2, NONE, false);
832 scan(at40k, cell, NONE, false);
836 for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
837 if (c2!=null && !c2.relevant()) {
838 scan(at40k, cell, NONE, false);
840 scan(at40k, c2, NONE, true);
842 drone.readBus(new BCB(c, XLUT));
843 scan(at40k, c2, NONE, false);
848 for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
849 if (c2!=null && !c2.relevant()) {
852 scan(at40k, c2, NONE, true);
853 drone.readBus(new BCB(c, YLUT));
854 scan(at40k, c2, NONE, false);
863 scan(at40k, cell, NONE, false);
864 } catch (IOException e) {
865 throw new RuntimeException(e);
869 public static void scan(Fpslic dev, Fpslic.Cell cell, int source, boolean setup) {
871 if (source != NONE) cell.c(source);
872 if (cell.b()) cell.b(false);
873 if (cell.f()) cell.f(false);
875 if (cell.out(L3)!=setup) cell.out(L3, setup);
876 if (cell.vx(L3)!=setup) cell.v(L3, setup);
878 Fpslic.SectorWire sw = cell.vwire(L3);
879 //System.out.println("wire is: " + sw);
881 if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
882 sw.north().drives(sw, false);
883 while(sw.row > (12 & ~0x3) && sw.south() != null) {
884 //System.out.println(sw + " -> " + sw.south());
885 if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
888 if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
889 sw.north().drives(sw, false);
890 while(sw.row < (12 & ~0x3) && sw.north() != null) {
891 //System.out.println(sw + " -> " + sw.north());
892 if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
896 //cell = dev.cell(19, 15);
897 cell = dev.cell(cell.col, 15);
899 System.out.println("cell is " + cell);
908 if (cell.hx(L3) != setup) cell.h(L3, setup);
909 if (cell.vx(L3) != setup) cell.v(L3, setup);
912 if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
913 while(sw.east() != null) {
914 //System.out.println(sw + " -> " + sw.east());
915 if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
923 private class BCB extends FtdiBoard.ByteCallback {
926 public BCB(Gui.Cell c, int who) {
927 this.who = who; this.c = c;
930 public void call(byte b) throws Exception {
931 boolean on = (b & 0x80) != 0;
948 public Gate getGate(byte lut, boolean xlut) {
949 for(Gate g : knownGates)
950 if (g.setLut(lut, xlut))
955 public Gate unknownGate = new Circle("?");
956 public Gate[] knownGates =
960 new Circle("0") { public boolean result(boolean x, boolean y, boolean z) { return false; } },
961 new Circle("1") { public boolean result(boolean x, boolean y, boolean z) { return true; } },
962 new Circle("x") { public boolean result(boolean x, boolean y, boolean z) { return x; } },
963 new Circle("y") { public boolean result(boolean x, boolean y, boolean z) { return y; } },
964 new Circle("z") { public boolean result(boolean x, boolean y, boolean z) { return z; } },
965 new Circle("~x") { public boolean result(boolean x, boolean y, boolean z) { return !x; } },
966 new Circle("~y") { public boolean result(boolean x, boolean y, boolean z) { return !y; } },
967 new Circle("~z") { public boolean result(boolean x, boolean y, boolean z) { return !z; } }
970 // FIXME: 2-input gates?
971 public abstract class Gate {
972 public boolean invert_x;
973 public boolean invert_y;
974 public boolean invert_z;
975 public boolean invert_out;
976 public abstract boolean result(boolean x, boolean y, boolean z);
977 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
978 GeneralPath p = new GeneralPath();
985 AffineTransform a = g.getTransform();
987 if (label() != null) {
989 g.drawString(label(), 7, -14);
993 public String label() { return null; }
994 public boolean setLut(int lut, boolean xlut) {
996 for(int inverts = 0; inverts < 16; inverts++) {
997 invert_x = (inverts & 0x1) != 0;
998 invert_y = (inverts & 0x2) != 0;
999 invert_z = (inverts & 0x4) != 0;
1000 invert_out = (inverts & 0x8) != 0;
1002 boolean good = true;
1003 for(int bit=0; bit<8; bit++) {
1004 boolean x = xlut ? ((bit & 0x1) != 0) : ((bit & 0x2) != 0);
1005 boolean y = xlut ? ((bit & 0x2) != 0) : ((bit & 0x1) != 0);
1006 boolean z = (bit & 0x4) != 0;
1007 boolean expect = (lut & (1<<bit)) != 0;
1009 // FIXME symmetry issues here....
1010 boolean result = result(x ^ invert_x, y ^ invert_y, z ^ invert_z) ^ invert_out;
1011 if (result != expect) { good = false; break; }
1013 if (good) return true;
1019 public abstract void makePath(GeneralPath gp);
1022 public class Or extends Gate {
1023 public boolean result(boolean x, boolean y, boolean z) { return x || y || z; }
1024 public String label() { return "+"; }
1025 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1026 AffineTransform at = g.getTransform();
1028 g.translate(0, -40);
1029 super.draw(g, fill, stroke, text);
1032 public void makePath(GeneralPath gp) {
1033 gp.moveTo(29.141f, 36.301f);
1034 gp.lineTo(29.141f, 36.301f-7.161f);
1035 gp.curveTo(27.71f, 11.24f, 23.413f, 9.45f, 14.82f, 0.5f);
1036 gp.curveTo(6.229f, 9.45f, 1.932f, 11.24f, 0.5f, 29.141f);
1037 gp.lineTo(0.5f, 29.141f+7.161f);
1039 float y = 29.141f+7.161f;
1040 gp.curveTo(5.729f+x, -1.789f+y,
1041 6.444f+x, -2.686f+y,
1042 14.32f+x, -3.58f+y);
1043 gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1047 public class Circle extends Gate {
1049 public Circle(String label) { this.label = label; }
1050 public boolean result(boolean x, boolean y, boolean z) { return false; }
1051 public String label() { return label; }
1052 public void makePath(GeneralPath gp) {
1063 public class And extends Gate {
1064 public boolean result(boolean x, boolean y, boolean z) { return x && y && z; }
1065 public String label() { return "&"; }
1066 public void makePath(GeneralPath gp) {
1069 gp.curveTo(0, 27, 3, 35, 13, 35);
1070 gp.curveTo(20, 35, 23, 27, 23, 19);
1076 public class Muller extends And {
1077 public String label() { return "C"; }
1078 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1079 super.draw(g, fill, stroke, text);
1081 g.drawLine(0, 0, 23, 0);
1085 public class Xor extends Or {
1086 public boolean result(boolean x, boolean y, boolean z) { return x ^ y ^ z; }
1087 public String label() { return "^"; }
1088 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1089 super.draw(g, fill, stroke, text);
1091 AffineTransform at = g.getTransform();
1093 g.translate(0, -40);
1096 GeneralPath gp = new GeneralPath();
1098 float y = 29.141f+7.161f;
1100 gp.curveTo(5.729f+x, -1.789f+y,
1101 6.444f+x, -2.686f+y,
1102 14.32f+x, -3.58f+y);
1103 gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1110 public abstract class Mux extends Gate {
1111 public String label() { return "?"; }
1112 public void makePath(GeneralPath gp) {
1121 public class X_Mux extends Mux {
1122 public boolean result(boolean x, boolean y, boolean z) { return x ? y : z; }
1124 public class Y_Mux extends Mux {
1125 public boolean result(boolean x, boolean y, boolean z) { return y ? x : z; }
1127 public class Z_Mux extends Mux {
1128 public boolean result(boolean x, boolean y, boolean z) { return z ? x : y; }
1131 public abstract class Buf extends Gate {
1132 public void makePath(GeneralPath gp) {
1140 public class X extends Buf {
1141 public boolean result(boolean x, boolean y, boolean z) { return x; }
1143 public class Y extends Buf {
1144 public boolean result(boolean x, boolean y, boolean z) { return y; }
1146 public class Z extends Buf {
1147 public boolean result(boolean x, boolean y, boolean z) { return z; }
1150 private static Image keyboard1 = Toolkit.getDefaultToolkit().createImage("keyboard1.png");
1151 private static Image keyboard2 = Toolkit.getDefaultToolkit().createImage("keyboard2.png");
1152 private static Image keyboard3 = Toolkit.getDefaultToolkit().createImage("keyboard3.png");