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