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