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         try {
220             scan(dev, cell, NONE, true);
221             drone.readBus(bc);
222             //scan(dev, cell, XLUT, true);
223             //boolean x = (drone.readBus() & 0x80) != 0;
224             scan(dev, cell, NONE, false);
225         } catch (IOException ex) {
226             throw new RuntimeException(ex);
227         }
228     }
229
230     public void mouseMoved(MouseEvent e) {
231         int x = e.getX();
232         int y = e.getY();
233         if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
234             int cx = selx;
235             int cy = sely;
236             Fpslic.Cell cell = dev.cell(cx, cy);
237             selx = -1;
238             sely = -1;
239             /*
240               drawCell(getGraphics(), cx, cy);
241               drawSector(getGraphics(), dev.cell(cx, cy).sector());
242             */
243         }
244         selx = (x-20)/(WIDTH+2);
245         sely = (23 - (y-20)/(HEIGHT+2))+1;
246         /*
247           Fpslic.Cell cell = dev.cell(selx, sely);
248           if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
249           drawCell(getGraphics(), selx, sely);
250           drawSector(getGraphics(), dev.cell(selx, sely).sector());
251           }
252         */
253     }
254     public void mouseDragged(MouseEvent e) { mousePressed(e); }
255     public void paint(Graphics g) {
256         System.out.println("paintall");
257         g.setColor(Color.white);
258         g.fillRect(0, 0, getWidth(), getHeight());
259         g.setFont(new Font("sansserif", Font.BOLD, 24));
260         for(int x=0; x<24; x++)
261             for(int y=0; y<24; y++)
262                 drawCell(g,x,y);
263         for(int x=0; x<=23; x+=4)
264             for(int y=23; y>=0; y-=4) 
265                 drawSector(g, dev.cell(x, y).sector());
266         /*
267           g.setColor(BLUE);
268           g.drawString("Ready", (5*(WIDTH+2))+20, 40);
269           g.setColor(RED);
270           g.drawString("Send",  (3*(WIDTH+2))+20, 40);
271           g.setColor(BLUE);
272         */
273         refresh();
274     }
275     public void refresh() {
276         Graphics g = getGraphics();
277         /*
278           int data = drone.readBus() & 0xff;
279           for(int i=0; i<8; i++) {
280           g.setColor((data & (1<<i))==0 ? Color.black : Color.green);
281           g.drawString("D"+i,  (24*(WIDTH+2))+20, ((23-(i+7))*(HEIGHT+2))+60-HEIGHT/2);
282           }
283         */
284     }
285     public static int left(Fpslic.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
286     public static int top(Fpslic.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
287     public void drawSector(Graphics g, Fpslic.Sector sector) {
288         g.setColor(Color.gray);
289         ((Graphics2D)g).setStroke(new BasicStroke(1));
290         int px = ((sector.col)*(WIDTH+2))+20-1;
291         int py = ((23-(sector.row+3))*(HEIGHT+2))+60-1;
292         g.drawRect(px, py, (WIDTH+2)*4+2, (HEIGHT+2)*4+2);
293         /*
294           for(int dir=0; dir<2; dir++) {
295           boolean h = dir==0;
296           for(int y=h?sector.row:sector.col; y<(h?sector.row+4:sector.col+4); y++)
297           for(int plane=0; plane<=4; plane++) {
298           Fpslic.Cell cell      = h ? dev.cell(sector.col,   y) : dev.cell(y, sector.row);
299           Fpslic.Cell cell_east = h ? dev.cell(sector.col-1, y) : dev.cell(y, sector.row-1);
300           Fpslic.Cell cell_west = h ? dev.cell(sector.col+4, y) : dev.cell(y, sector.row+4);
301           boolean draw = false;
302           if (h) {
303           if (cell_east!=null &&
304           (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
305           cell_east.hwire(plane).drives(cell.hwire(plane))))
306           draw = true;
307           if (cell_west!=null &&
308           (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
309           cell_west.hwire(plane).drives(cell.hwire(plane))))
310           draw = true;
311           } else {
312           if (cell_east!=null &&
313           (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
314           cell_east.vwire(plane).drives(cell.vwire(plane))))
315           draw = true;
316           if (cell_west!=null &&
317           (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
318           cell_west.vwire(plane).drives(cell.vwire(plane))))
319           draw = true;
320           }
321           if (!draw)
322           for(int x=h?sector.col:sector.row; x<(h?sector.col+4:sector.row+4); x++)
323           if (((h ? dev.cell(x,y).hx(plane) : dev.cell(y,x).vx(plane))) ||
324           (h?dev.cell(x,y).out(plane):dev.cell(y,x).out(plane)))
325           draw = true;
326           if (draw) {
327           g.setColor(new Color(0xff, 0x00, 0xff));
328           if (h) {
329           g.drawLine(left(cell),
330           top(cell)+3,
331           left(cell) + 4*(WIDTH+2),
332           top(cell)+3
333           );
334           } else {
335           g.drawLine(left(cell)+3,
336           top(cell) + (HEIGHT+2),
337           left(cell)+3,
338           top(cell) - 3*(HEIGHT+2)
339           );
340           }
341           }
342           }
343           }
344         */
345     }
346     public void drawCell(Graphics g, int cx, int cy) {
347         int x = (cx*(WIDTH+2))+20;
348         int y = ((23-cy)*(HEIGHT+2))+60;
349         if (g.getClipBounds() != null && !g.getClipBounds().intersects(new Rectangle(x, y, x+WIDTH, y+HEIGHT))) return;
350         drawCell(g, cx, cy, Color.white);
351     }
352     public void drawCell(Graphics g, int cx, int cy, Color bg) {
353         int x = (cx*(WIDTH+2))+20;
354         int y = ((23-cy)*(HEIGHT+2))+60;
355
356         //System.out.println("drawcell " + cx + "," + cy);
357         Fpslic.Cell cell = dev.cell(cx, cy);
358         g.setColor(bg);
359         g.fillRect(x, y, WIDTH, HEIGHT);
360
361         g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
362         g.drawRect(x, y, WIDTH, HEIGHT);
363
364         //g.setColor((selx==cx && sely==cy) ? Color.red : Color.gray);
365         //g.drawRect(x+(WIDTH-(LW*2))/2-1,    y+(HEIGHT-LW)/2-1, LW*2+1, LH+1);
366
367         //g.setColor(RED);
368         //g.fillRect(x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2, LW,   LH);
369         //g.setColor(Color.white);
370         //g.drawString("1", x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2);
371
372         //g.setColor(BLUE);
373         //g.fillRect(x+(WIDTH-(LW*2))/2+LW, y+(HEIGHT-LW)/2, LW,   LH);
374         //g.setColor(Color.white);
375         //g.drawString("0", x+(WIDTH-(LW*2))/2+LW,    y+(HEIGHT-LW)/2);
376
377         /*
378           g.setColor(BLUE);
379           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
380           switch(cell.yi()) {
381           case NORTH: g.drawLine(x+WIDTH/2+5,  y-10,        x+WIDTH/2+5, y+HEIGHT/2); break;
382           case SOUTH: g.drawLine(x+WIDTH/2-5,  y+HEIGHT+10, x+WIDTH/2-5, y+HEIGHT/2); break;
383           case EAST:  g.drawLine(x+WIDTH+10, y+HEIGHT/2+5,  x+WIDTH/2, y+HEIGHT/2+5); break;
384           case WEST:  g.drawLine(x-10,       y+HEIGHT/2-5,  x+WIDTH/2, y+HEIGHT/2-5); break;
385           case NONE:  break;
386           }
387           g.setColor(RED);
388           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
389           switch(cell.xi()) {
390           case NW: g.drawLine(x-10+3,       y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
391           case SW: g.drawLine(x-10-3,       y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
392           case NE: g.drawLine(x+WIDTH+10+3, y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
393           case SE: g.drawLine(x+WIDTH+10-3, y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
394           case NONE:  break;
395           }
396           ((Graphics2D)g).setStroke(new BasicStroke(1));
397         */
398         /*
399           if (selx==cx && sely==cy) {
400           int xp = 23 * (WIDTH+2) + 100;
401           int yp = 100;
402           g.setColor(Color.white);
403           g.fillRect(xp, yp, 300, 1000);
404           g.setColor(Color.black);
405           g.drawString("Cell " + cx + "," + cy,       xp, (yp+=15));
406           //g.drawString("X-Lut: " + bin8(cell.xlut()), xp, (yp+=15));
407           g.drawString("X-Lut: " + cell.printXLut(), xp, (yp+=15));
408           //g.drawString("Y-Lut: " + bin8(cell.ylut()), xp, (yp+=15));
409           g.drawString("Y-Lut: " + cell.printYLutX(), xp, (yp+=15));
410           }
411         */
412         if ((cell.ylut()&0xff)==0xff && (cell.xlut()&0xff)==0xff) {
413             g.setColor(new Color(0x00, 0x00, 0xff));
414             g.drawString("1", left(cell) + 12, top(cell) + 30);
415         }
416         if ((cell.ylut()&0xff)==0x00 && (cell.xlut()&0xff)==0x00) {
417             g.setColor(new Color(0x00, 0x00, 0xff));
418             g.drawString("0", left(cell) + 12, top(cell) + 30);
419         }
420         if ((cell.ylut()&0xff)==0xB2) {
421             //System.out.println("muller @ " + cell);
422             //g.setColor(RED);
423             //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
424             g.setColor(new Color(0x00, 0xaa, 0x00));
425             g.drawString("C", left(cell) + 12, top(cell) + 30);
426         }
427
428     }
429
430     public static void showit(Fpslic dev, FtdiBoard drone, final DemoVisualizer vis) {
431         final long then = System.currentTimeMillis();
432         final Graphics g = vis.getGraphics();
433         g.setFont(new Font("sansserif", Font.BOLD, 24));
434         final Color red = new Color(0xff, 0x99, 0x99);
435         final Color green = new Color(0x99, 0xff, 0x99);
436         for(int xx=0; xx<=22; xx++) {
437             for(int yy=23; yy>=0; yy--) {
438                 //for(int xx=5; xx<=PIPELEN-1; xx++) {
439                 //for(int yy=21; yy<=22; yy++) {
440                 final int x = xx;
441                 final int y = yy;
442                 final Fpslic.Cell cell = dev.cell(x, y);
443                 if ((cell.ylut()&0xff)!=0xB2) continue;
444                 FtdiBoard.ByteCallback bc = new FtdiBoard.ByteCallback() {
445                         public void call(byte b) throws Exception {
446                             boolean v = (b & 0x80) != 0;
447                             vis.drawCell(g, x, y, v?red:green);
448                             //if (x==PIPELEN-1 && y==22) System.out.println("time: " + (System.currentTimeMillis()-then));
449                         }
450                     };
451                 scan(dev, cell, NONE, true);
452                 try {
453                     drone.readBus(bc);
454                     //scan(dev, cell, YLUT, false);
455                     cell.v(L3, false);
456                     dev.cell(x, 15).h(L3, false);
457                     dev.cell(x, 15).v(L3, false);
458                 } catch (IOException ex) {
459                     throw new RuntimeException(ex);
460                 }
461             }
462         }
463     }
464
465 }