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