checkpoint
[slipway.git] / src / edu / berkeley / slipway / Demo.java
1 package edu.berkeley.slipway;
2
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.*;
8 import java.awt.*;
9 import java.awt.event.*;
10 import java.awt.color.*;
11 import org.ibex.util.*;
12 import java.io.*;
13 import java.util.*;
14 import gnu.io.*;
15
16 public class Demo {
17
18     public static boolean mullers = true;
19     public static int masterx = 1;
20
21     public static int PIPELEN=20;
22     public static void main(String[] s) throws Exception {
23         FtdiBoard device = new FtdiBoard();
24         Fpslic at40k = device;
25         try {
26             long begin = System.currentTimeMillis();
27             FpslicUtil.readMode4(new ProgressInputStream("configuring fabric", System.in, 111740), device);
28             long end = System.currentTimeMillis();
29             Log.info(Demo.class, "finished in " + ((end-begin)/1000) + "s");
30             Thread.sleep(1000);
31             Log.info(Demo.class, "issuing command");
32
33             //at40k.iob_top(2, true).oe(false);
34             //at40k.iob_top(2, false).oe(false);
35             //at40k.iob_top(1, true).oe(false);
36
37             // this command confirmed to turn *on* led0
38             //at40k.iob_top(1, false).output(0);
39             /*
40             for(int i=0; i<20; i++) {
41                 at40k.iob_bot(i, false).output(0);
42                 at40k.iob_bot(i, true).output(0);
43             }
44             */
45
46             //System.out.println("tick");
47                 //Thread.sleep(3000);
48                 //System.out.println("tick");
49                 //at40k.cell(0x01, 0x17).xlut((byte)0x);
50
51             /*
52             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).xlut(), 16));
53             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).ylut(), 16));
54             at40k.cell(0x01, 0x17).ylut((byte)0xff);
55             */
56
57             //at40k.cell(0x01, 0x17).wi(L1);
58             /*
59             System.out.println("a: " + at40k.new SectorWire(true, 0, 4, 0x17).driverRight());
60             System.out.println("b: " + at40k.new SectorWire(true, 1, 4, 0x17).driverRight());
61             Fpslic.SectorWire h0p0 = at40k.new SectorWire(true, 0, 0, 0x17);
62             Fpslic.SectorWire h0p1 = at40k.new SectorWire(true, 1, 0, 0x17);
63             Fpslic.SectorWire h0p2 = at40k.new SectorWire(true, 2, 0, 0x17);
64             Fpslic.SectorWire h4p0 = at40k.new SectorWire(true, 0, 4, 0x17);
65             Fpslic.SectorWire h4p1 = at40k.new SectorWire(true, 1, 4, 0x17);
66             Fpslic.SectorWire h4p2 = at40k.new SectorWire(true, 2, 4, 0x17);
67
68             //h4p1.drives(h0p1, false);
69             //at40k.cell(0x04, 0x17).out(L1, false);
70             //at40k.cell(0x04, 0x17).h(L0, false);
71
72             for(int plane=0; plane<5; plane++) {
73                 at40k.new SectorWire(true, plane,     4, 0x17).drives(at40k.new SectorWire(true, plane,     0, 0x17), false);
74                 at40k.cell(0x04, 0x17).out(plane, false);
75                 at40k.cell(0x04, 0x17).h(plane, false);
76                 at40k.cell(0x01, 0x17).h(plane, false);
77             }
78             try { Thread.sleep(2000); } catch (Exception e) { }
79
80             int plane=0;
81             at40k.new SectorWire(true, plane, 4, 0x17).drives(at40k.new SectorWire(true, plane, 0, 0x17), true);
82             at40k.cell(0x04, 0x17).out(plane, true);
83             at40k.cell(0x04, 0x17).h(plane, true);
84             at40k.cell(0x01, 0x17).h(plane, true);
85             at40k.cell(0x01, 0x17).wi(plane);
86
87             */
88
89             /*
90             System.out.println("xlut is " + hex(at40k.cell(0x04, 0x17).xlut()));
91             System.out.println("ylut is " + hex(at40k.cell(0x04, 0x17).ylut()));
92             Fpslic.Cell cell = at40k.cell(0x04, 0x17);
93             //cell.xlut(0xff);
94             //cell.f(false);
95             System.out.println(cell.c());
96             cell.c(YLUT);
97             cell.ylut(0x4D);
98             cell.xlut(0x00);
99
100             cell.b(false);
101             cell.f(false);
102             //cell.t(false, false, true);
103             cell.t(false, true, false);
104             cell.out(L3, true);
105             cell.wi(L3);
106
107             cell.yo(false);
108             cell.h(L0, false);
109             cell.h(L1, false);
110             cell.h(L2, false);
111             cell.h(L3, false);
112             cell.h(L4, false);
113
114             for(int i=3; i>=1; i--) {
115                 at40k.cell(i, 0x17).yi(EAST);
116                 at40k.cell(i, 0x17).ylut(0x55);
117                 at40k.cell(i, 0x17).yo(false);
118             }
119             */
120
121             //System.out.println("reading port status: " + Integer.toString(device.readBus() & 0xff, 16));
122
123
124             // blank these out
125             /*
126             at40k.cell(23, 8).ylut(0xff);
127             at40k.cell(23, 11).ylut(0xff);
128             at40k.iob_right(8, true).enableOutput();
129             at40k.iob_right(11, true).enableOutput();
130             */
131             //for(int x=4;  x<=22; x++) swap(at40k.cell(x, 22), NW, NORTH);
132
133
134             // entry cell: just copy X->X Y->Y
135             //at40k.cell(4,23).b(false);
136             //at40k.cell(4,23).yo(false);
137             //at40k.cell(4,23).ylut(at40k.cell(4,23).xlut());
138             //at40k.cell(4,23).xo(false);
139             /*
140             at40k.cell(4,23).xlut(0x55);
141             at40k.cell(4,23).ylut(0x55);
142             */
143             /*
144             at40k.cell(4,23).xlut(0x71);
145             at40k.cell(4,23).ylut(0x44);
146             at40k.cell(4,23).c(YLUT);
147             at40k.cell(4,23).f(false);
148             at40k.cell(4,23).t(false, false, true);
149             */
150
151             //for(int x=6;  x<=23; x++) copy(at40k.cell(x, 23), NW, WEST);  // top row copies to the right
152             /*
153             copy(at40k.cell(5, 22), NW, NORTH);
154             for(int x=6;  x<=22; x++) copy(at40k.cell(x, 22), NW, WEST);  // second top row copies to the right
155             //for(int y=22; y>=10; y--) copy(at40k.cell(23, y), NW, NORTH); // right edge copies down
156             for(int y=21; y>=9;  y--) copy(at40k.cell(22, y), NW, NORTH); // second right edge copies down
157             copy(at40k.cell(23, 9), NW, WEST);                            // second output
158             */
159             /*
160             handshaker(at40k.cell(4,23));
161             at40k.cell(4,23).xi(NW);
162             at40k.cell(4,23).yi(SOUTH);
163
164             //handshaker(at40k.cell(5,23));
165             //at40k.cell(5,23).yi(NORTH);
166
167             at40k.cell(5,23).yi(NORTH);
168             at40k.cell(5,23).xlut(0x55);
169             at40k.cell(5,23).xi(SW);
170             at40k.cell(5,23).ylut(0x55);
171             at40k.cell(5,22).yi(NORTH);
172             at40k.cell(5,22).xlut(0x55);
173
174             bounce(at40k.cell(4,22));
175
176             // cell southeast of entry cell
177             at40k.cell(3,22).xi(NE);      // NW->xin
178             at40k.cell(3,22).ylut(0x33);  // xin->y
179             at40k.cell(3,22).yo(false);   // y->yout
180             copy(at40k.cell(3, 21), NW, NORTH);  // second top row copies to the right
181             copy(at40k.cell(4, 21), NW, EAST);  // second top row copies to the right
182             copy(at40k.cell(5, 21), NW, EAST);  // second top row copies to the right
183             copy(at40k.cell(6, 21), NW, EAST);  // second top row copies to the right
184             copy(at40k.cell(6, 22), NW, SOUTH);  // second top row copies to the right
185             */
186             /*
187             at40k.cell(05,22).xlut(0xff);
188             at40k.cell(05,22).ylut(0xff);
189             at40k.cell(05,22).c(XLUT);
190             at40k.cell(05,22).f(false);
191             at40k.cell(05,22).b(false);
192             at40k.cell(05,22).oe(NONE);
193             at40k.cell(05,22).v(L3, true);
194             at40k.cell(05,22).out(L3, true);
195             */
196             /*
197             at40k.cell(4,23).ylut(~0xCC);
198             at40k.cell(4,23).xlut(~0xAA);
199             at40k.cell(5,23).ylut(~0xAA);
200             at40k.cell(5,23).xlut(~0xAA);
201             for(int i=6; i<PIPELEN+2; i++) {
202                 at40k.cell(i, 23).ylut(0xAA);
203                 at40k.cell(i, 23).xlut(0xCC);
204                 at40k.cell(i, 23).yi(WEST);
205             }
206             */
207
208             /* LAST
209             System.out.println("doit");
210             if (mullers) doitx(at40k, device);
211             //System.out.println("counter");
212             //counter(at40k, device);
213
214             at40k.cell(21,15).yi(WEST);
215             at40k.cell(21,15).ylut(0xAA);
216
217             at40k.cell(22,15).yi(WEST);
218             at40k.cell(22,15).ylut(0xAA);
219             */
220
221             Fpslic.Cell root = at40k.cell(10,20);
222             
223             root.yo(root.north());
224             root.ylut(~LUT_SELF);
225             root.c(YLUT);
226             root = root.north();
227
228             root.yo(root.east());
229             root.ylut(~LUT_SELF);
230             root.c(YLUT);
231             root = root.east();
232
233             root.yo(root.south());
234             root.ylut(~LUT_SELF);
235             root.c(YLUT);
236             root = root.south();
237
238             root.yo(root.west());
239             root.c(YLUT);
240             root = root.west();
241
242             //////////////////////////////////////////////////////////////////////////////
243
244             at40k.cell(23,15).h(3, true);
245             at40k.cell(23,15).yi(L3);
246             at40k.cell(23,15).ylut(0xAA);
247             at40k.iob_right(15, true).enableOutput(WEST);
248
249
250             Fpslic.Cell c = at40k.cell(10,10);
251             c.ylut(~LUT_SELF);
252             c.xlut(LUT_Z);
253             c.yi(WEST);
254             c.c(YLUT);
255             c.f(false);
256             c.t(TMUX_FB);
257             copy(c.west(), EAST, NW);
258             copy(c.west().north().west(), SE, SE);
259
260             c = c.east();
261             c.ylut(~LUT_SELF);
262             c.xlut(LUT_Z);
263             c.yi(EAST);
264             c.c(YLUT);
265             c.f(false);
266             c.t(TMUX_FB);
267             copy(c.east(), WEST, SE);
268             copy(c.east().south().east(), NW, NW);
269
270             c = c.north();
271             copy(c.north(), SOUTH, SOUTH);
272             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
273             c.ylut(LUT_Z);
274             c.yi(SOUTH);
275             c.c(XLUT);
276             c.xi(SW);
277             c.wi(L4);
278             c.f(false);
279             c.t(TMUX_W_AND_FB);
280             c.v(L4, false);
281             c.h(L4, true);
282             c.v(L2, false);
283             c.h(L2, true);
284
285             c = c.west();
286             copy(c.north(), SOUTH, SOUTH);
287             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
288             c.ylut(~LUT_Z);
289             c.yi(SOUTH);
290             c.xi(SE);
291             c.c(XLUT);
292             c.wi(L4);
293             c.f(false);
294             c.t(TMUX_W_AND_FB);
295             c.v(L4, false);
296             c.h(L4, true);
297             c.v(L2, false);
298             c.h(L2, true);
299
300             c = c.west();
301             c.v(L4, false);
302             c.h(L4, true);
303             c.out(L4, true);
304             c.f(false);
305             c.b(false);
306             c.oe(NONE);
307             c.c(YLUT);
308             c.hwire(L4).west().drives(c.hwire(L4), false);
309             c.hwire(L4).east().drives(c.hwire(L4), false);
310
311             c = c.south();
312             c = c.south();
313             c.v(L4, false);
314             c.h(L4, true);
315             c.out(L4, true);
316             c.f(false);
317             c.b(false);
318             c.oe(NONE);
319             c.c(YLUT);
320             c.hwire(L4).west().drives(c.hwire(L4), false);
321             c.hwire(L4).east().drives(c.hwire(L4), false);
322
323             c = c.east();
324             c = c.east();
325             copy(c.south(), NORTH, NORTH);
326             c.xlut(((~LUT_SELF) & (~LUT_OTHER)) | LUT_Z);
327             c.ylut(LUT_Z);
328             c.yi(NORTH);
329             c.c(XLUT);
330             c.xi(NW);
331             c.wi(L4);
332             c.f(false);
333             c.t(TMUX_W_AND_FB);
334             c.v(L4, false);
335             c.h(L4, true);
336             c.v(L2, false);
337             c.h(L2, true);
338
339             c = c.west();
340             copy(c.south(), NORTH, NORTH);
341             c.xlut((LUT_SELF & LUT_OTHER) | LUT_Z);
342             c.ylut(LUT_Z);
343             c.yi(NORTH);
344             c.xi(NE);
345             c.c(XLUT);
346             c.wi(L4);
347             c.f(false);
348             c.t(TMUX_W_AND_FB);
349             c.v(L4, false);
350             c.h(L4, true);
351             c.v(L2, false);
352             c.h(L2, true);
353
354
355             // catch a rising transition
356             /*
357             c = c.west();
358             c.v(L2, false);
359             c.h(L2, true);
360             c.out(L2, true);
361             c.f(false);
362             c.b(false);
363             c.oe(NONE);
364             c.c(YLUT);
365             */
366             c.hwire(L2).west().drives(c.hwire(L2), false);
367             c.hwire(L2).east().drives(c.hwire(L2), false);
368
369
370
371             //////
372
373             c = at40k.cell(20,20);
374             c.yi(WEST);
375             c.ylut(LUT_SELF);
376             c.c(YLUT);
377             c.oe(H4);
378             c.h(L4, true);
379             c.b(false);
380             c.f(false);
381             c.out(L4);
382
383             c = at40k.cell(21,20);
384             c.c(YLUT);
385             c.oe(NONE);
386             c.h(L4, true);
387             c.b(false);
388             c.f(false);
389
390
391             c = at40k.cell(8,8);
392             c.f(true);
393             c.b(true);
394             c.xo(true);
395             c.xi(NE);
396             c.zi(L3);
397             c.wi(L0);
398             c.yi(NORTH);
399             c.oe(H4);
400             c.h(L0, true);
401             c.h(L2, true);
402             c.h(L4, true);
403             c.v(L1, true);
404             c.v(L3, true);
405             c.out(L0, true);
406             c.out(L1, true);
407             c.out(L2, true);
408             c.out(L3, true);
409             c.out(L4, true);
410             c.xo(true);
411             c.yo(true);
412             c.c(ZMUX);
413
414             at40k.cell(9,10).xo(true);
415             at40k.cell(9,10).yo(true);
416             at40k.cell(9,10).c(YLUT);
417             at40k.cell(9,10).b(false);
418
419             //for(int x=5; x<PIPELEN; x++) {
420             //at40k.cell(x,23).hwire(L0).drives(at40k.cell(x,23).hwire(L0).east());
421             //}
422
423             /*
424             at40k.cell(22,11).ylut(0xff);
425             at40k.cell(23,11).yi(L3);
426             //at40k.cell(23,11).yi(WEST);
427             //at40k.cell(23,11).xi(L1);
428             at40k.cell(23,11).ylut(0xAA);
429             at40k.iob_right(11, true).enableOutput(WEST);
430             at40k.cell(23,11).v(L3, true);
431             at40k.cell(23,11).yo(false);
432             //at40k.flush();
433             */
434             int vx=04;
435             int vv=23;
436             /*
437             System.out.println("correct: " + at40k.cell(19,15).hwire(L3) + " drives " + at40k.cell(20,15).hwire(L3));
438             System.out.println("correct: " + at40k.cell(15,15).hwire(L3) + " drives " + at40k.cell(19,15).hwire(L3));
439             System.out.println("correct: " + at40k.cell(11,15).hwire(L3) + " drives " + at40k.cell(15,15).hwire(L3));
440             System.out.println("correct: " + at40k.cell(07,15).hwire(L3) + " drives " + at40k.cell(11,15).hwire(L3));
441
442             at40k.cell(19,15).hwire(L3).drives(at40k.cell(20,15).hwire(L3), true);
443             at40k.cell(15,15).hwire(L3).drives(at40k.cell(19,15).hwire(L3), true);
444             at40k.cell(11,15).hwire(L3).drives(at40k.cell(15,15).hwire(L3), true);
445             at40k.cell(07,15).hwire(L3).drives(at40k.cell(11,15).hwire(L3), true);
446             */
447             //at40k.cell(05,vv).xlut(0xff);
448             //at40k.cell(05,vv).ylut(0xff);
449             /*
450             at40k.cell(vx,vv).c(YLUT);
451             at40k.cell(vx,vv).f(false);
452             at40k.cell(vx,vv).b(false);
453             at40k.cell(vx,vv).oe(NONE);
454             at40k.cell(vx,vv).v(L3, true);
455             at40k.cell(vx,vv).out(L3, true);
456             */
457             /*
458             at40k.cell(vx,15).v(L3, true);
459             at40k.cell(vx,15).h(L3, true);
460             at40k.cell(vx,19).vwire(L3).drives(at40k.cell(vx,15).vwire(L3), true);
461             at40k.cell(vx,23).vwire(L3).drives(at40k.cell(vx,19).vwire(L3), true);
462             */
463
464             //at40k.cell(5,23).ylut(0x00);
465             //at40k.cell(6,22).ylut(0xff);
466             //at40k.cell(22,11).ylut(0xff);
467             /*
468             Fpslic.Cell cell = at40k.cell(4, 16);
469             cell.xlut(0xff);
470             cell.ylut(0xff);
471             cell.b(false);
472             cell.f(false);
473             cell.c(XLUT);
474             cell.h(L3, true);
475             cell.v(L3, true);
476             cell.out(L3, true);
477             cell.oe(NONE);
478             */
479             //scan(at40k, cell, YLUT, true);
480             //scan(at40k, cell, YLUT, false);
481
482             //device.scanFPGA(true);
483
484             at40k.cell(10,10).f(true);
485             at40k.cell(10,10).c(ZMUX);
486
487             at40k.cell(8,7).xlut(LUT_SELF);
488             at40k.cell(8,7).xi(NW);
489
490             at40k.cell(7,8).xlut(LUT_SELF & LUT_Z);
491             at40k.cell(7,8).xi(SE);
492             at40k.cell(7,8).c(XLUT);
493             at40k.cell(7,8).f(false);
494             at40k.cell(7,8).b(false);
495             at40k.cell(7,8).t(TMUX_FB);
496             at40k.cell(7,8).xo(false);
497             System.out.println(at40k.cell(7,8).fb_relevant());
498
499             at40k.cell(6,13).xi(SE);
500             at40k.cell(6,13).c(ZMUX);
501             at40k.cell(6,13).xlut(LUT_SELF);
502             at40k.cell(6,13).ylut(LUT_OTHER);
503             at40k.cell(6,13).xo(false);
504             at40k.cell(6,13).yo(false);
505             at40k.cell(7,12).xi(SE);
506
507             for(int i=0; i<24; i++) {
508                 at40k.iob_bot(i, true).enableOutput(NORTH);
509                 at40k.iob_bot(i, false).enableOutput(NW);
510                 at40k.cell(i, 0).xlut(0xff);
511                 at40k.cell(i, 0).ylut(0xff);
512             }
513
514             device.flush();
515
516             Gui vis = new Gui(at40k, device);
517             Frame fr = new Frame();
518             fr.addKeyListener(vis);
519             fr.setLayout(new BorderLayout());
520             fr.add(vis, BorderLayout.CENTER);
521             fr.pack();
522             fr.setSize(900, 900);
523             vis.repaint();
524             fr.repaint();
525             fr.show();
526             synchronized(Demo.class) { Demo.class.wait(); }
527
528
529
530             /*
531             Visualizer v = new Visualizer(at40k, device);
532             v.show();
533             v.setSize(1380, 1080);
534             Fpslic.Cell cell = at40k.cell(4, 23);
535             */
536             //Image img = v.createImage(v.getWidth(), v.getHeight());
537             /*
538             int x = 1;
539             int y = 14;
540             cell = at40k.cell(x,y);
541             scan(at40k, cell, YLUT, true);
542             cell.c(YLUT);
543             cell.b(false);
544             cell.f(false);
545             cell.oe(NONE);
546             cell.ylut(0xff);
547             */
548             //int x = 5;
549             //int y = 11;
550
551             //selfTest(device, at40k, v);
552             //System.out.println("save: " + FtdiBoard.save + " of " + (FtdiBoard.saveof*5));
553
554             at40k.iob_top(0, true).enableInput();
555             copy(at40k.cell(0, 23), NORTH, NORTH);
556             at40k.iob_bot(0, true).enableOutput(NORTH);
557
558             for(int i=0; i<10000; i++) {
559                 //v.refresh();
560                 try { Thread.sleep(100); } catch (Exception e) { }
561             }
562             //cell.ylut(0x09);
563
564             //at40k.cell(0x01, 0x17).h(0, false);
565             //at40k.cell(0x01, 0x17).xi(NE);
566             //at40k.cell(0x01, 0x17).ylut((byte)0x55);
567
568             //at40k.cell(0x04, 0x17).xlut((byte)0x10);
569             //at40k.cell(0x04, 0x17).ylut((byte)0x10);
570             //at40k.cell(0x04, 0x17).yo(false);
571             //at40k.cell(0x04, 0x17).xo();
572
573             /*
574             at40k.cell(0x01, 0x17).xi(L0);
575             at40k.cell(0x01, 0x17).h(L0, true);
576             */
577             /*
578             at40k.cell(0x03, 0x17).xlut((byte)0x55);
579             at40k.cell(0x03, 0x17).ylut((byte)0x55);
580             at40k.cell(0x03, 0x17).yi(EAST);
581             at40k.cell(0x03, 0x17).ylut((byte)0x55);
582             at40k.cell(0x03, 0x17).yo(true);
583
584             at40k.cell(0x03, 0x17).f(false);
585             at40k.cell(0x03, 0x17).c(XLUT);
586             at40k.cell(0x03, 0x17).oe(NONE);
587             at40k.cell(0x03, 0x17).out(L0, true);
588
589             at40k.cell(0x02, 0x17).yi(EAST);
590             at40k.cell(0x02, 0x17).ylut((byte)0x55);
591             at40k.cell(0x02, 0x17).yo(false);
592
593             at40k.cell(0x01, 0x17).yi(EAST);
594             at40k.cell(0x01, 0x17).ylut((byte)0x55);
595             at40k.cell(0x01, 0x17).yo(false);
596
597             at40k.cell(0x01, 0x17).h(L0, true);
598             at40k.cell(0x01, 0x17).v(L0, false);
599             */
600             //at40k.cell(0x01, 0x17).yi(L0);
601             //at40k.cell(0x01, 0x17).xi(L0);
602             //at40k.cell(0x01, 0x17).ylut((byte)0x33);
603
604             /*
605             at40k.cell(0x03, 0x17).h(L0, true);
606             at40k.cell(0x03, 0x17).out(L0, true);
607             at40k.cell(0x03, 0x17).c(XLUT);
608             at40k.cell(0x03, 0x17).f(false);
609             */
610             /*
611             at40k.cell(0x01, 0x17).xin(4);
612             at40k.cell(0x01, 0x17).yin(4);
613             at40k.cell(0x01, 0x16).ylut((byte)0x00);
614             device.mode4(2, 0x17, 0x01, 0);
615
616             for(int i=0; i<10; i++) {
617                 Thread.sleep(3000);
618                 System.out.println("tick");
619                 //at40k.cell(0x01, 0x17).xlut((byte)0xFF);
620                 at40k.cell(0x00, 0x17).ylut((byte)0x00);
621                 device.flush();
622                 Thread.sleep(3000);
623                 System.out.println("tick");
624                 //at40k.cell(0x01, 0x17).xlut((byte)0x00);
625                 at40k.cell(0x00, 0x17).ylut((byte)0xFF);
626                 device.flush();
627             }
628             */
629
630
631             /*
632             at40k.iob_top(0, true).output(0);
633             at40k.iob_top(0, true).oe(false);
634             at40k.iob_top(0, true).pullup();
635             device.flush();
636             Thread.sleep(3000);
637
638             Log.info(Demo.class, "issuing command");
639             at40k.iob_top(1, true).pulldown();
640             device.flush();
641             */
642             Log.info(Demo.class, "done");
643             System.exit(0);
644         } catch (Exception e) { e.printStackTrace(); }
645     }
646
647     public static void scan(Fpslic dev, Fpslic.Cell cell, int source, boolean setup) {
648         if (setup) {
649             if (source != NONE) cell.c(source);
650             if (cell.b()) cell.b(false);
651             if (cell.f()) cell.f(false);
652         }
653         if (cell.out(L3)!=setup) cell.out(L3, setup);
654         if (cell.vx(L3)!=setup) cell.v(L3, setup);
655
656         Fpslic.SectorWire sw = cell.vwire(L3);
657         //System.out.println("wire is: " + sw);
658
659         if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
660             sw.north().drives(sw, false);
661         while(sw.row > (12 & ~0x3) && sw.south() != null) {
662             //System.out.println(sw + " -> " + sw.south());
663             if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
664             sw = sw.south();
665         }
666         if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
667             sw.north().drives(sw, false);
668         while(sw.row < (12 & ~0x3) && sw.north() != null) {
669             //System.out.println(sw + " -> " + sw.north());
670             if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
671             sw = sw.north();
672         }
673
674         //cell = dev.cell(19, 15);
675         cell = dev.cell(cell.col, 15);
676         /*
677         System.out.println("cell is " + cell);
678         cell.xlut(0xff);
679         cell.ylut(0xff);
680         cell.b(false);
681         cell.f(false);
682         cell.c(XLUT);
683         cell.out(L3, true);
684         cell.oe(NONE);
685         */
686         if (cell.hx(L3) != setup) cell.h(L3, setup);
687         if (cell.vx(L3) != setup) cell.v(L3, setup);
688         sw = cell.hwire(L3);
689
690         if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
691         while(sw.east() != null) {
692             //System.out.println(sw + " -> " + sw.east());
693             if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
694             sw = sw.east();
695         }
696
697     }
698
699     public static void copy(Fpslic.Cell c, int xdir, int ydir) {
700         switch(xdir) {
701             case NW: case NE: case SW: case SE: {
702                 c.xi(xdir);
703                 c.xlut(LUT_SELF);
704                 break;
705             }
706             case NORTH: case SOUTH: case EAST: case WEST: {
707                 c.yi(xdir);
708                 c.xlut(LUT_OTHER);
709                 break;
710             }
711             case NONE: break;
712             default: throw new Error();
713         }
714         switch(ydir) {
715             case NW: case NE: case SW: case SE: {
716                 c.xi(ydir);
717                 c.ylut(LUT_OTHER);
718                 break;
719             }
720             case NORTH: case SOUTH: case EAST: case WEST: {
721                 c.yi(ydir);
722                 c.ylut(LUT_SELF);
723                 break;
724             }
725             case NONE: break;
726             default: throw new Error();
727         }
728         c.xo(false);
729         c.yo(false);
730     }
731     public static String hex(int x) {
732         return Long.toString(x & 0xffffffffL, 16);
733     }
734
735     public static void handshaker(Fpslic.Cell cell) {
736         cell.xlut(0x22);
737         cell.ylut(0x71);
738         cell.c(XLUT);
739         cell.f(false);
740         cell.t(false, false, true);
741     }
742
743
744     private static String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
745     public static String bin8(byte b) {
746         int n = b & 0xff;
747         String ret = "";
748         for(int i=7; i>=0; i--)
749             ret += (n & (1<<i))==0 ? "0" : "1";
750         return ret;
751     }
752
753     public static void bounce(Fpslic.Cell cell, int xi, int yi) {
754         cell.xlut((byte)0xCC);
755         cell.ylut((byte)0xCC);
756         cell.xi(xi);
757         cell.yi(yi);
758         cell.xo(false);
759         cell.yo(false);
760     }
761     public static void muller(Fpslic.Cell cell, int xi, int yi) {
762         cell.ylut(0xB2);
763         cell.c(YLUT);
764         cell.f(false);
765         cell.t(false, false, true);
766         cell.xi(xi);
767         cell.yi(yi);
768         cell.yo(false);
769         cell.xo(false);
770     }
771
772     /** watches for a rising/falling edge on Yin, emits a pulse on Xout */
773     public static void pulse_detect(Fpslic.Cell c, int in, boolean falling) {
774         c.ylut(0x00);
775         c.xlut(0x00);
776         switch(in) {
777             case NW: case NE: case SW: case SE: {
778                 c.xi(in);
779                 loopback(c, XLUT);
780                 if (!falling) c.ylut(FpslicUtil.lutSwap(0x0C)); /* x & !z */
781                 else          c.ylut(FpslicUtil.lutSwap(0x30)); /* !x & z */
782                 c.xlut(LUT_SELF);
783                 break;
784             }
785             case NORTH: case SOUTH: case EAST: case WEST: {
786                 c.yi(in);
787                 loopback(c, YLUT);
788                 if (!falling) c.xlut(0x0C); /* y & !z */
789                 else          c.xlut(0x30); /* !y & z */
790                 c.ylut(LUT_SELF);
791                 break;
792             }
793             default: throw new Error();
794         }
795     }
796
797     /** watches for a pulse on Xin, copies value of Yin */
798     public static void pulse_copy(Fpslic.Cell cell, int xi, int yi, boolean invert) {
799         loopback(cell, YLUT);
800         if (!invert) cell.ylut(0xB8);   /* yo = x ?  yi : z => 1011 1000 */
801         else         cell.ylut(0x74);   /* yo = x ? !yi : z => 0111 0100 */
802         if (!invert) cell.xlut(FpslicUtil.lutSwap(0xB8));   /* yo = x ?  yi : z => 1011 1000 */
803         else         cell.xlut(FpslicUtil.lutSwap(0x74));   /* yo = x ? !yi : z => 0111 0100 */
804         cell.xi(xi);
805         cell.yi(yi);
806     }
807
808     public static void loopback(Fpslic.Cell cell, int cin) {
809         cell.f(false);
810         cell.b(false);
811         cell.t(false, false, true);
812         cell.yo(false);
813         cell.xo(false);
814         cell.c(cin);
815     }
816
817     public static void doit(Fpslic at40k, FtdiBoard device) throws Exception {
818
819         Fpslic.Cell led = at40k.cell(1, 23);
820         led.v(L2, true);
821         led.h(L2, false);
822         led.yi(L2);
823         led.ylut(~LUT_SELF);
824         led.xlut(LUT_SELF);
825         led.yo(false);
826
827         Fpslic.Cell c = at40k.cell(1, 22);
828         c.out(L1, true);
829         c.out(L0, true);
830         c.oe(V4);
831         c.ylut(0xff);
832         c.h(L1, true);
833         c.h(L0, false);
834
835         c.v(L0, /*false*/true);
836
837         c.v(L1, true);
838         c.f(false);
839         c.b(false);
840         c.c(YLUT);
841
842         for(int i=0; i<4; i++) at40k.cell(i, 20).h(L0, false);
843         Fpslic.Cell z = at40k.cell(1, 20);
844         z.out(L0, true);
845         z.xlut(0xff);
846         z.c(XLUT);
847         z.yi(L0);
848         z.ylut(~LUT_SELF);
849         z.v(L0, true);
850         //z.h(L0, true);
851         z.h(L0, false);
852         z.f(false);
853         z.b(false);
854         z.hwire(L0).east().drives(z.hwire(L0), false);
855         z.hwire(L1).east().drives(z.hwire(L1), false);
856         z.vwire(L0).south().drives(z.vwire(L0), false);
857         z.vwire(L1).south().drives(z.vwire(L1), false);
858         z.oe(H4);
859
860         z = at40k.cell(0, 20);
861         z.oe(NONE);
862         z.out(L0, true);
863         z.out(L1, true);
864         z.out(L2, true);
865         //z.out(L3, true);
866         z.out(L4, true);
867         z.h(L0, true);
868         z.h(L1, true);
869         z.h(L2, true);
870         //z.h(L3, true);
871         z.h(L4, true);
872         z.f(false);
873         z.b(false);
874         z.yi(EAST);
875         z.ylut(LUT_SELF);
876         z.c(YLUT);
877
878         for(int y=20; y<=22; y++)
879             for(int x=2; x<=5; x++) {
880                 c = at40k.cell(x, y);
881                 copy(c, NW, WEST);
882             }
883
884         //c = at40k.cell(2, 22);
885         //c.h(L0, true);
886         //c.yi(L0);
887
888         c = at40k.cell(1, 21);
889         c.v(L0, true);
890         c.v(L2, true);
891         c.yi(L0);
892         c.out(L2, true);
893         c.ylut(LUT_SELF);
894         c.c(YLUT);
895         c.b(false);
896         c.f(false);
897         c.oe(NONE);
898         c.yo(false);
899
900         
901
902         c = at40k.cell(13, 22);
903         c.xlut(LUT_OTHER | 0xF0);
904         c.c(XLUT);
905         c.t(false, false, true);
906         c.b(false);
907         c.f(false);
908         c.ylut(0xF0);
909         c.yi(EAST);
910         c.yo(false);
911         /*
912         // this gate detects a rising edge on its Xin (delayed copy on Yin); when viewed, it inverts its state
913         c = at40k.cell(14, 22);
914         c.ylut(0x00);
915         c.c(XLUT);
916         c.f(false);
917         c.b(false);
918         c.t(false, false, true);
919         c.xi(SE);
920         c.yi(SOUTH);
921         c.yo(false);
922         c.xo(false);
923         c.ylut(0xA6); // (x & !z) ? ~y : y
924         c.xlut(LUT_SELF); 
925
926         c = at40k.cell(14, 20);
927         c.ylut(LUT_OTHER);
928         c.xi(NE);
929         c = at40k.cell(14, 21);
930         c.ylut(LUT_SELF);
931         c.xi(SOUTH);
932
933         c = at40k.cell(13, 22);
934         c.xlut(0x00);
935         c.xlut(LUT_OTHER);// | 0xF0);
936 */
937         //c = at40k.cell(13, 22);
938         //copy(c, NW, EAST);
939         /*
940         c.ylut(0x00);
941         c.c(YLUT);
942         c.f(false);
943         c.b(false);
944         c.t(false, false, true);
945         c.xi(SE);
946         c.yi(NORTH);
947         c.yo(false);
948         c.xo(false);
949         c.ylut(0x54);  // (x || z) & !y
950         */
951
952         /*        
953         c = at40k.cell(2, 21);
954         c.ylut(0x00);
955         c.c(YLUT);
956         c.f(false);
957         c.b(false);
958         c.t(false, false, true);
959         c.xi(SE);
960         c.yi(WEST);
961         c.yo(false);
962         c.xo(false);
963         c.ylut(0xE8);
964  
965        //at40k.cell(2, 21).xlut(0xF0);
966
967         at40k.cell(3, 22).ylut(LUT_OTHER);
968         at40k.cell(3, 22).xi(SW);
969         */
970         //at40k.iob_top(5, true).enableOutput(SOUTH);
971         //at40k.iob_top(5, false).enableOutput(SE);
972     }
973
974     public static int yofs = mullers ? 19 : 22;
975     public static void counter(Fpslic at40k, FtdiBoard device) throws Exception {
976         // watch for rising edge from south, emit pulse on Xout (to NE)
977         //copy(at40k.cell(16,23), SW, WEST);
978         
979         for(int x=22; x>=1; x-=2) {
980             pulse_detect(at40k.cell(x, yofs), SE,      false);
981             pulse_detect(at40k.cell(x, yofs-1), EAST,    true);
982             pulse_copy(at40k.cell(x-1, yofs), SE, SOUTH, false);
983             pulse_copy(at40k.cell(x-1, yofs-1), NE, NORTH, true);
984
985             //pulse_detect(at40k.cell(15, 22), NORTH, false);
986             //pulse_detect(at40k.cell(16, 22), NW,    true);
987             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
988             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
989         }
990         for(int x=23; x>1; x-=2) {
991             pulse_detect(at40k.cell(x-1, yofs-2), SW,    false);
992             pulse_detect(at40k.cell(x-1, yofs-3), WEST,  true);
993             pulse_copy(at40k.cell(x, yofs-2), SW, SOUTH, false);
994             pulse_copy(at40k.cell(x, yofs-3), NW, NORTH, true);
995
996             //pulse_detect(at40k.cell(15, 22), NORTH, false);
997             //pulse_detect(at40k.cell(16, 22), NW,    true);
998             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
999             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1000         }
1001         copy(at40k.cell(1, yofs-2), SOUTH, SOUTH);
1002         copy(at40k.cell(1, yofs-3), NORTH, NORTH);
1003         at40k.cell(1, yofs-3).ylut(~at40k.cell(1, yofs-3).ylut());
1004         at40k.cell(1, yofs-3).xlut(~at40k.cell(1, yofs-3).xlut());
1005
1006         copy(at40k.cell(23, yofs), SOUTH, SOUTH);
1007         copy(at40k.cell(23, yofs-1), SOUTH, SOUTH);
1008
1009         for(int i=23; i>yofs; i--) copy(at40k.cell(1, i), SOUTH, SOUTH);
1010
1011         //at40k.iob_top(1, true).slew(SLOW);
1012         //at40k.iob_top(1, false).slew(SLOW);
1013
1014     }
1015
1016     public static void fill(Fpslic at40k, FtdiBoard device, int num) throws Exception {
1017         //muller(at40k.cell(PIPELEN,22), NE, WEST);
1018         Fpslic.Cell a = at40k.cell(10,22);
1019         Fpslic.Cell b = at40k.cell(11,22);
1020         a.ylut(0x00);
1021         for(int i=0; i<num; i++) {
1022             //System.out.println(i);
1023             b.lut(0xff, 0xff);
1024             device.flush();
1025             try { Thread.sleep(1); } catch (Exception e) { }
1026             b.lut(0x00, 0x00);
1027             device.flush();
1028             try { Thread.sleep(1); } catch (Exception e) { }
1029         }
1030         b.ylut(0xB2);
1031         a.ylut(0xB2);
1032     }
1033
1034     public static void drain(Fpslic at40k, FtdiBoard device) throws Exception {
1035         Fpslic.Cell a = at40k.cell(10,22);
1036         Fpslic.Cell b = at40k.cell(11,22);
1037         a.lut(0x00, 0x00);
1038         b.lut(0x00, 0x00);
1039         for(int i=0; i<30; i++) {
1040             //System.out.println(i);
1041             a.lut(0xff, 0xff);
1042             device.flush();
1043             try { Thread.sleep(1); } catch (Exception e) { }
1044             a.lut(0x00, 0x00);
1045             device.flush();
1046             try { Thread.sleep(1); } catch (Exception e) { }
1047         }
1048         b.ylut(0xB2);
1049         a.ylut(0xB2);
1050     }
1051
1052     public static void doitx(Fpslic at40k, FtdiBoard device) throws Exception {
1053         for(int i=5; i<PIPELEN+1; i++) bounce(at40k.cell(i, 23), SE,                     SOUTH);
1054         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 22), x==PIPELEN-1 ? SE : NE, WEST);
1055         
1056         bounce(at40k.cell(PIPELEN,  21), NW, WEST);
1057         
1058         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 21), SW,                     x==PIPELEN-1 ? NORTH : EAST);
1059         for(int x=4; x<PIPELEN+1; x++) bounce(at40k.cell(x, 20), NW,                     NORTH);
1060         
1061         bounce(at40k.cell(4, 22), SE, EAST);
1062         //muller(at40k.cell(4PIPELEN-1,21), SW, NORTH);
1063         
1064         //muller(at40k.cell(4,22), NE, WEST);
1065         //at40k.cell(4,22).ylut(0xEE);
1066         muller(at40k.cell(5, 22), NE, SOUTH);
1067         muller(at40k.cell(5, 21), NW, EAST);
1068         /*
1069         for(int x=4; x>=0; x--) {
1070             at40k.cell(x, 21).ylut(0xAA);
1071             at40k.cell(x, 21).yi(EAST);
1072             at40k.cell(x, 21).yo(false);
1073         }
1074
1075         at40k.cell(0, 22).ylut(0xAA);
1076         at40k.cell(0, 22).yi(SOUTH);
1077         at40k.cell(0, 22).yo(false);
1078
1079         at40k.cell(0, 23).ylut(~0xAA);
1080         at40k.cell(0, 23).xlut(~0xcc);
1081         at40k.cell(0, 23).yi(SOUTH);
1082         at40k.cell(0, 23).yo(false);
1083         */
1084         for(int x=3; x<=23; x+=2) {
1085             pulse_detect(at40k.cell(x-1, 19), SW,    false);
1086             pulse_detect(at40k.cell(x-1, 18), WEST,  true);
1087             pulse_copy(at40k.cell(x, 19), SW, SOUTH, false);
1088             pulse_copy(at40k.cell(x, 18), NW, NORTH, true);
1089
1090             if (x<17) {
1091                 pulse_detect(at40k.cell(x-1, 16), SW,    false);
1092                 pulse_detect(at40k.cell(x-1, 15), WEST,  true);
1093                 pulse_copy(at40k.cell(x, 16), SW, SOUTH, false);
1094                 pulse_copy(at40k.cell(x, 15), NW, NORTH, true);
1095             }
1096             //pulse_detect(at40k.cell(15, 22), NORTH, false);
1097             //pulse_detect(at40k.cell(16, 22), NW,    true);
1098             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
1099             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1100         }
1101         for(int x=14; x>=1; x--)
1102             copy(at40k.cell(x, 17), EAST, EAST);
1103         for(int x=4; x>=0; x--)
1104             copy(at40k.cell(x, 21), EAST, EAST);
1105         copy(at40k.cell(13, 17), SOUTH, SOUTH);
1106
1107         copy(at40k.cell(0, 20), NORTH, NORTH);
1108         copy(at40k.cell(0, 19), NORTH, NORTH);
1109         copy(at40k.cell(0, 18), NORTH, NORTH);
1110         copy(at40k.cell(0, 17), NORTH, NORTH);
1111         copy(at40k.cell(0, 16), NORTH, NORTH);
1112         copy(at40k.cell(1, 16), WEST, WEST);
1113         copy(at40k.cell(1, 15), NORTH, NORTH);
1114
1115         copy(at40k.cell(1, 20), SOUTH, SOUTH);
1116         copy(at40k.cell(1, 19), SOUTH, SOUTH);
1117         copy(at40k.cell(1, 18), SOUTH, SOUTH);
1118
1119         for(int y=20; y<=23; y++)
1120             copy(at40k.cell(23, y), SOUTH, SOUTH);
1121
1122
1123         //for(int x=19; x<=23; x++)
1124         //copy(at40k.cell(x, 0), WEST, WEST);
1125         //copy(at40k.cell(18, 19), NW, NW);
1126         //at40k.iob_top(5, true).enableOutput(SOUTH);
1127         //at40k.iob_top(5, false).enableOutput(SOUTH);
1128     }
1129 }