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