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.*;
16 public class AsyncPaperDemo {
20 public AsyncPaperDemo() throws Exception {
21 fpslic = new FtdiBoard();
25 public void main(String[] s) throws Exception {
32 for(int i=0; i<255; i++)
36 for(int i=46; i<400; 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, false);
46 System.out.println("actual size => " + rsize);
47 pipe(start.west().north(), start.west(), new int[] { NE, EAST, SW, SOUTH });
49 for(int i=1; i<22; i+=2)
50 divider(fpslic.cell(21, i));
51 fpslic.cell(23,0).yo(fpslic.cell(22,0));
52 fpslic.cell(21,22).yo(fpslic.cell(20,22));
53 fpslic.cell(21,22).xo(fpslic.cell(20,22));
61 String sizes = rsize+"";
62 while(sizes.length()<3) sizes = "0"+sizes;
63 String fname = "data/size"+sizes+".csv";
64 if (!new File(fname).exists()) {
65 PrintWriter outfile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fname)));
66 //for(int i=0; i<rsize/2+1; i++) test(i, rsize, outfile);
67 for(int i=rsize/2; i>=0; i--) test(i, rsize, outfile);
71 System.out.println("done.");
74 public void test(int count, int size, PrintWriter outfile) throws Exception {
83 long now = System.currentTimeMillis();
88 long then = System.currentTimeMillis();
90 int tokens = fpslic.readCount();
91 double elapsed = (double)((FtdiBoard)fpslic).timer;
93 double occupancy = ((double)(2*count))/((double)size);
95 //double elapsed = (then-now);
96 double result = (tokens*1000)/elapsed;
99 //result *= 2*2*2*2*2*2*2*2*2*2*2;
101 // ...and the interrupt pin counts *pairs* of transitions
104 // result is transitions/sec => 633mcell/sec velocity! =)
105 outfile.println(occupancy + ", " + result);
106 System.out.println((2*count)+"/"+size+" "+occupancy + ", " + result + " @ " + elapsed);
110 private void drain(int size) {
112 topLeft().xlut(0x00);
113 for(int i=0; i<size*4; i++) {
114 topLeft().ylut(0xff);
116 topLeft().ylut(0x00);
123 try { Thread.sleep(100); } catch (Exception e) { }
124 int rc = fpslic.readCount();
126 System.err.println("flush() failed REALLY BADLY => " + rc);
127 //try { System.in.read(); } catch (Exception _) { }
136 try { Thread.sleep(100); } catch (Exception e) { }
137 rc = fpslic.readCount();
139 System.err.println("flush() failed => " + rc);
140 //try { System.in.read(); } catch (Exception _) { }
147 private void fill(int count, int size) {
148 //topLeft().ylut((count>0 && count<size/2-1) ? 0xff : 0x00);
150 topLeft().ylut(0x00);
152 for(int i=0; i<count; i++) {
154 topLeft().xlut(0xff);
156 topLeft().xlut(0x00);
160 //System.out.println("fill => " + yes);
161 //try { Thread.sleep(500); } catch (Exception _) { }
163 //System.out.println("done filling.");
164 //try { Thread.sleep(2000); } catch (Exception _) { }
166 //System.out.println("reconfigured.");
167 //try { System.in.read(); } catch (Exception _) { }
170 if (count>0 && count<size/2-1) {
171 reconfigTopLeftPreserve(yes);
172 } else if (count>0) {
173 topLeft().xlut(0xff);
175 topLeft().ylut(0xff);
176 reconfigTopLeftPreserve(false);
179 //System.out.println("running.");
180 //try { System.in.read(); } catch (Exception _) { }
182 //try { Thread.sleep(2000); } catch (Exception _) { }
185 private Fpslic.Cell topLeft() { return start.north().north(); }
186 private Fpslic.Cell topRight() { return start.north().ne(); }
187 private void reconfigTopLeft() {
188 Fpslic.Cell c = topLeft();
194 c.ylut((LUT_SELF & ~LUT_OTHER) |
195 (LUT_Z & ~LUT_OTHER) |
203 private void reconfigTopLeftNice() {
204 Fpslic.Cell c = topLeft();
208 c.ylut((LUT_SELF & ~LUT_OTHER) |
209 (LUT_Z & ~LUT_OTHER) |
213 private void reconfigTopLeftPreserve(boolean on) {
214 Fpslic.Cell c = topLeft();
216 if (on) c.ylut(0x00);
221 c.ylut((LUT_SELF & ~LUT_OTHER) |
222 (LUT_Z & ~LUT_OTHER) |
226 private void reconfigTopRight() { micropipelineStage(topRight(), topRight().west(), topRight().sw()); }
228 private Fpslic.Cell pipe(Fpslic.Cell c, Fpslic.Cell prev, int[] dirs) {
229 for(int i=0; i<dirs.length; i++) {
230 Fpslic.Cell next = c.dir(dirs[i]);
231 micropipelineStage(c, prev, next);
238 private int createPipeline(Fpslic.Cell c, boolean downward, int length, boolean start) {
239 boolean stop = false;
243 if (length < 10+4) { stop = true; break; }
245 c = pipe(c, c.north(), new int[] { SW, EAST, SW, WEST, NW, NORTH });
247 c = pipe(c, c.north(), new int[] { NE, NORTH });
251 if (length < 8+4) { stop = true; break; }
253 c = micropipelineStage(c, c.north(), c.sw());
254 c = micropipelineStage(c, c.ne(), c.south());
255 c = micropipelineStage(c, c.north(), c.se());
256 c = micropipelineStage(c, c.nw(), c.south());
258 c = micropipelineStage(c, c.south(), c.ne());
259 c = micropipelineStage(c, c.sw(), c.north());
260 c = micropipelineStage(c, c.south(), c.nw());
261 micropipelineStage(c, c.se(), c.north());
262 c = c.south().south().south().south().east();
265 if (c.row > c.fpslic().getHeight()-7) {
266 if (length < 10+4) { stop = true; break; }
268 c = pipe(c, c.south(), new int[] { NW, SOUTH });
270 c = pipe(c, c.south(), new int[] { NE, EAST, SE, WEST, SE, SOUTH });
274 if (length < 8+4) { stop = true; break; }
276 Fpslic.Cell ret = c = pipe(c, c.south(), new int[] { NE, NORTH, NW, NORTH });
278 c = pipe(c, c.north(), new int[] { SW, SOUTH, SE, SOUTH });
286 c = micropipelineStage(c, c.north(), c.sw());
287 c = micropipelineStage(c, c.ne(), c.west());
288 c = micropipelineStage(c, c.east(), c.ne());
289 c = micropipelineStage(c, c.sw(), c.north());
291 c = pipe(c, c.south(), new int[] { NW, EAST, SE, SOUTH });
295 return createPipeline(c, downward, length, false);
300 private void createPipeline(Fpslic.Cell c, boolean downward, int length) {
304 c = micropipelineStage(c, c.ne(), c.west());
305 c = micropipelineStage(c, c.east(), c.nw());
306 if (length > 0) createPipeline(c, false, length);
308 c = micropipelineStage(c, c.ne(), c.east());
309 c = micropipelineStage(c, c.west(), c.sw());
310 if (length > 0) createPipeline(c, true, length);
313 if (c.row == c.fpslic().getHeight()-1) {
314 c = micropipelineStage(c, c.se(), c.west());
315 c = micropipelineStage(c, c.east(), c.sw());
316 if (length > 0) createPipeline(c, true, length);
318 c = micropipelineStage(c, c.se(), c.east());
319 c = micropipelineStage(c, c.west(), c.nw());
320 if (length > 0) createPipeline(c, false, length);
326 private Fpslic.Cell micropipelineStage(Fpslic.Cell c, Fpslic.Cell prev, Fpslic.Cell next) {
327 return micropipelineStage(c, prev, next, true);
329 private Fpslic.Cell micropipelineStage(Fpslic.Cell c, Fpslic.Cell prev, Fpslic.Cell next, boolean configDir) {
334 switch(c.dir(next)) {
335 case NORTH: case SOUTH: case EAST: case WEST:
336 switch (c.dir(prev)) {
337 case NORTH: case SOUTH: case EAST: case WEST: throw new Error("cannot have prev&next both use y");
349 c.ylut((LUT_SELF & ~LUT_OTHER) |
350 (LUT_Z & ~LUT_OTHER) |
357 case NW: case SE: case SW: case NE:
358 switch (c.dir(prev)) {
359 case NW: case SE: case SW: case NE: throw new Error("cannot have prev&next both use x");
371 c.xlut((LUT_SELF & ~LUT_OTHER) |
372 (LUT_Z & ~LUT_OTHER) |
379 default: throw new Error();
385 private void turnOnLeds() {
386 for(int i=0; i<24; i++) {
387 //fpslic.iob_bot(i, true).enableOutput(NORTH);
388 //fpslic.iob_bot(i, false).enableOutput(NW);
389 fpslic.cell(i, 0).xlut(0xff);
390 fpslic.cell(i, 0).ylut(0xff);
394 private void setupScanCell() {
395 fpslic.cell(23,15).h(3, true);
396 fpslic.cell(23,15).yi(L3);
397 fpslic.cell(23,15).ylut(0xAA);
398 fpslic.iob_right(15, true).enableOutput(WEST);
400 fpslic.cell(23,0).ylut(0x00);
401 fpslic.iob_right(0, true).enableOutput(WEST);
405 private void divider(Fpslic.Cell c) {
406 Fpslic.Cell detect1 = c;
407 Fpslic.Cell detect2 = c.east();
410 detect1.ylut(LUT_SELF);
411 detect1.xlut(LUT_OTHER & (~LUT_Z));
418 detect2.ylut(LUT_OTHER);
419 detect2.xlut((~LUT_SELF) & LUT_Z);
425 detect1.south().yi(EAST);
426 detect1.south().xi(NE);
427 detect1.south().c(YLUT);
428 detect1.south().t(TMUX_FB);
429 detect1.south().f(false);
430 detect1.south().b(false);
431 detect1.south().ylut( (LUT_OTHER & (~LUT_SELF)) |
432 ((~LUT_OTHER) & LUT_Z)
434 detect1.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
435 ((~LUT_SELF) & LUT_Z)
438 detect2.south().yi(WEST);
439 detect2.south().xi(NW);
440 detect2.south().c(YLUT);
441 detect2.south().t(TMUX_FB);
442 detect2.south().f(false);
443 detect2.south().b(false);
444 detect2.south().ylut( (LUT_OTHER & (LUT_SELF)) |
445 ((~LUT_OTHER) & LUT_Z)
447 detect2.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
448 ((~LUT_SELF) & LUT_Z)
453 private void runGui(int width, int height) throws Exception {
454 Gui vis = new Gui(fpslic, fpslic, width, height);
455 Frame fr = new Frame();
456 fr.addKeyListener(vis);
457 fr.setLayout(new BorderLayout());
458 fr.add(vis, BorderLayout.CENTER);
460 fr.setSize(900, 900);