1 package edu.berkeley.slipway.demos;
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.*;
9 import java.awt.event.*;
10 import java.awt.color.*;
11 import org.ibex.util.*;
14 import static edu.berkeley.slipway.demos.Demo.*;
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;
27 public DemoVisualizer(final FpslicDevice dev, final SlipwayBoard drone) {
31 addMouseMotionListener(this);
32 addMouseListener(this);
39 if (!enabled) continue;
41 Fpslic.Cell cell = dev.cell(21, 22);
51 } catch (Exception e) {
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) {
63 public void keyTyped(KeyEvent k) {
65 public void keyReleased(KeyEvent k) {
67 public void keyPressed(KeyEvent keyevent) {
69 switch(keyevent==null ? '_' : keyevent.getKeyChar()) {
71 if (selx==-1 || sely==-1) break;
72 FpslicDevice.Cell cell = dev.cell(selx, sely);
75 drawCell(getGraphics(), selx, sely);
80 System.out.println("interrupt_count: " + drone.readInterruptCount());
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());
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());
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());
114 int mx = 23-(masterx-23);
115 System.out.println("high => " + mx);
116 copy(dev.cell(mx, Demo.yofs), SOUTH, SOUTH);
118 copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
120 copy(dev.cell(mx, Demo.yofs-1), NORTH, SW);
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);
129 copy(lc, NORTH, NORTH);
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);
137 dev.cell(mx+2, Demo.yofs).ylut(0x00);
138 dev.cell(mx+2, Demo.yofs).xlut(0x00);
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);
146 dev.cell(mx, Demo.yofs).ylut(~LUT_SELF);
147 dev.cell(mx, Demo.yofs).xlut(~LUT_OTHER);
153 //enabled = !enabled;
158 //enabled = !enabled;
160 for(int cap=0; cap<15; cap++) {
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)));
172 } catch (Exception e) { e.printStackTrace(); }
176 if (selx==-1 || sely==-1) break;
177 FpslicDevice.Cell cell = dev.cell(selx, sely);
179 drawCell(getGraphics(), selx, sely);
183 if (selx==-1 || sely==-1) break;
184 FpslicDevice.Cell cell = dev.cell(selx, sely);
187 drawCell(getGraphics(), selx, sely);
193 showit(dev, drone, this);
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;
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);
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);
216 g.drawString(v+"="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
220 scan(dev, cell, NONE, true);
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);
231 public void mouseMoved(MouseEvent e) {
234 if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
237 FpslicDevice.Cell cell = dev.cell(cx, cy);
241 drawCell(getGraphics(), cx, cy);
242 drawSector(getGraphics(), dev.cell(cx, cy).sector());
245 selx = (x-20)/(WIDTH+2);
246 sely = (23 - (y-20)/(HEIGHT+2))+1;
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());
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++)
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());
269 g.drawString("Ready", (5*(WIDTH+2))+20, 40);
271 g.drawString("Send", (3*(WIDTH+2))+20, 40);
276 public void refresh() {
277 Graphics g = getGraphics();
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);
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);
295 for(int dir=0; dir<2; dir++) {
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;
304 if (cell_east!=null &&
305 (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
306 cell_east.hwire(plane).drives(cell.hwire(plane))))
308 if (cell_west!=null &&
309 (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
310 cell_west.hwire(plane).drives(cell.hwire(plane))))
313 if (cell_east!=null &&
314 (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
315 cell_east.vwire(plane).drives(cell.vwire(plane))))
317 if (cell_west!=null &&
318 (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
319 cell_west.vwire(plane).drives(cell.vwire(plane))))
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)))
328 g.setColor(new Color(0xff, 0x00, 0xff));
330 g.drawLine(left(cell),
332 left(cell) + 4*(WIDTH+2),
336 g.drawLine(left(cell)+3,
337 top(cell) + (HEIGHT+2),
339 top(cell) - 3*(HEIGHT+2)
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);
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;
357 //System.out.println("drawcell " + cx + "," + cy);
358 FpslicDevice.Cell cell = dev.cell(cx, cy);
360 g.fillRect(x, y, WIDTH, HEIGHT);
362 g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
363 g.drawRect(x, y, WIDTH, HEIGHT);
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);
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);
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);
380 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
389 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
397 ((Graphics2D)g).setStroke(new BasicStroke(1));
400 if (selx==cx && sely==cy) {
401 int xp = 23 * (WIDTH+2) + 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));
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);
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);
421 if ((cell.ylut()&0xff)==0xB2) {
422 //System.out.println("muller @ " + cell);
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);
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++) {
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));
453 scan(dev, cell, NONE, true);
456 //scan(dev, cell, YLUT, 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);