checkpoint
[slipway.git] / src / edu / berkeley / slipway / gui / Gui.java
1 package edu.berkeley.slipway.gui;
2
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.*;
8 import java.awt.*;
9 import java.awt.geom.*;
10 import java.awt.event.*;
11 import java.awt.color.*;
12 import org.ibex.util.*;
13 import java.io.*;
14 import java.util.*;
15 import javax.swing.*;
16 import static edu.berkeley.slipway.gui.GuiConstants.*;
17
18 public class Gui extends ZoomingPanel implements KeyListener, MouseMotionListener {
19
20     Graphics2D g;
21     G gg;
22
23     Fpslic at40k;
24     FtdiBoard drone;
25
26     private Cell[][] ca = new Cell[128][];
27
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);
31
32     public void writeMode4() {
33         try {
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);
38             pw.flush();
39             pw.close();
40             System.err.println("done writing");
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44     }
45
46     public void readMode4() {
47         try {
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");
52             repaint();
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57
58     public Gui(Fpslic at40k, FtdiBoard drone) {
59         this(at40k, drone, 24, 24);
60     }
61     public Gui(Fpslic at40k, FtdiBoard drone, int width, int height) {
62         super(drone);
63         this.at40k = at40k;
64         this.drone = drone;
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));
70
71
72
73         /*
74           new Thread() {
75             public void run() {
76                 try { Thread.sleep(2000); } catch (Exception e) { }
77                 while(true) scan();
78             }
79             }.start();
80         */
81
82
83         /*
84         Fpslic.Cell c = at40k.cell(0,0);
85         for(int i=0; i<256; i++) {
86             c.ylut(i);
87             System.out.println(c.printYLut());
88         }
89         */
90     }
91
92     public class Cell {
93         Fpslic.Cell cell;
94         boolean in = false;
95         public boolean scanme = false;
96         public boolean xon = false;
97         public boolean yon = false;
98         public boolean xknown = false;
99         public boolean yknown = false;
100         int _x, _y;
101         public Cell(int x, int y, Fpslic.Cell cell) {
102             _x = x;
103             _y = y;
104             ca[_x][_y] = this;
105             this.cell = cell;
106             cells.add(this);
107         }
108         public boolean scanme() { 
109             return cell.relevant();
110         }
111         public void clear() {
112             gg.color(in ? selectedcell : (scanme() ? new Color(0xbb, 0xbb, 0xbb) : nonselectedcell));
113             g.fillRect(0, 0, SIZE, SIZE);
114         }
115         public void draw() {
116
117             if (cell.relevant() || scanme()) {
118                 drawWires();
119                 drawLocal();
120                 
121                 AffineTransform t = g.getTransform();
122                 
123                 drawBuffer();
124                 g.transform(rotateInnerTransform());
125                 drawMux();
126                 drawRegister();
127                 drawInternalRouting();
128                 g.setTransform(t);
129                 
130                 drawGates();
131             }
132             drawBorder();
133         }
134         public void drawBuffer() {
135             /*
136             if (!cell.out_relevant()) return;
137
138             GeneralPath p = new GeneralPath();
139             p.moveTo(24, 61);
140             p.lineTo(22, 68);
141             p.lineTo(29, 66);
142             p.lineTo(24, 61);
143             gg.color(cell.oe() == NONE ? Color.white : Color.green);
144             g.fill(p);
145             gg.color(Color.green);
146             g.draw(p);
147
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);
153             }
154             */
155         }
156         public void drawWires() {
157             gg.color(MAGENTA);
158             for(int i=0; i<5; i++)
159                 if (i!=3)
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));
163                 }
164             for(int i=0; i<5; i++)
165                 if (i!=3)
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);
169                 }
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);
174         }
175
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)),
181                       3);
182         }
183
184         public P corner(int corner, int size, int ring) {
185             switch(corner) {
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;
191             }
192         }
193
194         public void drawInternalRouting() {
195             gg.color(ORANGE);
196             if (cell.fb_relevant()) {
197                 if (cell.f()) {
198                     gg.line(51, 74, 37, 74);
199                     gg.line(37, 74, 51, 12);
200                 } else if (cell.c() == XLUT) {
201                     gg.color(LIGHTRED);
202                     gg.line(33, 52, 51, 52);
203                     gg.line(51, 52, 51, 12);
204                 } else if (cell.c() == YLUT) {
205                     gg.color(LIGHTBLUE);
206                     gg.line(67, 52, 51, 52);
207                     gg.line(51, 52, 51, 12);
208                 } else {
209                     gg.line(51, 56, 41, 56);
210                     gg.line(41, 56, 51, 12);
211                 }
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);
215                 }
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);
219                 }
220             }
221         }
222
223         public void drawLocal() {
224             if (!cell.ylut_relevant() && !xlut_relevant(cell)) return;
225
226             P in = rotateOuter(new P(HOFF, 0));
227             P join = rotateInner(new P(HOFF, CORE_OFFSET));
228             int rot = rot();
229             switch(rot) {
230                 case 0: case 2:
231                     join = new P(in.getX(), join.getY());
232                     break;
233                 case 1: case 3:
234                     join = new P(join.getX(), in.getY());
235                     break;
236             }
237
238             // x-input to cell
239             gg.color(RED);
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)
244                 ) {
245                 P xi2 = corner(cell.xi(), SIZE + 2*BEVEL, -1).translate(-BEVEL, -BEVEL);
246                 switch(cell.xi()) {
247                     case NW: case NE:
248                         xi = translate(xi, 0, -3);
249                         xi2 = translate(xi2, 0, -3);
250                         break;
251                     case SW: case SE:
252                         xi = translate(xi, 0, 3);
253                         xi2 = translate(xi2, 0, 3);
254                         break;
255                 }
256                 gg.line(xi2, xi);
257             }
258
259             if (xlut_relevant(cell)) {
260
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);
263
264                 // xlut y-input
265                 gg.color(BLUE);
266                 if (cell.yi_to_xlut_relevant())
267                     route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 6, 20)), 5);
268
269                 // xlut output
270                 int xring = 4;
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));
273                 if (cell.xo()) {
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);
278                 }
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);
282                 }
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);
286                 }
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);
290                 }
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);
294                 }
295             }
296
297             if (cell.ylut_relevant()) {
298
299                 // ylut y-input
300                 gg.color(BLUE);
301                 if (cell.yi_to_ylut_relevant())
302                     route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 6, 20)), 5);
303
304                 // ylut x-input
305                 gg.color(RED);
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);
308
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));
312                 if (cell.yo()) {
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);
317                 }
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);
322             }
323
324         }
325
326         private AffineTransform rotateOuterTransform() {
327             int rot = rot();
328             AffineTransform a = new AffineTransform();
329             a.rotate((Math.PI/2) * rot);
330             switch(rot) {
331                 case 0: break;
332                 case 1: a.translate(0,  -SIZE); break;
333                 case 2: a.translate(-SIZE, -SIZE); break;
334                 case 3: a.translate(-SIZE, 0); break;
335             }
336             return a;
337         }
338
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()); }
342
343         private AffineTransform rotateInnerTransform() {
344             int rot = rot();            
345             AffineTransform a = new AffineTransform();
346             a.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
347             a.rotate((Math.PI/2) * rot);
348             switch(rot) {
349                 case 0: break;
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;
353             }
354             a.translate(-1 * (SIZE-CORE_SIZE-CORE_OFFSET), -CORE_OFFSET);
355             return a;
356         }
357
358
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);
368
369         }
370
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);
375             p1 = projected;
376
377             projected = project(p2, ring);
378             gg.line(p2, projected);
379             p2 = projected;
380
381             if (p1.x==p2.x || p1.y==p2.y) {
382                 gg.line(p1, p2);
383                 return;
384             }
385
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);
388                 gg.line(p1, p3);
389                 route(p3, p2, ring);
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);
392                 gg.line(p1, p3);
393                 route(p3, p2, ring);
394             } else
395                 route(p2, p1, ring);
396         }
397         
398         private int rot() {
399             int rot = 0;
400             switch(cell.yi()) {
401                 case SOUTH: rot = 0; break;
402                 case NORTH: rot = 2; break;
403                 case EAST: rot = 1; break;
404                 case WEST: rot = 3; break;
405                 default: {
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; }
411                 }
412             }
413             return rot;
414         }
415         
416         public void drawGates() {
417             AffineTransform t = g.getTransform();
418             try {
419                 g.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
420                 
421                 int rot = rot();
422                 g.rotate((Math.PI/2) * rot);
423                 switch(rot) {
424                     case 0: break;
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;
428                 }
429
430                 //gg.color(Color.gray);
431                 //g.drawRect(0, 0, CORE_SIZE, CORE_SIZE);
432                 //g.scale(1, -1);
433
434
435                 g.translate(2,   5f);
436                 if (xlut_relevant(cell) || scanme()) {
437                     Gate gate = getGate(cell.xlut(), true);
438                     gate.draw(g,
439                               !xknown ? Color.gray : xon ? Color.red : Color.white,
440                               (xon && xknown) ? Color.white : Color.red,
441                               xon ? Color.white : Color.red
442                               );
443                 }
444
445                 g.translate(34f, 0f);
446                 if (cell.ylut_relevant() || scanme()) {
447                     Gate gate = getGate(cell.ylut(), false);
448                     gate.draw(g,
449                               !yknown ? Color.gray : yon ? Color.blue : Color.white,
450                               (yon && yknown) ? Color.white : Color.blue,
451                               yon ? Color.white : Color.blue
452                               );
453                 }
454
455             } finally {
456                 g.setTransform(t);
457             }
458         }
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)) {
463                 gg.color(LIGHTRED);
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));
466             }
467             if (cell.ylut_relevant() && (cell.c() == ZMUX || cell.c() == YLUT)) {
468                 gg.color(LIGHTBLUE);
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));
471             }
472             if (cell.c() == ZMUX)
473                 gg.color(ORANGE);
474
475             gg.line(51, 52, 51, 51+23);
476
477             if (cell.register_relevant() && cell.f() && !cell.b()) {
478                 gg.line(51, 56, 60, 56);
479                 gg.line(60, 56, 60, 51+25);
480             } else {
481                 gg.line(51, 51+23, 51, 51+25);
482             }
483
484             if (cell.c() == ZMUX) {
485
486                 gg.color(GREEN);
487                 int plane = cell.zi()+1;
488                 route(unRotateInner(new P(8 + 2*(plane-L0),  76 + 2*(plane-L0))),
489                       new P(51, 20),
490                       3);
491                 gg.line(new P(51, 20), new P(51, 50));
492
493                 GeneralPath p = new GeneralPath();
494                 p.moveTo(45, 50);
495                 p.lineTo(47, 54);
496                 p.lineTo(56, 54);
497                 p.lineTo(58, 50);
498                 p.lineTo(45, 50);
499                 gg.color(WHITE);
500                 gg.g.fill(p);
501
502                 gg.color(ORANGE);
503                 gg.g.draw(p);
504
505             }
506         }
507         public int ccolor() {
508             switch(cell.c()) {
509                 case XLUT: return LIGHTRED;
510                 case YLUT: return LIGHTBLUE;
511                 case ZMUX: return ORANGE;
512             }
513             return BLACK;
514         }
515         public void drawRegister() {
516             if (!cell.register_relevant()) return;
517
518             int dark = ccolor();
519             gg.color(Color.white);
520             g.fillRect(47, 58, 9, 14);
521             gg.color(dark);
522             g.drawRect(47, 58, 9, 14);
523
524             GeneralPath p = new GeneralPath();
525             p.moveTo(56, 70);
526             p.lineTo(53, 68);
527             p.lineTo(56, 66);
528             p.lineTo(56, 70);
529             gg.color(cell.ff_reset_value() ? WHITE : dark);
530             g.fill(p);
531             gg.color(dark);
532             g.draw(p);
533         }
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);
540             /*            
541             gg.color(0xdddddd);
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);
546             */
547         }
548     }
549
550     public void pressed() {
551         dragFrom = oldcell;
552     }
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);
565         repaint();
566     }
567     public void drawKeyboard(Image keyboardImage, Graphics2D g) {
568         /*
569                 int width = 300;
570                 int height = (keyboardImage.getHeight(null) * width) / keyboardImage.getWidth(null);
571                 g.drawImage(keyboardImage,
572                             0, getHeight() - height,
573                             width, getHeight(),
574                             0, 0,
575                             keyboardImage.getWidth(null), keyboardImage.getHeight(null),
576                             null);
577         */
578     }
579
580     public void _paint(Graphics2D g) {
581
582         this.g = g;
583         this.gg = new G(g);
584         g.setStroke(new BasicStroke((float)0.5));
585
586         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
587         g.setRenderingHint(RenderingHints.KEY_RENDERING,    RenderingHints.VALUE_RENDER_QUALITY);
588
589         AffineTransform t  = g.getTransform();
590         for(Cell c : cells) {
591             g.setTransform(t);
592             g.translate(     c._x * SIZE /*+ (10 * (c._x/4))*/,      c._y * SIZE /*+ (10 * (c._y/4))*/);
593             c.clear();
594         }
595         for(Cell c : cells) {
596             g.setTransform(t);
597             g.translate(     c._x * SIZE /*+ (10 * (c._x/4))*/,      c._y * SIZE /*+ (10 * (c._y/4))*/);
598             c.draw();
599         }
600         g.setTransform(t);
601
602         g.setTransform(new AffineTransform());
603
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);
608
609         Cell newcell = whichCell(mousex, mousey);
610         int line = 10;
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));
616             String xi = "??";
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;
624             }
625             g.drawString("x-in mux: " + xi, getWidth() - 200 + 10, (line += 15));
626
627             String yi = "??";
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;
635             }
636             g.drawString("y-in mux: " + yi, getWidth() - 200 + 10, (line += 15));
637
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));
642
643             String tm = "??";
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;
650             }
651             g.drawString("t-in mux: " + tm, getWidth() - 200 + 10, (line += 15));
652
653             g.drawString(" set/rst: " + (newcell.cell.ff_reset_value() ? "reset=SET" : "."),
654                          getWidth() - 200 + 10, (line += 15));
655
656             String outs = "";
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));
660             String hs = "";
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));
664             String vs = "";
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));
680
681         }
682         if (shiftkey) {
683             drawKeyboard(keyboard2, g);
684         } else switch(lastChar) {
685             case 'x':
686             case 'y':
687             case 'z':
688             case 'w':
689             case 'o':
690             case 'h':
691             case 'v':
692                 drawKeyboard(keyboard3, g);
693                 break;
694             default:
695                 drawKeyboard(keyboard1, g);
696                 break;
697         }
698
699         while (mousebutton) {
700
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;
704             g.setTransform(t);
705             if (dragFrom._x == oldcell._x || dragFrom._y == oldcell._y)
706                 gg.color(BLUE);
707             else
708                 gg.color(RED);
709
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);
714             break;
715         }
716
717         this.g = null;
718         this.gg = null;
719     }
720     Cell dragFrom = null;
721
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());
727     }
728
729     public static final P translate(P p, int dx, int dy) {
730         return new P(p.getX()+dx, p.getY()+dy);
731     }
732
733     public Cell whichCell(int x, int y) {
734         P p = new P(x,y);
735         try {
736             p = p.inverseTransform(transform);
737         } catch (Exception e) {
738             e.printStackTrace();
739         }
740         int col = ((int)p.getX()+0) / SIZE;
741         int row = ((int)p.getY()+0) / SIZE;
742         for(Cell c : cells)
743             if (c._x == col && c._y == row)
744                 return c;
745         return null;
746     }
747
748     public static boolean xlut_relevant(Fpslic.Cell c) {
749         return c.xlut_relevant();
750     }
751
752     public void mouseClicked(MouseEvent e) {
753         final Cell c = whichCell(e.getX(), e.getY());
754         if (c==null) return;
755         scan(c);
756     }
757
758     public void scan() {
759         for(int x=0; x<at40k.getWidth(); x++)
760             for(int y=0; y<at40k.getHeight(); y++)
761                 if (ca[x][y] != null)
762                     if (ca[x][y].scanme())
763                         scan(ca[x][y]);
764     }
765     public void scan(final Gui.Cell c) {
766         try {
767             final Fpslic.Cell cell = c.cell;
768             scan(at40k, 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++)
773                 if (cell.out(i))
774                     safe = false;
775             if (safe) {
776                 int oldc = cell.c();
777                 if (cell.xlut_relevant()) {
778                     cell.c(XLUT);
779                     drone.readBus(new BCB(c, XLUT));
780                 } else {
781                     c.xknown = false;
782                 }
783                 if (cell.ylut_relevant()) {
784                     cell.c(YLUT);
785                     drone.readBus(new BCB(c, YLUT));
786                 } else {
787                     c.yknown = false;
788                 }
789                 cell.c(oldc);
790             } else {
791                 switch(cell.c()) {
792                     case XLUT:
793                         if (!cell.xlut_relevant()) {
794                             c.xknown = false;
795                         } else {
796                             drone.readBus(new BCB(c, XLUT));
797                         }
798                         /*
799                         if (!cell.yo())
800                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
801                             if (c2!=null && !c2.relevant()) {
802                                 scan(at40k, cell, NONE, false);
803                                 c2.yo(cell);
804                                 c2.c(YLUT);
805                                 scan(at40k, c2, NONE, true);
806                                 drone.readBus(new BCB(c, YLUT));
807                                 scan(at40k, c2, NONE, false);
808                                 c2.yi(NONE);
809                                 return;
810                             }
811                         */
812                         c.yknown = false;
813                         break;
814                     case YLUT:
815                         if (!cell.ylut_relevant()) {
816                             c.yknown = false;
817                         } else {
818                             drone.readBus(new BCB(c, YLUT));
819                         }
820                         /*
821                         if (!cell.xo())
822                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
823                             if (c2!=null && !c2.relevant()) {
824                                 scan(at40k, cell, NONE, false);
825                                 c2.xo(cell);
826                                 scan(at40k, c2, NONE, true);
827                                 c2.c(XLUT);
828                                 drone.readBus(new BCB(c, XLUT));
829                                 scan(at40k, c2, NONE, false);
830                                 c2.xi(NONE);
831                                 return;
832                             }
833                         */
834                         c.xknown = false;
835                         break;
836                     case ZMUX: {
837                         /*
838                         scan(at40k, cell, NONE, false);
839                         c.xknown = false;
840                         c.yknown = false;
841                         if (!cell.xo())
842                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
843                             if (c2!=null && !c2.relevant()) {
844                                 scan(at40k, cell, NONE, false);
845                                 c2.xo(cell);
846                                 scan(at40k, c2, NONE, true);
847                                 c2.c(XLUT);
848                                 drone.readBus(new BCB(c, XLUT));
849                                 scan(at40k, c2, NONE, false);
850                                 c2.xi(NONE);
851                                 return;
852                             }
853                         if (!cell.yo())
854                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
855                             if (c2!=null && !c2.relevant()) {
856                                 c2.yo(cell);
857                                 c2.c(YLUT);
858                                 scan(at40k, c2, NONE, true);
859                                 drone.readBus(new BCB(c, YLUT));
860                                 scan(at40k, c2, NONE, false);
861                                 c2.yi(NONE);
862                                 break;
863                             }
864                         return;
865                         */
866                     }
867                 }
868                 
869             }
870             scan(at40k, cell, NONE, false);
871         } catch (IOException e) {
872             throw new RuntimeException(e);
873         }
874     }
875
876     public static void scan(Fpslic dev, Fpslic.Cell cell, int source, boolean setup) {
877         if (setup) {
878             if (source != NONE) cell.c(source);
879             if (cell.b()) cell.b(false);
880             if (cell.f()) cell.f(false);
881         }
882         if (cell.out(L3)!=setup) cell.out(L3, setup);
883         if (cell.vx(L3)!=setup) cell.v(L3, setup);
884
885         Fpslic.SectorWire sw = cell.vwire(L3);
886         //System.out.println("wire is: " + sw);
887
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);
893             sw = sw.south();
894         }
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);
900             sw = sw.north();
901         }
902
903         //cell = dev.cell(19, 15);
904         cell = dev.cell(cell.col, 15);
905         /*
906         System.out.println("cell is " + cell);
907         cell.xlut(0xff);
908         cell.ylut(0xff);
909         cell.b(false);
910         cell.f(false);
911         cell.c(XLUT);
912         cell.out(L3, true);
913         cell.oe(NONE);
914         */
915         if (cell.hx(L3) != setup) cell.h(L3, setup);
916         if (cell.vx(L3) != setup) cell.v(L3, setup);
917         sw = cell.hwire(L3);
918
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);
923             sw = sw.east();
924         }
925
926     }
927
928
929     int made = 0;
930     private class BCB extends FtdiBoard.ByteCallback {
931         Gui.Cell c;
932         int who;
933         public BCB(Gui.Cell c, int who) {
934             this.who = who; this.c = c;
935             made++;
936         }
937         public void call(byte b) throws Exception {
938             boolean on = (b & 0x80) != 0;
939             switch(who) {
940                 case YLUT:
941                     c.yknown = true;
942                     c.yon = on;
943                     repaint();
944                     break;
945                 case XLUT:
946                     c.xknown = true;
947                     c.xon = on;
948                     repaint();
949                     break;
950             }
951             made--;
952         }
953     }
954
955     public Gate getGate(byte lut, boolean xlut) {
956         for(Gate g : knownGates)
957             if (g.setLut(lut, xlut))
958                 return g;
959         return unknownGate;
960     }
961
962     public Gate unknownGate = new Circle("?");
963     public Gate[] knownGates =
964         new Gate[] {
965             new And(),
966             new Or(),
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; } }
975         };
976
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();
986             makePath(p);
987             g.setColor(fill);
988             g.fill(p);
989             g.setColor(stroke);
990             g.draw(p);
991
992             AffineTransform a = g.getTransform();
993             g.scale(1, -1);
994             if (label() != null) {
995                 g.setColor(text);
996                 g.drawString(label(), 7, -14);
997             }
998             g.setTransform(a);
999         }
1000         public String label() { return null; }
1001         public boolean setLut(int lut, boolean xlut) {
1002             /*
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;
1008             */
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;
1015
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; }
1019                 }
1020                 if (good) return true;
1021                 /*
1022             }
1023                 */
1024             return false;
1025         }
1026         public abstract void makePath(GeneralPath gp);
1027     }
1028
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();
1034             g.scale(1, -1);
1035             g.translate(0, -40);
1036             super.draw(g, fill, stroke, text);
1037             g.setTransform(at);
1038         }
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);
1045             float x = 0.5f;
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);
1051         }
1052     }
1053
1054     public class Circle extends Gate {
1055         String label;
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) {
1060             int S = 30;
1061             gp.moveTo(0, S/2);
1062             gp.lineTo(S/2, S);
1063             gp.lineTo(S, S/2);
1064             gp.lineTo(S/2, 0);
1065             gp.lineTo(0, S/2);
1066             gp.closePath();
1067         }
1068     }
1069
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) {
1074             gp.moveTo(0, 2);
1075             gp.lineTo(0, 19);
1076             gp.curveTo(0, 27, 3, 35, 13, 35);
1077             gp.curveTo(20, 35, 23, 27, 23, 19);
1078             gp.lineTo(23, 2);
1079             gp.closePath();
1080         }
1081     }
1082
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);
1087             g.setColor(stroke);
1088             g.drawLine(0, 0, 23, 0);
1089         }
1090     }
1091
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);
1097             g.setColor(stroke);
1098             AffineTransform at = g.getTransform();
1099             g.scale(1, -1);
1100             g.translate(0, -40);
1101
1102             g.translate(0, 4);
1103             GeneralPath gp = new GeneralPath();
1104             float x = 0.5f;
1105             float y = 29.141f+7.161f;
1106             gp.moveTo(x,y);
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);
1111             g.draw(gp);
1112
1113             g.setTransform(at);
1114         }
1115     }
1116
1117     public abstract class Mux extends Gate {
1118         public String label() { return "?"; }
1119         public void makePath(GeneralPath gp) {
1120             gp.moveTo(0, 15);
1121             gp.lineTo(2, 23);
1122             gp.lineTo(23, 23);
1123             gp.lineTo(25, 15);
1124             gp.lineTo(0, 15);
1125         }
1126     }
1127
1128     public class X_Mux extends Mux {
1129         public boolean result(boolean x, boolean y, boolean z) { return x ? y : z; }
1130     }
1131     public class Y_Mux extends Mux {
1132         public boolean result(boolean x, boolean y, boolean z) { return y ? x : z; }
1133     }
1134     public class Z_Mux extends Mux {
1135         public boolean result(boolean x, boolean y, boolean z) { return z ? x : y; }
1136     }
1137
1138     public abstract class Buf extends Gate {
1139         public void makePath(GeneralPath gp) {
1140             gp.moveTo(0, 15);
1141             gp.lineTo(13, 35);
1142             gp.lineTo(25, 15);
1143             gp.lineTo(0, 15);
1144         }
1145     }
1146
1147     public class X extends Buf {
1148         public boolean result(boolean x, boolean y, boolean z) { return x; }
1149     }
1150     public class Y extends Buf {
1151         public boolean result(boolean x, boolean y, boolean z) { return y; }
1152     }
1153     public class Z extends Buf {
1154         public boolean result(boolean x, boolean y, boolean z) { return z; }
1155     }
1156
1157     private static Image keyboard1 = Toolkit.getDefaultToolkit().createImage("keyboard1.png");
1158     private static Image keyboard2 = Toolkit.getDefaultToolkit().createImage("keyboard2.png");
1159     private static Image keyboard3 = Toolkit.getDefaultToolkit().createImage("keyboard3.png");
1160
1161 }