1 package edu.berkeley.slipway;
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.*;
15 import static edu.berkeley.slipway.Demo.*;
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;
28 public DemoVisualizer(final Fpslic dev, final FtdiBoard drone) {
32 addMouseMotionListener(this);
33 addMouseListener(this);
40 if (!enabled) continue;
42 Fpslic.Cell cell = dev.cell(21, 22);
52 } catch (Exception e) {
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) {
64 public void keyTyped(KeyEvent k) {
66 public void keyReleased(KeyEvent k) {
68 public void keyPressed(KeyEvent keyevent) {
70 switch(keyevent==null ? '_' : keyevent.getKeyChar()) {
72 if (selx==-1 || sely==-1) break;
73 Fpslic.Cell cell = dev.cell(selx, sely);
76 drawCell(getGraphics(), selx, sely);
81 System.out.println("interrupt_count: " + drone.readCount());
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());
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());
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());
115 int mx = 23-(masterx-23);
116 System.out.println("high => " + mx);
117 copy(dev.cell(mx, Demo.yofs), SOUTH, SOUTH);
119 copy(dev.cell(mx, Demo.yofs-1), NORTH, NORTH);
121 copy(dev.cell(mx, Demo.yofs-1), NORTH, SW);
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);
130 copy(lc, NORTH, NORTH);
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);
138 dev.cell(mx+2, Demo.yofs).ylut(0x00);
139 dev.cell(mx+2, Demo.yofs).xlut(0x00);
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);
147 dev.cell(mx, Demo.yofs).ylut(~LUT_SELF);
148 dev.cell(mx, Demo.yofs).xlut(~LUT_OTHER);
154 //enabled = !enabled;
159 //enabled = !enabled;
161 for(int cap=0; cap<15; cap++) {
163 try { Thread.sleep(100); } catch (Exception e) { }
164 //showit(dev, drone, this);
165 fill(dev, drone, cap);
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)));
173 } catch (Exception e) { e.printStackTrace(); }
177 if (selx==-1 || sely==-1) break;
178 Fpslic.Cell cell = dev.cell(selx, sely);
180 drawCell(getGraphics(), selx, sely);
184 if (selx==-1 || sely==-1) break;
185 Fpslic.Cell cell = dev.cell(selx, sely);
188 drawCell(getGraphics(), selx, sely);
194 showit(dev, drone, this);
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;
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);
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);
217 g.drawString(v+"="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
221 scan(dev, cell, NONE, true);
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);
232 public void mouseMoved(MouseEvent e) {
235 if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
238 Fpslic.Cell cell = dev.cell(cx, cy);
242 drawCell(getGraphics(), cx, cy);
243 drawSector(getGraphics(), dev.cell(cx, cy).sector());
246 selx = (x-20)/(WIDTH+2);
247 sely = (23 - (y-20)/(HEIGHT+2))+1;
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());
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++)
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());
270 g.drawString("Ready", (5*(WIDTH+2))+20, 40);
272 g.drawString("Send", (3*(WIDTH+2))+20, 40);
277 public void refresh() {
278 Graphics g = getGraphics();
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);
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);
296 for(int dir=0; dir<2; dir++) {
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;
305 if (cell_east!=null &&
306 (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
307 cell_east.hwire(plane).drives(cell.hwire(plane))))
309 if (cell_west!=null &&
310 (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
311 cell_west.hwire(plane).drives(cell.hwire(plane))))
314 if (cell_east!=null &&
315 (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
316 cell_east.vwire(plane).drives(cell.vwire(plane))))
318 if (cell_west!=null &&
319 (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
320 cell_west.vwire(plane).drives(cell.vwire(plane))))
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)))
329 g.setColor(new Color(0xff, 0x00, 0xff));
331 g.drawLine(left(cell),
333 left(cell) + 4*(WIDTH+2),
337 g.drawLine(left(cell)+3,
338 top(cell) + (HEIGHT+2),
340 top(cell) - 3*(HEIGHT+2)
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);
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;
358 //System.out.println("drawcell " + cx + "," + cy);
359 Fpslic.Cell cell = dev.cell(cx, cy);
361 g.fillRect(x, y, WIDTH, HEIGHT);
363 g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
364 g.drawRect(x, y, WIDTH, HEIGHT);
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);
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);
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);
381 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
390 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
398 ((Graphics2D)g).setStroke(new BasicStroke(1));
401 if (selx==cx && sely==cy) {
402 int xp = 23 * (WIDTH+2) + 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));
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);
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);
422 if ((cell.ylut()&0xff)==0xB2) {
423 //System.out.println("muller @ " + cell);
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);
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++) {
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));
454 scan(dev, cell, NONE, true);
457 //scan(dev, cell, YLUT, 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);