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