checkpoint
[slipway.git] / src / edu / berkeley / slipway / DemoVisualizer.java
1 package edu.berkeley.slipway;
2
3 import edu.berkeley.slipway.*;
4 import com.atmel.fpslic.*;
5 import static com.atmel.fpslic.FpslicConstants.*;
6 import static com.atmel.fpslic.FpslicUtil.*;
7 import edu.berkeley.slipway.gui.*;
8 import java.awt.*;
9 import java.awt.event.*;
10 import java.awt.color.*;
11 import org.ibex.util.*;
12 import java.io.*;
13 import java.util.*;
14 import gnu.io.*;
15 import static edu.berkeley.slipway.Demo.*;
16
17 public class DemoVisualizer extends Frame implements KeyListener, MouseMotionListener, MouseListener {
18     public static final int WIDTH = 40;
19     public static final int HEIGHT = 40;
20     public static final int LW = 15;
21     public static final int LH = 15;
22     public static final Color RED  = new Color(0xaa, 0x55, 0x55);
23     public static final Color BLUE = new Color(0x55, 0x55, 0xaa);
24     private final Fpslic dev;
25     private final FtdiBoard drone;
26     int selx = -1;
27     int sely = -1;
28     public DemoVisualizer(final Fpslic dev, final FtdiBoard drone) {
29         this.dev = dev;
30         this.drone = drone;
31         show();
32         addMouseMotionListener(this);
33         addMouseListener(this);
34         addKeyListener(this);
35         new Thread() {
36             public void run() {
37                 try {
38                     while(true) {
39                         Thread.sleep(500);
40                         if (!enabled) continue;
41                         /*
42                           Fpslic.Cell cell = dev.cell(21, 22);
43                           cell.xlut(0xff);
44                           cell.ylut(0xff);
45                         */
46                         keyPressed(null);
47                         /*
48                           cell.xlut(0x00);
49                           cell.ylut(0x00);
50                         */
51                     }
52                 } catch (Exception e) {
53                     e.printStackTrace();
54                 }
55             }
56         }.start();
57     }
58     public boolean enabled = false;
59     public void mouseClicked(MouseEvent e) { }
60     public void mouseEntered(MouseEvent e) { }
61     public void mouseExited(MouseEvent e) { }
62     public void mouseReleased(MouseEvent e) {
63     }
64     public void keyTyped(KeyEvent k) {
65     }
66     public void keyReleased(KeyEvent k) {
67     }
68     public void keyPressed(KeyEvent keyevent) {
69         boolean scan = false;
70         switch(keyevent==null ? '_' : keyevent.getKeyChar()) {
71             case '1': {
72                 if (selx==-1 || sely==-1) break;
73                 Fpslic.Cell cell = dev.cell(selx, sely);
74                 cell.xlut(0xff);
75                 cell.ylut(0xff);
76                 drawCell(getGraphics(), selx, sely);
77                 drone.flush();
78                 break;
79             }
80             case 'i': {
81                 System.out.println("interrupt_count: " + drone.readCount());
82                 break;
83             }
84             case 'x': {
85                 masterx+=2;
86                 if (mullers) {
87                     if (masterx <= 22) {
88                         int mx = masterx;
89                         System.out.println("low => " + mx);
90                         copy(dev.cell(mx, Demo.yofs-2), NORTH, NORTH);
91                         copy(dev.cell(mx, Demo.yofs-3), NORTH, NORTH);
92                         //dev.cell(mx, Demo.yofs-3).ylut(~dev.cell(mx, Demo.yofs-3).ylut());
93                         //dev.cell(mx, Demo.yofs-3).xlut(~dev.cell(mx, Demo.yofs-3).xlut());
94                     } else {
95                         int mx = 23-(masterx-23);
96                         System.out.println("high => " + mx);
97                         copy(dev.cell(mx, Demo.yofs), NW, NW);//NORTH, NORTH);
98                         copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
99                         //for(int x=mx-1; x>=1; x--)
100                         //copy(dev.cell(x, Demo.yofs), EAST, EAST);
101                         for(int y=Demo.yofs+1; y<=23; y++)
102                             copy(dev.cell(1, y), SOUTH, SOUTH);
103                         //dev.cell(mx, Demo.yofs-1).ylut(~dev.cell(mx, Demo.yofs-1).ylut());
104                         //dev.cell(mx, Demo.yofs-1).xlut(~dev.cell(mx, Demo.yofs-1).xlut());
105                     }
106                 } else {
107                     if (masterx <= 22) {
108                         int mx = masterx;
109                         System.out.println("low => " + mx);
110                         copy(dev.cell(mx, Demo.yofs-2), SOUTH, SOUTH);
111                         copy(dev.cell(mx, Demo.yofs-3), NORTH, NORTH);
112                         dev.cell(mx, Demo.yofs-3).ylut(~dev.cell(mx, Demo.yofs-3).ylut());
113                         dev.cell(mx, Demo.yofs-3).xlut(~dev.cell(mx, Demo.yofs-3).xlut());
114                     } else {
115                         int mx = 23-(masterx-23);
116                         System.out.println("high => " + mx);
117                         copy(dev.cell(mx, Demo.yofs), SOUTH, SOUTH);
118                         /*
119                           copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
120                         */
121                         copy(dev.cell(mx, Demo.yofs-1), NORTH, SW);
122                         boolean left = true;
123                         Fpslic.Cell lc = null;
124                         for(int k=0; k<10; k++) {
125                             int y = Demo.yofs-2-(k*2);
126                             copy(dev.cell(left?(mx-1):mx, y),        SOUTH, left?NE:NW);
127                             copy(lc = dev.cell(left?(mx-1):mx, y-1), NORTH, left?SE:SW); 
128                             left = !left;
129                         }
130                         copy(lc, NORTH, NORTH);
131
132                         //for(int x=mx-1; x>=1; x--)
133                         //copy(dev.cell(x, Demo.yofs), EAST, EAST);
134                         //for(int y=Demo.yofs+1; y<=23; y++)
135                         //copy(dev.cell(1, y), SOUTH, SOUTH);
136
137                         if (mx<21) {
138                             dev.cell(mx+2, Demo.yofs).ylut(0x00);
139                             dev.cell(mx+2, Demo.yofs).xlut(0x00);
140                         }
141
142                         /*
143                           dev.cell(mx, Demo.yofs-1).ylut(~LUT_Z);
144                           dev.cell(mx, Demo.yofs-1).xlut(LUT_Z);
145                           loopback(dev.cell(mx, Demo.yofs-1), YLUT);
146                         */
147                         dev.cell(mx, Demo.yofs).ylut(~LUT_SELF);
148                         dev.cell(mx, Demo.yofs).xlut(~LUT_OTHER);
149                     }
150                 }
151                 break;
152             }
153             case ' ': {
154                 //enabled = !enabled;
155                 scan = true;
156                 break;
157             }
158             case '4': {
159                 //enabled = !enabled;
160                 try {
161                     for(int cap=0; cap<15; cap++) {
162                         drain(dev, drone);
163                         try { Thread.sleep(100); } catch (Exception e) { }
164                         //showit(dev, drone, this);
165                         fill(dev, drone, cap);
166                         drone.readCount();
167                         long now = System.currentTimeMillis();
168                         try { Thread.sleep(4000); } catch (Exception e) { }
169                         int count = drone.readCount();
170                         long now2 = System.currentTimeMillis();
171                         System.out.println(cap + " ,  " + (((float)count * (2*2*2*2*2*2*2*2*2*1000))/(now2-now)));
172                     }
173                 } catch (Exception e) { e.printStackTrace(); }
174                 break;
175             }
176             case 'C': {
177                 if (selx==-1 || sely==-1) break;
178                 Fpslic.Cell cell = dev.cell(selx, sely);
179                 cell.ylut(0xB2);
180                 drawCell(getGraphics(), selx, sely);
181                 break;
182             }
183             case '0': {
184                 if (selx==-1 || sely==-1) break;
185                 Fpslic.Cell cell = dev.cell(selx, sely);
186                 cell.xlut(0x00);
187                 cell.ylut(0x00);
188                 drawCell(getGraphics(), selx, sely);
189                 drone.flush();
190                 break;
191             }
192         } 
193         if (!scan) return;
194         showit(dev, drone, this);
195     }
196     public void mousePressed(MouseEvent e) {
197         final Fpslic.Cell cell = dev.cell(selx, sely);
198         if (cell==null) return;
199         final int old = cell.c();
200         FtdiBoard.ByteCallback bc = new FtdiBoard.ByteCallback() {
201                 public void call(byte b) throws Exception {
202                     boolean y = (b & 0x80) != 0;
203                     //cell.c(old);
204                     Graphics g = getGraphics();
205                     g.setFont(new Font("sansserif", Font.BOLD, 14));
206                     g.setColor(Color.white);
207                     //g.drawString("X=0", left(cell) + 10, top(cell) + 20);
208                     //g.drawString("X=1", left(cell) + 10, top(cell) + 20);
209                     //g.setColor(Color.white);
210                     //g.drawString("Y=0", left(cell) + 8, top(cell) + 35);
211                     //g.drawString("Y=1", left(cell) + 8, top(cell) + 35);
212                     //g.setColor(RED);
213                     //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
214                     String v = (cell.c()==YLUT ? "Y" : cell.c()==XLUT ? "X" : "C");
215                     g.drawString(v+"="+(y?"0":"1"), left(cell) + 8, top(cell) + 35);
216                     g.setColor(BLUE);
217                     g.drawString(v+"="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
218                 } };
219             /*
220         try {
221             scan(dev, cell, NONE, true);
222             drone.readBus(bc);
223             //scan(dev, cell, XLUT, true);
224             //boolean x = (drone.readBus() & 0x80) != 0;
225             scan(dev, cell, NONE, false);
226         } catch (IOException ex) {
227             throw new RuntimeException(ex);
228         }
229             */
230     }
231
232     public void mouseMoved(MouseEvent e) {
233         int x = e.getX();
234         int y = e.getY();
235         if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
236             int cx = selx;
237             int cy = sely;
238             Fpslic.Cell cell = dev.cell(cx, cy);
239             selx = -1;
240             sely = -1;
241             /*
242               drawCell(getGraphics(), cx, cy);
243               drawSector(getGraphics(), dev.cell(cx, cy).sector());
244             */
245         }
246         selx = (x-20)/(WIDTH+2);
247         sely = (23 - (y-20)/(HEIGHT+2))+1;
248         /*
249           Fpslic.Cell cell = dev.cell(selx, sely);
250           if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
251           drawCell(getGraphics(), selx, sely);
252           drawSector(getGraphics(), dev.cell(selx, sely).sector());
253           }
254         */
255     }
256     public void mouseDragged(MouseEvent e) { mousePressed(e); }
257     public void paint(Graphics g) {
258         System.out.println("paintall");
259         g.setColor(Color.white);
260         g.fillRect(0, 0, getWidth(), getHeight());
261         g.setFont(new Font("sansserif", Font.BOLD, 24));
262         for(int x=0; x<24; x++)
263             for(int y=0; y<24; y++)
264                 drawCell(g,x,y);
265         for(int x=0; x<=23; x+=4)
266             for(int y=23; y>=0; y-=4) 
267                 drawSector(g, dev.cell(x, y).sector());
268         /*
269           g.setColor(BLUE);
270           g.drawString("Ready", (5*(WIDTH+2))+20, 40);
271           g.setColor(RED);
272           g.drawString("Send",  (3*(WIDTH+2))+20, 40);
273           g.setColor(BLUE);
274         */
275         refresh();
276     }
277     public void refresh() {
278         Graphics g = getGraphics();
279         /*
280           int data = drone.readBus() & 0xff;
281           for(int i=0; i<8; i++) {
282           g.setColor((data & (1<<i))==0 ? Color.black : Color.green);
283           g.drawString("D"+i,  (24*(WIDTH+2))+20, ((23-(i+7))*(HEIGHT+2))+60-HEIGHT/2);
284           }
285         */
286     }
287     public static int left(Fpslic.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
288     public static int top(Fpslic.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
289     public void drawSector(Graphics g, Fpslic.Sector sector) {
290         g.setColor(Color.gray);
291         ((Graphics2D)g).setStroke(new BasicStroke(1));
292         int px = ((sector.col)*(WIDTH+2))+20-1;
293         int py = ((23-(sector.row+3))*(HEIGHT+2))+60-1;
294         g.drawRect(px, py, (WIDTH+2)*4+2, (HEIGHT+2)*4+2);
295         /*
296           for(int dir=0; dir<2; dir++) {
297           boolean h = dir==0;
298           for(int y=h?sector.row:sector.col; y<(h?sector.row+4:sector.col+4); y++)
299           for(int plane=0; plane<=4; plane++) {
300           Fpslic.Cell cell      = h ? dev.cell(sector.col,   y) : dev.cell(y, sector.row);
301           Fpslic.Cell cell_east = h ? dev.cell(sector.col-1, y) : dev.cell(y, sector.row-1);
302           Fpslic.Cell cell_west = h ? dev.cell(sector.col+4, y) : dev.cell(y, sector.row+4);
303           boolean draw = false;
304           if (h) {
305           if (cell_east!=null &&
306           (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
307           cell_east.hwire(plane).drives(cell.hwire(plane))))
308           draw = true;
309           if (cell_west!=null &&
310           (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
311           cell_west.hwire(plane).drives(cell.hwire(plane))))
312           draw = true;
313           } else {
314           if (cell_east!=null &&
315           (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
316           cell_east.vwire(plane).drives(cell.vwire(plane))))
317           draw = true;
318           if (cell_west!=null &&
319           (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
320           cell_west.vwire(plane).drives(cell.vwire(plane))))
321           draw = true;
322           }
323           if (!draw)
324           for(int x=h?sector.col:sector.row; x<(h?sector.col+4:sector.row+4); x++)
325           if (((h ? dev.cell(x,y).hx(plane) : dev.cell(y,x).vx(plane))) ||
326           (h?dev.cell(x,y).out(plane):dev.cell(y,x).out(plane)))
327           draw = true;
328           if (draw) {
329           g.setColor(new Color(0xff, 0x00, 0xff));
330           if (h) {
331           g.drawLine(left(cell),
332           top(cell)+3,
333           left(cell) + 4*(WIDTH+2),
334           top(cell)+3
335           );
336           } else {
337           g.drawLine(left(cell)+3,
338           top(cell) + (HEIGHT+2),
339           left(cell)+3,
340           top(cell) - 3*(HEIGHT+2)
341           );
342           }
343           }
344           }
345           }
346         */
347     }
348     public void drawCell(Graphics g, int cx, int cy) {
349         int x = (cx*(WIDTH+2))+20;
350         int y = ((23-cy)*(HEIGHT+2))+60;
351         if (g.getClipBounds() != null && !g.getClipBounds().intersects(new Rectangle(x, y, x+WIDTH, y+HEIGHT))) return;
352         drawCell(g, cx, cy, Color.white);
353     }
354     public void drawCell(Graphics g, int cx, int cy, Color bg) {
355         int x = (cx*(WIDTH+2))+20;
356         int y = ((23-cy)*(HEIGHT+2))+60;
357
358         //System.out.println("drawcell " + cx + "," + cy);
359         Fpslic.Cell cell = dev.cell(cx, cy);
360         g.setColor(bg);
361         g.fillRect(x, y, WIDTH, HEIGHT);
362
363         g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
364         g.drawRect(x, y, WIDTH, HEIGHT);
365
366         //g.setColor((selx==cx && sely==cy) ? Color.red : Color.gray);
367         //g.drawRect(x+(WIDTH-(LW*2))/2-1,    y+(HEIGHT-LW)/2-1, LW*2+1, LH+1);
368
369         //g.setColor(RED);
370         //g.fillRect(x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2, LW,   LH);
371         //g.setColor(Color.white);
372         //g.drawString("1", x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2);
373
374         //g.setColor(BLUE);
375         //g.fillRect(x+(WIDTH-(LW*2))/2+LW, y+(HEIGHT-LW)/2, LW,   LH);
376         //g.setColor(Color.white);
377         //g.drawString("0", x+(WIDTH-(LW*2))/2+LW,    y+(HEIGHT-LW)/2);
378
379         /*
380           g.setColor(BLUE);
381           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
382           switch(cell.yi()) {
383           case NORTH: g.drawLine(x+WIDTH/2+5,  y-10,        x+WIDTH/2+5, y+HEIGHT/2); break;
384           case SOUTH: g.drawLine(x+WIDTH/2-5,  y+HEIGHT+10, x+WIDTH/2-5, y+HEIGHT/2); break;
385           case EAST:  g.drawLine(x+WIDTH+10, y+HEIGHT/2+5,  x+WIDTH/2, y+HEIGHT/2+5); break;
386           case WEST:  g.drawLine(x-10,       y+HEIGHT/2-5,  x+WIDTH/2, y+HEIGHT/2-5); break;
387           case NONE:  break;
388           }
389           g.setColor(RED);
390           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
391           switch(cell.xi()) {
392           case NW: g.drawLine(x-10+3,       y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
393           case SW: g.drawLine(x-10-3,       y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
394           case NE: g.drawLine(x+WIDTH+10+3, y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
395           case SE: g.drawLine(x+WIDTH+10-3, y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
396           case NONE:  break;
397           }
398           ((Graphics2D)g).setStroke(new BasicStroke(1));
399         */
400         /*
401           if (selx==cx && sely==cy) {
402           int xp = 23 * (WIDTH+2) + 100;
403           int yp = 100;
404           g.setColor(Color.white);
405           g.fillRect(xp, yp, 300, 1000);
406           g.setColor(Color.black);
407           g.drawString("Cell " + cx + "," + cy,       xp, (yp+=15));
408           //g.drawString("X-Lut: " + bin8(cell.xlut()), xp, (yp+=15));
409           g.drawString("X-Lut: " + cell.printXLut(), xp, (yp+=15));
410           //g.drawString("Y-Lut: " + bin8(cell.ylut()), xp, (yp+=15));
411           g.drawString("Y-Lut: " + cell.printYLutX(), xp, (yp+=15));
412           }
413         */
414         if ((cell.ylut()&0xff)==0xff && (cell.xlut()&0xff)==0xff) {
415             g.setColor(new Color(0x00, 0x00, 0xff));
416             g.drawString("1", left(cell) + 12, top(cell) + 30);
417         }
418         if ((cell.ylut()&0xff)==0x00 && (cell.xlut()&0xff)==0x00) {
419             g.setColor(new Color(0x00, 0x00, 0xff));
420             g.drawString("0", left(cell) + 12, top(cell) + 30);
421         }
422         if ((cell.ylut()&0xff)==0xB2) {
423             //System.out.println("muller @ " + cell);
424             //g.setColor(RED);
425             //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
426             g.setColor(new Color(0x00, 0xaa, 0x00));
427             g.drawString("C", left(cell) + 12, top(cell) + 30);
428         }
429
430     }
431
432     public static void showit(Fpslic dev, FtdiBoard drone, final DemoVisualizer vis) {
433         final long then = System.currentTimeMillis();
434         final Graphics g = vis.getGraphics();
435         g.setFont(new Font("sansserif", Font.BOLD, 24));
436         final Color red = new Color(0xff, 0x99, 0x99);
437         final Color green = new Color(0x99, 0xff, 0x99);
438         for(int xx=0; xx<=22; xx++) {
439             for(int yy=23; yy>=0; yy--) {
440                 //for(int xx=5; xx<=PIPELEN-1; xx++) {
441                 //for(int yy=21; yy<=22; yy++) {
442                 final int x = xx;
443                 final int y = yy;
444                 final Fpslic.Cell cell = dev.cell(x, y);
445                 if ((cell.ylut()&0xff)!=0xB2) continue;
446                 FtdiBoard.ByteCallback bc = new FtdiBoard.ByteCallback() {
447                         public void call(byte b) throws Exception {
448                             boolean v = (b & 0x80) != 0;
449                             vis.drawCell(g, x, y, v?red:green);
450                             //if (x==PIPELEN-1 && y==22) System.out.println("time: " + (System.currentTimeMillis()-then));
451                         }
452                     };
453                 /*
454                 scan(dev, cell, NONE, true);
455                 try {
456                     drone.readBus(bc);
457                     //scan(dev, cell, YLUT, false);
458                     cell.v(L3, false);
459                     dev.cell(x, 15).h(L3, false);
460                     dev.cell(x, 15).v(L3, false);
461                 } catch (IOException ex) {
462                     throw new RuntimeException(ex);
463                 }
464                 */
465             }
466         }
467     }
468
469 }