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