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
75         new Thread() {
76             public void run() {
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(0xffffff);
113             //gg.color(in ? selectedcell : (scanme() ? new Color(0xbb, 0xbb, 0xbb) : nonselectedcell));
114             g.fillRect(0, 0, SIZE, SIZE);
115         }
116         public void draw() {
117
118             if (cell.relevant() || scanme()) {
119                 drawWires();
120                 drawLocal();
121                 
122                 AffineTransform t = g.getTransform();
123                 
124                 drawBuffer();
125                 g.transform(rotateInnerTransform());
126                 drawMux();
127                 drawRegister();
128                 drawInternalRouting();
129                 g.setTransform(t);
130                 
131                 drawGates();
132             }
133             drawBorder();
134         }
135         public void drawBuffer() {
136             /*
137             if (!cell.out_relevant()) return;
138
139             GeneralPath p = new GeneralPath();
140             p.moveTo(24, 61);
141             p.lineTo(22, 68);
142             p.lineTo(29, 66);
143             p.lineTo(24, 61);
144             gg.color(cell.oe() == NONE ? Color.white : Color.green);
145             g.fill(p);
146             gg.color(Color.green);
147             g.draw(p);
148
149             gg.color(Color.magenta);
150             if (cell.oe() == V4) {
151                 gg.line(24, 62, 16, 63);
152             } else if (cell.oe() == H4) {
153                 gg.line(24, 67, 24, 76);
154             }
155             */
156         }
157         public void drawWires() {
158             gg.color(MAGENTA);
159             for(int i=0; i<5; i++)
160                 if (i!=3)
161                 if (cell.hwire(i).isDriven()) {
162                     gg.color(cell.out(i) ? ORANGE : MAGENTA);
163                     gg.line(0, SIZE-(2*(1+RINGS)+2*i), SIZE, SIZE-(2*(1+RINGS)+2*i));
164                 }
165             for(int i=0; i<5; i++)
166                 if (i!=3)
167                 if (cell.vwire(i).isDriven()) {
168                     gg.color(cell.out(i) ? ORANGE : MAGENTA);
169                     gg.line(2*(1+RINGS)+2*i, 0, 2*(1+RINGS)+2*i, SIZE);
170                 }
171             if (cell.zi_to_xlut_relevant()) wire(cell.zi(), true);
172             if (cell.zi_to_ylut_relevant()) wire(cell.zi(), false);
173             if (cell.zi_to_xlut_relevant()) wire(cell.wi(), true);
174             if (cell.zi_to_ylut_relevant()) wire(cell.wi(), false);
175         }
176
177         public void wire(int plane, boolean xlut) {
178             if (!(plane >= L0 && plane <= L4 && cell.vx(plane))) return;
179             if (xlut ? xlut_relevant(cell) : cell.ylut_relevant())
180                 route(new P(17 - 2*(plane-L0), 8),
181                       rotateInner(xlut ? new P(38, 18) : new P(64, 18)),
182                       3);
183         }
184
185         public P corner(int corner, int size, int ring) {
186             switch(corner) {
187                 case NW: return new P(0    +2*ring,  size -2*ring);
188                 case SW: return new P(0    +2*ring,  0    +2*ring);
189                 case NE: return new P(size -2*ring,  size -2*ring);
190                 case SE: return new P(size -2*ring,  0    +2*ring);
191                 default: return null;
192             }
193         }
194
195         public void drawInternalRouting() {
196             gg.color(ORANGE);
197             if (cell.fb_relevant()) {
198                 if (cell.f()) {
199                     gg.line(51, 74, 37, 74);
200                     gg.line(37, 74, 51, 12);
201                 } else if (cell.c() == XLUT) {
202                     gg.color(LIGHTRED);
203                     gg.line(33, 52, 51, 52);
204                     gg.line(51, 52, 51, 12);
205                 } else if (cell.c() == YLUT) {
206                     gg.color(LIGHTBLUE);
207                     gg.line(67, 52, 51, 52);
208                     gg.line(51, 52, 51, 12);
209                 } else {
210                     gg.line(51, 56, 41, 56);
211                     gg.line(41, 56, 51, 12);
212                 }
213                 if (xlut_relevant(cell)) {
214                     gg.line(52, 12, XLUT_OUTPUT_POINT.getX(), 12);
215                     gg.line(XLUT_OUTPUT_POINT.getX(), 12, XLUT_OUTPUT_POINT.getX(), 32);
216                 }
217                 if (cell.ylut_relevant()) {
218                     gg.line(52, 12, YLUT_OUTPUT_POINT.getX(), 12);
219                     gg.line(YLUT_OUTPUT_POINT.getX(), 12, YLUT_OUTPUT_POINT.getX(), 32);
220                 }
221             }
222         }
223
224         public void drawLocal() {
225             if (!cell.ylut_relevant() && !xlut_relevant(cell)) return;
226
227             P in = rotateOuter(new P(HOFF, 0));
228             P join = rotateInner(new P(HOFF, CORE_OFFSET));
229             int rot = rot();
230             switch(rot) {
231                 case 0: case 2:
232                     join = new P(in.getX(), join.getY());
233                     break;
234                 case 1: case 3:
235                     join = new P(join.getX(), in.getY());
236                     break;
237             }
238
239             // x-input to cell
240             gg.color(RED);
241             P xi  = corner(cell.xi(), SIZE, 4);
242             if (((cell.xlut_relevant() && cell.xi_to_xlut_relevant()) ||
243                 (cell.ylut_relevant() && cell.xi_to_ylut_relevant()))
244                 && (cell.xi() != NONE) && (cell.xi() < L0 || cell.xi() > L4)
245                 ) {
246                 P xi2 = corner(cell.xi(), SIZE + 2*BEVEL, -1).translate(-BEVEL, -BEVEL);
247                 switch(cell.xi()) {
248                     case NW: case NE:
249                         xi = translate(xi, 0, -3);
250                         xi2 = translate(xi2, 0, -3);
251                         break;
252                     case SW: case SE:
253                         xi = translate(xi, 0, 3);
254                         xi2 = translate(xi2, 0, 3);
255                         break;
256                 }
257                 gg.line(xi2, xi);
258             }
259
260             if (xlut_relevant(cell)) {
261
262                 if (cell.xi_to_xlut_relevant() && xi != null)
263                     route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 3, 20)), 4);
264
265                 // xlut y-input
266                 gg.color(BLUE);
267                 if (cell.yi_to_xlut_relevant())
268                     route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 - CORE_SIZE / 6, 20)), 5);
269
270                 // xlut output
271                 int xring = 4;
272                 gg.color(cell.xo() ? ORANGE : LIGHTRED);
273                 P xout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+17 - 2, CORE_OFFSET + CORE_SIZE - 3));
274                 if (cell.xo()) {
275                     xout = rotateInner(new P(51, 74));
276                     gg.line(rotateInner(new P(51, 62)), xout);
277                 } else if (cell.xo_relevant()) {
278                     gg.line(rotateInner(XLUT_OUTPUT_POINT), xout);
279                 }
280                 if (cell.xo_relevant(NE)) {
281                     gg.line(corner(NE, SIZE, xring).translate(0, 3), corner(NE, SIZE, 0).translate(0, 3));
282                     route(xout, corner(NE, SIZE, xring).translate(0, 3), xring);
283                 }
284                 if (cell.xo_relevant(NW)) {
285                     gg.line(corner(NW, SIZE, xring).translate(0, 3),  corner(NW, SIZE, 0).translate(0, 3));
286                     route(xout, corner(NW, SIZE, xring).translate(0, 3),  xring);
287                 }
288                 if (cell.xo_relevant(SE)) {
289                     gg.line(corner(SE, SIZE, xring).translate(0, -3), corner(SE, SIZE, 0).translate(0, -3));
290                     route(xout, corner(SE, SIZE, xring).translate(0, -3), xring);
291                 }
292                 if (cell.xo_relevant(SW)) {
293                     gg.line(corner(SW, SIZE, xring).translate(0, -3),  corner(SW, SIZE, 0).translate(0, -3));
294                     route(xout, corner(SW, SIZE, xring).translate(0, -3),  xring);
295                 }
296             }
297
298             if (cell.ylut_relevant()) {
299
300                 // ylut y-input
301                 gg.color(BLUE);
302                 if (cell.yi_to_ylut_relevant())
303                     route(in, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 6, 20)), 5);
304
305                 // ylut x-input
306                 gg.color(RED);
307                 if (xi != null && cell.xi_to_ylut_relevant())
308                     route(xi, rotateInner(new P(SIZE - CORE_OFFSET - CORE_SIZE/2 + CORE_SIZE / 3, 20)), 4);
309
310                 // lines directly from the ylut output to the four neighbors
311                 gg.color(cell.yo() ? ORANGE : LIGHTBLUE);
312                 P yout = rotateInner(new P(SIZE-CORE_OFFSET-CORE_SIZE+51 - 2, CORE_OFFSET + CORE_SIZE - 3));
313                 if (cell.yo()) {
314                     yout = rotateInner(new P(51, 74));
315                     gg.line(rotateInner(new P(51, 62)), yout);
316                 } else if (cell.yo_relevant()) {
317                     gg.line(rotateInner(YLUT_OUTPUT_POINT), yout);
318                 }
319                 if (cell.yo_relevant(NORTH)) route(yout, new P(SIZE-40, SIZE+ 0), 2);
320                 if (cell.yo_relevant(EAST))  route(yout, new P(SIZE+ 0,      40), 2);
321                 if (cell.yo_relevant(SOUTH)) route(yout, new P(     40,       0), 2);
322                 if (cell.yo_relevant(WEST))  route(yout, new P(      0, SIZE-40), 2);
323             }
324
325         }
326
327         private AffineTransform rotateOuterTransform() {
328             int rot = rot();
329             AffineTransform a = new AffineTransform();
330             a.rotate((Math.PI/2) * rot);
331             switch(rot) {
332                 case 0: break;
333                 case 1: a.translate(0,  -SIZE); break;
334                 case 2: a.translate(-SIZE, -SIZE); break;
335                 case 3: a.translate(-SIZE, 0); break;
336             }
337             return a;
338         }
339
340         private P rotateOuter(P p) { return p.transform(rotateOuterTransform()); }
341         private P rotateInner(P p) { return p.transform(rotateInnerTransform()); }
342         private P unRotateInner(P p) { return p.inverseTransform(rotateInnerTransform()); }
343
344         private AffineTransform rotateInnerTransform() {
345             int rot = rot();            
346             AffineTransform a = new AffineTransform();
347             a.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
348             a.rotate((Math.PI/2) * rot);
349             switch(rot) {
350                 case 0: break;
351                 case 1: a.translate(0,  -CORE_SIZE); break;
352                 case 2: a.translate(-CORE_SIZE, -CORE_SIZE); break;
353                 case 3: a.translate(-CORE_SIZE, 0); break;
354             }
355             a.translate(-1 * (SIZE-CORE_SIZE-CORE_OFFSET), -CORE_OFFSET);
356             return a;
357         }
358
359
360         private P project(P p1, int ring) {
361             double north = Math.abs( (SIZE-(ring*2)) - p1.getY() );
362             double south = Math.abs( (     (ring*2)) - p1.getY() );
363             double east  = Math.abs( (SIZE-(ring*2)) - p1.getX() );
364             double west  = Math.abs( (     (ring*2)) - p1.getX() );
365             if (north < south && north < east && north < west) return new P(p1.x,        SIZE-ring*2);
366             else if (south < east && south < west)             return new P(p1.x,        ring*2);
367             else if (east < west)                              return new P(SIZE-ring*2, p1.y);
368             else                                               return new P(ring*2,      p1.y);
369
370         }
371
372         private void route(P p1, P p2, int ring) {
373             int ringpos = ring * 2;
374             P projected = project(p1, ring);
375             gg.line(p1, projected);
376             p1 = projected;
377
378             projected = project(p2, ring);
379             gg.line(p2, projected);
380             p2 = projected;
381
382             if (p1.x==p2.x || p1.y==p2.y) {
383                 gg.line(p1, p2);
384                 return;
385             }
386
387             if ((p1.x==SIZE-ring*2 || p1.x==ring*2) && !(p1.y==SIZE-ring*2 || p1.y==ring*2)) {
388                 P p3 = new P(p1.x, p2.y > SIZE/2 ? SIZE-ring*2 : ring*2);
389                 gg.line(p1, p3);
390                 route(p3, p2, ring);
391             } else if ((p1.y==SIZE-ring*2 || p1.y==ring*2) && !(p1.x==SIZE-ring*2 || p1.x==ring*2)) {
392                 P p3 = new P(p2.x > SIZE/2 ? SIZE-ring*2 : ring*2, p1.y);
393                 gg.line(p1, p3);
394                 route(p3, p2, ring);
395             } else
396                 route(p2, p1, ring);
397         }
398         
399         private int rot() {
400             int rot = 0;
401             switch(cell.yi()) {
402                 case SOUTH: rot = 0; break;
403                 case NORTH: rot = 2; break;
404                 case EAST: rot = 1; break;
405                 case WEST: rot = 3; break;
406                 default: {
407                     // FIXME: choose based on xin
408                     if (cell.north() != null && cell.north().yi()==SOUTH) { rot = 0; break; }
409                     if (cell.south() != null && cell.south().yi()==NORTH) { rot = 2; break; }
410                     if (cell.east()  != null && cell.east().yi()==WEST) { rot = 3; break; }
411                     if (cell.west()  != null && cell.west().yi()==EAST) { rot = 1; break; }
412                 }
413             }
414             return rot;
415         }
416         
417         public void drawGates() {
418             AffineTransform t = g.getTransform();
419             try {
420                 g.translate(SIZE-CORE_SIZE-CORE_OFFSET, CORE_OFFSET);
421                 
422                 int rot = rot();
423                 g.rotate((Math.PI/2) * rot);
424                 switch(rot) {
425                     case 0: break;
426                     case 1: g.translate(0,  -CORE_SIZE); break;
427                     case 2: g.translate(-CORE_SIZE, -CORE_SIZE); break;
428                     case 3: g.translate(-CORE_SIZE, 0); break;
429                 }
430
431                 //gg.color(Color.gray);
432                 //g.drawRect(0, 0, CORE_SIZE, CORE_SIZE);
433                 //g.scale(1, -1);
434
435
436                 g.translate(2,   5f);
437                 if (xlut_relevant(cell) || scanme()) {
438                     Gate gate = getGate(cell.xlut(), true);
439                     gate.draw(g,
440                               !xknown ? Color.gray : xon ? Color.red : Color.white,
441                               (xon && xknown) ? Color.white : Color.red,
442                               xon ? Color.white : Color.red
443                               );
444                 }
445
446                 g.translate(34f, 0f);
447                 if (cell.ylut_relevant() || scanme()) {
448                     Gate gate = getGate(cell.ylut(), false);
449                     gate.draw(g,
450                               !yknown ? Color.gray : yon ? Color.blue : Color.white,
451                               (yon && yknown) ? Color.white : Color.blue,
452                               yon ? Color.white : Color.blue
453                               );
454                 }
455
456             } finally {
457                 g.setTransform(t);
458             }
459         }
460         public void drawMux() {
461             if (!cell.c_relevant()) return;
462             gg.color(Color.black);
463             if (xlut_relevant(cell) && (cell.c() == ZMUX || cell.c() == XLUT)) {
464                 gg.color(LIGHTRED);
465                 gg.line(XLUT_OUTPUT_POINT, new P(XLUT_OUTPUT_POINT.getX(), 52));
466                 gg.line(new P(XLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
467             }
468             if (cell.ylut_relevant() && (cell.c() == ZMUX || cell.c() == YLUT)) {
469                 gg.color(LIGHTBLUE);
470                 gg.line(YLUT_OUTPUT_POINT, new P(YLUT_OUTPUT_POINT.getX(), 52));
471                 gg.line(new P(YLUT_OUTPUT_POINT.getX(), 52), new P(51, 52));
472             }
473             if (cell.c() == ZMUX)
474                 gg.color(ORANGE);
475
476             gg.line(51, 52, 51, 51+23);
477
478             if (cell.register_relevant() && cell.f() && !cell.b()) {
479                 gg.line(51, 56, 60, 56);
480                 gg.line(60, 56, 60, 51+25);
481             } else {
482                 gg.line(51, 51+23, 51, 51+25);
483             }
484
485             if (cell.c() == ZMUX) {
486
487                 gg.color(GREEN);
488                 int plane = cell.zi()+1;
489                 route(unRotateInner(new P(8 + 2*(plane-L0),  76 + 2*(plane-L0))),
490                       new P(51, 20),
491                       3);
492                 gg.line(new P(51, 20), new P(51, 50));
493
494                 GeneralPath p = new GeneralPath();
495                 p.moveTo(45, 50);
496                 p.lineTo(47, 54);
497                 p.lineTo(56, 54);
498                 p.lineTo(58, 50);
499                 p.lineTo(45, 50);
500                 gg.color(WHITE);
501                 gg.g.fill(p);
502
503                 gg.color(ORANGE);
504                 gg.g.draw(p);
505
506             }
507         }
508         public int ccolor() {
509             switch(cell.c()) {
510                 case XLUT: return LIGHTRED;
511                 case YLUT: return LIGHTBLUE;
512                 case ZMUX: return ORANGE;
513             }
514             return BLACK;
515         }
516         public void drawRegister() {
517             if (!cell.register_relevant()) return;
518
519             int dark = ccolor();
520             gg.color(Color.white);
521             g.fillRect(47, 58, 9, 14);
522             gg.color(dark);
523             g.drawRect(47, 58, 9, 14);
524
525             GeneralPath p = new GeneralPath();
526             p.moveTo(56, 70);
527             p.lineTo(53, 68);
528             p.lineTo(56, 66);
529             p.lineTo(56, 70);
530             gg.color(cell.ff_reset_value() ? WHITE : dark);
531             g.fill(p);
532             gg.color(dark);
533             g.draw(p);
534         }
535         public void drawBorder() {
536             gg.color(Color.gray);
537             gg.line(BEVEL, 0, SIZE-BEVEL, 0);
538             gg.line(SIZE, BEVEL, SIZE, SIZE-BEVEL);
539             gg.line(SIZE-BEVEL, SIZE, BEVEL, SIZE);
540             gg.line(0, SIZE-BEVEL, 0, BEVEL);
541             /*            
542             gg.color(0xdddddd);
543             gg.line(0, BEVEL, BEVEL,    0);
544             gg.line(SIZE-BEVEL, 0, SIZE, BEVEL);
545             gg.line(SIZE, SIZE-BEVEL, SIZE-BEVEL, SIZE);
546             gg.line(BEVEL, SIZE, 0, SIZE-BEVEL);
547             */
548         }
549     }
550
551     public void pressed() {
552         dragFrom = oldcell;
553     }
554     public void released() {
555         if (dragFrom == null || oldcell == null) return;
556         if (Math.abs(dragFrom._y - oldcell._y) > 1) return;
557         if (Math.abs(dragFrom._x - oldcell._x) > 1) return;
558         if (dragFrom._x == oldcell._x   && dragFrom._y == oldcell._y+1) oldcell.cell.yi(NORTH);
559         if (dragFrom._x == oldcell._x   && dragFrom._y == oldcell._y-1) oldcell.cell.yi(SOUTH);
560         if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y)   oldcell.cell.yi(EAST);
561         if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y)   oldcell.cell.yi(WEST);
562         if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NE);
563         if (dragFrom._x == oldcell._x+1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SE);
564         if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y+1) oldcell.cell.xi(NW);
565         if (dragFrom._x == oldcell._x-1 && dragFrom._y == oldcell._y-1) oldcell.cell.xi(SW);
566         repaint();
567     }
568     public void drawKeyboard(Image keyboardImage, Graphics2D g) {
569
570                 int width = 300;
571                 int height = (keyboardImage.getHeight(null) * width) / keyboardImage.getWidth(null);
572                 g.drawImage(keyboardImage,
573                             0, getHeight() - height,
574                             width, getHeight(),
575                             0, 0,
576                             keyboardImage.getWidth(null), keyboardImage.getHeight(null),
577                             null);
578
579     }
580
581     public void _paint(Graphics2D g) {
582
583         this.g = g;
584         this.gg = new G(g);
585         g.setStroke(new BasicStroke((float)0.5));
586
587         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
588         g.setRenderingHint(RenderingHints.KEY_RENDERING,    RenderingHints.VALUE_RENDER_QUALITY);
589
590         AffineTransform t  = g.getTransform();
591         for(Cell c : cells) {
592             g.setTransform(t);
593             g.translate(     c._x * SIZE /*+ (10 * (c._x/4))*/,      c._y * SIZE /*+ (10 * (c._y/4))*/);
594             c.clear();
595         }
596         for(Cell c : cells) {
597             g.setTransform(t);
598             g.translate(     c._x * SIZE /*+ (10 * (c._x/4))*/,      c._y * SIZE /*+ (10 * (c._y/4))*/);
599             c.draw();
600         }
601         g.setTransform(t);
602
603         g.setTransform(new AffineTransform());
604
605         gg.color(selectedcell);
606         g.fillRect(getWidth() - 200, 0, 200, 600);
607         gg.color(Color.white);
608         g.drawRect(getWidth() - 200, 0, 200, 600);
609
610         Cell newcell = whichCell(mousex, mousey);
611         int line = 10;
612         g.setFont(new Font("monospaced", 0, 14));
613         if (newcell != null && newcell.cell != null) {
614             g.drawString("selected: " + newcell._x + "," + newcell._y, getWidth() - 200 + 10, (line += 15));
615             g.drawString("    xlut: " + XLUT_EQUATIONS[newcell.cell.xlut() & 0xff], getWidth() - 200 + 10, (line += 15));
616             g.drawString("    ylut: " + YLUT_EQUATIONS[newcell.cell.ylut() & 0xff], getWidth() - 200 + 10, (line += 15));
617             String xi = "??";
618             switch(newcell.cell.xi()) {
619                 case NW : xi = "NW"; break;
620                 case NE : xi = "NE"; break;
621                 case SW : xi = "SW"; break;
622                 case SE : xi = "SE"; break;
623                 case NONE  : xi = "."; break;
624                 default:  xi = "L"+(newcell.cell.xi()-L0); break;
625             }
626             g.drawString("x-in mux: " + xi, getWidth() - 200 + 10, (line += 15));
627
628             String yi = "??";
629             switch(newcell.cell.yi()) {
630                 case NORTH : yi = "NORTH"; break;
631                 case SOUTH : yi = "SOUTH"; break;
632                 case EAST  : yi = "EAST"; break;
633                 case WEST  : yi = "WEST"; break;
634                 case NONE  : yi = "."; break;
635                 default:     yi = "L"+(newcell.cell.yi()-L0); break;
636             }
637             g.drawString("y-in mux: " + yi, getWidth() - 200 + 10, (line += 15));
638
639             g.drawString("w-in mux: " + (newcell.cell.wi()==NONE ? "." : ("L"+(newcell.cell.wi()-L0))),
640                          getWidth() - 200 + 10, (line += 15));
641             g.drawString("z-in mux: " + (newcell.cell.zi()==NONE ? "." : ("L"+(newcell.cell.zi()-L0))),
642                          getWidth() - 200 + 10, (line += 15));
643
644             String tm = "??";
645             switch(newcell.cell.t()) {
646                 case TMUX_FB:       tm = "fb"; break;
647                 case TMUX_W_AND_FB: tm = "w&fb"; break;
648                 case TMUX_Z:        tm = "z"; break;
649                 case TMUX_W_AND_Z:  tm = "w&z"; break;
650                 case TMUX_W:        tm = "w"; break;
651             }
652             g.drawString("t-in mux: " + tm, getWidth() - 200 + 10, (line += 15));
653
654             g.drawString(" set/rst: " + (newcell.cell.ff_reset_value() ? "reset=SET" : "."),
655                          getWidth() - 200 + 10, (line += 15));
656
657             String outs = "";
658             for(int i=0; i<5; i++) outs += (newcell.cell.out(L0+i) ? (i+" ") : ". ");
659             g.drawString("     out: " + outs,
660                          getWidth() - 200 + 10, (line += 15));
661             String hs = "";
662             for(int i=0; i<5; i++) hs += (newcell.cell.hx(L0+i) ? (i+" ") : ". ");
663             g.drawString("  h conn: " + hs,
664                          getWidth() - 200 + 10, (line += 15));
665             String vs = "";
666             for(int i=0; i<5; i++) vs += (newcell.cell.vx(L0+i) ? (i+" ") : ". ");
667             g.drawString("  v conn: " + vs,
668                          getWidth() - 200 + 10, (line += 15));
669             g.drawString("out enab: " + (newcell.cell.oe()==H4 ? "H4" : newcell.cell.oe()==V4 ? "V4" : "."),
670                          getWidth() - 200 + 10, (line += 15));
671             g.drawString("   c-mux: " + (newcell.cell.c()==ZMUX ? "zmux" : newcell.cell.c()==XLUT ? "x-lut" : "y-lut"),
672                          getWidth() - 200 + 10, (line += 15));
673             g.drawString(" fb src: " + (newcell.cell.f() ? "clocked" : "."),
674                          getWidth() - 200 + 10, (line += 15));
675             g.drawString("  bypass: " + (newcell.cell.b() ? "clocked" : "."),
676                          getWidth() - 200 + 10, (line += 15));
677             g.drawString("   x out: " + (newcell.cell.xo() ? (newcell.cell.b() ? "register" : "center") : "."),
678                          getWidth() - 200 + 10, (line += 15));
679             g.drawString("   y out: " + (newcell.cell.yo() ? (newcell.cell.b() ? "register" : "center") : "."),
680                          getWidth() - 200 + 10, (line += 15));
681
682         }
683         if (shiftkey) {
684             drawKeyboard(keyboard2, g);
685         } else switch(lastChar) {
686             case 'x':
687             case 'y':
688             case 'z':
689             case 'w':
690             case 'o':
691             case 'h':
692             case 'v':
693                 drawKeyboard(keyboard3, g);
694                 break;
695             default:
696                 drawKeyboard(keyboard1, g);
697                 break;
698         }
699
700         while (mousebutton) {
701
702             if (dragFrom == null || oldcell == null) break;
703             if (Math.abs(dragFrom._y - oldcell._y) > 1) break;
704             if (Math.abs(dragFrom._x - oldcell._x) > 1) break;
705             g.setTransform(t);
706             if (dragFrom._x == oldcell._x || dragFrom._y == oldcell._y)
707                 gg.color(BLUE);
708             else
709                 gg.color(RED);
710
711             gg.line( oldcell._x * SIZE + SIZE/2,
712                      oldcell._y * SIZE + SIZE/2,
713                      dragFrom._x * SIZE + SIZE/2,
714                      dragFrom._y * SIZE + SIZE/2, 5);
715             break;
716         }
717
718         this.g = null;
719         this.gg = null;
720     }
721     Cell dragFrom = null;
722
723     public void clear() {
724         Graphics2D g = (Graphics2D)getGraphics();
725         //gg.color(Color.black);
726         //gg.color(Color.lightGray);
727         g.clearRect(0, 0, getWidth(), getHeight());
728     }
729
730     public static final P translate(P p, int dx, int dy) {
731         return new P(p.getX()+dx, p.getY()+dy);
732     }
733
734     public Cell whichCell(int x, int y) {
735         P p = new P(x,y);
736         try {
737             p = p.inverseTransform(transform);
738         } catch (Exception e) {
739             e.printStackTrace();
740         }
741         int col = ((int)p.getX()+0) / SIZE;
742         int row = ((int)p.getY()+0) / SIZE;
743         for(Cell c : cells)
744             if (c._x == col && c._y == row)
745                 return c;
746         return null;
747     }
748
749     public static boolean xlut_relevant(Fpslic.Cell c) {
750         return c.xlut_relevant();
751     }
752
753     public void mouseClicked(MouseEvent e) {
754         final Cell c = whichCell(e.getX(), e.getY());
755         if (c==null) return;
756         scan(c);
757     }
758
759     public void scan() {
760         for(int x=0; x<at40k.getWidth(); x++)
761             for(int y=0; y<at40k.getHeight(); y++)
762                 if (ca[x][y] != null)
763                     if (ca[x][y].scanme())
764                         scan(ca[x][y]);
765     }
766     public void scan(final Gui.Cell c) {
767         try {
768             final Fpslic.Cell cell = c.cell;
769             scan(at40k, cell, NONE, true);
770             boolean safe = !cell.fb_relevant();
771             if (cell.xo()) safe = false;
772             if (cell.yo()) safe = false;
773             for(int i=0; i<5; i++)
774                 if (cell.out(i))
775                     safe = false;
776             if (safe) {
777                 int oldc = cell.c();
778                 if (cell.xlut_relevant()) {
779                     cell.c(XLUT);
780                     drone.readBus(new BCB(c, XLUT));
781                 } else {
782                     c.xknown = false;
783                 }
784                 if (cell.ylut_relevant()) {
785                     cell.c(YLUT);
786                     drone.readBus(new BCB(c, YLUT));
787                 } else {
788                     c.yknown = false;
789                 }
790                 cell.c(oldc);
791             } else {
792                 switch(cell.c()) {
793                     case XLUT:
794                         if (!cell.xlut_relevant()) {
795                             c.xknown = false;
796                         } else {
797                             drone.readBus(new BCB(c, XLUT));
798                         }
799                         /*
800                         if (!cell.yo())
801                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
802                             if (c2!=null && !c2.relevant()) {
803                                 scan(at40k, cell, NONE, false);
804                                 c2.yo(cell);
805                                 c2.c(YLUT);
806                                 scan(at40k, c2, NONE, true);
807                                 drone.readBus(new BCB(c, YLUT));
808                                 scan(at40k, c2, NONE, false);
809                                 c2.yi(NONE);
810                                 return;
811                             }
812                         */
813                         c.yknown = false;
814                         break;
815                     case YLUT:
816                         if (!cell.ylut_relevant()) {
817                             c.yknown = false;
818                         } else {
819                             drone.readBus(new BCB(c, YLUT));
820                         }
821                         /*
822                         if (!cell.xo())
823                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
824                             if (c2!=null && !c2.relevant()) {
825                                 scan(at40k, cell, NONE, false);
826                                 c2.xo(cell);
827                                 scan(at40k, c2, NONE, true);
828                                 c2.c(XLUT);
829                                 drone.readBus(new BCB(c, XLUT));
830                                 scan(at40k, c2, NONE, false);
831                                 c2.xi(NONE);
832                                 return;
833                             }
834                         */
835                         c.xknown = false;
836                         break;
837                     case ZMUX: {
838                         /*
839                         scan(at40k, cell, NONE, false);
840                         c.xknown = false;
841                         c.yknown = false;
842                         if (!cell.xo())
843                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.nw(), cell.sw(), cell.ne(), cell.se() })
844                             if (c2!=null && !c2.relevant()) {
845                                 scan(at40k, cell, NONE, false);
846                                 c2.xo(cell);
847                                 scan(at40k, c2, NONE, true);
848                                 c2.c(XLUT);
849                                 drone.readBus(new BCB(c, XLUT));
850                                 scan(at40k, c2, NONE, false);
851                                 c2.xi(NONE);
852                                 return;
853                             }
854                         if (!cell.yo())
855                         for(Fpslic.Cell c2 : new Fpslic.Cell[] { cell.north(), cell.south(), cell.east(), cell.west() })
856                             if (c2!=null && !c2.relevant()) {
857                                 c2.yo(cell);
858                                 c2.c(YLUT);
859                                 scan(at40k, c2, NONE, true);
860                                 drone.readBus(new BCB(c, YLUT));
861                                 scan(at40k, c2, NONE, false);
862                                 c2.yi(NONE);
863                                 break;
864                             }
865                         return;
866                         */
867                     }
868                 }
869                 
870             }
871             scan(at40k, cell, NONE, false);
872         } catch (IOException e) {
873             throw new RuntimeException(e);
874         }
875     }
876
877     public static void scan(Fpslic dev, Fpslic.Cell cell, int source, boolean setup) {
878         if (setup) {
879             if (source != NONE) cell.c(source);
880             if (cell.b()) cell.b(false);
881             if (cell.f()) cell.f(false);
882         }
883         if (cell.out(L3)!=setup) cell.out(L3, setup);
884         if (cell.vx(L3)!=setup) cell.v(L3, setup);
885
886         Fpslic.SectorWire sw = cell.vwire(L3);
887         //System.out.println("wire is: " + sw);
888
889         if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
890             sw.north().drives(sw, false);
891         while(sw.row > (12 & ~0x3) && sw.south() != null) {
892             //System.out.println(sw + " -> " + sw.south());
893             if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
894             sw = sw.south();
895         }
896         if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
897             sw.north().drives(sw, false);
898         while(sw.row < (12 & ~0x3) && sw.north() != null) {
899             //System.out.println(sw + " -> " + sw.north());
900             if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
901             sw = sw.north();
902         }
903
904         //cell = dev.cell(19, 15);
905         cell = dev.cell(cell.col, 15);
906         /*
907         System.out.println("cell is " + cell);
908         cell.xlut(0xff);
909         cell.ylut(0xff);
910         cell.b(false);
911         cell.f(false);
912         cell.c(XLUT);
913         cell.out(L3, true);
914         cell.oe(NONE);
915         */
916         if (cell.hx(L3) != setup) cell.h(L3, setup);
917         if (cell.vx(L3) != setup) cell.v(L3, setup);
918         sw = cell.hwire(L3);
919
920         if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
921         while(sw.east() != null) {
922             //System.out.println(sw + " -> " + sw.east());
923             if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
924             sw = sw.east();
925         }
926
927     }
928
929
930     int made = 0;
931     private class BCB extends FtdiBoard.ByteCallback {
932         Gui.Cell c;
933         int who;
934         public BCB(Gui.Cell c, int who) {
935             this.who = who; this.c = c;
936             made++;
937         }
938         public void call(byte b) throws Exception {
939             boolean on = (b & 0x80) != 0;
940             switch(who) {
941                 case YLUT:
942                     c.yknown = true;
943                     c.yon = on;
944                     repaint();
945                     break;
946                 case XLUT:
947                     c.xknown = true;
948                     c.xon = on;
949                     repaint();
950                     break;
951             }
952             made--;
953         }
954     }
955
956     public Gate getGate(byte lut, boolean xlut) {
957         for(Gate g : knownGates)
958             if (g.setLut(lut, xlut))
959                 return g;
960         return unknownGate;
961     }
962
963     public Gate unknownGate = new Circle("?");
964     public Gate[] knownGates =
965         new Gate[] {
966             new And(),
967             new Or(),
968             new Circle("0") { public boolean result(boolean x, boolean y, boolean z) { return false; } },
969             new Circle("1") { public boolean result(boolean x, boolean y, boolean z) { return true; } },
970             new Circle("x") { public boolean result(boolean x, boolean y, boolean z) { return x; } },
971             new Circle("y") { public boolean result(boolean x, boolean y, boolean z) { return y; } },
972             new Circle("z") { public boolean result(boolean x, boolean y, boolean z) { return z; } },
973             new Circle("~x") { public boolean result(boolean x, boolean y, boolean z) { return !x; } },
974             new Circle("~y") { public boolean result(boolean x, boolean y, boolean z) { return !y; } },
975             new Circle("~z") { public boolean result(boolean x, boolean y, boolean z) { return !z; } }
976         };
977
978     // FIXME: 2-input gates?
979     public abstract class Gate {
980         public boolean invert_x;
981         public boolean invert_y;
982         public boolean invert_z;
983         public boolean invert_out;
984         public abstract boolean result(boolean x, boolean y, boolean z);
985         public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
986             GeneralPath p = new GeneralPath();
987             makePath(p);
988             g.setColor(fill);
989             g.fill(p);
990             g.setColor(stroke);
991             g.draw(p);
992
993             AffineTransform a = g.getTransform();
994             g.scale(1, -1);
995             if (label() != null) {
996                 g.setColor(text);
997                 g.drawString(label(), 7, -14);
998             }
999             g.setTransform(a);
1000         }
1001         public String label() { return null; }
1002         public boolean setLut(int lut, boolean xlut) {
1003             /*
1004             for(int inverts = 0; inverts < 16; inverts++) {
1005                 invert_x   = (inverts & 0x1) != 0;
1006                 invert_y   = (inverts & 0x2) != 0;
1007                 invert_z   = (inverts & 0x4) != 0;
1008                 invert_out = (inverts & 0x8) != 0;
1009             */
1010                 boolean good = true;
1011                 for(int bit=0; bit<8; bit++) {
1012                     boolean x = xlut ? ((bit & 0x1) != 0) : ((bit & 0x2) != 0);
1013                     boolean y = xlut ? ((bit & 0x2) != 0) : ((bit & 0x1) != 0);
1014                     boolean z = (bit & 0x4) != 0;
1015                     boolean expect = (lut & (1<<bit)) != 0;
1016
1017                     // FIXME symmetry issues here....
1018                     boolean result = result(x ^ invert_x, y ^ invert_y, z ^ invert_z) ^ invert_out;
1019                     if (result != expect) { good = false; break; }
1020                 }
1021                 if (good) return true;
1022                 /*
1023             }
1024                 */
1025             return false;
1026         }
1027         public abstract void makePath(GeneralPath gp);
1028     }
1029
1030     public class Or extends Gate {
1031         public boolean result(boolean x, boolean y, boolean z) { return x || y || z; }
1032         public String label() { return "+"; }
1033         public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1034             AffineTransform at = g.getTransform();
1035             g.scale(1, -1);
1036             g.translate(0, -40);
1037             super.draw(g, fill, stroke, text);
1038             g.setTransform(at);
1039         }
1040         public void makePath(GeneralPath gp) {
1041             gp.moveTo(29.141f, 36.301f);
1042             gp.lineTo(29.141f, 36.301f-7.161f);
1043             gp.curveTo(27.71f, 11.24f, 23.413f, 9.45f, 14.82f, 0.5f);
1044             gp.curveTo(6.229f, 9.45f, 1.932f, 11.24f, 0.5f, 29.141f);
1045             gp.lineTo(0.5f, 29.141f+7.161f);
1046             float x = 0.5f;
1047             float y = 29.141f+7.161f;
1048             gp.curveTo(5.729f+x, -1.789f+y,
1049                        6.444f+x, -2.686f+y,
1050                        14.32f+x, -3.58f+y);
1051             gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1052         }
1053     }
1054
1055     public class Circle extends Gate {
1056         String label;
1057         public Circle(String label) { this.label = label; }
1058         public boolean result(boolean x, boolean y, boolean z) { return false; }
1059         public String label() { return label; }
1060         public void makePath(GeneralPath gp) {
1061             int S = 30;
1062             gp.moveTo(0, S/2);
1063             gp.lineTo(S/2, S);
1064             gp.lineTo(S, S/2);
1065             gp.lineTo(S/2, 0);
1066             gp.lineTo(0, S/2);
1067             gp.closePath();
1068         }
1069     }
1070
1071     public class And extends Gate {
1072         public boolean result(boolean x, boolean y, boolean z) { return x && y && z; }
1073         public String label() { return "&"; }
1074         public void makePath(GeneralPath gp) {
1075             gp.moveTo(0, 2);
1076             gp.lineTo(0, 19);
1077             gp.curveTo(0, 27, 3, 35, 13, 35);
1078             gp.curveTo(20, 35, 23, 27, 23, 19);
1079             gp.lineTo(23, 2);
1080             gp.closePath();
1081         }
1082     }
1083
1084     public class Muller extends And {
1085         public String label() { return "C"; }
1086         public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1087             super.draw(g, fill, stroke, text);
1088             g.setColor(stroke);
1089             g.drawLine(0, 0, 23, 0);
1090         }
1091     }
1092
1093     public class Xor extends Or {
1094         public boolean result(boolean x, boolean y, boolean z) { return x ^ y ^ z; }
1095         public String label() { return "^"; }
1096         public void draw(Graphics2D g, Color fill, Color stroke, Color text) {
1097             super.draw(g, fill, stroke, text);
1098             g.setColor(stroke);
1099             AffineTransform at = g.getTransform();
1100             g.scale(1, -1);
1101             g.translate(0, -40);
1102
1103             g.translate(0, 4);
1104             GeneralPath gp = new GeneralPath();
1105             float x = 0.5f;
1106             float y = 29.141f+7.161f;
1107             gp.moveTo(x,y);
1108             gp.curveTo(5.729f+x, -1.789f+y,
1109                        6.444f+x, -2.686f+y,
1110                        14.32f+x, -3.58f+y);
1111             gp.curveTo(22.697f, 33.616f, 23.413f, 34.512f, 29.141f, 36.301f);
1112             g.draw(gp);
1113
1114             g.setTransform(at);
1115         }
1116     }
1117
1118     public abstract class Mux extends Gate {
1119         public String label() { return "?"; }
1120         public void makePath(GeneralPath gp) {
1121             gp.moveTo(0, 15);
1122             gp.lineTo(2, 23);
1123             gp.lineTo(23, 23);
1124             gp.lineTo(25, 15);
1125             gp.lineTo(0, 15);
1126         }
1127     }
1128
1129     public class X_Mux extends Mux {
1130         public boolean result(boolean x, boolean y, boolean z) { return x ? y : z; }
1131     }
1132     public class Y_Mux extends Mux {
1133         public boolean result(boolean x, boolean y, boolean z) { return y ? x : z; }
1134     }
1135     public class Z_Mux extends Mux {
1136         public boolean result(boolean x, boolean y, boolean z) { return z ? x : y; }
1137     }
1138
1139     public abstract class Buf extends Gate {
1140         public void makePath(GeneralPath gp) {
1141             gp.moveTo(0, 15);
1142             gp.lineTo(13, 35);
1143             gp.lineTo(25, 15);
1144             gp.lineTo(0, 15);
1145         }
1146     }
1147
1148     public class X extends Buf {
1149         public boolean result(boolean x, boolean y, boolean z) { return x; }
1150     }
1151     public class Y extends Buf {
1152         public boolean result(boolean x, boolean y, boolean z) { return y; }
1153     }
1154     public class Z extends Buf {
1155         public boolean result(boolean x, boolean y, boolean z) { return z; }
1156     }
1157
1158     private static Image keyboard1 =
1159         Toolkit.getDefaultToolkit().createImage("images/keyboard1.png");
1160     private static Image keyboard2 =
1161         Toolkit.getDefaultToolkit().createImage("images/keyboard2.png");
1162     private static Image keyboard3 =
1163         Toolkit.getDefaultToolkit().createImage("images/keyboard3.png");
1164
1165 }