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 edu.berkeley.slipway.*;
8 import java.awt.geom.*;
9 import java.awt.event.*;
10 import java.awt.color.*;
11 import org.ibex.util.*;
15 import static edu.berkeley.slipway.gui.GuiConstants.*;
17 public class Gui extends ZoomingPanel implements KeyListener, MouseMotionListener {
25 private Cell[][] ca = new Cell[128][];
27 //public static final Color nonselectedcell = new Color(0x44, 0x44, 0x44);
28 public static final Color nonselectedcell = new Color(0xee, 0xee, 0xee);
29 public static final Color selectedcell = new Color(0x00, 0x00, 0x00);
31 public void writeMode4() {
33 final JFileChooser fc = new JFileChooser();
34 int returnVal = fc.showSaveDialog(this);
35 Writer pw = new OutputStreamWriter(new FileOutputStream(fc.getSelectedFile()));
36 slipway.getFpslicDevice().writeMode4(pw);
39 System.err.println("done writing");
40 } catch (Exception e) {
45 public void readMode4() {
47 final JFileChooser fc = new JFileChooser();
48 int returnVal = fc.showOpenDialog(this);
49 slipway.getFpslicDevice().readMode4(new FileReader(fc.getSelectedFile()));
50 System.err.println("done reading");
52 } catch (Exception e) {
57 public Gui(FpslicDevice fpslic, SlipwayBoard slipway) {
58 this(fpslic, slipway, 24, 24);
60 public Gui(FpslicDevice fpslic, SlipwayBoard slipway, int width, int height) {
63 this.slipway = slipway;
64 for(int i=0; i<ca.length; i++)
65 ca[i] = new Cell[128];
66 for(int x=0; x<width; x++)
67 for(int y=0; y<height; y++)
68 new Cell(x,y, fpslic.cell(x, y));
83 FpslicDevice.Cell c = fpslic.cell(0,0);
84 for(int i=0; i<256; i++) {
86 System.out.println(c.printYLut());
92 FpslicDevice.Cell cell;
94 public boolean scanme = false;
95 public boolean xon = false;
96 public boolean yon = false;
97 public boolean xknown = false;
98 public boolean yknown = false;
100 public Cell(int x, int y, FpslicDevice.Cell cell) {
107 public boolean scanme() {
108 return cell.relevant();
110 public void clear() {
112 //gg.color(in ? selectedcell : (scanme() ? new Color(0xbb, 0xbb, 0xbb) : nonselectedcell));
113 g.fillRect(0, 0, SIZE, SIZE);
117 if (cell.relevant() || scanme()) {
121 AffineTransform t = g.getTransform();
124 g.transform(rotateInnerTransform());
127 drawInternalRouting();
134 public void drawBuffer() {
136 if (!cell.out_relevant()) return;
138 GeneralPath p = new GeneralPath();
143 gg.color(cell.oe() == NONE ? Color.white : Color.green);
145 gg.color(Color.green);
148 gg.color(Color.magenta);
149 if (cell.oe() == V4) {
150 gg.line(24, 62, 16, 63);
151 } else if (cell.oe() == H4) {
152 gg.line(24, 67, 24, 76);
156 public void drawWires() {
158 for(int i=0; i<5; i++)
160 if (cell.hwire(i).isDriven()) {
161 gg.color(cell.out(i) ? ORANGE : MAGENTA);
162 gg.line(0, SIZE-(2*(1+RINGS)+2*i), SIZE, SIZE-(2*(1+RINGS)+2*i));
164 for(int i=0; i<5; i++)
166 if (cell.vwire(i).isDriven()) {
167 gg.color(cell.out(i) ? ORANGE : MAGENTA);
168 gg.line(2*(1+RINGS)+2*i, 0, 2*(1+RINGS)+2*i, SIZE);
170 if (cell.zi_to_xlut_relevant()) wire(cell.zi(), true);
171 if (cell.zi_to_ylut_relevant()) wire(cell.zi(), false);
172 if (cell.zi_to_xlut_relevant()) wire(cell.wi(), true);
173 if (cell.zi_to_ylut_relevant()) wire(cell.wi(), false);
176 public void wire(int plane, boolean xlut) {
177 if (!(plane >= L0 && plane <= L4 && cell.vx(plane))) return;
178 if (xlut ? xlut_relevant(cell) : cell.ylut_relevant())
179 route(new P(17 - 2*(plane-L0), 8),
180 rotateInner(xlut ? new P(38, 18) : new P(64, 18)),
184 public P corner(int corner, int size, int ring) {
186 case NW: return new P(0 +2*ring, size -2*ring);
187 case SW: return new P(0 +2*ring, 0 +2*ring);
188 case NE: return new P(size -2*ring, size -2*ring);
189 case SE: return new P(size -2*ring, 0 +2*ring);
190 default: return null;
194 public void drawInternalRouting() {
196 if (cell.fb_relevant()) {
198 gg.line(51, 74, 37, 74);
199 gg.line(37, 74, 51, 12);
200 } else if (cell.c() == XLUT) {
202 gg.line(33, 52, 51, 52);
203 gg.line(51, 52, 51, 12);
204 } else if (cell.c() == YLUT) {
206 gg.line(67, 52, 51, 52);
207 gg.line(51, 52, 51, 12);
209 gg.line(51, 56, 41, 56);
210 gg.line(41, 56, 51, 12);
212 if (xlut_relevant(cell)) {
213 gg.line(52, 12, XLUT_OUTPUT_POINT.getX(), 12);
214 gg.line(XLUT_OUTPUT_POINT.getX(), 12, XLUT_OUTPUT_POINT.getX(), 32);
216 if (cell.ylut_relevant()) {
217 gg.line(52, 12, YLUT_OUTPUT_POINT.getX(), 12);
218 gg.line(YLUT_OUTPUT_POINT.getX(), 12, YLUT_OUTPUT_POINT.getX(), 32);
223 public void drawLocal() {
224 if (!cell.ylut_relevant() && !xlut_relevant(cell)) return;
226 P in = rotateOuter(new P(HOFF, 0));
227 P join = rotateInner(new P(HOFF, CORE_OFFSET));
231 join = new P(in.getX(), join.getY());
234 join = new P(join.getX(), in.getY());
240 P xi = corner(cell.xi(), SIZE, 4);
241 if (((cell.xlut_relevant() && cell.xi_to_xlut_relevant()) ||
242 (cell.ylut_relevant() && cell.xi_to_ylut_relevant()))
243 && (cell.xi() != NONE) && (cell.xi() < L0 || cell.xi() > L4)
245 P xi2 = corner(cell.xi(), SIZE + 2*BEVEL, -1).translate(-BEVEL, -BEVEL);
248 xi = translate(xi, 0, -3);
249 xi2 = translate(xi2, 0, -3);
252 xi = translate(xi, 0, 3);
253 xi2 = translate(xi2, 0, 3);
259 if (xlut_relevant(cell)) {
261 if (cell.xi_to_xlut_relevant() && xi != null)
262 route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 3, 20)), 4);
266 if (cell.yi_to_xlut_relevant())
267 route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 6, 20)), 5);
271 gg.color(cell.xo() ? ORANGE : LIGHTRED);
272 P xout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+17 - 2, CORE_OFFSET + CORE_SIZE - 3));
274 xout = rotateInner(new P(51, 74));
275 gg.line(rotateInner(new P(51, 62)), xout);
276 } else if (cell.xo_relevant()) {
277 gg.line(rotateInner(XLUT_OUTPUT_POINT), xout);
279 if (cell.xo_relevant(NE)) {
280 gg.line(corner(NE, SIZE, xring).translate(0, 3), corner(NE, SIZE, 0).translate(0, 3));
281 route(xout, corner(NE, SIZE, xring).translate(0, 3), xring);
283 if (cell.xo_relevant(NW)) {
284 gg.line(corner(NW, SIZE, xring).translate(0, 3), corner(NW, SIZE, 0).translate(0, 3));
285 route(xout, corner(NW, SIZE, xring).translate(0, 3), xring);
287 if (cell.xo_relevant(SE)) {
288 gg.line(corner(SE, SIZE, xring).translate(0, -3), corner(SE, SIZE, 0).translate(0, -3));
289 route(xout, corner(SE, SIZE, xring).translate(0, -3), xring);
291 if (cell.xo_relevant(SW)) {
292 gg.line(corner(SW, SIZE, xring).translate(0, -3), corner(SW, SIZE, 0).translate(0, -3));
293 route(xout, corner(SW, SIZE, xring).translate(0, -3), xring);
297 if (cell.ylut_relevant()) {
301 if (cell.yi_to_ylut_relevant())
302 route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 6, 20)), 5);
306 if (xi != null && cell.xi_to_ylut_relevant())
307 route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 3, 20)), 4);
309 // lines directly from the ylut output to the four neighbors
310 gg.color(cell.yo() ? ORANGE : LIGHTBLUE);
311 P yout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+51 - 2, CORE_OFFSET + CORE_SIZE - 3));
313 yout = rotateInner(new P(51, 74));
314 gg.line(rotateInner(new P(51, 62)), yout);
315 } else if (cell.yo_relevant()) {
316 gg.line(rotateInner(YLUT_OUTPUT_POINT), yout);
318 if (cell.yo_relevant(NORTH)) route(yout, new P(SIZE-40, SIZE+ 0), 2);
319 if (cell.yo_relevant(EAST)) route(yout, new P(SIZE+ 0, 40), 2);
320 if (cell.yo_relevant(SOUTH)) route(yout, new P( 40, 0), 2);
321 if (cell.yo_relevant(WEST)) route(yout, new P( 0, SIZE-40), 2);
326 private AffineTransform rotateOuterTransform() {
328 AffineTransform a = new AffineTransform();
329 a.rotate((Math.PI/2) * rot);
332 case 1: a.translate(0, -SIZE); break;
333 case 2: a.translate(-SIZE, -SIZE); break;
334 case 3: a.translate(-SIZE, 0); break;
339 private P rotateOuter(P p) { return p.transform(rotateOuterTransform()); }
340 private P rotateInner(P p) { return p.transform(rotateInnerTransform()); }
341 private P unRotateInner(P p) { return p.inverseTransform(rotateInnerTransform()); }
343 private AffineTransform rotateInnerTransform() {
345 AffineTransform a = new AffineTransform();
346 a.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
347 a.rotate((Math.PI/2) * rot);
350 case 1: a.translate(0, -CORE_SIZE); break;
351 case 2: a.translate(-CORE_SIZE, -CORE_SIZE); break;
352 case 3: a.translate(-CORE_SIZE, 0); break;
354 a.translate(-1 * (SIZE-CORE_SIZE-CORE_OFFSET), -CORE_OFFSET);
359 private P project(P p1, int ring) {
360 double north = Math.abs( (SIZE-(ring*2)) - p1.getY() );
361 double south = Math.abs( ( (ring*2)) - p1.getY() );
362 double east = Math.abs( (SIZE-(ring*2)) - p1.getX() );
363 double west = Math.abs( ( (ring*2)) - p1.getX() );
364 if (north < south && north < east && north < west) return new P(p1.x, SIZE-ring*2);
365 else if (south < east && south < west) return new P(p1.x, ring*2);
366 else if (east < west) return new P(SIZE-ring*2, p1.y);
367 else return new P(ring*2, p1.y);
371 private void route(P p1, P p2, int ring) {
372 int ringpos = ring * 2;
373 P projected = project(p1, ring);
374 gg.line(p1, projected);
377 projected = project(p2, ring);
378 gg.line(p2, projected);
381 if (p1.x==p2.x || p1.y==p2.y) {
386 if ((p1.x==SIZE-ring*2 || p1.x==ring*2) && !(p1.y==SIZE-ring*2 || p1.y==ring*2)) {
387 P p3 = new P(p1.x, p2.y > SIZE/2 ? SIZE-ring*2 : ring*2);
390 } else if ((p1.y==SIZE-ring*2 || p1.y==ring*2) && !(p1.x==SIZE-ring*2 || p1.x==ring*2)) {
391 P p3 = new P(p2.x > SIZE/2 ? SIZE-ring*2 : ring*2, p1.y);
401 case SOUTH: rot = 0; break;
402 case NORTH: rot = 2; break;
403 case EAST: rot = 1; break;
404 case WEST: rot = 3; break;
406 // FIXME: choose based on xin
407 if (cell.north() != null && cell.north().yi()==SOUTH) { rot = 0; break; }
408 if (cell.south() != null && cell.south().yi()==NORTH) { rot = 2; break; }
409 if (cell.east() != null && cell.east().yi()==WEST) { rot = 3; break; }
410 if (cell.west() != null && cell.west().yi()==EAST) { rot = 1; break; }
416 public void drawGates() {
417 AffineTransform t = g.getTransform();
419 g.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
422 g.rotate((Math.PI/2) * rot);
425 case 1: g.translate(0, -CORE_SIZE); break;
426 case 2: g.translate(-CORE_SIZE, -CORE_SIZE); break;
427 case 3: g.translate(-CORE_SIZE, 0); break;
430 //gg.color(Color.gray);
431 //g.drawRect(0, 0, CORE_SIZE, CORE_SIZE);
436 if (xlut_relevant(cell) || scanme()) {
437 Gate gate = getGate(cell.xlut(), true);
439 !xknown ? Color.gray : xon ? Color.red : Color.white,
440 (xon && xknown) ? Color.white : Color.red,
441 xon ? Color.white : Color.red
445 g.translate(34f, 0f);
446 if (cell.ylut_relevant() || scanme()) {
447 Gate gate = getGate(cell.ylut(), false);
449 !yknown ? Color.gray : yon ? Color.blue : Color.white,
450 (yon && yknown) ? Color.white : Color.blue,
451 yon ? Color.white : Color.blue
459 public void drawMux() {
460 if (!cell.c_relevant()) return;
461 gg.color(Color.black);
462 if (xlut_relevant(cell) && (cell.c() == ZMUX || cell.c() == XLUT)) {
464 gg.line(XLUT_OUTPUT_POINT, new P(XLUT_OUTPUT_POINT.getX(), 52));
465 gg.line(new P(XLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
467 if (cell.ylut_relevant() && (cell.c() == ZMUX || cell.c() == YLUT)) {
469 gg.line(YLUT_OUTPUT_POINT, new P(YLUT_OUTPUT_POINT.getX(), 52));
470 gg.line(new P(YLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
472 if (cell.c() == ZMUX)
475 gg.line(51, 52, 51, 51+23);
477 if (cell.register_relevant() && cell.f() && !cell.b()) {
478 gg.line(51, 56, 60, 56);
479 gg.line(60, 56, 60, 51+25);
481 gg.line(51, 51+23, 51, 51+25);
484 if (cell.c() == ZMUX) {
487 int plane = cell.zi()+1;
488 route(unRotateInner(new P(8 + 2*(plane-L0), 76 + 2*(plane-L0))),
491 gg.line(new P(51, 20), new P(51, 50));
493 GeneralPath p = new GeneralPath();
507 public int ccolor() {
509 case XLUT: return LIGHTRED;
510 case YLUT: return LIGHTBLUE;
511 case ZMUX: return ORANGE;
515 public void drawRegister() {
516 if (!cell.register_relevant()) return;
519 gg.color(Color.white);
520 g.fillRect(47, 58, 9, 14);
522 g.drawRect(47, 58, 9, 14);
524 GeneralPath p = new GeneralPath();
529 gg.color(cell.ff_reset_value() ? WHITE : dark);
534 public void drawBorder() {
535 gg.color(Color.gray);
536 gg.line(BEVEL, 0, SIZE-BEVEL, 0);
537 gg.line(SIZE, BEVEL, SIZE, SIZE-BEVEL);
538 gg.line(SIZE-BEVEL, SIZE, BEVEL, SIZE);
539 gg.line(0, SIZE-BEVEL, 0, BEVEL);
542 gg.line(0, BEVEL, BEVEL, 0);
543 gg.line(SIZE-BEVEL, 0, SIZE, BEVEL);
544 gg.line(SIZE, SIZE-BEVEL, SIZE-BEVEL, SIZE);
545 gg.line(BEVEL, SIZE, 0, SIZE-BEVEL);
550 public void pressed() {
553 public void released() {
554 if (dragFrom == null || oldcell == null) return;
555 if (Math.abs(dragFrom._y - oldcell._y) > 1) return;
556 if (Math.abs(dragFrom._x - oldcell._x) > 1) return;
557 if (dragFrom._x == oldcell._x && dragFrom._y == oldcell._y+1) oldcell.cell.yi(NORTH);
558 if (dragFrom._x == oldcell._x && dragFrom._y == oldcell._y-1) oldcell.cell.yi(SOUTH);
559 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y) oldcell.cell.yi(EAST);
560 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y) oldcell.cell.yi(WEST);
561 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NE);
562 if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SE);
563 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NW);
564 if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SW);
567 public void drawKeyboard(Image keyboardImage, Graphics2D g) {
570 int height = (keyboardImage.getHeight(null) * width) / keyboardImage.getWidth(null);
571 g.drawImage(keyboardImage,
572 0, getHeight() - height,
575 keyboardImage.getWidth(null), keyboardImage.getHeight(null),
580 public void _paint(Graphics2D g) {
584 g.setStroke(new BasicStroke((float)0.5));
586 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
587 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
589 AffineTransform t = g.getTransform();
590 for(Cell c : cells) {
592 g.translate( c._x * SIZE /*+ (10 * (c._x/4))*/, c._y * SIZE /*+ (10 * (c._y/4))*/);
595 for(Cell c : cells) {
597 g.translate( c._x * SIZE /*+ (10 * (c._x/4))*/, c._y * SIZE /*+ (10 * (c._y/4))*/);
602 g.setTransform(new AffineTransform());
604 gg.color(selectedcell);
605 g.fillRect(getWidth() - 200, 0, 200, 600);
606 gg.color(Color.white);
607 g.drawRect(getWidth() - 200, 0, 200, 600);
609 Cell newcell = whichCell(mousex, mousey);
611 g.setFont(new Font("monospaced", 0, 14));
612 if (newcell != null && newcell.cell != null) {
613 g.drawString("selected: " + newcell._x + "," + newcell._y, getWidth() - 200 + 10, (line += 15));
614 g.drawString(" xlut: " + XLUT_EQUATIONS[newcell.cell.xlut() & 0xff], getWidth() - 200 + 10, (line += 15));
615 g.drawString(" ylut: " + YLUT_EQUATIONS[newcell.cell.ylut() & 0xff], getWidth() - 200 + 10, (line += 15));
617 switch(newcell.cell.xi()) {
618 case NW : xi = "NW"; break;
619 case NE : xi = "NE"; break;
620 case SW : xi = "SW"; break;
621 case SE : xi = "SE"; break;
622 case NONE : xi = "."; break;
623 default: xi = "L"+(newcell.cell.xi()-L0); break;
625 g.drawString("x-in mux: " + xi, getWidth() - 200 + 10, (line += 15));
628 switch(newcell.cell.yi()) {
629 case NORTH : yi = "NORTH"; break;
630 case SOUTH : yi = "SOUTH"; break;
631 case EAST : yi = "EAST"; break;
632 case WEST : yi = "WEST"; break;
633 case NONE : yi = "."; break;
634 default: yi = "L"+(newcell.cell.yi()-L0); break;
636 g.drawString("y-in mux: " + yi, getWidth() - 200 + 10, (line += 15));
638 g.drawString("w-in mux: " + (newcell.cell.wi()==NONE ? "." : ("L"+(newcell.cell.wi()-L0))),
639 getWidth() - 200 + 10, (line += 15));
640 g.drawString("z-in mux: " + (newcell.cell.zi()==NONE ? "." : ("L"+(newcell.cell.zi()-L0))),
641 getWidth() - 200 + 10, (line += 15));
644 switch(newcell.cell.t()) {
645 case TMUX_FB: tm = "fb"; break;
646 case TMUX_W_AND_FB: tm = "w&fb"; break;
647 case TMUX_Z: tm = "z"; break;
648 case TMUX_W_AND_Z: tm = "w&z"; break;
649 case TMUX_W: tm = "w"; break;
651 g.drawString("t-in mux: " + tm, getWidth() - 200 + 10, (line += 15));
653 g.drawString(" set/rst: " + (newcell.cell.ff_reset_value() ? "reset=SET" : "."),
654 getWidth() - 200 + 10, (line += 15));
657 for(int i=0; i<5; i++) outs += (newcell.cell.out(L0+i) ? (i+" ") : ". ");
658 g.drawString(" out: " + outs,
659 getWidth() - 200 + 10, (line += 15));
661 for(int i=0; i<5; i++) hs += (newcell.cell.hx(L0+i) ? (i+" ") : ". ");
662 g.drawString(" h conn: " + hs,
663 getWidth() - 200 + 10, (line += 15));
665 for(int i=0; i<5; i++) vs += (newcell.cell.vx(L0+i) ? (i+" ") : ". ");
666 g.drawString(" v conn: " + vs,
667 getWidth() - 200 + 10, (line += 15));
668 g.drawString("out enab: " + (newcell.cell.oe()==H4 ? "H4" : newcell.cell.oe()==V4 ? "V4" : "."),
669 getWidth() - 200 + 10, (line += 15));
670 g.drawString(" c-mux: " + (newcell.cell.c()==ZMUX ? "zmux" : newcell.cell.c()==XLUT ? "x-lut" : "y-lut"),
671 getWidth() - 200 + 10, (line += 15));
672 g.drawString(" fb src: " + (newcell.cell.f() ? "clocked" : "."),
673 getWidth() - 200 + 10, (line += 15));
674 g.drawString(" bypass: " + (newcell.cell.b() ? "clocked" : "."),
675 getWidth() - 200 + 10, (line += 15));
676 g.drawString(" x out: " + (newcell.cell.xo() ? (newcell.cell.b() ? "register" : "center") : "."),
677 getWidth() - 200 + 10, (line += 15));
678 g.drawString(" y out: " + (newcell.cell.yo() ? (newcell.cell.b() ? "register" : "center") : "."),
679 getWidth() - 200 + 10, (line += 15));
683 drawKeyboard(keyboard2, g);
684 } else switch(lastChar) {
692 drawKeyboard(keyboard3, g);
695 drawKeyboard(keyboard1, g);
699 while (mousebutton) {
701 if (dragFrom == null || oldcell == null) break;
702 if (Math.abs(dragFrom._y - oldcell._y) > 1) break;
703 if (Math.abs(dragFrom._x - oldcell._x) > 1) break;
705 if (dragFrom._x == oldcell._x || dragFrom._y == oldcell._y)
710 gg.line( oldcell._x * SIZE + SIZE/2,
711 oldcell._y * SIZE + SIZE/2,
712 dragFrom._x * SIZE + SIZE/2,
713 dragFrom._y * SIZE + SIZE/2, 5);
720 Cell dragFrom = null;
722 public void clear() {
723 Graphics2D g = (Graphics2D)getGraphics();
724 //gg.color(Color.black);
725 //gg.color(Color.lightGray);
726 g.clearRect(0, 0, getWidth(), getHeight());
729 public static final P translate(P p, int dx, int dy) {
730 return new P(p.getX()+dx, p.getY()+dy);
733 public Cell whichCell(int x, int y) {
736 p = p.inverseTransform(transform);
737 } catch (Exception e) {
740 int col = ((int)p.getX()+0) / SIZE;
741 int row = ((int)p.getY()+0) / SIZE;
743 if (c._x == col && c._y == row)
748 public static boolean xlut_relevant(FpslicDevice.Cell c) {
749 return c.xlut_relevant();
752 public void mouseClicked(MouseEvent e) {
753 final Cell c = whichCell(e.getX(), e.getY());
759 for(int x=0; x<fpslic.getWidth(); x++)
760 for(int y=0; y<fpslic.getHeight(); y++)
761 if (ca[x][y] != null)
762 if (ca[x][y].scanme())
765 public void scan(final Gui.Cell c) {
767 final FpslicDevice.Cell cell = c.cell;
768 scan(fpslic, cell, NONE, true);
769 boolean safe = !cell.fb_relevant();
770 if (cell.xo()) safe = false;
771 if (cell.yo()) safe = false;
772 for(int i=0; i<5; i++)
777 if (cell.xlut_relevant()) {
779 slipway.readFpgaData(new BCB(c, XLUT));
783 if (cell.ylut_relevant()) {
785 slipway.readFpgaData(new BCB(c, YLUT));
793 if (!cell.xlut_relevant()) {
796 slipway.readFpgaData(new BCB(c, XLUT));
800 for(FpslicDevice.Cell c2 : new FpslicDevice.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
801 if (c2!=null && !c2.relevant()) {
802 scan(fpslic, cell, NONE, false);
805 scan(fpslic, c2, NONE, true);
806 slipway.readFpgaData(new BCB(c, YLUT));
807 scan(fpslic, c2, NONE, false);
815 if (!cell.ylut_relevant()) {
818 slipway.readFpgaData(new BCB(c, YLUT));
822 for(FpslicDevice.Cell c2 : new FpslicDevice.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
823 if (c2!=null && !c2.relevant()) {
824 scan(fpslic, cell, NONE, false);
826 scan(fpslic, c2, NONE, true);
828 slipway.readFpgaData(new BCB(c, XLUT));
829 scan(fpslic, c2, NONE, false);
838 scan(fpslic, cell, NONE, false);
842 for(FpslicDevice.Cell c2 : new FpslicDevice.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
843 if (c2!=null && !c2.relevant()) {
844 scan(fpslic, cell, NONE, false);
846 scan(fpslic, c2, NONE, true);
848 slipway.readFpgaData(new BCB(c, XLUT));
849 scan(fpslic, c2, NONE, false);
854 for(FpslicDevice.Cell c2 : new FpslicDevice.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
855 if (c2!=null && !c2.relevant()) {
858 scan(fpslic, c2, NONE, true);
859 slipway.readFpgaData(new BCB(c, YLUT));
860 scan(fpslic, c2, NONE, false);
870 scan(fpslic, cell, NONE, false);
871 } catch (IOException e) {
872 throw new RuntimeException(e);
876 public static void scan(FpslicDevice dev, FpslicDevice.Cell cell, int source, boolean setup) {
878 if (source != NONE) cell.c(source);
879 if (cell.b()) cell.b(false);
880 if (cell.f()) cell.f(false);
882 if (cell.out(L3)!=setup) cell.out(L3, setup);
883 if (cell.vx(L3)!=setup) cell.v(L3, setup);
885 FpslicDevice.SectorWire sw = cell.vwire(L3);
886 //System.out.println("wire is: " + sw);
888 if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
889 sw.north().drives(sw, false);
890 while(sw.row > (12 & ~0x3) && sw.south() != null) {
891 //System.out.println(sw + " -> " + sw.south());
892 if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
895 if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
896 sw.north().drives(sw, false);
897 while(sw.row < (12 & ~0x3) && sw.north() != null) {
898 //System.out.println(sw + " -> " + sw.north());
899 if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
903 //cell = dev.cell(19, 15);
904 cell = dev.cell(cell.col, 15);
906 System.out.println("cell is " + cell);
915 if (cell.hx(L3) != setup) cell.h(L3, setup);
916 if (cell.vx(L3) != setup) cell.v(L3, setup);
919 if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
920 while(sw.east() != null) {
921 //System.out.println(sw + " -> " + sw.east());
922 if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
930 private class BCB extends SlipwayBoard.ByteCallback {
933 public BCB(Gui.Cell c, int who) {
934 this.who = who; this.c = c;
937 public void call(byte b) throws Exception {
938 boolean on = (b & 0x80) != 0;
955 public Gate getGate(byte lut, boolean xlut) {
956 for(Gate g : knownGates)
957 if (g.setLut(lut, xlut))
962 public Gate unknownGate = new Circle("?");
963 public Gate[] knownGates =
967 new Circle("0") { public boolean result(boolean x, boolean y, boolean z) { return false; } },
968 new Circle("1") { public boolean result(boolean x, boolean y, boolean z) { return true; } },
969 new Circle("x") { public boolean result(boolean x, boolean y, boolean z) { return x; } },
970 new Circle("y") { public boolean result(boolean x, boolean y, boolean z) { return y; } },
971 new Circle("z") { public boolean result(boolean x, boolean y, boolean z) { return z; } },
972 new Circle("~x") { public boolean result(boolean x, boolean y, boolean z) { return !x; } },
973 new Circle("~y") { public boolean result(boolean x, boolean y, boolean z) { return !y; } },
974 new Circle("~z") { public boolean result(boolean x, boolean y, boolean z) { return !z; } }
977 // FIXME: 2-input gates?
978 public abstract class Gate {
979 public boolean invert_x;
980 public boolean invert_y;
981 public boolean invert_z;
982 public boolean invert_out;
983 public abstract boolean result(boolean x, boolean y, boolean z);
984 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
985 GeneralPath p = new GeneralPath();
992 AffineTransform a = g.getTransform();
994 if (label() != null) {
996 g.drawString(label(), 7, -14);
1000 public String label() { return null; }
1001 public boolean setLut(int lut, boolean xlut) {
1003 for(int inverts = 0; inverts < 16; inverts++) {
1004 invert_x = (inverts & 0x1) != 0;
1005 invert_y = (inverts & 0x2) != 0;
1006 invert_z = (inverts & 0x4) != 0;
1007 invert_out = (inverts & 0x8) != 0;
1009 boolean good = true;
1010 for(int bit=0; bit<8; bit++) {
1011 boolean x = xlut ? ((bit & 0x1) != 0) : ((bit & 0x2) != 0);
1012 boolean y = xlut ? ((bit & 0x2) != 0) : ((bit & 0x1) != 0);
1013 boolean z = (bit & 0x4) != 0;
1014 boolean expect = (lut & (1<<bit)) != 0;
1016 // FIXME symmetry issues here....
1017 boolean result = result(x ^ invert_x, y ^ invert_y, z ^ invert_z) ^ invert_out;
1018 if (result != expect) { good = false; break; }
1020 if (good) return true;
1026 public abstract void makePath(GeneralPath gp);
1029 public class Or extends Gate {
1030 public boolean result(boolean x, boolean y, boolean z) { return x || y || z; }
1031 public String label() { return "+"; }
1032 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1033 AffineTransform at = g.getTransform();
1035 g.translate(0, -40);
1036 super.draw(g, fill, stroke, text);
1039 public void makePath(GeneralPath gp) {
1040 gp.moveTo(29.141f, 36.301f);
1041 gp.lineTo(29.141f, 36.301f-7.161f);
1042 gp.curveTo(27.71f, 11.24f, 23.413f, 9.45f, 14.82f, 0.5f);
1043 gp.curveTo(6.229f, 9.45f, 1.932f, 11.24f, 0.5f, 29.141f);
1044 gp.lineTo(0.5f, 29.141f+7.161f);
1046 float y = 29.141f+7.161f;
1047 gp.curveTo(5.729f+x, -1.789f+y,
1048 6.444f+x, -2.686f+y,
1049 14.32f+x, -3.58f+y);
1050 gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1054 public class Circle extends Gate {
1056 public Circle(String label) { this.label = label; }
1057 public boolean result(boolean x, boolean y, boolean z) { return false; }
1058 public String label() { return label; }
1059 public void makePath(GeneralPath gp) {
1070 public class And extends Gate {
1071 public boolean result(boolean x, boolean y, boolean z) { return x && y && z; }
1072 public String label() { return "&"; }
1073 public void makePath(GeneralPath gp) {
1076 gp.curveTo(0, 27, 3, 35, 13, 35);
1077 gp.curveTo(20, 35, 23, 27, 23, 19);
1083 public class Muller extends And {
1084 public String label() { return "C"; }
1085 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1086 super.draw(g, fill, stroke, text);
1088 g.drawLine(0, 0, 23, 0);
1092 public class Xor extends Or {
1093 public boolean result(boolean x, boolean y, boolean z) { return x ^ y ^ z; }
1094 public String label() { return "^"; }
1095 public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1096 super.draw(g, fill, stroke, text);
1098 AffineTransform at = g.getTransform();
1100 g.translate(0, -40);
1103 GeneralPath gp = new GeneralPath();
1105 float y = 29.141f+7.161f;
1107 gp.curveTo(5.729f+x, -1.789f+y,
1108 6.444f+x, -2.686f+y,
1109 14.32f+x, -3.58f+y);
1110 gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1117 public abstract class Mux extends Gate {
1118 public String label() { return "?"; }
1119 public void makePath(GeneralPath gp) {
1128 public class X_Mux extends Mux {
1129 public boolean result(boolean x, boolean y, boolean z) { return x ? y : z; }
1131 public class Y_Mux extends Mux {
1132 public boolean result(boolean x, boolean y, boolean z) { return y ? x : z; }
1134 public class Z_Mux extends Mux {
1135 public boolean result(boolean x, boolean y, boolean z) { return z ? x : y; }
1138 public abstract class Buf extends Gate {
1139 public void makePath(GeneralPath gp) {
1147 public class X extends Buf {
1148 public boolean result(boolean x, boolean y, boolean z) { return x; }
1150 public class Y extends Buf {
1151 public boolean result(boolean x, boolean y, boolean z) { return y; }
1153 public class Z extends Buf {
1154 public boolean result(boolean x, boolean y, boolean z) { return z; }
1157 private static Image keyboard1 =
1158 Toolkit.getDefaultToolkit().createImage("images/keyboard1.png");
1159 private static Image keyboard2 =
1160 Toolkit.getDefaultToolkit().createImage("images/keyboard2.png");
1161 private static Image keyboard3 =
1162 Toolkit.getDefaultToolkit().createImage("images/keyboard3.png");