a06cf8fd92aa070a2f9228c2949f0244ba4f3509
[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 java.awt.*;
8 import java.awt.event.*;
9 import java.awt.color.*;
10 import org.ibex.util.*;
11 import java.io.*;
12 import java.util.*;
13
14 public class Demo {
15
16     public static boolean mullers = true;
17     public static int masterx = 1;
18
19     public static int PIPELEN=20;
20
21     public static void main(String[] s) throws Exception {
22         SlipwayBoard slipway = new SlipwayBoard();
23         FpslicDevice device = slipway.getFpslicDevice();
24         FpslicDevice at40k = device;
25         try {
26             Log.info(Demo.class, "issuing command");
27
28             //at40k.iob_top(2, true).oe(false);
29             //at40k.iob_top(2, false).oe(false);
30             //at40k.iob_top(1, true).oe(false);
31
32             // this command confirmed to turn *on* led0
33             //at40k.iob_top(1, false).output(0);
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             Gui vis = new Gui(device, slipway);
205             Frame fr = new Frame();
206             fr.addKeyListener(vis);
207             fr.setLayout(new BorderLayout());
208             fr.add(vis, BorderLayout.CENTER);
209             fr.pack();
210             fr.setSize(900, 900);
211             vis.repaint();
212             fr.repaint();
213             fr.show();
214             synchronized(Demo.class) { Demo.class.wait(); }
215             /* LAST
216                System.out.println("doit");
217                if (mullers) doitx(at40k, device);
218                //System.out.println("counter");
219                //counter(at40k, device);
220
221                at40k.cell(21,15).yi(WEST);
222                at40k.cell(21,15).ylut(0xAA);
223
224                at40k.cell(22,15).yi(WEST);
225                at40k.cell(22,15).ylut(0xAA);
226             */
227
228             FpslicDevice.Cell root = at40k.cell(10,20);
229             
230             root.yo(root.north());
231             root.ylut(~LUT_SELF);
232             root.c(YLUT);
233             root = root.north();
234
235             root.yo(root.east());
236             root.ylut(~LUT_SELF);
237             root.c(YLUT);
238             root = root.east();
239
240             root.yo(root.south());
241             root.ylut(~LUT_SELF);
242             root.c(YLUT);
243             root = root.south();
244
245             root.yo(root.west());
246             root.c(YLUT);
247             root = root.west();
248
249             //////////////////////////////////////////////////////////////////////////////
250
251             at40k.cell(23,15).h(3, true);
252             at40k.cell(23,15).yi(L3);
253             at40k.cell(23,15).ylut(0xAA);
254             at40k.iob_right(15, true).enableOutput(WEST);
255
256
257             FpslicDevice.Cell c = at40k.cell(10,10);
258             c.ylut(~LUT_SELF);
259             c.xlut(LUT_Z);
260             c.yi(WEST);
261             c.c(YLUT);
262             c.f(false);
263             c.t(TMUX_FB);
264             copy(c.west(), EAST, NW);
265             copy(c.west().north().west(), SE, SE);
266
267             c = c.east();
268             c.ylut(~LUT_SELF);
269             c.xlut(LUT_Z);
270             c.yi(EAST);
271             c.c(YLUT);
272             c.f(false);
273             c.t(TMUX_FB);
274             copy(c.east(), WEST, SE);
275             copy(c.east().south().east(), NW, NW);
276
277             c = c.north();
278             copy(c.north(), SOUTH, SOUTH);
279             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
280             c.ylut(LUT_Z);
281             c.yi(SOUTH);
282             c.c(XLUT);
283             c.xi(SW);
284             c.wi(L4);
285             c.f(false);
286             c.t(TMUX_W_AND_FB);
287             c.v(L4, false);
288             c.h(L4, true);
289             c.v(L2, false);
290             c.h(L2, true);
291
292             c = c.west();
293             copy(c.north(), SOUTH, SOUTH);
294             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
295             c.ylut(~LUT_Z);
296             c.yi(SOUTH);
297             c.xi(SE);
298             c.c(XLUT);
299             c.wi(L4);
300             c.f(false);
301             c.t(TMUX_W_AND_FB);
302             c.v(L4, false);
303             c.h(L4, true);
304             c.v(L2, false);
305             c.h(L2, true);
306
307             c = c.west();
308             c.v(L4, false);
309             c.h(L4, true);
310             c.out(L4, true);
311             c.f(false);
312             c.b(false);
313             c.oe(NONE);
314             c.c(YLUT);
315             c.hwire(L4).west().drives(c.hwire(L4), false);
316             c.hwire(L4).east().drives(c.hwire(L4), false);
317
318             c = c.south();
319             c = c.south();
320             c.v(L4, false);
321             c.h(L4, true);
322             c.out(L4, true);
323             c.f(false);
324             c.b(false);
325             c.oe(NONE);
326             c.c(YLUT);
327             c.hwire(L4).west().drives(c.hwire(L4), false);
328             c.hwire(L4).east().drives(c.hwire(L4), false);
329
330             c = c.east();
331             c = c.east();
332             copy(c.south(), NORTH, NORTH);
333             c.xlut(((~LUT_SELF) & (~LUT_OTHER)) | LUT_Z);
334             c.ylut(LUT_Z);
335             c.yi(NORTH);
336             c.c(XLUT);
337             c.xi(NW);
338             c.wi(L4);
339             c.f(false);
340             c.t(TMUX_W_AND_FB);
341             c.v(L4, false);
342             c.h(L4, true);
343             c.v(L2, false);
344             c.h(L2, true);
345
346             c = c.west();
347             copy(c.south(), NORTH, NORTH);
348             c.xlut((LUT_SELF & LUT_OTHER) | LUT_Z);
349             c.ylut(LUT_Z);
350             c.yi(NORTH);
351             c.xi(NE);
352             c.c(XLUT);
353             c.wi(L4);
354             c.f(false);
355             c.t(TMUX_W_AND_FB);
356             c.v(L4, false);
357             c.h(L4, true);
358             c.v(L2, false);
359             c.h(L2, true);
360
361
362             // catch a rising transition
363             /*
364               c = c.west();
365               c.v(L2, false);
366               c.h(L2, true);
367               c.out(L2, true);
368               c.f(false);
369               c.b(false);
370               c.oe(NONE);
371               c.c(YLUT);
372             */
373             c.hwire(L2).west().drives(c.hwire(L2), false);
374             c.hwire(L2).east().drives(c.hwire(L2), false);
375
376
377
378             //////
379
380             c = at40k.cell(20,20);
381             c.yi(WEST);
382             c.ylut(LUT_SELF);
383             c.c(YLUT);
384             c.oe(H4);
385             c.h(L4, true);
386             c.b(false);
387             c.f(false);
388             c.out(L4);
389
390             c = at40k.cell(21,20);
391             c.c(YLUT);
392             c.oe(NONE);
393             c.h(L4, true);
394             c.b(false);
395             c.f(false);
396
397
398             c = at40k.cell(8,8);
399             c.f(true);
400             c.b(true);
401             c.xo(true);
402             c.xi(NE);
403             c.zi(L3);
404             c.wi(L0);
405             c.yi(NORTH);
406             c.oe(H4);
407             c.h(L0, true);
408             c.h(L2, true);
409             c.h(L4, true);
410             c.v(L1, true);
411             c.v(L3, true);
412             c.out(L0, true);
413             c.out(L1, true);
414             c.out(L2, true);
415             c.out(L3, true);
416             c.out(L4, true);
417             c.xo(true);
418             c.yo(true);
419             c.c(ZMUX);
420
421             at40k.cell(9,10).xo(true);
422             at40k.cell(9,10).yo(true);
423             at40k.cell(9,10).c(YLUT);
424             at40k.cell(9,10).b(false);
425
426             //for(int x=5; x<PIPELEN; x++) {
427             //at40k.cell(x,23).hwire(L0).drives(at40k.cell(x,23).hwire(L0).east());
428             //}
429
430             /*
431               at40k.cell(22,11).ylut(0xff);
432               at40k.cell(23,11).yi(L3);
433               //at40k.cell(23,11).yi(WEST);
434               //at40k.cell(23,11).xi(L1);
435               at40k.cell(23,11).ylut(0xAA);
436               at40k.iob_right(11, true).enableOutput(WEST);
437               at40k.cell(23,11).v(L3, true);
438               at40k.cell(23,11).yo(false);
439               //at40k.flush();
440               */
441             int vx=04;
442             int vv=23;
443             /*
444               System.out.println("correct: " + at40k.cell(19,15).hwire(L3) + " drives " + at40k.cell(20,15).hwire(L3));
445               System.out.println("correct: " + at40k.cell(15,15).hwire(L3) + " drives " + at40k.cell(19,15).hwire(L3));
446               System.out.println("correct: " + at40k.cell(11,15).hwire(L3) + " drives " + at40k.cell(15,15).hwire(L3));
447               System.out.println("correct: " + at40k.cell(07,15).hwire(L3) + " drives " + at40k.cell(11,15).hwire(L3));
448
449               at40k.cell(19,15).hwire(L3).drives(at40k.cell(20,15).hwire(L3), true);
450               at40k.cell(15,15).hwire(L3).drives(at40k.cell(19,15).hwire(L3), true);
451               at40k.cell(11,15).hwire(L3).drives(at40k.cell(15,15).hwire(L3), true);
452               at40k.cell(07,15).hwire(L3).drives(at40k.cell(11,15).hwire(L3), true);
453             */
454             //at40k.cell(05,vv).xlut(0xff);
455             //at40k.cell(05,vv).ylut(0xff);
456             /*
457               at40k.cell(vx,vv).c(YLUT);
458               at40k.cell(vx,vv).f(false);
459               at40k.cell(vx,vv).b(false);
460               at40k.cell(vx,vv).oe(NONE);
461               at40k.cell(vx,vv).v(L3, true);
462               at40k.cell(vx,vv).out(L3, true);
463             */
464             /*
465               at40k.cell(vx,15).v(L3, true);
466               at40k.cell(vx,15).h(L3, true);
467               at40k.cell(vx,19).vwire(L3).drives(at40k.cell(vx,15).vwire(L3), true);
468               at40k.cell(vx,23).vwire(L3).drives(at40k.cell(vx,19).vwire(L3), true);
469             */
470
471             //at40k.cell(5,23).ylut(0x00);
472             //at40k.cell(6,22).ylut(0xff);
473             //at40k.cell(22,11).ylut(0xff);
474             /*
475               FpslicDevice.Cell cell = at40k.cell(4, 16);
476               cell.xlut(0xff);
477               cell.ylut(0xff);
478               cell.b(false);
479               cell.f(false);
480               cell.c(XLUT);
481               cell.h(L3, true);
482               cell.v(L3, true);
483               cell.out(L3, true);
484               cell.oe(NONE);
485             */
486             //scan(at40k, cell, YLUT, true);
487             //scan(at40k, cell, YLUT, false);
488
489             //device.scanFPGA(true);
490
491             at40k.cell(10,10).f(true);
492             at40k.cell(10,10).c(ZMUX);
493
494             at40k.cell(8,7).xlut(LUT_SELF);
495             at40k.cell(8,7).xi(NW);
496
497             at40k.cell(7,8).xlut(LUT_SELF & LUT_Z);
498             at40k.cell(7,8).xi(SE);
499             at40k.cell(7,8).c(XLUT);
500             at40k.cell(7,8).f(false);
501             at40k.cell(7,8).b(false);
502             at40k.cell(7,8).t(TMUX_FB);
503             at40k.cell(7,8).xo(false);
504             System.out.println(at40k.cell(7,8).fb_relevant());
505
506             at40k.cell(6,13).xi(SE);
507             at40k.cell(6,13).c(ZMUX);
508             at40k.cell(6,13).xlut(LUT_SELF);
509             at40k.cell(6,13).ylut(LUT_OTHER);
510             at40k.cell(6,13).xo(false);
511             at40k.cell(6,13).yo(false);
512             at40k.cell(7,12).xi(SE);
513
514             for(int i=0; i<24; i++) {
515                 at40k.iob_bot(i, true).enableOutput(NORTH);
516                 at40k.iob_bot(i, false).enableOutput(NW);
517                 at40k.cell(i, 0).xlut(0xff);
518                 at40k.cell(i, 0).ylut(0xff);
519             }
520
521             device.flush();
522
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               FpslicDevice.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: " + SlipwayBoard.save + " of " + (SlipwayBoard.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.getFpslicDevice().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.getFpslicDevice().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.getFpslicDevice().flush();
641               Thread.sleep(3000);
642
643               Log.info(Demo.class, "issuing command");
644               at40k.iob_top(1, true).pulldown();
645               device.getFpslicDevice().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(FpslicDevice.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(FpslicDevice.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(FpslicDevice.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(FpslicDevice.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     public static int lutSwap(int x) {
727         return
728             (x & 0x80)        |
729             ((x & 0x20) << 1) |
730             ((x & 0x40) >> 1) |
731             (x & 0x10) |
732             (x & 0x08)        |
733             ((x & 0x02) << 1) |
734             ((x & 0x04) >> 1) |
735             (x & 0x01);
736     }
737
738     /** watches for a rising/falling edge on Yin, emits a pulse on Xout */
739     public static void pulse_detect(FpslicDevice.Cell c, int in, boolean falling) {
740         c.ylut(0x00);
741         c.xlut(0x00);
742         switch(in) {
743             case NW: case NE: case SW: case SE: {
744                 c.xi(in);
745                 loopback(c, XLUT);
746                 if (!falling) c.ylut(lutSwap(0x0C)); /* x & !z */
747                 else          c.ylut(lutSwap(0x30)); /* !x & z */
748                 c.xlut(LUT_SELF);
749                 break;
750             }
751             case NORTH: case SOUTH: case EAST: case WEST: {
752                 c.yi(in);
753                 loopback(c, YLUT);
754                 if (!falling) c.xlut(0x0C); /* y & !z */
755                 else          c.xlut(0x30); /* !y & z */
756                 c.ylut(LUT_SELF);
757                 break;
758             }
759             default: throw new Error();
760         }
761     }
762
763     /** watches for a pulse on Xin, copies value of Yin */
764     public static void pulse_copy(FpslicDevice.Cell cell, int xi, int yi, boolean invert) {
765         loopback(cell, YLUT);
766         if (!invert) cell.ylut(0xB8);   /* yo = x ?  yi : z => 1011 1000 */
767         else         cell.ylut(0x74);   /* yo = x ? !yi : z => 0111 0100 */
768         if (!invert) cell.xlut(lutSwap(0xB8));   /* yo = x ?  yi : z => 1011 1000 */
769         else         cell.xlut(lutSwap(0x74));   /* yo = x ? !yi : z => 0111 0100 */
770         cell.xi(xi);
771         cell.yi(yi);
772     }
773
774     public static void loopback(FpslicDevice.Cell cell, int cin) {
775         cell.f(false);
776         cell.b(false);
777         cell.t(false, false, true);
778         cell.yo(false);
779         cell.xo(false);
780         cell.c(cin);
781     }
782
783     public static void doit(FpslicDevice at40k, SlipwayBoard device) throws Exception {
784
785         FpslicDevice.Cell led = at40k.cell(1, 23);
786         led.v(L2, true);
787         led.h(L2, false);
788         led.yi(L2);
789         led.ylut(~LUT_SELF);
790         led.xlut(LUT_SELF);
791         led.yo(false);
792
793         FpslicDevice.Cell c = at40k.cell(1, 22);
794         c.out(L1, true);
795         c.out(L0, true);
796         c.oe(V4);
797         c.ylut(0xff);
798         c.h(L1, true);
799         c.h(L0, false);
800
801         c.v(L0, /*false*/true);
802
803         c.v(L1, true);
804         c.f(false);
805         c.b(false);
806         c.c(YLUT);
807
808         for(int i=0; i<4; i++) at40k.cell(i, 20).h(L0, false);
809         FpslicDevice.Cell z = at40k.cell(1, 20);
810         z.out(L0, true);
811         z.xlut(0xff);
812         z.c(XLUT);
813         z.yi(L0);
814         z.ylut(~LUT_SELF);
815         z.v(L0, true);
816         //z.h(L0, true);
817         z.h(L0, false);
818         z.f(false);
819         z.b(false);
820         z.hwire(L0).east().drives(z.hwire(L0), false);
821         z.hwire(L1).east().drives(z.hwire(L1), false);
822         z.vwire(L0).south().drives(z.vwire(L0), false);
823         z.vwire(L1).south().drives(z.vwire(L1), false);
824         z.oe(H4);
825
826         z = at40k.cell(0, 20);
827         z.oe(NONE);
828         z.out(L0, true);
829         z.out(L1, true);
830         z.out(L2, true);
831         //z.out(L3, true);
832         z.out(L4, true);
833         z.h(L0, true);
834         z.h(L1, true);
835         z.h(L2, true);
836         //z.h(L3, true);
837         z.h(L4, true);
838         z.f(false);
839         z.b(false);
840         z.yi(EAST);
841         z.ylut(LUT_SELF);
842         z.c(YLUT);
843
844         for(int y=20; y<=22; y++)
845             for(int x=2; x<=5; x++) {
846                 c = at40k.cell(x, y);
847                 copy(c, NW, WEST);
848             }
849
850         //c = at40k.cell(2, 22);
851         //c.h(L0, true);
852         //c.yi(L0);
853
854         c = at40k.cell(1, 21);
855         c.v(L0, true);
856         c.v(L2, true);
857         c.yi(L0);
858         c.out(L2, true);
859         c.ylut(LUT_SELF);
860         c.c(YLUT);
861         c.b(false);
862         c.f(false);
863         c.oe(NONE);
864         c.yo(false);
865
866         
867
868         c = at40k.cell(13, 22);
869         c.xlut(LUT_OTHER | 0xF0);
870         c.c(XLUT);
871         c.t(false, false, true);
872         c.b(false);
873         c.f(false);
874         c.ylut(0xF0);
875         c.yi(EAST);
876         c.yo(false);
877         /*
878         // this gate detects a rising edge on its Xin (delayed copy on Yin); when viewed, it inverts its state
879         c = at40k.cell(14, 22);
880         c.ylut(0x00);
881         c.c(XLUT);
882         c.f(false);
883         c.b(false);
884         c.t(false, false, true);
885         c.xi(SE);
886         c.yi(SOUTH);
887         c.yo(false);
888         c.xo(false);
889         c.ylut(0xA6); // (x & !z) ? ~y : y
890         c.xlut(LUT_SELF); 
891
892         c = at40k.cell(14, 20);
893         c.ylut(LUT_OTHER);
894         c.xi(NE);
895         c = at40k.cell(14, 21);
896         c.ylut(LUT_SELF);
897         c.xi(SOUTH);
898
899         c = at40k.cell(13, 22);
900         c.xlut(0x00);
901         c.xlut(LUT_OTHER);// | 0xF0);
902         */
903         //c = at40k.cell(13, 22);
904         //copy(c, NW, EAST);
905         /*
906           c.ylut(0x00);
907           c.c(YLUT);
908           c.f(false);
909           c.b(false);
910           c.t(false, false, true);
911           c.xi(SE);
912           c.yi(NORTH);
913           c.yo(false);
914           c.xo(false);
915           c.ylut(0x54);  // (x || z) & !y
916         */
917
918         /*        
919                   c = at40k.cell(2, 21);
920                   c.ylut(0x00);
921                   c.c(YLUT);
922                   c.f(false);
923                   c.b(false);
924                   c.t(false, false, true);
925                   c.xi(SE);
926                   c.yi(WEST);
927                   c.yo(false);
928                   c.xo(false);
929                   c.ylut(0xE8);
930  
931                   //at40k.cell(2, 21).xlut(0xF0);
932
933                   at40k.cell(3, 22).ylut(LUT_OTHER);
934                   at40k.cell(3, 22).xi(SW);
935         */
936         //at40k.iob_top(5, true).enableOutput(SOUTH);
937         //at40k.iob_top(5, false).enableOutput(SE);
938     }
939
940     public static int yofs = mullers ? 19 : 22;
941     public static void counter(FpslicDevice at40k, SlipwayBoard device) throws Exception {
942         // watch for rising edge from south, emit pulse on Xout (to NE)
943         //copy(at40k.cell(16,23), SW, WEST);
944         
945         for(int x=22; x>=1; x-=2) {
946             pulse_detect(at40k.cell(x, yofs), SE,      false);
947             pulse_detect(at40k.cell(x, yofs-1), EAST,    true);
948             pulse_copy(at40k.cell(x-1, yofs), SE, SOUTH, false);
949             pulse_copy(at40k.cell(x-1, yofs-1), NE, NORTH, true);
950
951             //pulse_detect(at40k.cell(15, 22), NORTH, false);
952             //pulse_detect(at40k.cell(16, 22), NW,    true);
953             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
954             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
955         }
956         for(int x=23; x>1; x-=2) {
957             pulse_detect(at40k.cell(x-1, yofs-2), SW,    false);
958             pulse_detect(at40k.cell(x-1, yofs-3), WEST,  true);
959             pulse_copy(at40k.cell(x, yofs-2), SW, SOUTH, false);
960             pulse_copy(at40k.cell(x, yofs-3), NW, NORTH, true);
961
962             //pulse_detect(at40k.cell(15, 22), NORTH, false);
963             //pulse_detect(at40k.cell(16, 22), NW,    true);
964             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
965             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
966         }
967         copy(at40k.cell(1, yofs-2), SOUTH, SOUTH);
968         copy(at40k.cell(1, yofs-3), NORTH, NORTH);
969         at40k.cell(1, yofs-3).ylut(~at40k.cell(1, yofs-3).ylut());
970         at40k.cell(1, yofs-3).xlut(~at40k.cell(1, yofs-3).xlut());
971
972         copy(at40k.cell(23, yofs), SOUTH, SOUTH);
973         copy(at40k.cell(23, yofs-1), SOUTH, SOUTH);
974
975         for(int i=23; i>yofs; i--) copy(at40k.cell(1, i), SOUTH, SOUTH);
976
977         //at40k.iob_top(1, true).slew(SLOW);
978         //at40k.iob_top(1, false).slew(SLOW);
979
980     }
981
982     public static void fill(FpslicDevice at40k, SlipwayBoard device, int num) throws Exception {
983         //muller(at40k.cell(PIPELEN,22), NE, WEST);
984         FpslicDevice.Cell a = at40k.cell(10,22);
985         FpslicDevice.Cell b = at40k.cell(11,22);
986         a.ylut(0x00);
987         for(int i=0; i<num; i++) {
988             //System.out.println(i);
989             b.lut(0xff, 0xff);
990             device.getFpslicDevice().flush();
991             try { Thread.sleep(1); } catch (Exception e) { }
992             b.lut(0x00, 0x00);
993             device.getFpslicDevice().flush();
994             try { Thread.sleep(1); } catch (Exception e) { }
995         }
996         b.ylut(0xB2);
997         a.ylut(0xB2);
998     }
999
1000     public static void drain(FpslicDevice at40k, SlipwayBoard device) throws Exception {
1001         FpslicDevice.Cell a = at40k.cell(10,22);
1002         FpslicDevice.Cell b = at40k.cell(11,22);
1003         a.lut(0x00, 0x00);
1004         b.lut(0x00, 0x00);
1005         for(int i=0; i<30; i++) {
1006             //System.out.println(i);
1007             a.lut(0xff, 0xff);
1008             device.getFpslicDevice().flush();
1009             try { Thread.sleep(1); } catch (Exception e) { }
1010             a.lut(0x00, 0x00);
1011             device.getFpslicDevice().flush();
1012             try { Thread.sleep(1); } catch (Exception e) { }
1013         }
1014         b.ylut(0xB2);
1015         a.ylut(0xB2);
1016     }
1017
1018     public static void doitx(FpslicDevice at40k, SlipwayBoard device) throws Exception {
1019         for(int i=5; i<PIPELEN+1; i++) bounce(at40k.cell(i, 23), SE,                     SOUTH);
1020         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 22), x==PIPELEN-1 ? SE : NE, WEST);
1021         
1022         bounce(at40k.cell(PIPELEN,  21), NW, WEST);
1023         
1024         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 21), SW,                     x==PIPELEN-1 ? NORTH : EAST);
1025         for(int x=4; x<PIPELEN+1; x++) bounce(at40k.cell(x, 20), NW,                     NORTH);
1026         
1027         bounce(at40k.cell(4, 22), SE, EAST);
1028         //muller(at40k.cell(4PIPELEN-1,21), SW, NORTH);
1029         
1030         //muller(at40k.cell(4,22), NE, WEST);
1031         //at40k.cell(4,22).ylut(0xEE);
1032         muller(at40k.cell(5, 22), NE, SOUTH);
1033         muller(at40k.cell(5, 21), NW, EAST);
1034         /*
1035           for(int x=4; x>=0; x--) {
1036           at40k.cell(x, 21).ylut(0xAA);
1037           at40k.cell(x, 21).yi(EAST);
1038           at40k.cell(x, 21).yo(false);
1039           }
1040
1041           at40k.cell(0, 22).ylut(0xAA);
1042           at40k.cell(0, 22).yi(SOUTH);
1043           at40k.cell(0, 22).yo(false);
1044
1045           at40k.cell(0, 23).ylut(~0xAA);
1046           at40k.cell(0, 23).xlut(~0xcc);
1047           at40k.cell(0, 23).yi(SOUTH);
1048           at40k.cell(0, 23).yo(false);
1049         */
1050         for(int x=3; x<=23; x+=2) {
1051             pulse_detect(at40k.cell(x-1, 19), SW,    false);
1052             pulse_detect(at40k.cell(x-1, 18), WEST,  true);
1053             pulse_copy(at40k.cell(x, 19), SW, SOUTH, false);
1054             pulse_copy(at40k.cell(x, 18), NW, NORTH, true);
1055
1056             if (x<17) {
1057                 pulse_detect(at40k.cell(x-1, 16), SW,    false);
1058                 pulse_detect(at40k.cell(x-1, 15), WEST,  true);
1059                 pulse_copy(at40k.cell(x, 16), SW, SOUTH, false);
1060                 pulse_copy(at40k.cell(x, 15), NW, NORTH, true);
1061             }
1062             //pulse_detect(at40k.cell(15, 22), NORTH, false);
1063             //pulse_detect(at40k.cell(16, 22), NW,    true);
1064             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
1065             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1066         }
1067         for(int x=14; x>=1; x--)
1068             copy(at40k.cell(x, 17), EAST, EAST);
1069         for(int x=4; x>=0; x--)
1070             copy(at40k.cell(x, 21), EAST, EAST);
1071         copy(at40k.cell(13, 17), SOUTH, SOUTH);
1072
1073         copy(at40k.cell(0, 20), NORTH, NORTH);
1074         copy(at40k.cell(0, 19), NORTH, NORTH);
1075         copy(at40k.cell(0, 18), NORTH, NORTH);
1076         copy(at40k.cell(0, 17), NORTH, NORTH);
1077         copy(at40k.cell(0, 16), NORTH, NORTH);
1078         copy(at40k.cell(1, 16), WEST, WEST);
1079         copy(at40k.cell(1, 15), NORTH, NORTH);
1080
1081         copy(at40k.cell(1, 20), SOUTH, SOUTH);
1082         copy(at40k.cell(1, 19), SOUTH, SOUTH);
1083         copy(at40k.cell(1, 18), SOUTH, SOUTH);
1084
1085         for(int y=20; y<=23; y++)
1086             copy(at40k.cell(23, y), SOUTH, SOUTH);
1087
1088
1089         //for(int x=19; x<=23; x++)
1090         //copy(at40k.cell(x, 0), WEST, WEST);
1091         //copy(at40k.cell(18, 19), NW, NW);
1092         //at40k.iob_top(5, true).enableOutput(SOUTH);
1093         //at40k.iob_top(5, false).enableOutput(SOUTH);
1094     }
1095 }