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