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