checkpoint
[slipway.git] / src / edu / berkeley / obits / AtmelSerial.java
1 package edu.berkeley.obits;
2
3 import edu.berkeley.slipway.*;
4 import com.atmel.fpslic.*;
5 import static com.atmel.fpslic.FpslicConstants.*;
6 import static com.atmel.fpslic.Fpslic.Util.*;
7 import edu.berkeley.obits.device.atmel.*;
8 import edu.berkeley.obits.gui.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.color.*;
12 import org.ibex.util.*;
13 import java.io.*;
14 import java.util.*;
15 import gnu.io.*;
16
17 public class AtmelSerial {
18
19     //public static boolean mullers = false;
20     public static boolean mullers = true;
21     public static int masterx = 1;
22     public static SerialPort detectObitsPort() throws Exception {
23         Enumeration e = CommPortIdentifier.getPortIdentifiers();
24         while(e.hasMoreElements()) {
25             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
26             Log.info(AtmelSerial.class, "trying " + cpi.getName());
27             if (cpi.getName().startsWith("/dev/cu.usbserial-"))
28                 return new RXTXPort(cpi.getName());
29             if (cpi.getName().startsWith("/dev/ttyS0"))
30                 return new RXTXPort(cpi.getName());
31         }
32         Log.info(AtmelSerial.class, "returning null...");
33         return null;
34     }
35     public static int PIPELEN=20;
36     public static void main(String[] s) throws Exception {
37         FtdiBoard device = new FtdiBoard();
38         Fpslic at40k = device;
39         try {
40             long begin = System.currentTimeMillis();
41             device.readMode4(new ProgressInputStream("configuring fabric", System.in, 111740));
42             long end = System.currentTimeMillis();
43             Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s");
44             Thread.sleep(1000);
45             Log.info(AtmelSerial.class, "issuing command");
46
47             //at40k.iob_top(2, true).oe(false);
48             //at40k.iob_top(2, false).oe(false);
49             //at40k.iob_top(1, true).oe(false);
50
51             // this command confirmed to turn *on* led0
52             //at40k.iob_top(1, false).output(0);
53             /*
54             for(int i=0; i<20; i++) {
55                 at40k.iob_bot(i, false).output(0);
56                 at40k.iob_bot(i, true).output(0);
57             }
58             */
59
60             //System.out.println("tick");
61                 //Thread.sleep(3000);
62                 //System.out.println("tick");
63                 //at40k.cell(0x01, 0x17).xlut((byte)0x);
64
65             /*
66             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).xlut(), 16));
67             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).ylut(), 16));
68             at40k.cell(0x01, 0x17).ylut((byte)0xff);
69             */
70
71             //at40k.cell(0x01, 0x17).wi(L1);
72             /*
73             System.out.println("a: " + at40k.new SectorWire(true, 0, 4, 0x17).driverRight());
74             System.out.println("b: " + at40k.new SectorWire(true, 1, 4, 0x17).driverRight());
75             Fpslic.SectorWire h0p0 = at40k.new SectorWire(true, 0, 0, 0x17);
76             Fpslic.SectorWire h0p1 = at40k.new SectorWire(true, 1, 0, 0x17);
77             Fpslic.SectorWire h0p2 = at40k.new SectorWire(true, 2, 0, 0x17);
78             Fpslic.SectorWire h4p0 = at40k.new SectorWire(true, 0, 4, 0x17);
79             Fpslic.SectorWire h4p1 = at40k.new SectorWire(true, 1, 4, 0x17);
80             Fpslic.SectorWire h4p2 = at40k.new SectorWire(true, 2, 4, 0x17);
81
82             //h4p1.drives(h0p1, false);
83             //at40k.cell(0x04, 0x17).out(L1, false);
84             //at40k.cell(0x04, 0x17).h(L0, false);
85
86             for(int plane=0; plane<5; plane++) {
87                 at40k.new SectorWire(true, plane,     4, 0x17).drives(at40k.new SectorWire(true, plane,     0, 0x17), false);
88                 at40k.cell(0x04, 0x17).out(plane, false);
89                 at40k.cell(0x04, 0x17).h(plane, false);
90                 at40k.cell(0x01, 0x17).h(plane, false);
91             }
92             try { Thread.sleep(2000); } catch (Exception e) { }
93
94             int plane=0;
95             at40k.new SectorWire(true, plane, 4, 0x17).drives(at40k.new SectorWire(true, plane, 0, 0x17), true);
96             at40k.cell(0x04, 0x17).out(plane, true);
97             at40k.cell(0x04, 0x17).h(plane, true);
98             at40k.cell(0x01, 0x17).h(plane, true);
99             at40k.cell(0x01, 0x17).wi(plane);
100
101             */
102
103             /*
104             System.out.println("xlut is " + hex(at40k.cell(0x04, 0x17).xlut()));
105             System.out.println("ylut is " + hex(at40k.cell(0x04, 0x17).ylut()));
106             Fpslic.Cell cell = at40k.cell(0x04, 0x17);
107             //cell.xlut(0xff);
108             //cell.f(false);
109             System.out.println(cell.c());
110             cell.c(YLUT);
111             cell.ylut(0x4D);
112             cell.xlut(0x00);
113
114             cell.b(false);
115             cell.f(false);
116             //cell.t(false, false, true);
117             cell.t(false, true, false);
118             cell.out(L3, true);
119             cell.wi(L3);
120
121             cell.yo(false);
122             cell.h(L0, false);
123             cell.h(L1, false);
124             cell.h(L2, false);
125             cell.h(L3, false);
126             cell.h(L4, false);
127
128             for(int i=3; i>=1; i--) {
129                 at40k.cell(i, 0x17).yi(EAST);
130                 at40k.cell(i, 0x17).ylut(0x55);
131                 at40k.cell(i, 0x17).yo(false);
132             }
133             */
134
135             //System.out.println("reading port status: " + Integer.toString(device.readBus() & 0xff, 16));
136
137
138             // blank these out
139             /*
140             at40k.cell(23, 8).ylut(0xff);
141             at40k.cell(23, 11).ylut(0xff);
142             at40k.iob_right(8, true).enableOutput();
143             at40k.iob_right(11, true).enableOutput();
144             */
145             //for(int x=4;  x<=22; x++) swap(at40k.cell(x, 22), NW, NORTH);
146
147
148             // entry cell: just copy X->X Y->Y
149             //at40k.cell(4,23).b(false);
150             //at40k.cell(4,23).yo(false);
151             //at40k.cell(4,23).ylut(at40k.cell(4,23).xlut());
152             //at40k.cell(4,23).xo(false);
153             /*
154             at40k.cell(4,23).xlut(0x55);
155             at40k.cell(4,23).ylut(0x55);
156             */
157             /*
158             at40k.cell(4,23).xlut(0x71);
159             at40k.cell(4,23).ylut(0x44);
160             at40k.cell(4,23).c(YLUT);
161             at40k.cell(4,23).f(false);
162             at40k.cell(4,23).t(false, false, true);
163             */
164
165             //for(int x=6;  x<=23; x++) copy(at40k.cell(x, 23), NW, WEST);  // top row copies to the right
166             /*
167             copy(at40k.cell(5, 22), NW, NORTH);
168             for(int x=6;  x<=22; x++) copy(at40k.cell(x, 22), NW, WEST);  // second top row copies to the right
169             //for(int y=22; y>=10; y--) copy(at40k.cell(23, y), NW, NORTH); // right edge copies down
170             for(int y=21; y>=9;  y--) copy(at40k.cell(22, y), NW, NORTH); // second right edge copies down
171             copy(at40k.cell(23, 9), NW, WEST);                            // second output
172             */
173             /*
174             handshaker(at40k.cell(4,23));
175             at40k.cell(4,23).xi(NW);
176             at40k.cell(4,23).yi(SOUTH);
177
178             //handshaker(at40k.cell(5,23));
179             //at40k.cell(5,23).yi(NORTH);
180
181             at40k.cell(5,23).yi(NORTH);
182             at40k.cell(5,23).xlut(0x55);
183             at40k.cell(5,23).xi(SW);
184             at40k.cell(5,23).ylut(0x55);
185             at40k.cell(5,22).yi(NORTH);
186             at40k.cell(5,22).xlut(0x55);
187
188             bounce(at40k.cell(4,22));
189
190             // cell southeast of entry cell
191             at40k.cell(3,22).xi(NE);      // NW->xin
192             at40k.cell(3,22).ylut(0x33);  // xin->y
193             at40k.cell(3,22).yo(false);   // y->yout
194             copy(at40k.cell(3, 21), NW, NORTH);  // second top row copies to the right
195             copy(at40k.cell(4, 21), NW, EAST);  // second top row copies to the right
196             copy(at40k.cell(5, 21), NW, EAST);  // second top row copies to the right
197             copy(at40k.cell(6, 21), NW, EAST);  // second top row copies to the right
198             copy(at40k.cell(6, 22), NW, SOUTH);  // second top row copies to the right
199             */
200             /*
201             at40k.cell(05,22).xlut(0xff);
202             at40k.cell(05,22).ylut(0xff);
203             at40k.cell(05,22).c(XLUT);
204             at40k.cell(05,22).f(false);
205             at40k.cell(05,22).b(false);
206             at40k.cell(05,22).oe(NONE);
207             at40k.cell(05,22).v(L3, true);
208             at40k.cell(05,22).out(L3, true);
209             */
210             /*
211             at40k.cell(4,23).ylut(~0xCC);
212             at40k.cell(4,23).xlut(~0xAA);
213             at40k.cell(5,23).ylut(~0xAA);
214             at40k.cell(5,23).xlut(~0xAA);
215             for(int i=6; i<PIPELEN+2; i++) {
216                 at40k.cell(i, 23).ylut(0xAA);
217                 at40k.cell(i, 23).xlut(0xCC);
218                 at40k.cell(i, 23).yi(WEST);
219             }
220             */
221
222             /* LAST
223             System.out.println("doit");
224             if (mullers) doitx(at40k, device);
225             //System.out.println("counter");
226             //counter(at40k, device);
227
228             at40k.cell(21,15).yi(WEST);
229             at40k.cell(21,15).ylut(0xAA);
230
231             at40k.cell(22,15).yi(WEST);
232             at40k.cell(22,15).ylut(0xAA);
233             */
234             at40k.cell(23,15).h(3, true);
235             at40k.cell(23,15).yi(L3);
236             at40k.cell(23,15).ylut(0xAA);
237             at40k.iob_right(15, true).enableOutput(WEST);
238
239
240             Fpslic.Cell c = at40k.cell(10,10);
241             c.ylut(~LUT_SELF);
242             c.xlut(LUT_Z);
243             c.yi(WEST);
244             c.c(YLUT);
245             c.f(false);
246             c.t(TMUX_FB);
247             copy(c.west(), EAST, NW);
248             copy(c.west().north().west(), SE, SE);
249
250             c = c.east();
251             c.ylut(~LUT_SELF);
252             c.xlut(LUT_Z);
253             c.yi(EAST);
254             c.c(YLUT);
255             c.f(false);
256             c.t(TMUX_FB);
257             copy(c.east(), WEST, SE);
258             copy(c.east().south().east(), NW, NW);
259
260             c = c.north();
261             copy(c.north(), SOUTH, SOUTH);
262             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
263             c.ylut(LUT_Z);
264             c.yi(SOUTH);
265             c.c(XLUT);
266             c.xi(SW);
267             c.wi(L4);
268             c.f(false);
269             c.t(TMUX_W_AND_FB);
270             c.v(L4, false);
271             c.h(L4, true);
272             c.v(L2, false);
273             c.h(L2, true);
274
275             c = c.west();
276             copy(c.north(), SOUTH, SOUTH);
277             c.xlut((LUT_SELF & ~LUT_OTHER) | LUT_Z);
278             c.ylut(~LUT_Z);
279             c.yi(SOUTH);
280             c.xi(SE);
281             c.c(XLUT);
282             c.wi(L4);
283             c.f(false);
284             c.t(TMUX_W_AND_FB);
285             c.v(L4, false);
286             c.h(L4, true);
287             c.v(L2, false);
288             c.h(L2, true);
289
290             c = c.west();
291             c.v(L4, false);
292             c.h(L4, true);
293             c.out(L4, true);
294             c.f(false);
295             c.b(false);
296             c.oe(NONE);
297             c.c(YLUT);
298             c.hwire(L4).west().drives(c.hwire(L4), false);
299             c.hwire(L4).east().drives(c.hwire(L4), false);
300
301             c = c.south();
302             c = c.south();
303             c.v(L4, false);
304             c.h(L4, true);
305             c.out(L4, true);
306             c.f(false);
307             c.b(false);
308             c.oe(NONE);
309             c.c(YLUT);
310             c.hwire(L4).west().drives(c.hwire(L4), false);
311             c.hwire(L4).east().drives(c.hwire(L4), false);
312
313             c = c.east();
314             c = c.east();
315             copy(c.south(), NORTH, NORTH);
316             c.xlut(((~LUT_SELF) & (~LUT_OTHER)) | LUT_Z);
317             c.ylut(LUT_Z);
318             c.yi(NORTH);
319             c.c(XLUT);
320             c.xi(NW);
321             c.wi(L4);
322             c.f(false);
323             c.t(TMUX_W_AND_FB);
324             c.v(L4, false);
325             c.h(L4, true);
326             c.v(L2, false);
327             c.h(L2, true);
328
329             c = c.west();
330             copy(c.south(), NORTH, NORTH);
331             c.xlut((LUT_SELF & LUT_OTHER) | LUT_Z);
332             c.ylut(LUT_Z);
333             c.yi(NORTH);
334             c.xi(NE);
335             c.c(XLUT);
336             c.wi(L4);
337             c.f(false);
338             c.t(TMUX_W_AND_FB);
339             c.v(L4, false);
340             c.h(L4, true);
341             c.v(L2, false);
342             c.h(L2, true);
343
344
345             // catch a rising transition
346             /*
347             c = c.west();
348             c.v(L2, false);
349             c.h(L2, true);
350             c.out(L2, true);
351             c.f(false);
352             c.b(false);
353             c.oe(NONE);
354             c.c(YLUT);
355             */
356             c.hwire(L2).west().drives(c.hwire(L2), false);
357             c.hwire(L2).east().drives(c.hwire(L2), false);
358
359
360
361             //////
362
363             c = at40k.cell(20,20);
364             c.yi(WEST);
365             c.ylut(LUT_SELF);
366             c.c(YLUT);
367             c.oe(H4);
368             c.h(L4, true);
369             c.b(false);
370             c.f(false);
371             c.out(L4);
372
373             c = at40k.cell(21,20);
374             c.c(YLUT);
375             c.oe(NONE);
376             c.h(L4, true);
377             c.b(false);
378             c.f(false);
379
380
381             c = at40k.cell(8,8);
382             c.f(true);
383             c.b(true);
384             c.xo(true);
385             c.xi(NE);
386             c.zi(L3);
387             c.wi(L0);
388             c.yi(NORTH);
389             c.oe(H4);
390             c.h(L0, true);
391             c.h(L2, true);
392             c.h(L4, true);
393             c.v(L1, true);
394             c.v(L3, true);
395             c.out(L0, true);
396             c.out(L1, true);
397             c.out(L2, true);
398             c.out(L3, true);
399             c.out(L4, true);
400             c.xo(true);
401             c.yo(true);
402             c.c(ZMUX);
403
404             at40k.cell(9,10).xo(true);
405             at40k.cell(9,10).yo(true);
406             at40k.cell(9,10).c(YLUT);
407             at40k.cell(9,10).b(false);
408
409             //for(int x=5; x<PIPELEN; x++) {
410             //at40k.cell(x,23).hwire(L0).drives(at40k.cell(x,23).hwire(L0).east());
411             //}
412
413             /*
414             at40k.cell(22,11).ylut(0xff);
415             at40k.cell(23,11).yi(L3);
416             //at40k.cell(23,11).yi(WEST);
417             //at40k.cell(23,11).xi(L1);
418             at40k.cell(23,11).ylut(0xAA);
419             at40k.iob_right(11, true).enableOutput(WEST);
420             at40k.cell(23,11).v(L3, true);
421             at40k.cell(23,11).yo(false);
422             //at40k.flush();
423             */
424             int vx=04;
425             int vv=23;
426             /*
427             System.out.println("correct: " + at40k.cell(19,15).hwire(L3) + " drives " + at40k.cell(20,15).hwire(L3));
428             System.out.println("correct: " + at40k.cell(15,15).hwire(L3) + " drives " + at40k.cell(19,15).hwire(L3));
429             System.out.println("correct: " + at40k.cell(11,15).hwire(L3) + " drives " + at40k.cell(15,15).hwire(L3));
430             System.out.println("correct: " + at40k.cell(07,15).hwire(L3) + " drives " + at40k.cell(11,15).hwire(L3));
431
432             at40k.cell(19,15).hwire(L3).drives(at40k.cell(20,15).hwire(L3), true);
433             at40k.cell(15,15).hwire(L3).drives(at40k.cell(19,15).hwire(L3), true);
434             at40k.cell(11,15).hwire(L3).drives(at40k.cell(15,15).hwire(L3), true);
435             at40k.cell(07,15).hwire(L3).drives(at40k.cell(11,15).hwire(L3), true);
436             */
437             //at40k.cell(05,vv).xlut(0xff);
438             //at40k.cell(05,vv).ylut(0xff);
439             /*
440             at40k.cell(vx,vv).c(YLUT);
441             at40k.cell(vx,vv).f(false);
442             at40k.cell(vx,vv).b(false);
443             at40k.cell(vx,vv).oe(NONE);
444             at40k.cell(vx,vv).v(L3, true);
445             at40k.cell(vx,vv).out(L3, true);
446             */
447             /*
448             at40k.cell(vx,15).v(L3, true);
449             at40k.cell(vx,15).h(L3, true);
450             at40k.cell(vx,19).vwire(L3).drives(at40k.cell(vx,15).vwire(L3), true);
451             at40k.cell(vx,23).vwire(L3).drives(at40k.cell(vx,19).vwire(L3), true);
452             */
453
454             //at40k.cell(5,23).ylut(0x00);
455             //at40k.cell(6,22).ylut(0xff);
456             //at40k.cell(22,11).ylut(0xff);
457             /*
458             Fpslic.Cell cell = at40k.cell(4, 16);
459             cell.xlut(0xff);
460             cell.ylut(0xff);
461             cell.b(false);
462             cell.f(false);
463             cell.c(XLUT);
464             cell.h(L3, true);
465             cell.v(L3, true);
466             cell.out(L3, true);
467             cell.oe(NONE);
468             */
469             //scan(at40k, cell, YLUT, true);
470             //scan(at40k, cell, YLUT, false);
471
472             //device.scanFPGA(true);
473
474             at40k.cell(10,10).f(true);
475             at40k.cell(10,10).c(ZMUX);
476
477             at40k.cell(8,7).xlut(LUT_SELF);
478             at40k.cell(8,7).xi(NW);
479
480             at40k.cell(7,8).xlut(LUT_SELF & LUT_Z);
481             at40k.cell(7,8).xi(SE);
482             at40k.cell(7,8).c(XLUT);
483             at40k.cell(7,8).f(false);
484             at40k.cell(7,8).b(false);
485             at40k.cell(7,8).t(TMUX_FB);
486             at40k.cell(7,8).xo(false);
487             System.out.println(at40k.cell(7,8).fb_relevant());
488
489             at40k.cell(6,13).xi(SE);
490             at40k.cell(6,13).c(ZMUX);
491             at40k.cell(6,13).xlut(LUT_SELF);
492             at40k.cell(6,13).ylut(LUT_OTHER);
493             at40k.cell(6,13).xo(false);
494             at40k.cell(6,13).yo(false);
495             at40k.cell(7,12).xi(SE);
496
497             for(int i=0; i<24; i++) {
498                 at40k.iob_bot(i, true).enableOutput(NORTH);
499                 at40k.iob_bot(i, false).enableOutput(NW);
500                 at40k.cell(i, 0).xlut(0xff);
501                 at40k.cell(i, 0).ylut(0xff);
502             }
503
504             device.flush();
505
506             Gui vis = new Gui(at40k, device);
507             Frame fr = new Frame();
508             fr.addKeyListener(vis);
509             fr.setLayout(new BorderLayout());
510             fr.add(vis, BorderLayout.CENTER);
511             fr.pack();
512             fr.setSize(900, 900);
513             vis.repaint();
514             fr.repaint();
515             fr.show();
516             synchronized(AtmelSerial.class) { AtmelSerial.class.wait(); }
517
518
519
520             Visualizer v = new Visualizer(at40k, device);
521             v.show();
522             v.setSize(1380, 1080);
523             Fpslic.Cell cell = at40k.cell(4, 23);
524
525             Image img = v.createImage(v.getWidth(), v.getHeight());
526             /*
527             int x = 1;
528             int y = 14;
529             cell = at40k.cell(x,y);
530             scan(at40k, cell, YLUT, true);
531             cell.c(YLUT);
532             cell.b(false);
533             cell.f(false);
534             cell.oe(NONE);
535             cell.ylut(0xff);
536             */
537             //int x = 5;
538             //int y = 11;
539
540             //selfTest(device, at40k, v);
541             System.out.println("save: " + FtdiBoard.save + " of " + (FtdiBoard.saveof*5));
542
543             at40k.iob_top(0, true).enableInput();
544             copy(at40k.cell(0, 23), NORTH, NORTH);
545             at40k.iob_bot(0, true).enableOutput(NORTH);
546
547             for(int i=0; i<10000; i++) {
548                 v.refresh();
549                 try { Thread.sleep(100); } catch (Exception e) { }
550             }
551             //cell.ylut(0x09);
552
553             //at40k.cell(0x01, 0x17).h(0, false);
554             //at40k.cell(0x01, 0x17).xi(NE);
555             //at40k.cell(0x01, 0x17).ylut((byte)0x55);
556
557             //at40k.cell(0x04, 0x17).xlut((byte)0x10);
558             //at40k.cell(0x04, 0x17).ylut((byte)0x10);
559             //at40k.cell(0x04, 0x17).yo(false);
560             //at40k.cell(0x04, 0x17).xo();
561
562             /*
563             at40k.cell(0x01, 0x17).xi(L0);
564             at40k.cell(0x01, 0x17).h(L0, true);
565             */
566             /*
567             at40k.cell(0x03, 0x17).xlut((byte)0x55);
568             at40k.cell(0x03, 0x17).ylut((byte)0x55);
569             at40k.cell(0x03, 0x17).yi(EAST);
570             at40k.cell(0x03, 0x17).ylut((byte)0x55);
571             at40k.cell(0x03, 0x17).yo(true);
572
573             at40k.cell(0x03, 0x17).f(false);
574             at40k.cell(0x03, 0x17).c(XLUT);
575             at40k.cell(0x03, 0x17).oe(NONE);
576             at40k.cell(0x03, 0x17).out(L0, true);
577
578             at40k.cell(0x02, 0x17).yi(EAST);
579             at40k.cell(0x02, 0x17).ylut((byte)0x55);
580             at40k.cell(0x02, 0x17).yo(false);
581
582             at40k.cell(0x01, 0x17).yi(EAST);
583             at40k.cell(0x01, 0x17).ylut((byte)0x55);
584             at40k.cell(0x01, 0x17).yo(false);
585
586             at40k.cell(0x01, 0x17).h(L0, true);
587             at40k.cell(0x01, 0x17).v(L0, false);
588             */
589             //at40k.cell(0x01, 0x17).yi(L0);
590             //at40k.cell(0x01, 0x17).xi(L0);
591             //at40k.cell(0x01, 0x17).ylut((byte)0x33);
592
593             /*
594             at40k.cell(0x03, 0x17).h(L0, true);
595             at40k.cell(0x03, 0x17).out(L0, true);
596             at40k.cell(0x03, 0x17).c(XLUT);
597             at40k.cell(0x03, 0x17).f(false);
598             */
599             /*
600             at40k.cell(0x01, 0x17).xin(4);
601             at40k.cell(0x01, 0x17).yin(4);
602             at40k.cell(0x01, 0x16).ylut((byte)0x00);
603             device.mode4(2, 0x17, 0x01, 0);
604
605             for(int i=0; i<10; i++) {
606                 Thread.sleep(3000);
607                 System.out.println("tick");
608                 //at40k.cell(0x01, 0x17).xlut((byte)0xFF);
609                 at40k.cell(0x00, 0x17).ylut((byte)0x00);
610                 device.flush();
611                 Thread.sleep(3000);
612                 System.out.println("tick");
613                 //at40k.cell(0x01, 0x17).xlut((byte)0x00);
614                 at40k.cell(0x00, 0x17).ylut((byte)0xFF);
615                 device.flush();
616             }
617             */
618
619
620             /*
621             at40k.iob_top(0, true).output(0);
622             at40k.iob_top(0, true).oe(false);
623             at40k.iob_top(0, true).pullup();
624             device.flush();
625             Thread.sleep(3000);
626
627             Log.info(AtmelSerial.class, "issuing command");
628             at40k.iob_top(1, true).pulldown();
629             device.flush();
630             */
631             Log.info(AtmelSerial.class, "done");
632             System.exit(0);
633         } catch (Exception e) { e.printStackTrace(); }
634     }
635
636     public static void scan(Fpslic dev, Fpslic.Cell cell, int source, boolean setup) {
637         if (setup) {
638             if (source != NONE) cell.c(source);
639             if (cell.b()) cell.b(false);
640             if (cell.f()) cell.f(false);
641         }
642         if (cell.out(L3)!=setup) cell.out(L3, setup);
643         if (cell.vx(L3)!=setup) cell.v(L3, setup);
644
645         Fpslic.SectorWire sw = cell.vwire(L3);
646         //System.out.println("wire is: " + sw);
647
648         if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
649             sw.north().drives(sw, false);
650         while(sw.row > (12 & ~0x3) && sw.south() != null) {
651             //System.out.println(sw + " -> " + sw.south());
652             if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
653             sw = sw.south();
654         }
655         if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
656             sw.north().drives(sw, false);
657         while(sw.row < (12 & ~0x3) && sw.north() != null) {
658             //System.out.println(sw + " -> " + sw.north());
659             if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
660             sw = sw.north();
661         }
662
663         //cell = dev.cell(19, 15);
664         cell = dev.cell(cell.col, 15);
665         /*
666         System.out.println("cell is " + cell);
667         cell.xlut(0xff);
668         cell.ylut(0xff);
669         cell.b(false);
670         cell.f(false);
671         cell.c(XLUT);
672         cell.out(L3, true);
673         cell.oe(NONE);
674         */
675         if (cell.hx(L3) != setup) cell.h(L3, setup);
676         if (cell.vx(L3) != setup) cell.v(L3, setup);
677         sw = cell.hwire(L3);
678
679         if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
680         while(sw.east() != null) {
681             //System.out.println(sw + " -> " + sw.east());
682             if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
683             sw = sw.east();
684         }
685
686     }
687
688     public static void copy(Fpslic.Cell c, int xdir, int ydir) {
689         switch(xdir) {
690             case NW: case NE: case SW: case SE: {
691                 c.xi(xdir);
692                 c.xlut(LUT_SELF);
693                 break;
694             }
695             case NORTH: case SOUTH: case EAST: case WEST: {
696                 c.yi(xdir);
697                 c.xlut(LUT_OTHER);
698                 break;
699             }
700             case NONE: break;
701             default: throw new Error();
702         }
703         switch(ydir) {
704             case NW: case NE: case SW: case SE: {
705                 c.xi(ydir);
706                 c.ylut(LUT_OTHER);
707                 break;
708             }
709             case NORTH: case SOUTH: case EAST: case WEST: {
710                 c.yi(ydir);
711                 c.ylut(LUT_SELF);
712                 break;
713             }
714             case NONE: break;
715             default: throw new Error();
716         }
717         c.xo(false);
718         c.yo(false);
719     }
720     public static String hex(int x) {
721         return Long.toString(x & 0xffffffffL, 16);
722     }
723
724     public static void handshaker(Fpslic.Cell cell) {
725         cell.xlut(0x22);
726         cell.ylut(0x71);
727         cell.c(XLUT);
728         cell.f(false);
729         cell.t(false, false, true);
730     }
731
732     public static class Visualizer extends Frame implements KeyListener, MouseMotionListener, MouseListener {
733         public static final int WIDTH = 40;
734         public static final int HEIGHT = 40;
735         public static final int LW = 15;
736         public static final int LH = 15;
737         public static final Color RED  = new Color(0xaa, 0x55, 0x55);
738         public static final Color BLUE = new Color(0x55, 0x55, 0xaa);
739         private final Fpslic dev;
740         private final FtdiBoard drone;
741         int selx = -1;
742         int sely = -1;
743         public Visualizer(final Fpslic dev, final FtdiBoard drone) {
744             this.dev = dev;
745             this.drone = drone;
746             show();
747             addMouseMotionListener(this);
748             addMouseListener(this);
749             addKeyListener(this);
750             new Thread() {
751                 public void run() {
752                     try {
753                         while(true) {
754                             Thread.sleep(500);
755                             if (!enabled) continue;
756                             /*
757                             Fpslic.Cell cell = dev.cell(21, 22);
758                             cell.xlut(0xff);
759                             cell.ylut(0xff);
760                             */
761                             keyPressed(null);
762                             /*
763                             cell.xlut(0x00);
764                             cell.ylut(0x00);
765                             */
766                         }
767                     } catch (Exception e) {
768                         e.printStackTrace();
769                     }
770                 }
771             }.start();
772         }
773         public boolean enabled = false;
774         public void mouseClicked(MouseEvent e) { }
775         public void mouseEntered(MouseEvent e) { }
776         public void mouseExited(MouseEvent e) { }
777         public void mouseReleased(MouseEvent e) {
778         }
779         public void keyTyped(KeyEvent k) {
780         }
781         public void keyReleased(KeyEvent k) {
782         }
783         public void keyPressed(KeyEvent keyevent) {
784             boolean scan = false;
785             switch(keyevent==null ? '_' : keyevent.getKeyChar()) {
786                 case '1': {
787                     if (selx==-1 || sely==-1) break;
788                     Fpslic.Cell cell = dev.cell(selx, sely);
789                     cell.xlut(0xff);
790                     cell.ylut(0xff);
791                     drawCell(getGraphics(), selx, sely);
792                     drone.flush();
793                     break;
794                 }
795                 case 'i': {
796                     System.out.println("interrupt_count: " + drone.readCount());
797                     break;
798                 }
799                 case 'x': {
800                     masterx+=2;
801                     if (mullers) {
802                         if (masterx <= 22) {
803                             int mx = masterx;
804                             System.out.println("low => " + mx);
805                             copy(dev.cell(mx, yofs-2), NORTH, NORTH);
806                             copy(dev.cell(mx, yofs-3), NORTH, NORTH);
807                             //dev.cell(mx, yofs-3).ylut(~dev.cell(mx, yofs-3).ylut());
808                             //dev.cell(mx, yofs-3).xlut(~dev.cell(mx, yofs-3).xlut());
809                         } else {
810                             int mx = 23-(masterx-23);
811                             System.out.println("high => " + mx);
812                             copy(dev.cell(mx, yofs), NW, NW);//NORTH, NORTH);
813                             copy(dev.cell(mx, yofs-1), NORTH, NORTH);
814                             //for(int x=mx-1; x>=1; x--)
815                             //copy(dev.cell(x, yofs), EAST, EAST);
816                             for(int y=yofs+1; y<=23; y++)
817                                 copy(dev.cell(1, y), SOUTH, SOUTH);
818                             //dev.cell(mx, yofs-1).ylut(~dev.cell(mx, yofs-1).ylut());
819                             //dev.cell(mx, yofs-1).xlut(~dev.cell(mx, yofs-1).xlut());
820                         }
821                     } else {
822                         if (masterx <= 22) {
823                             int mx = masterx;
824                             System.out.println("low => " + mx);
825                             copy(dev.cell(mx, yofs-2), SOUTH, SOUTH);
826                             copy(dev.cell(mx, yofs-3), NORTH, NORTH);
827                             dev.cell(mx, yofs-3).ylut(~dev.cell(mx, yofs-3).ylut());
828                             dev.cell(mx, yofs-3).xlut(~dev.cell(mx, yofs-3).xlut());
829                         } else {
830                             int mx = 23-(masterx-23);
831                             System.out.println("high => " + mx);
832                             copy(dev.cell(mx, yofs), SOUTH, SOUTH);
833                             /*
834                             copy(dev.cell(mx, yofs-1), NORTH, NORTH);
835                             */
836                             copy(dev.cell(mx, yofs-1), NORTH, SW);
837                             boolean left = true;
838                             Fpslic.Cell lc = null;
839                             for(int k=0; k<10; k++) {
840                                 int y = yofs-2-(k*2);
841                                 copy(dev.cell(left?(mx-1):mx, y),        SOUTH, left?NE:NW);
842                                 copy(lc = dev.cell(left?(mx-1):mx, y-1), NORTH, left?SE:SW); 
843                                 left = !left;
844                             }
845                             copy(lc, NORTH, NORTH);
846
847                             //for(int x=mx-1; x>=1; x--)
848                             //copy(dev.cell(x, yofs), EAST, EAST);
849                             //for(int y=yofs+1; y<=23; y++)
850                             //copy(dev.cell(1, y), SOUTH, SOUTH);
851
852                             if (mx<21) {
853                                 dev.cell(mx+2, yofs).ylut(0x00);
854                                 dev.cell(mx+2, yofs).xlut(0x00);
855                             }
856
857                             /*
858                             dev.cell(mx, yofs-1).ylut(~LUT_Z);
859                             dev.cell(mx, yofs-1).xlut(LUT_Z);
860                             loopback(dev.cell(mx, yofs-1), YLUT);
861                             */
862                             dev.cell(mx, yofs).ylut(~LUT_SELF);
863                             dev.cell(mx, yofs).xlut(~LUT_OTHER);
864                         }
865                     }
866                     break;
867                 }
868                 case ' ': {
869                     //enabled = !enabled;
870                     scan = true;
871                     break;
872                 }
873                 case '4': {
874                     //enabled = !enabled;
875                     try {
876                         for(int cap=0; cap<15; cap++) {
877                             drain(dev, drone);
878                             try { Thread.sleep(100); } catch (Exception e) { }
879                             //showit(dev, drone, this);
880                             fill(dev, drone, cap);
881                             drone.readCount();
882                             long now = System.currentTimeMillis();
883                             try { Thread.sleep(4000); } catch (Exception e) { }
884                             int count = drone.readCount();
885                             long now2 = System.currentTimeMillis();
886                             System.out.println(cap + " ,  " + (((float)count * (2*2*2*2*2*2*2*2*2*1000))/(now2-now)));
887                         }
888                     } catch (Exception e) { e.printStackTrace(); }
889                     break;
890                 }
891                 case 'C': {
892                     if (selx==-1 || sely==-1) break;
893                     Fpslic.Cell cell = dev.cell(selx, sely);
894                     cell.ylut(0xB2);
895                     drawCell(getGraphics(), selx, sely);
896                     break;
897                 }
898                 case '0': {
899                     if (selx==-1 || sely==-1) break;
900                     Fpslic.Cell cell = dev.cell(selx, sely);
901                     cell.xlut(0x00);
902                     cell.ylut(0x00);
903                     drawCell(getGraphics(), selx, sely);
904                     drone.flush();
905                     break;
906                 }
907             } 
908             if (!scan) return;
909             showit(dev, drone, this);
910         }
911         public void mousePressed(MouseEvent e) {
912             final Fpslic.Cell cell = dev.cell(selx, sely);
913             if (cell==null) return;
914             final int old = cell.c();
915             FtdiBoard.ByteCallback bc = new FtdiBoard.ByteCallback() {
916                     public void call(byte b) throws Exception {
917                         boolean y = (b & 0x80) != 0;
918                         //cell.c(old);
919                         Graphics g = getGraphics();
920                         g.setFont(new Font("sansserif", Font.BOLD, 14));
921                         g.setColor(Color.white);
922                         //g.drawString("X=0", left(cell) + 10, top(cell) + 20);
923                         //g.drawString("X=1", left(cell) + 10, top(cell) + 20);
924                         //g.setColor(Color.white);
925                         //g.drawString("Y=0", left(cell) + 8, top(cell) + 35);
926                         //g.drawString("Y=1", left(cell) + 8, top(cell) + 35);
927                         //g.setColor(RED);
928                         //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
929                         String v = (cell.c()==YLUT ? "Y" : cell.c()==XLUT ? "X" : "C");
930                         g.drawString(v+"="+(y?"0":"1"), left(cell) + 8, top(cell) + 35);
931                         g.setColor(BLUE);
932                         g.drawString(v+"="+(y?"1":"0"), left(cell) + 8, top(cell) + 35);
933                     } };
934             try {
935                 scan(dev, cell, NONE, true);
936                 drone.readBus(bc);
937                 //scan(dev, cell, XLUT, true);
938                 //boolean x = (drone.readBus() & 0x80) != 0;
939                 scan(dev, cell, NONE, false);
940             } catch (IOException ex) {
941                 throw new RuntimeException(ex);
942             }
943         }
944
945         public void mouseMoved(MouseEvent e) {
946             int x = e.getX();
947             int y = e.getY();
948             if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
949                 int cx = selx;
950                 int cy = sely;
951                 Fpslic.Cell cell = dev.cell(cx, cy);
952                 selx = -1;
953                 sely = -1;
954                 /*
955                 drawCell(getGraphics(), cx, cy);
956                 drawSector(getGraphics(), dev.cell(cx, cy).sector());
957                 */
958             }
959             selx = (x-20)/(WIDTH+2);
960             sely = (23 - (y-20)/(HEIGHT+2))+1;
961             /*
962             Fpslic.Cell cell = dev.cell(selx, sely);
963             if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
964                 drawCell(getGraphics(), selx, sely);
965                 drawSector(getGraphics(), dev.cell(selx, sely).sector());
966             }
967             */
968         }
969         public void mouseDragged(MouseEvent e) { mousePressed(e); }
970         public void paint(Graphics g) {
971             System.out.println("paintall");
972             g.setColor(Color.white);
973             g.fillRect(0, 0, getWidth(), getHeight());
974             g.setFont(new Font("sansserif", Font.BOLD, 24));
975             for(int x=0; x<24; x++)
976                 for(int y=0; y<24; y++)
977                     drawCell(g,x,y);
978             for(int x=0; x<=23; x+=4)
979                 for(int y=23; y>=0; y-=4) 
980                     drawSector(g, dev.cell(x, y).sector());
981             /*
982             g.setColor(BLUE);
983             g.drawString("Ready", (5*(WIDTH+2))+20, 40);
984             g.setColor(RED);
985             g.drawString("Send",  (3*(WIDTH+2))+20, 40);
986             g.setColor(BLUE);
987             */
988             refresh();
989         }
990         public void refresh() {
991             Graphics g = getGraphics();
992             /*
993             int data = drone.readBus() & 0xff;
994             for(int i=0; i<8; i++) {
995                 g.setColor((data & (1<<i))==0 ? Color.black : Color.green);
996                 g.drawString("D"+i,  (24*(WIDTH+2))+20, ((23-(i+7))*(HEIGHT+2))+60-HEIGHT/2);
997             }
998             */
999         }
1000         public static int left(Fpslic.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
1001         public static int top(Fpslic.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
1002         public void drawSector(Graphics g, Fpslic.Sector sector) {
1003             g.setColor(Color.gray);
1004             ((Graphics2D)g).setStroke(new BasicStroke(1));
1005             int px = ((sector.col)*(WIDTH+2))+20-1;
1006             int py = ((23-(sector.row+3))*(HEIGHT+2))+60-1;
1007             g.drawRect(px, py, (WIDTH+2)*4+2, (HEIGHT+2)*4+2);
1008             /*
1009             for(int dir=0; dir<2; dir++) {
1010                 boolean h = dir==0;
1011                 for(int y=h?sector.row:sector.col; y<(h?sector.row+4:sector.col+4); y++)
1012                     for(int plane=0; plane<=4; plane++) {
1013                         Fpslic.Cell cell      = h ? dev.cell(sector.col,   y) : dev.cell(y, sector.row);
1014                         Fpslic.Cell cell_east = h ? dev.cell(sector.col-1, y) : dev.cell(y, sector.row-1);
1015                         Fpslic.Cell cell_west = h ? dev.cell(sector.col+4, y) : dev.cell(y, sector.row+4);
1016                         boolean draw = false;
1017                         if (h) {
1018                             if (cell_east!=null &&
1019                                 (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
1020                                  cell_east.hwire(plane).drives(cell.hwire(plane))))
1021                                 draw = true;
1022                             if (cell_west!=null &&
1023                                 (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
1024                                  cell_west.hwire(plane).drives(cell.hwire(plane))))
1025                                 draw = true;
1026                         } else {
1027                             if (cell_east!=null &&
1028                                 (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
1029                                  cell_east.vwire(plane).drives(cell.vwire(plane))))
1030                                 draw = true;
1031                             if (cell_west!=null &&
1032                                 (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
1033                                  cell_west.vwire(plane).drives(cell.vwire(plane))))
1034                                 draw = true;
1035                         }
1036                         if (!draw)
1037                             for(int x=h?sector.col:sector.row; x<(h?sector.col+4:sector.row+4); x++)
1038                                 if (((h ? dev.cell(x,y).hx(plane) : dev.cell(y,x).vx(plane))) ||
1039                                     (h?dev.cell(x,y).out(plane):dev.cell(y,x).out(plane)))
1040                                     draw = true;
1041                         if (draw) {
1042                             g.setColor(new Color(0xff, 0x00, 0xff));
1043                             if (h) {
1044                                 g.drawLine(left(cell),
1045                                            top(cell)+3,
1046                                            left(cell) + 4*(WIDTH+2),
1047                                            top(cell)+3
1048                                            );
1049                             } else {
1050                                 g.drawLine(left(cell)+3,
1051                                            top(cell) + (HEIGHT+2),
1052                                            left(cell)+3,
1053                                            top(cell) - 3*(HEIGHT+2)
1054                                            );
1055                             }
1056                         }
1057                     }
1058             }
1059             */
1060         }
1061         public void drawCell(Graphics g, int cx, int cy) {
1062             int x = (cx*(WIDTH+2))+20;
1063             int y = ((23-cy)*(HEIGHT+2))+60;
1064             if (g.getClipBounds() != null && !g.getClipBounds().intersects(new Rectangle(x, y, x+WIDTH, y+HEIGHT))) return;
1065             drawCell(g, cx, cy, Color.white);
1066         }
1067         public void drawCell(Graphics g, int cx, int cy, Color bg) {
1068             int x = (cx*(WIDTH+2))+20;
1069             int y = ((23-cy)*(HEIGHT+2))+60;
1070
1071             //System.out.println("drawcell " + cx + "," + cy);
1072             Fpslic.Cell cell = dev.cell(cx, cy);
1073             g.setColor(bg);
1074             g.fillRect(x, y, WIDTH, HEIGHT);
1075
1076             g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
1077             g.drawRect(x, y, WIDTH, HEIGHT);
1078
1079             //g.setColor((selx==cx && sely==cy) ? Color.red : Color.gray);
1080             //g.drawRect(x+(WIDTH-(LW*2))/2-1,    y+(HEIGHT-LW)/2-1, LW*2+1, LH+1);
1081
1082             //g.setColor(RED);
1083             //g.fillRect(x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2, LW,   LH);
1084             //g.setColor(Color.white);
1085             //g.drawString("1", x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2);
1086
1087             //g.setColor(BLUE);
1088             //g.fillRect(x+(WIDTH-(LW*2))/2+LW, y+(HEIGHT-LW)/2, LW,   LH);
1089             //g.setColor(Color.white);
1090             //g.drawString("0", x+(WIDTH-(LW*2))/2+LW,    y+(HEIGHT-LW)/2);
1091
1092             /*
1093               g.setColor(BLUE);
1094             ((Graphics2D)g).setStroke(new BasicStroke((float)1));
1095             switch(cell.yi()) {
1096                 case NORTH: g.drawLine(x+WIDTH/2+5,  y-10,        x+WIDTH/2+5, y+HEIGHT/2); break;
1097                 case SOUTH: g.drawLine(x+WIDTH/2-5,  y+HEIGHT+10, x+WIDTH/2-5, y+HEIGHT/2); break;
1098                 case EAST:  g.drawLine(x+WIDTH+10, y+HEIGHT/2+5,  x+WIDTH/2, y+HEIGHT/2+5); break;
1099                 case WEST:  g.drawLine(x-10,       y+HEIGHT/2-5,  x+WIDTH/2, y+HEIGHT/2-5); break;
1100                 case NONE:  break;
1101             }
1102             g.setColor(RED);
1103             ((Graphics2D)g).setStroke(new BasicStroke((float)1));
1104             switch(cell.xi()) {
1105                 case NW: g.drawLine(x-10+3,       y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
1106                 case SW: g.drawLine(x-10-3,       y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
1107                 case NE: g.drawLine(x+WIDTH+10+3, y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
1108                 case SE: g.drawLine(x+WIDTH+10-3, y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
1109                 case NONE:  break;
1110             }
1111             ((Graphics2D)g).setStroke(new BasicStroke(1));
1112             */
1113             /*
1114             if (selx==cx && sely==cy) {
1115                 int xp = 23 * (WIDTH+2) + 100;
1116                 int yp = 100;
1117                 g.setColor(Color.white);
1118                 g.fillRect(xp, yp, 300, 1000);
1119                 g.setColor(Color.black);
1120                 g.drawString("Cell " + cx + "," + cy,       xp, (yp+=15));
1121                 //g.drawString("X-Lut: " + bin8(cell.xlut()), xp, (yp+=15));
1122                 g.drawString("X-Lut: " + cell.printXLut(), xp, (yp+=15));
1123                 //g.drawString("Y-Lut: " + bin8(cell.ylut()), xp, (yp+=15));
1124                 g.drawString("Y-Lut: " + cell.printYLutX(), xp, (yp+=15));
1125             }
1126             */
1127             if ((cell.ylut()&0xff)==0xff && (cell.xlut()&0xff)==0xff) {
1128                 g.setColor(new Color(0x00, 0x00, 0xff));
1129                 g.drawString("1", left(cell) + 12, top(cell) + 30);
1130             }
1131             if ((cell.ylut()&0xff)==0x00 && (cell.xlut()&0xff)==0x00) {
1132                 g.setColor(new Color(0x00, 0x00, 0xff));
1133                 g.drawString("0", left(cell) + 12, top(cell) + 30);
1134             }
1135             if ((cell.ylut()&0xff)==0xB2) {
1136                 //System.out.println("muller @ " + cell);
1137                 //g.setColor(RED);
1138                 //g.drawString("X="+(x?"1":"0"), left(cell) + 10, top(cell) + 20);
1139                 g.setColor(new Color(0x00, 0xaa, 0x00));
1140                 g.drawString("C", left(cell) + 12, top(cell) + 30);
1141             }
1142
1143         }
1144     }
1145
1146     private static String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
1147     public static String bin8(byte b) {
1148         int n = b & 0xff;
1149         String ret = "";
1150         for(int i=7; i>=0; i--)
1151             ret += (n & (1<<i))==0 ? "0" : "1";
1152         return ret;
1153     }
1154
1155     public static void selfTest(FtdiBoard device, Fpslic at40k, Visualizer v) {
1156         /*
1157             int fail = 0;
1158             long now = System.currentTimeMillis();
1159             for(int x=0; x<24; x++)
1160                 for(int y=0; y<24; y++) {
1161                     Fpslic.Cell cell = at40k.cell(x,y);
1162                     scan(at40k, cell, YLUT, true);
1163                     //v.paint(img.getGraphics());
1164                     //v.getGraphics().drawImage(img, 0, 0, null);
1165                     cell.ylut(0xff);
1166                     boolean a = (device.readBus() & 0x80)!=0;
1167                     cell.ylut(0x00);
1168                     boolean b = (device.readBus() & 0x80)!=0;
1169                     if (a & !b) {
1170                         //System.out.println("pass " + x+","+y);
1171                         Graphics g = v.getGraphics();
1172                         g.setColor(Color.green);
1173                         g.drawString("pass", v.left(cell) + 10, v.top(cell) + 20);
1174                     } else {
1175                         System.out.println("FAIL!!!! " + x+","+y+" => " + a + " " + b);
1176                         fail++;
1177                         Graphics g = v.getGraphics();
1178                         g.setColor(Color.red);
1179                         g.drawString("FAIL", v.left(cell) + 10, v.top(cell) + 20);
1180                     }
1181                     scan(at40k, cell, YLUT, false);
1182                 }
1183
1184             System.out.println("failures: " + fail);
1185             System.out.println("scan time: " + (System.currentTimeMillis()-now) + "ms");
1186         */
1187     }
1188     
1189     public static void bounce(Fpslic.Cell cell, int xi, int yi) {
1190         cell.xlut((byte)0xCC);
1191         cell.ylut((byte)0xCC);
1192         cell.xi(xi);
1193         cell.yi(yi);
1194         cell.xo(false);
1195         cell.yo(false);
1196     }
1197     public static void muller(Fpslic.Cell cell, int xi, int yi) {
1198         cell.ylut(0xB2);
1199         cell.c(YLUT);
1200         cell.f(false);
1201         cell.t(false, false, true);
1202         cell.xi(xi);
1203         cell.yi(yi);
1204         cell.yo(false);
1205         cell.xo(false);
1206     }
1207
1208     /** watches for a rising/falling edge on Yin, emits a pulse on Xout */
1209     public static void pulse_detect(Fpslic.Cell c, int in, boolean falling) {
1210         c.ylut(0x00);
1211         c.xlut(0x00);
1212         switch(in) {
1213             case NW: case NE: case SW: case SE: {
1214                 c.xi(in);
1215                 loopback(c, XLUT);
1216                 if (!falling) c.ylut(lutSwap(0x0C)); /* x & !z */
1217                 else          c.ylut(lutSwap(0x30)); /* !x & z */
1218                 c.xlut(LUT_SELF);
1219                 break;
1220             }
1221             case NORTH: case SOUTH: case EAST: case WEST: {
1222                 c.yi(in);
1223                 loopback(c, YLUT);
1224                 if (!falling) c.xlut(0x0C); /* y & !z */
1225                 else          c.xlut(0x30); /* !y & z */
1226                 c.ylut(LUT_SELF);
1227                 break;
1228             }
1229             default: throw new Error();
1230         }
1231     }
1232
1233     /** watches for a pulse on Xin, copies value of Yin */
1234     public static void pulse_copy(Fpslic.Cell cell, int xi, int yi, boolean invert) {
1235         loopback(cell, YLUT);
1236         if (!invert) cell.ylut(0xB8);   /* yo = x ?  yi : z => 1011 1000 */
1237         else         cell.ylut(0x74);   /* yo = x ? !yi : z => 0111 0100 */
1238         if (!invert) cell.xlut(lutSwap(0xB8));   /* yo = x ?  yi : z => 1011 1000 */
1239         else         cell.xlut(lutSwap(0x74));   /* yo = x ? !yi : z => 0111 0100 */
1240         cell.xi(xi);
1241         cell.yi(yi);
1242     }
1243
1244     public static void loopback(Fpslic.Cell cell, int cin) {
1245         cell.f(false);
1246         cell.b(false);
1247         cell.t(false, false, true);
1248         cell.yo(false);
1249         cell.xo(false);
1250         cell.c(cin);
1251     }
1252     public static void doit(Fpslic at40k, FtdiBoard device) throws Exception {
1253
1254         Fpslic.Cell led = at40k.cell(1, 23);
1255         led.v(L2, true);
1256         led.h(L2, false);
1257         led.yi(L2);
1258         led.ylut(~LUT_SELF);
1259         led.xlut(LUT_SELF);
1260         led.yo(false);
1261
1262         Fpslic.Cell c = at40k.cell(1, 22);
1263         c.out(L1, true);
1264         c.out(L0, true);
1265         c.oe(V4);
1266         c.ylut(0xff);
1267         c.h(L1, true);
1268         c.h(L0, false);
1269
1270         c.v(L0, /*false*/true);
1271
1272         c.v(L1, true);
1273         c.f(false);
1274         c.b(false);
1275         c.c(YLUT);
1276
1277         for(int i=0; i<4; i++) at40k.cell(i, 20).h(L0, false);
1278         Fpslic.Cell z = at40k.cell(1, 20);
1279         z.out(L0, true);
1280         z.xlut(0xff);
1281         z.c(XLUT);
1282         z.yi(L0);
1283         z.ylut(~LUT_SELF);
1284         z.v(L0, true);
1285         //z.h(L0, true);
1286         z.h(L0, false);
1287         z.f(false);
1288         z.b(false);
1289         z.hwire(L0).east().drives(z.hwire(L0), false);
1290         z.hwire(L1).east().drives(z.hwire(L1), false);
1291         z.vwire(L0).south().drives(z.vwire(L0), false);
1292         z.vwire(L1).south().drives(z.vwire(L1), false);
1293         z.oe(H4);
1294
1295         z = at40k.cell(0, 20);
1296         z.oe(NONE);
1297         z.out(L0, true);
1298         z.out(L1, true);
1299         z.out(L2, true);
1300         //z.out(L3, true);
1301         z.out(L4, true);
1302         z.h(L0, true);
1303         z.h(L1, true);
1304         z.h(L2, true);
1305         //z.h(L3, true);
1306         z.h(L4, true);
1307         z.f(false);
1308         z.b(false);
1309         z.yi(EAST);
1310         z.ylut(LUT_SELF);
1311         z.c(YLUT);
1312
1313         for(int y=20; y<=22; y++)
1314             for(int x=2; x<=5; x++) {
1315                 c = at40k.cell(x, y);
1316                 copy(c, NW, WEST);
1317             }
1318
1319         //c = at40k.cell(2, 22);
1320         //c.h(L0, true);
1321         //c.yi(L0);
1322
1323         c = at40k.cell(1, 21);
1324         c.v(L0, true);
1325         c.v(L2, true);
1326         c.yi(L0);
1327         c.out(L2, true);
1328         c.ylut(LUT_SELF);
1329         c.c(YLUT);
1330         c.b(false);
1331         c.f(false);
1332         c.oe(NONE);
1333         c.yo(false);
1334
1335         
1336
1337         c = at40k.cell(13, 22);
1338         c.xlut(LUT_OTHER | 0xF0);
1339         c.c(XLUT);
1340         c.t(false, false, true);
1341         c.b(false);
1342         c.f(false);
1343         c.ylut(0xF0);
1344         c.yi(EAST);
1345         c.yo(false);
1346         /*
1347         // this gate detects a rising edge on its Xin (delayed copy on Yin); when viewed, it inverts its state
1348         c = at40k.cell(14, 22);
1349         c.ylut(0x00);
1350         c.c(XLUT);
1351         c.f(false);
1352         c.b(false);
1353         c.t(false, false, true);
1354         c.xi(SE);
1355         c.yi(SOUTH);
1356         c.yo(false);
1357         c.xo(false);
1358         c.ylut(0xA6); // (x & !z) ? ~y : y
1359         c.xlut(LUT_SELF); 
1360
1361         c = at40k.cell(14, 20);
1362         c.ylut(LUT_OTHER);
1363         c.xi(NE);
1364         c = at40k.cell(14, 21);
1365         c.ylut(LUT_SELF);
1366         c.xi(SOUTH);
1367
1368         c = at40k.cell(13, 22);
1369         c.xlut(0x00);
1370         c.xlut(LUT_OTHER);// | 0xF0);
1371 */
1372         //c = at40k.cell(13, 22);
1373         //copy(c, NW, EAST);
1374         /*
1375         c.ylut(0x00);
1376         c.c(YLUT);
1377         c.f(false);
1378         c.b(false);
1379         c.t(false, false, true);
1380         c.xi(SE);
1381         c.yi(NORTH);
1382         c.yo(false);
1383         c.xo(false);
1384         c.ylut(0x54);  // (x || z) & !y
1385         */
1386
1387         /*        
1388         c = at40k.cell(2, 21);
1389         c.ylut(0x00);
1390         c.c(YLUT);
1391         c.f(false);
1392         c.b(false);
1393         c.t(false, false, true);
1394         c.xi(SE);
1395         c.yi(WEST);
1396         c.yo(false);
1397         c.xo(false);
1398         c.ylut(0xE8);
1399  
1400        //at40k.cell(2, 21).xlut(0xF0);
1401
1402         at40k.cell(3, 22).ylut(LUT_OTHER);
1403         at40k.cell(3, 22).xi(SW);
1404         */
1405         //at40k.iob_top(5, true).enableOutput(SOUTH);
1406         //at40k.iob_top(5, false).enableOutput(SE);
1407     }
1408
1409     public static int yofs = mullers ? 19 : 22;
1410     public static void counter(Fpslic at40k, FtdiBoard device) throws Exception {
1411         // watch for rising edge from south, emit pulse on Xout (to NE)
1412         //copy(at40k.cell(16,23), SW, WEST);
1413         
1414         for(int x=22; x>=1; x-=2) {
1415             pulse_detect(at40k.cell(x, yofs), SE,      false);
1416             pulse_detect(at40k.cell(x, yofs-1), EAST,    true);
1417             pulse_copy(at40k.cell(x-1, yofs), SE, SOUTH, false);
1418             pulse_copy(at40k.cell(x-1, yofs-1), NE, NORTH, true);
1419
1420             //pulse_detect(at40k.cell(15, 22), NORTH, false);
1421             //pulse_detect(at40k.cell(16, 22), NW,    true);
1422             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
1423             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1424         }
1425         for(int x=23; x>1; x-=2) {
1426             pulse_detect(at40k.cell(x-1, yofs-2), SW,    false);
1427             pulse_detect(at40k.cell(x-1, yofs-3), WEST,  true);
1428             pulse_copy(at40k.cell(x, yofs-2), SW, SOUTH, false);
1429             pulse_copy(at40k.cell(x, yofs-3), NW, NORTH, true);
1430
1431             //pulse_detect(at40k.cell(15, 22), NORTH, false);
1432             //pulse_detect(at40k.cell(16, 22), NW,    true);
1433             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
1434             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1435         }
1436         copy(at40k.cell(1, yofs-2), SOUTH, SOUTH);
1437         copy(at40k.cell(1, yofs-3), NORTH, NORTH);
1438         at40k.cell(1, yofs-3).ylut(~at40k.cell(1, yofs-3).ylut());
1439         at40k.cell(1, yofs-3).xlut(~at40k.cell(1, yofs-3).xlut());
1440
1441         copy(at40k.cell(23, yofs), SOUTH, SOUTH);
1442         copy(at40k.cell(23, yofs-1), SOUTH, SOUTH);
1443
1444         for(int i=23; i>yofs; i--) copy(at40k.cell(1, i), SOUTH, SOUTH);
1445
1446         //at40k.iob_top(1, true).slew(SLOW);
1447         //at40k.iob_top(1, false).slew(SLOW);
1448
1449     }
1450     public static void fill(Fpslic at40k, FtdiBoard device, int num) throws Exception {
1451         //muller(at40k.cell(PIPELEN,22), NE, WEST);
1452         Fpslic.Cell a = at40k.cell(10,22);
1453         Fpslic.Cell b = at40k.cell(11,22);
1454         a.ylut(0x00);
1455         for(int i=0; i<num; i++) {
1456             //System.out.println(i);
1457             b.lut(0xff, 0xff);
1458             device.flush();
1459             try { Thread.sleep(1); } catch (Exception e) { }
1460             b.lut(0x00, 0x00);
1461             device.flush();
1462             try { Thread.sleep(1); } catch (Exception e) { }
1463         }
1464         b.ylut(0xB2);
1465         a.ylut(0xB2);
1466     }
1467     public static void showit(Fpslic dev, FtdiBoard drone, final Visualizer vis) {
1468         final long then = System.currentTimeMillis();
1469         final Graphics g = vis.getGraphics();
1470         g.setFont(new Font("sansserif", Font.BOLD, 24));
1471         final Color red = new Color(0xff, 0x99, 0x99);
1472         final Color green = new Color(0x99, 0xff, 0x99);
1473         for(int xx=0; xx<=22; xx++) {
1474             for(int yy=23; yy>=0; yy--) {
1475                 //for(int xx=5; xx<=PIPELEN-1; xx++) {
1476                 //for(int yy=21; yy<=22; yy++) {
1477                 final int x = xx;
1478                 final int y = yy;
1479                 final Fpslic.Cell cell = dev.cell(x, y);
1480                 if ((cell.ylut()&0xff)!=0xB2) continue;
1481                 FtdiBoard.ByteCallback bc = new FtdiBoard.ByteCallback() {
1482                         public void call(byte b) throws Exception {
1483                             boolean v = (b & 0x80) != 0;
1484                             vis.drawCell(g, x, y, v?red:green);
1485                             //if (x==PIPELEN-1 && y==22) System.out.println("time: " + (System.currentTimeMillis()-then));
1486                         }
1487                     };
1488                 scan(dev, cell, NONE, true);
1489                 try {
1490                     drone.readBus(bc);
1491                     //scan(dev, cell, YLUT, false);
1492                     cell.v(L3, false);
1493                     dev.cell(x, 15).h(L3, false);
1494                     dev.cell(x, 15).v(L3, false);
1495                 } catch (IOException ex) {
1496                     throw new RuntimeException(ex);
1497                 }
1498             }
1499         }
1500     }
1501     public static void drain(Fpslic at40k, FtdiBoard device) throws Exception {
1502         Fpslic.Cell a = at40k.cell(10,22);
1503         Fpslic.Cell b = at40k.cell(11,22);
1504         a.lut(0x00, 0x00);
1505         b.lut(0x00, 0x00);
1506         for(int i=0; i<30; i++) {
1507             //System.out.println(i);
1508             a.lut(0xff, 0xff);
1509             device.flush();
1510             try { Thread.sleep(1); } catch (Exception e) { }
1511             a.lut(0x00, 0x00);
1512             device.flush();
1513             try { Thread.sleep(1); } catch (Exception e) { }
1514         }
1515         b.ylut(0xB2);
1516         a.ylut(0xB2);
1517     }
1518     public static void doitx(Fpslic at40k, FtdiBoard device) throws Exception {
1519         for(int i=5; i<PIPELEN+1; i++) bounce(at40k.cell(i, 23), SE,                     SOUTH);
1520         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 22), x==PIPELEN-1 ? SE : NE, WEST);
1521         
1522         bounce(at40k.cell(PIPELEN,  21), NW, WEST);
1523         
1524         for(int x=5; x<PIPELEN;   x++) muller(at40k.cell(x, 21), SW,                     x==PIPELEN-1 ? NORTH : EAST);
1525         for(int x=4; x<PIPELEN+1; x++) bounce(at40k.cell(x, 20), NW,                     NORTH);
1526         
1527         bounce(at40k.cell(4, 22), SE, EAST);
1528         //muller(at40k.cell(4PIPELEN-1,21), SW, NORTH);
1529         
1530         //muller(at40k.cell(4,22), NE, WEST);
1531         //at40k.cell(4,22).ylut(0xEE);
1532         muller(at40k.cell(5, 22), NE, SOUTH);
1533         muller(at40k.cell(5, 21), NW, EAST);
1534         /*
1535         for(int x=4; x>=0; x--) {
1536             at40k.cell(x, 21).ylut(0xAA);
1537             at40k.cell(x, 21).yi(EAST);
1538             at40k.cell(x, 21).yo(false);
1539         }
1540
1541         at40k.cell(0, 22).ylut(0xAA);
1542         at40k.cell(0, 22).yi(SOUTH);
1543         at40k.cell(0, 22).yo(false);
1544
1545         at40k.cell(0, 23).ylut(~0xAA);
1546         at40k.cell(0, 23).xlut(~0xcc);
1547         at40k.cell(0, 23).yi(SOUTH);
1548         at40k.cell(0, 23).yo(false);
1549         */
1550         for(int x=3; x<=23; x+=2) {
1551             pulse_detect(at40k.cell(x-1, 19), SW,    false);
1552             pulse_detect(at40k.cell(x-1, 18), WEST,  true);
1553             pulse_copy(at40k.cell(x, 19), SW, SOUTH, false);
1554             pulse_copy(at40k.cell(x, 18), NW, NORTH, true);
1555
1556             if (x<17) {
1557                 pulse_detect(at40k.cell(x-1, 16), SW,    false);
1558                 pulse_detect(at40k.cell(x-1, 15), WEST,  true);
1559                 pulse_copy(at40k.cell(x, 16), SW, SOUTH, false);
1560                 pulse_copy(at40k.cell(x, 15), NW, NORTH, true);
1561             }
1562             //pulse_detect(at40k.cell(15, 22), NORTH, false);
1563             //pulse_detect(at40k.cell(16, 22), NW,    true);
1564             //pulse_copy(at40k.cell(16, 21), NW, WEST, false);
1565             //pulse_copy(at40k.cell(15, 21), NE, EAST, true);
1566         }
1567         for(int x=14; x>=1; x--)
1568             copy(at40k.cell(x, 17), EAST, EAST);
1569         for(int x=4; x>=0; x--)
1570             copy(at40k.cell(x, 21), EAST, EAST);
1571         copy(at40k.cell(13, 17), SOUTH, SOUTH);
1572
1573         copy(at40k.cell(0, 20), NORTH, NORTH);
1574         copy(at40k.cell(0, 19), NORTH, NORTH);
1575         copy(at40k.cell(0, 18), NORTH, NORTH);
1576         copy(at40k.cell(0, 17), NORTH, NORTH);
1577         copy(at40k.cell(0, 16), NORTH, NORTH);
1578         copy(at40k.cell(1, 16), WEST, WEST);
1579         copy(at40k.cell(1, 15), NORTH, NORTH);
1580
1581         copy(at40k.cell(1, 20), SOUTH, SOUTH);
1582         copy(at40k.cell(1, 19), SOUTH, SOUTH);
1583         copy(at40k.cell(1, 18), SOUTH, SOUTH);
1584
1585         for(int y=20; y<=23; y++)
1586             copy(at40k.cell(23, y), SOUTH, SOUTH);
1587
1588
1589         //for(int x=19; x<=23; x++)
1590         //copy(at40k.cell(x, 0), WEST, WEST);
1591         //copy(at40k.cell(18, 19), NW, NW);
1592         //at40k.iob_top(5, true).enableOutput(SOUTH);
1593         //at40k.iob_top(5, false).enableOutput(SOUTH);
1594     }
1595 }