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);
70 System.out.println("done.");
73 public void test(int count, int size, PrintWriter outfile) throws Exception {
82 long now = System.currentTimeMillis();
87 long then = System.currentTimeMillis();
89 int tokens = fpslic.readCount();
90 double elapsed = (double)((FtdiBoard)fpslic).timer;
92 double occupancy = ((double)(2*count))/((double)size);
94 //double elapsed = (then-now);
95 double result = (tokens*1000)/elapsed;
98 //result *= 2*2*2*2*2*2*2*2*2*2*2;
100 // ...and the interrupt pin counts *pairs* of transitions
103 // result is transitions/sec => 633mcell/sec velocity! =)
104 outfile.println(occupancy + ", " + result);
105 System.out.println((2*count)+"/"+size+" "+occupancy + ", " + result + " @ " + elapsed);
109 private void drain(int size) {
111 topLeft().xlut(0x00);
112 for(int i=0; i<size*4; i++) {
113 topLeft().ylut(0xff);
115 topLeft().ylut(0x00);
122 try { Thread.sleep(100); } catch (Exception e) { }
123 int rc = fpslic.readCount();
125 System.err.println("flush() failed REALLY BADLY => " + rc);
126 //try { System.in.read(); } catch (Exception _) { }
135 try { Thread.sleep(100); } catch (Exception e) { }
136 rc = fpslic.readCount();
138 System.err.println("flush() failed => " + rc);
139 //try { System.in.read(); } catch (Exception _) { }
146 private void fill(int count, int size) {
147 //topLeft().ylut((count>0 && count<size/2-1) ? 0xff : 0x00);
148 topLeft().ylut(0x00);
150 for(int i=0; i<count; i++) {
152 topLeft().xlut(0xff);
154 topLeft().xlut(0x00);
158 //System.out.println("fill => " + yes);
159 //try { Thread.sleep(500); } catch (Exception _) { }
161 //System.out.println("done filling.");
162 //try { Thread.sleep(2000); } catch (Exception _) { }
164 //System.out.println("reconfigured.");
165 //try { System.in.read(); } catch (Exception _) { }
168 if (count>0 && count<size/2-1) {
169 reconfigTopLeftPreserve(yes);
174 //System.out.println("running.");
175 //try { System.in.read(); } catch (Exception _) { }
177 //try { Thread.sleep(2000); } catch (Exception _) { }
180 private Fpslic.Cell topLeft() { return start.north().north(); }
181 private Fpslic.Cell topRight() { return start.north().ne(); }
182 private void reconfigTopLeft() {
183 Fpslic.Cell c = topLeft();
189 c.ylut((LUT_SELF & ~LUT_OTHER) |
190 (LUT_Z & ~LUT_OTHER) |
198 private void reconfigTopLeftNice() {
199 Fpslic.Cell c = topLeft();
203 c.ylut((LUT_SELF & ~LUT_OTHER) |
204 (LUT_Z & ~LUT_OTHER) |
208 private void reconfigTopLeftPreserve(boolean on) {
209 Fpslic.Cell c = topLeft();
211 if (on) c.ylut(0x00);
214 c.ylut((LUT_SELF & ~LUT_OTHER) |
215 (LUT_Z & ~LUT_OTHER) |
219 private void reconfigTopRight() { micropipelineStage(topRight(), topRight().west(), topRight().sw()); }
221 private Fpslic.Cell pipe(Fpslic.Cell c, Fpslic.Cell prev, int[] dirs) {
222 for(int i=0; i<dirs.length; i++) {
223 Fpslic.Cell next = c.dir(dirs[i]);
224 micropipelineStage(c, prev, next);
231 private int createPipeline(Fpslic.Cell c, boolean downward, int length, boolean start) {
232 boolean stop = false;
236 if (length < 10+4) { stop = true; break; }
238 c = pipe(c, c.north(), new int[] { SW, EAST, SW, WEST, NW, NORTH });
240 c = pipe(c, c.north(), new int[] { NE, NORTH });
244 if (length < 8+4) { stop = true; break; }
246 c = micropipelineStage(c, c.north(), c.sw());
247 c = micropipelineStage(c, c.ne(), c.south());
248 c = micropipelineStage(c, c.north(), c.se());
249 c = micropipelineStage(c, c.nw(), c.south());
251 c = micropipelineStage(c, c.south(), c.ne());
252 c = micropipelineStage(c, c.sw(), c.north());
253 c = micropipelineStage(c, c.south(), c.nw());
254 micropipelineStage(c, c.se(), c.north());
255 c = c.south().south().south().south().east();
258 if (c.row > c.fpslic().getHeight()-7) {
259 if (length < 10+4) { stop = true; break; }
261 c = pipe(c, c.south(), new int[] { NW, SOUTH });
263 c = pipe(c, c.south(), new int[] { NE, EAST, SE, WEST, SE, SOUTH });
267 if (length < 8+4) { stop = true; break; }
269 Fpslic.Cell ret = c = pipe(c, c.south(), new int[] { NE, NORTH, NW, NORTH });
271 c = pipe(c, c.north(), new int[] { SW, SOUTH, SE, SOUTH });
279 c = micropipelineStage(c, c.north(), c.sw());
280 c = micropipelineStage(c, c.ne(), c.west());
281 c = micropipelineStage(c, c.east(), c.ne());
282 c = micropipelineStage(c, c.sw(), c.north());
284 c = pipe(c, c.south(), new int[] { NW, EAST, SE, SOUTH });
288 return createPipeline(c, downward, length, false);
293 private void createPipeline(Fpslic.Cell c, boolean downward, int length) {
297 c = micropipelineStage(c, c.ne(), c.west());
298 c = micropipelineStage(c, c.east(), c.nw());
299 if (length > 0) createPipeline(c, false, length);
301 c = micropipelineStage(c, c.ne(), c.east());
302 c = micropipelineStage(c, c.west(), c.sw());
303 if (length > 0) createPipeline(c, true, length);
306 if (c.row == c.fpslic().getHeight()-1) {
307 c = micropipelineStage(c, c.se(), c.west());
308 c = micropipelineStage(c, c.east(), c.sw());
309 if (length > 0) createPipeline(c, true, length);
311 c = micropipelineStage(c, c.se(), c.east());
312 c = micropipelineStage(c, c.west(), c.nw());
313 if (length > 0) createPipeline(c, false, length);
319 private Fpslic.Cell micropipelineStage(Fpslic.Cell c, Fpslic.Cell prev, Fpslic.Cell next) {
320 return micropipelineStage(c, prev, next, true);
322 private Fpslic.Cell micropipelineStage(Fpslic.Cell c, Fpslic.Cell prev, Fpslic.Cell next, boolean configDir) {
327 switch(c.dir(next)) {
328 case NORTH: case SOUTH: case EAST: case WEST:
329 switch (c.dir(prev)) {
330 case NORTH: case SOUTH: case EAST: case WEST: throw new Error("cannot have prev&next both use y");
342 c.ylut((LUT_SELF & ~LUT_OTHER) |
343 (LUT_Z & ~LUT_OTHER) |
350 case NW: case SE: case SW: case NE:
351 switch (c.dir(prev)) {
352 case NW: case SE: case SW: case NE: throw new Error("cannot have prev&next both use x");
364 c.xlut((LUT_SELF & ~LUT_OTHER) |
365 (LUT_Z & ~LUT_OTHER) |
372 default: throw new Error();
378 private void turnOnLeds() {
379 for(int i=0; i<24; i++) {
380 //fpslic.iob_bot(i, true).enableOutput(NORTH);
381 //fpslic.iob_bot(i, false).enableOutput(NW);
382 fpslic.cell(i, 0).xlut(0xff);
383 fpslic.cell(i, 0).ylut(0xff);
387 private void setupScanCell() {
388 fpslic.cell(23,15).h(3, true);
389 fpslic.cell(23,15).yi(L3);
390 fpslic.cell(23,15).ylut(0xAA);
391 fpslic.iob_right(15, true).enableOutput(WEST);
393 fpslic.cell(23,0).ylut(0x00);
394 fpslic.iob_right(0, true).enableOutput(WEST);
398 private void divider(Fpslic.Cell c) {
399 Fpslic.Cell detect1 = c;
400 Fpslic.Cell detect2 = c.east();
403 detect1.ylut(LUT_SELF);
404 detect1.xlut(LUT_OTHER & (~LUT_Z));
411 detect2.ylut(LUT_OTHER);
412 detect2.xlut((~LUT_SELF) & LUT_Z);
418 detect1.south().yi(EAST);
419 detect1.south().xi(NE);
420 detect1.south().c(YLUT);
421 detect1.south().t(TMUX_FB);
422 detect1.south().f(false);
423 detect1.south().b(false);
424 detect1.south().ylut( (LUT_OTHER & (~LUT_SELF)) |
425 ((~LUT_OTHER) & LUT_Z)
427 detect1.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
428 ((~LUT_SELF) & LUT_Z)
431 detect2.south().yi(WEST);
432 detect2.south().xi(NW);
433 detect2.south().c(YLUT);
434 detect2.south().t(TMUX_FB);
435 detect2.south().f(false);
436 detect2.south().b(false);
437 detect2.south().ylut( (LUT_OTHER & (LUT_SELF)) |
438 ((~LUT_OTHER) & LUT_Z)
440 detect2.south().xlut( (LUT_SELF & (~LUT_OTHER)) |
441 ((~LUT_SELF) & LUT_Z)
446 private void runGui(int width, int height) throws Exception {
447 Gui vis = new Gui(fpslic, fpslic, width, height);
448 Frame fr = new Frame();
449 fr.addKeyListener(vis);
450 fr.setLayout(new BorderLayout());
451 fr.add(vis, BorderLayout.CENTER);
453 fr.setSize(900, 900);