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.*;
15 public class AsyncPaperDemo {
17 public SlipwayBoard board;
18 public FpslicDevice fpslic;
20 public AsyncPaperDemo() throws Exception {
21 board = new SlipwayBoard();
22 fpslic = (FpslicDevice)board.getDevice();
25 FpslicDevice.Cell start;
26 public void main(String[] s) throws Exception {
31 for(int i=0; i<255; i++)
32 board.readInterruptCount();
35 for(int i=400; i<402; i+=2) {
39 System.out.println("done");
43 public void go(int size) throws Exception {
44 start = fpslic.cell(20, 21);
45 int rsize = size-createPipeline(start, true, size-4, false);
46 System.out.println("actual size => " + rsize);
47 pipe(start.west().north(), start.west(),
48 new int[] { NE, EAST, SW, SOUTH });
50 FpslicDevice.Cell div = start.east();
53 if (div.south()==null || div.south().south()==null) break;
54 div = div.south().south();
56 div = div.south().east();
58 //fpslic.cell(23,0).yo(fpslic.cell(22,0));
59 fpslic.cell(21,22).yo(fpslic.cell(20,22));
60 fpslic.cell(21,22).xo(fpslic.cell(20,22));
66 for(int i=0; i<23; i++){
67 FpslicDevice.Cell c = fpslic.cell(0, i);
72 for(FpslicDevice.SectorWire sw = c.hwire(L3).east();
75 sw.west().drives(sw, true);
78 String sizes = rsize+"";
79 while(sizes.length()<3) sizes = "0"+sizes;
80 String fname = "data/size"+sizes+".csv";
81 if (!new File(fname).exists()) {
82 PrintWriter outfile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fname)));
83 for(int i=rsize/2; i>=0; i--) {
84 test(i, rsize, outfile);
89 System.out.println("done.");
92 public void test(int count, int size, PrintWriter outfile) throws Exception {
100 board.readInterruptCount();
101 long now = System.currentTimeMillis();
103 topLeft().ylut(0xff);
104 topLeft().xlut(0xff);
106 long then = System.currentTimeMillis();
108 int tokens = board.readInterruptCount();
110 int clockrate = 24; // (in mhz)
111 double elapsed = (double)((((SlipwayBoard)board).readInterruptCount())/clockrate);
113 double occupancy = ((double)(2*count))/((double)size);
115 // eleven dividers... ...and the interrupt pin counts *pairs* of transitions
116 int multiplier = 2*2*2*2*2*2*2*2*2*2*2*2;
118 int clockdivisor = 64;
119 multiplier /= clockdivisor;
121 double result = (tokens*multiplier)/elapsed; // in millions
124 // result is transitions/sec => 633mcell/sec velocity! =)
125 outfile.println(occupancy + ", " + result);
126 System.out.println((2*count)+"/"+size+" "+occupancy + ", " + result/* + " @ " + elapsed*/);
130 private void drain(int size) {
134 topLeft().xlut(0x00);
135 for(int i=0; i<size*4; i++) {
136 topLeft().ylut(0xff);
137 topLeft().ylut(0x00);
141 board.readInterruptCount();
142 try { Thread.sleep(100); } catch (Exception e) { }
143 int rc = fpslic.readCount();
144 if (rc!=0) { System.err.println("flush() failed REALLY BADLY => " + rc); continue; }
147 for(int x=0; x<24; x++)
148 for(int y=0; y<24; y++) {
149 fpslic.cell(x,y).wi(L3);
150 fpslic.cell(x,y).h(L3, true);
153 for(int x=0; x<24; x++)
154 for(int y=0; y<24; y++) {
155 fpslic.cell(x,y).wi(NONE);
156 //fpslic.cell(x,y).h(L3, false);
160 board.readInterruptCount();
161 try { Thread.sleep(100); } catch (Exception e) { }
162 int rc = board.readInterruptCount();
164 System.err.println("flush() failed => " + rc);
165 try { Thread.sleep(1000); } catch (Exception e) { }
172 private void fill(int count, int size) {
173 //topLeft().ylut((count>0 && count<size/2-1) ? 0xff : 0x00);
175 topLeft().ylut(0x00);
178 topLeft().xlut(0x00);
181 for(int i=0; i<count-1; i++) {
183 topLeft().xlut(0xff);
185 topLeft().xlut(0x00);
189 //System.out.println("fill => " + yes);
190 //try { Thread.sleep(500); } catch (Exception _) { }
193 //System.out.println("done filling.");
194 //try { Thread.sleep(2000); } catch (Exception _) { }
196 //System.out.println("reconfigured.");
197 //try { System.in.read(); } catch (Exception _) { }
200 if (count>0 && count!=size/2-1) {
201 reconfigTopLeftPreserve(yes);
202 } else if (count>0) {
203 topLeft().xlut(0xff);
205 topLeft().ylut(0xff);
206 reconfigTopLeftPreserve(false);
209 //System.out.println("running.");
210 //try { System.in.read(); } catch (Exception _) { }
212 //try { Thread.sleep(2000); } catch (Exception _) { }
215 private FpslicDevice.Cell topLeft() { return start.north().north(); }
216 private FpslicDevice.Cell topRight() { return start.north().ne(); }
217 private void reconfigTopLeft() {
218 FpslicDevice.Cell c = topLeft();
224 c.ylut((LUT_SELF & ~LUT_OTHER) |
225 (LUT_Z & ~LUT_OTHER) |
233 private void reconfigTopLeftNice() {
234 FpslicDevice.Cell c = topLeft();
238 c.ylut((LUT_SELF & ~LUT_OTHER) |
239 (LUT_Z & ~LUT_OTHER) |
243 private void reconfigTopLeftPreserve(boolean on) {
244 FpslicDevice.Cell c = topLeft();
246 if (on) c.ylut(0x00);
251 c.ylut((LUT_SELF & ~LUT_OTHER) |
252 (LUT_Z & ~LUT_OTHER) |
256 private void reconfigTopRight() { micropipelineStage(topRight(), topRight().west(), topRight().sw()); }
258 private FpslicDevice.Cell pipe(FpslicDevice.Cell c, FpslicDevice.Cell prev, int[] dirs) {
259 for(int i=0; i<dirs.length; i++) {
260 FpslicDevice.Cell next = c.dir(dirs[i]);
261 micropipelineStage(c, prev, next);
268 private int createPipeline(FpslicDevice.Cell c, boolean downward, int length, boolean start) {
269 boolean stop = false;
273 if (length < 8+4) { stop = true; break; }
275 c = pipe(c, c.north(), new int[] { SW, EAST, SW, WEST, NW, NORTH });
277 c = pipe(c, c.north(), new int[] { NE, NORTH });
281 if (length < 8+4) { stop = true; break; }
283 c = micropipelineStage(c, c.north(), c.sw());
284 c = micropipelineStage(c, c.ne(), c.south());
285 c = micropipelineStage(c, c.north(), c.se());
286 c = micropipelineStage(c, c.nw(), c.south());
288 c = micropipelineStage(c, c.south(), c.ne());
289 c = micropipelineStage(c, c.sw(), c.north());
290 c = micropipelineStage(c, c.south(), c.nw());
291 micropipelineStage(c, c.se(), c.north());
292 c = c.south().south().south().south().east();
295 if (c.row > c.fpslic().getHeight()-7) {
296 if (length < 8+4) { stop = true; break; }
298 c = pipe(c, c.south(), new int[] { NW, SOUTH });
300 c = pipe(c, c.south(), new int[] { NE, EAST, SE, WEST, SE, SOUTH });
304 if (length < 8+4) { stop = true; break; }
306 FpslicDevice.Cell ret = c = pipe(c, c.south(), new int[] { NE, NORTH, NW, NORTH });
308 c = pipe(c, c.north(), new int[] { SW, SOUTH, SE, SOUTH });
316 c = micropipelineStage(c, c.north(), c.sw());
317 c = micropipelineStage(c, c.ne(), c.west());
318 c = micropipelineStage(c, c.east(), c.ne());
319 c = micropipelineStage(c, c.sw(), c.north());
321 c = pipe(c, c.south(), new int[] { NW, EAST, SE, SOUTH });
325 return createPipeline(c, downward, length, false);
330 private void createPipeline(FpslicDevice.Cell c, boolean downward, int length) {
334 c = micropipelineStage(c, c.ne(), c.west());
335 c = micropipelineStage(c, c.east(), c.nw());
336 if (length > 0) createPipeline(c, false, length);
338 c = micropipelineStage(c, c.ne(), c.east());
339 c = micropipelineStage(c, c.west(), c.sw());
340 if (length > 0) createPipeline(c, true, length);
343 if (c.row == c.fpslic().getHeight()-1) {
344 c = micropipelineStage(c, c.se(), c.west());
345 c = micropipelineStage(c, c.east(), c.sw());
346 if (length > 0) createPipeline(c, true, length);
348 c = micropipelineStage(c, c.se(), c.east());
349 c = micropipelineStage(c, c.west(), c.nw());
350 if (length > 0) createPipeline(c, false, length);
356 private FpslicDevice.Cell micropipelineStage(FpslicDevice.Cell c, FpslicDevice.Cell prev, FpslicDevice.Cell next) {
357 return micropipelineStage(c, prev, next, true);
359 private FpslicDevice.Cell micropipelineStage(FpslicDevice.Cell c, FpslicDevice.Cell prev, FpslicDevice.Cell next, boolean configDir) {
362 switch(c.dir(next)) {
363 case NORTH: case SOUTH: case EAST: case WEST:
364 switch (c.dir(prev)) {
365 case NORTH: case SOUTH: case EAST: case WEST: throw new Error("cannot have prev&next both use y");
372 if (c.row==topLeft().row && c.col==topLeft().col) {
388 FpslicDevice.SectorWire sw = c.vwire(L3);
389 while(sw.south() != null) {
390 sw.south().drives(sw, true);
393 fpslic.cell(sw.col,0).ylut(0x00);
394 fpslic.cell(sw.col,0).c(YLUT);
395 fpslic.cell(sw.col,0).out(L3, true);
396 fpslic.cell(sw.col,0).v(L3, true);
405 c.ylut((LUT_SELF & ~LUT_OTHER) |
406 (LUT_Z & ~LUT_OTHER) |
413 case NW: case SE: case SW: case NE:
414 switch (c.dir(prev)) {
415 case NW: case SE: case SW: case NE: throw new Error("cannot have prev&next both use x");
432 c.xlut((LUT_SELF & ~LUT_OTHER) |
433 (LUT_Z & ~LUT_OTHER) |
440 default: throw new Error();
447 private void turnOnLeds() {
448 for(int i=0; i<24; i++) {
449 //fpslic.iob_bot(i, true).enableOutput(NORTH);
450 //fpslic.iob_bot(i, false).enableOutput(NW);
451 fpslic.cell(i, 0).xlut(0xff);
452 fpslic.cell(i, 0).ylut(0xff);
456 private void setupScanCell() {
457 fpslic.cell(23,15).h(3, true);
458 fpslic.cell(23,15).yi(L3);
459 fpslic.cell(23,15).ylut(0xAA);
460 fpslic.iob_right(15, true).enableOutput(WEST);
462 fpslic.cell(23,0).ylut(0x00);
463 fpslic.iob_right(0, true).enableOutput(WEST);
467 public static void divider(FpslicDevice.Cell c) {
468 FpslicDevice.Cell detect1 = c;
469 FpslicDevice.Cell detect2 = c.east();
472 detect1.ylut(LUT_SELF);
473 detect1.xlut(LUT_OTHER & (~LUT_Z));
480 detect2.ylut(LUT_OTHER);
481 detect2.xlut((~LUT_SELF) & LUT_Z);
487 detect1.south().yi(EAST);
488 detect1.south().xi(NE);
489 detect1.south().c(YLUT);
490 detect1.south().t(TMUX_FB);
491 detect1.south().f(false);
492 detect1.south().b(false);
493 detect1.south().ylut( (LUT_OTHER & (~LUT_SELF)) |
494 ((~LUT_OTHER) & LUT_Z)
496 detect1.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
497 ((~LUT_SELF) & LUT_Z)
500 detect2.south().yi(WEST);
501 detect2.south().xi(NW);
502 detect2.south().c(YLUT);
503 detect2.south().t(TMUX_FB);
504 detect2.south().f(false);
505 detect2.south().b(false);
506 detect2.south().ylut( (LUT_OTHER & (LUT_SELF)) |
507 ((~LUT_OTHER) & LUT_Z)
509 detect2.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
510 ((~LUT_SELF) & LUT_Z)
515 public void runGui(int width, int height) throws Exception {
516 Gui vis = new Gui(fpslic, board, width, height);
517 Frame fr = new Frame();
518 fr.setTitle("SLIPWAY Live Fabric Debugger");
519 fr.addKeyListener(vis);
520 fr.setLayout(new BorderLayout());
521 fr.add(vis, BorderLayout.CENTER);
523 fr.setSize(900, 900);