revamp demos
[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 import static edu.berkeley.slipway.demos.ExperimentUtils.*;
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 FpslicDevice dev;
25     private final SlipwayBoard board;
26     int selx = -1;
27     int sely = -1;
28     public DemoVisualizer(final FpslicDevice dev, final SlipwayBoard board) {
29         this.dev = dev;
30         this.board = board;
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     public void keyTyped(KeyEvent k) { }
64     public void keyReleased(KeyEvent k) { }
65     public void keyPressed(KeyEvent keyevent) {
66         boolean scan = false;
67         switch(keyevent==null ? '_' : keyevent.getKeyChar()) {
68             case 'C': {
69                 if (selx==-1 || sely==-1) break;
70                 FpslicDevice.Cell cell = dev.cell(selx, sely);
71                 cell.ylut(0xB2);
72                 drawCell(getGraphics(), selx, sely);
73                 break;
74             }
75             case '0': {
76                 if (selx==-1 || sely==-1) break;
77                 FpslicDevice.Cell cell = dev.cell(selx, sely);
78                 cell.xlut(0x00);
79                 cell.ylut(0x00);
80                 drawCell(getGraphics(), selx, sely);
81                 dev.flush();
82                 break;
83             }
84             case '1': {
85                 if (selx==-1 || sely==-1) break;
86                 FpslicDevice.Cell cell = dev.cell(selx, sely);
87                 cell.xlut(0xff);
88                 cell.ylut(0xff);
89                 drawCell(getGraphics(), selx, sely);
90                 dev.flush();
91                 break;
92             }
93             case 'i': {
94                 System.out.println("interrupt_count: " + board.readInterruptCount());
95                 break;
96             }
97             case 'x': {
98                 masterx+=2;
99                 if (mullers) {
100                     if (masterx <= 22) {
101                         int mx = masterx;
102                         System.out.println("low => " + mx);
103                         copy(dev.cell(mx, Demo.yofs-2), NORTH, NORTH);
104                         copy(dev.cell(mx, Demo.yofs-3), NORTH, NORTH);
105                         //dev.cell(mx, Demo.yofs-3).ylut(~dev.cell(mx, Demo.yofs-3).ylut());
106                         //dev.cell(mx, Demo.yofs-3).xlut(~dev.cell(mx, Demo.yofs-3).xlut());
107                     } else {
108                         int mx = 23-(masterx-23);
109                         System.out.println("high => " + mx);
110                         copy(dev.cell(mx, Demo.yofs), NW, NW);//NORTH, NORTH);
111                         copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
112                         //for(int x=mx-1; x>=1; x--)
113                         //copy(dev.cell(x, Demo.yofs), EAST, EAST);
114                         for(int y=Demo.yofs+1; y<=23; y++)
115                             copy(dev.cell(1, y), SOUTH, SOUTH);
116                         //dev.cell(mx, Demo.yofs-1).ylut(~dev.cell(mx, Demo.yofs-1).ylut());
117                         //dev.cell(mx, Demo.yofs-1).xlut(~dev.cell(mx, Demo.yofs-1).xlut());
118                     }
119                 } else {
120                     if (masterx <= 22) {
121                         int mx = masterx;
122                         System.out.println("low => " + mx);
123                         copy(dev.cell(mx, Demo.yofs-2), SOUTH, SOUTH);
124                         copy(dev.cell(mx, Demo.yofs-3), NORTH, NORTH);
125                         dev.cell(mx, Demo.yofs-3).ylut(~dev.cell(mx, Demo.yofs-3).ylut());
126                         dev.cell(mx, Demo.yofs-3).xlut(~dev.cell(mx, Demo.yofs-3).xlut());
127                     } else {
128                         int mx = 23-(masterx-23);
129                         System.out.println("high => " + mx);
130                         copy(dev.cell(mx, Demo.yofs), SOUTH, SOUTH);
131                         /*
132                           copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
133                         */
134                         copy(dev.cell(mx, Demo.yofs-1), NORTH, SW);
135                         boolean left = true;
136                         FpslicDevice.Cell lc = null;
137                         for(int k=0; k<10; k++) {
138                             int y = Demo.yofs-2-(k*2);
139                             copy(dev.cell(left?(mx-1):mx, y),        SOUTH, left?NE:NW);
140                             copy(lc = dev.cell(left?(mx-1):mx, y-1), NORTH, left?SE:SW); 
141                             left = !left;
142                         }
143                         copy(lc, NORTH, NORTH);
144
145                         //for(int x=mx-1; x>=1; x--)
146                         //copy(dev.cell(x, Demo.yofs), EAST, EAST);
147                         //for(int y=Demo.yofs+1; y<=23; y++)
148                         //copy(dev.cell(1, y), SOUTH, SOUTH);
149
150                         if (mx<21) {
151                             dev.cell(mx+2, Demo.yofs).ylut(0x00);
152                             dev.cell(mx+2, Demo.yofs).xlut(0x00);
153                         }
154
155                         /*
156                           dev.cell(mx, Demo.yofs-1).ylut(~LUT_Z);
157                           dev.cell(mx, Demo.yofs-1).xlut(LUT_Z);
158                           loopback(dev.cell(mx, Demo.yofs-1), YLUT);
159                         */
160                         dev.cell(mx, Demo.yofs).ylut(~LUT_SELF);
161                         dev.cell(mx, Demo.yofs).xlut(~LUT_OTHER);
162                     }
163                 }
164                 break;
165             }
166             case ' ': {
167                 //enabled = !enabled;
168                 scan = true;
169                 break;
170             }
171             case '4': {
172                 //enabled = !enabled;
173                 try {
174                     for(int cap=0; cap<15; cap++) {
175                         drain(dev, board);
176                         try { Thread.sleep(100); } catch (Exception e) { }
177                         //showit(dev, board, this);
178                         fill(dev, board, cap);
179                         board.readInterruptCount();
180                         long now = System.currentTimeMillis();
181                         try { Thread.sleep(4000); } catch (Exception e) { }
182                         int count = board.readInterruptCount();
183                         long now2 = System.currentTimeMillis();
184                         System.out.println(cap + " ,  " + (((float)count * (2*2*2*2*2*2*2*2*2*1000))/(now2-now)));
185                     }
186                 } catch (Exception e) { e.printStackTrace(); }
187                 break;
188             }
189         } 
190         if (!scan) return;
191         showit(dev, board, this);
192     }
193     public void mousePressed(MouseEvent e) { }
194     public void mouseMoved(MouseEvent e) {
195         int x = e.getX();
196         int y = e.getY();
197         if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
198             int cx = selx;
199             int cy = sely;
200             FpslicDevice.Cell cell = dev.cell(cx, cy);
201             selx = -1;
202             sely = -1;
203         }
204         selx = (x-20)/(WIDTH+2);
205         sely = (23 - (y-20)/(HEIGHT+2))+1;
206     }
207     public void mouseDragged(MouseEvent e) { mousePressed(e); }
208     public void paint(Graphics g) {
209         System.out.println("paintall");
210         g.setColor(Color.white);
211         g.fillRect(0, 0, getWidth(), getHeight());
212         g.setFont(new Font("sansserif", Font.BOLD, 24));
213         for(int x=0; x<24; x++)
214             for(int y=0; y<24; y++)
215                 drawCell(g,x,y);
216         for(int x=0; x<=23; x+=4)
217             for(int y=23; y>=0; y-=4) 
218                 drawSector(g, dev.cell(x, y).sector());
219         refresh();
220     }
221     public void refresh() { }
222     public static int left(FpslicDevice.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
223     public static int top(FpslicDevice.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
224     public void drawSector(Graphics g, FpslicDevice.Sector sector) {
225         g.setColor(Color.gray);
226         ((Graphics2D)g).setStroke(new BasicStroke(1));
227         int px = ((sector.col)*(WIDTH+2))+20-1;
228         int py = ((23-(sector.row+3))*(HEIGHT+2))+60-1;
229         g.drawRect(px, py, (WIDTH+2)*4+2, (HEIGHT+2)*4+2);
230         /*
231           for(int dir=0; dir<2; dir++) {
232           boolean h = dir==0;
233           for(int y=h?sector.row:sector.col; y<(h?sector.row+4:sector.col+4); y++)
234           for(int plane=0; plane<=4; plane++) {
235           FpslicDevice.Cell cell      = h ? dev.cell(sector.col,   y) : dev.cell(y, sector.row);
236           FpslicDevice.Cell cell_east = h ? dev.cell(sector.col-1, y) : dev.cell(y, sector.row-1);
237           FpslicDevice.Cell cell_west = h ? dev.cell(sector.col+4, y) : dev.cell(y, sector.row+4);
238           boolean draw = false;
239           if (h) {
240           if (cell_east!=null &&
241           (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
242           cell_east.hwire(plane).drives(cell.hwire(plane))))
243           draw = true;
244           if (cell_west!=null &&
245           (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
246           cell_west.hwire(plane).drives(cell.hwire(plane))))
247           draw = true;
248           } else {
249           if (cell_east!=null &&
250           (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
251           cell_east.vwire(plane).drives(cell.vwire(plane))))
252           draw = true;
253           if (cell_west!=null &&
254           (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
255           cell_west.vwire(plane).drives(cell.vwire(plane))))
256           draw = true;
257           }
258           if (!draw)
259           for(int x=h?sector.col:sector.row; x<(h?sector.col+4:sector.row+4); x++)
260           if (((h ? dev.cell(x,y).hx(plane) : dev.cell(y,x).vx(plane))) ||
261           (h?dev.cell(x,y).out(plane):dev.cell(y,x).out(plane)))
262           draw = true;
263           if (draw) {
264           g.setColor(new Color(0xff, 0x00, 0xff));
265           if (h) {
266           g.drawLine(left(cell),
267           top(cell)+3,
268           left(cell) + 4*(WIDTH+2),
269           top(cell)+3
270           );
271           } else {
272           g.drawLine(left(cell)+3,
273           top(cell) + (HEIGHT+2),
274           left(cell)+3,
275           top(cell) - 3*(HEIGHT+2)
276           );
277           }
278           }
279           }
280           }
281         */
282     }
283     public void drawCell(Graphics g, int cx, int cy) {
284         int x = (cx*(WIDTH+2))+20;
285         int y = ((23-cy)*(HEIGHT+2))+60;
286         if (g.getClipBounds() != null && !g.getClipBounds().intersects(new Rectangle(x, y, x+WIDTH, y+HEIGHT))) return;
287         drawCell(g, cx, cy, Color.white);
288     }
289     public void drawCell(Graphics g, int cx, int cy, Color bg) {
290         int x = (cx*(WIDTH+2))+20;
291         int y = ((23-cy)*(HEIGHT+2))+60;
292
293         //System.out.println("drawcell " + cx + "," + cy);
294         FpslicDevice.Cell cell = dev.cell(cx, cy);
295         g.setColor(bg);
296         g.fillRect(x, y, WIDTH, HEIGHT);
297
298         g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
299         g.drawRect(x, y, WIDTH, HEIGHT);
300
301         //g.setColor((selx==cx && sely==cy) ? Color.red : Color.gray);
302         //g.drawRect(x+(WIDTH-(LW*2))/2-1,    y+(HEIGHT-LW)/2-1, LW*2+1, LH+1);
303
304         //g.setColor(RED);
305         //g.fillRect(x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2, LW,   LH);
306         //g.setColor(Color.white);
307         //g.drawString("1", x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2);
308
309         //g.setColor(BLUE);
310         //g.fillRect(x+(WIDTH-(LW*2))/2+LW, y+(HEIGHT-LW)/2, LW,   LH);
311         //g.setColor(Color.white);
312         //g.drawString("0", x+(WIDTH-(LW*2))/2+LW,    y+(HEIGHT-LW)/2);
313
314         /*
315           g.setColor(BLUE);
316           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
317           switch(cell.yi()) {
318           case NORTH: g.drawLine(x+WIDTH/2+5,  y-10,        x+WIDTH/2+5, y+HEIGHT/2); break;
319           case SOUTH: g.drawLine(x+WIDTH/2-5,  y+HEIGHT+10, x+WIDTH/2-5, y+HEIGHT/2); break;
320           case EAST:  g.drawLine(x+WIDTH+10, y+HEIGHT/2+5,  x+WIDTH/2, y+HEIGHT/2+5); break;
321           case WEST:  g.drawLine(x-10,       y+HEIGHT/2-5,  x+WIDTH/2, y+HEIGHT/2-5); break;
322           case NONE:  break;
323           }
324           g.setColor(RED);
325           ((Graphics2D)g).setStroke(new BasicStroke((float)1));
326           switch(cell.xi()) {
327           case NW: g.drawLine(x-10+3,       y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
328           case SW: g.drawLine(x-10-3,       y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
329           case NE: g.drawLine(x+WIDTH+10+3, y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
330           case SE: g.drawLine(x+WIDTH+10-3, y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
331           case NONE:  break;
332           }
333           ((Graphics2D)g).setStroke(new BasicStroke(1));
334         */
335         /*
336           if (selx==cx && sely==cy) {
337           int xp = 23 * (WIDTH+2) + 100;
338           int yp = 100;
339           g.setColor(Color.white);
340           g.fillRect(xp, yp, 300, 1000);
341           g.setColor(Color.black);
342           g.drawString("Cell " + cx + "," + cy,       xp, (yp+=15));
343           //g.drawString("X-Lut: " + bin8(cell.xlut()), xp, (yp+=15));
344           g.drawString("X-Lut: " + cell.printXLut(), xp, (yp+=15));
345           //g.drawString("Y-Lut: " + bin8(cell.ylut()), xp, (yp+=15));
346           g.drawString("Y-Lut: " + cell.printYLutX(), xp, (yp+=15));
347           }
348         */
349         if ((cell.ylut()&0xff)==0xff && (cell.xlut()&0xff)==0xff) {
350             g.setColor(new Color(0x00, 0x00, 0xff));
351             g.drawString("1", left(cell) + 12, top(cell) + 30);
352         }
353         if ((cell.ylut()&0xff)==0x00 && (cell.xlut()&0xff)==0x00) {
354             g.setColor(new Color(0x00, 0x00, 0xff));
355             g.drawString("0", left(cell) + 12, top(cell) + 30);
356         }
357         if ((cell.ylut()&0xff)==0xB2) {
358             //System.out.println("muller @ " + cell);
359             //g.setColor(RED);
360             //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
361             g.setColor(new Color(0x00, 0xaa, 0x00));
362             g.drawString("C", left(cell) + 12, top(cell) + 30);
363         }
364
365     }
366
367     public static void showit(FpslicDevice dev, SlipwayBoard board, final DemoVisualizer vis) {
368         final Color red = new Color(0xff, 0x99, 0x99);
369         final Color green = new Color(0x99, 0xff, 0x99);
370         final Graphics g = vis.getGraphics();
371         g.setFont(new Font("sansserif", Font.BOLD, 24));
372         for(int xx=0; xx<=dev.getWidth()-2; xx++) {
373             for(int yy=dev.getHeight()-1; yy>=0; yy--) {
374                 final int x = xx;
375                 final int y = yy;
376                 final FpslicDevice.Cell cell = dev.cell(x, y);
377                 if ((cell.ylut()&0xff)!=0xB2) continue;
378                 SlipwayBoard.ByteCallback bc = new SlipwayBoard.ByteCallback() {
379                         public void call(byte b) throws Exception {
380                             boolean v = (b & 0x80) != 0;
381                             vis.drawCell(g, x, y, v?red:green);
382                         }
383                     };
384                 try {
385                     ExperimentUtils.scan(cell, YLUT, true);
386                     board.readFpgaData(bc);
387                     ExperimentUtils.scan(cell, NONE, false);
388                 } catch (IOException ex) {
389                     throw new RuntimeException(ex);
390                 }
391             }
392         }
393     }
394
395 }