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);
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);
230 public void mouseMoved(MouseEvent e) {
233 if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
236 Fpslic.Cell cell = dev.cell(cx, cy);
240 drawCell(getGraphics(), cx, cy);
241 drawSector(getGraphics(), dev.cell(cx, cy).sector());
244 selx = (x-20)/(WIDTH+2);
245 sely = (23 - (y-20)/(HEIGHT+2))+1;
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());
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++)
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());
268 g.drawString("Ready", (5*(WIDTH+2))+20, 40);
270 g.drawString("Send", (3*(WIDTH+2))+20, 40);
275 public void refresh() {
276 Graphics g = getGraphics();
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);
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);
294 for(int dir=0; dir<2; dir++) {
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;
303 if (cell_east!=null &&
304 (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
305 cell_east.hwire(plane).drives(cell.hwire(plane))))
307 if (cell_west!=null &&
308 (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
309 cell_west.hwire(plane).drives(cell.hwire(plane))))
312 if (cell_east!=null &&
313 (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
314 cell_east.vwire(plane).drives(cell.vwire(plane))))
316 if (cell_west!=null &&
317 (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
318 cell_west.vwire(plane).drives(cell.vwire(plane))))
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)))
327 g.setColor(new Color(0xff, 0x00, 0xff));
329 g.drawLine(left(cell),
331 left(cell) + 4*(WIDTH+2),
335 g.drawLine(left(cell)+3,
336 top(cell) + (HEIGHT+2),
338 top(cell) - 3*(HEIGHT+2)
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);
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;
356 //System.out.println("drawcell " + cx + "," + cy);
357 Fpslic.Cell cell = dev.cell(cx, cy);
359 g.fillRect(x, y, WIDTH, HEIGHT);
361 g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
362 g.drawRect(x, y, WIDTH, HEIGHT);
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);
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);
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);
379 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
388 ((Graphics2D)g).setStroke(new BasicStroke((float)1));
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;
396 ((Graphics2D)g).setStroke(new BasicStroke(1));
399 if (selx==cx && sely==cy) {
400 int xp = 23 * (WIDTH+2) + 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));
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);
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);
420 if ((cell.ylut()&0xff)==0xB2) {
421 //System.out.println("muller @ " + cell);
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);
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++) {
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));
451 scan(dev, cell, NONE, true);
454 //scan(dev, cell, YLUT, 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);