checkpoint
[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 edu.berkeley.obits.device.atmel.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.color.*;
8 import org.ibex.util.*;
9 import java.io.*;
10 import java.util.*;
11 import gnu.io.*;
12
13 public class AtmelSerial {
14
15     public static SerialPort detectObitsPort() throws Exception {
16         Enumeration e = CommPortIdentifier.getPortIdentifiers();
17         while(e.hasMoreElements()) {
18             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
19             Log.info(AtmelSerial.class, "trying " + cpi.getName());
20             if (cpi.getName().startsWith("/dev/cu.usbserial-")) return new RXTXPort(cpi.getName());
21             if (cpi.getName().startsWith("/dev/ttyS0")) return new RXTXPort(cpi.getName());
22         }
23         Log.info(AtmelSerial.class, "returning null...");
24         return null;
25     }
26
27     public static void main(String[] s) throws Exception {
28         AvrDrone device = new AvrDrone(detectObitsPort());
29         At40k at40k = new At40k.At40k10(device);
30         int count = 0;
31         try {
32             long begin = System.currentTimeMillis();
33             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34             for(String str = br.readLine(); str != null; str = br.readLine()) {
35                 long foo = Long.parseLong(str, 16);
36                 device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
37                 count++;
38                 if (count % 100 == 0) Log.info(AtmelSerial.class, "wrote " + count + " configuration octets");
39             }
40             device.flush();
41             long end = System.currentTimeMillis();
42             Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s");
43             Thread.sleep(1000);
44             Log.info(AtmelSerial.class, "issuing command");
45
46             //at40k.iob_top(2, true).oe(false);
47             //at40k.iob_top(2, false).oe(false);
48             //at40k.iob_top(1, true).oe(false);
49
50             // this command confirmed to turn *on* led0
51             //at40k.iob_top(1, false).output(0);
52             /*
53             for(int i=0; i<20; i++) {
54                 at40k.iob_bot(i, false).output(0);
55                 at40k.iob_bot(i, true).output(0);
56             }
57             */
58
59             //System.out.println("tick");
60                 //Thread.sleep(3000);
61                 //System.out.println("tick");
62                 //at40k.cell(0x01, 0x17).xlut((byte)0x);
63
64             /*
65             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).xlut(), 16));
66             System.out.println(Integer.toString(0xff & at40k.cell(0x01, 0x17).ylut(), 16));
67             at40k.cell(0x01, 0x17).ylut((byte)0xff);
68             */
69
70             //at40k.cell(0x01, 0x17).wi(L1);
71             /*
72             System.out.println("a: " + at40k.new SectorWire(true, 0, 4, 0x17).driverRight());
73             System.out.println("b: " + at40k.new SectorWire(true, 1, 4, 0x17).driverRight());
74             At40k.SectorWire h0p0 = at40k.new SectorWire(true, 0, 0, 0x17);
75             At40k.SectorWire h0p1 = at40k.new SectorWire(true, 1, 0, 0x17);
76             At40k.SectorWire h0p2 = at40k.new SectorWire(true, 2, 0, 0x17);
77             At40k.SectorWire h4p0 = at40k.new SectorWire(true, 0, 4, 0x17);
78             At40k.SectorWire h4p1 = at40k.new SectorWire(true, 1, 4, 0x17);
79             At40k.SectorWire h4p2 = at40k.new SectorWire(true, 2, 4, 0x17);
80
81             //h4p1.drives(h0p1, false);
82             //at40k.cell(0x04, 0x17).out(L1, false);
83             //at40k.cell(0x04, 0x17).h(L0, false);
84
85             for(int plane=0; plane<5; plane++) {
86                 at40k.new SectorWire(true, plane,     4, 0x17).drives(at40k.new SectorWire(true, plane,     0, 0x17), false);
87                 at40k.cell(0x04, 0x17).out(plane, false);
88                 at40k.cell(0x04, 0x17).h(plane, false);
89                 at40k.cell(0x01, 0x17).h(plane, false);
90             }
91             try { Thread.sleep(2000); } catch (Exception e) { }
92
93             int plane=0;
94             at40k.new SectorWire(true, plane, 4, 0x17).drives(at40k.new SectorWire(true, plane, 0, 0x17), true);
95             at40k.cell(0x04, 0x17).out(plane, true);
96             at40k.cell(0x04, 0x17).h(plane, true);
97             at40k.cell(0x01, 0x17).h(plane, true);
98             at40k.cell(0x01, 0x17).wi(plane);
99
100             */
101
102             /*
103             System.out.println("xlut is " + hex(at40k.cell(0x04, 0x17).xlut()));
104             System.out.println("ylut is " + hex(at40k.cell(0x04, 0x17).ylut()));
105             At40k.Cell cell = at40k.cell(0x04, 0x17);
106             //cell.xlut(0xff);
107             //cell.f(false);
108             System.out.println(cell.c());
109             cell.c(YLUT);
110             cell.ylut(0x4D);
111             cell.xlut(0x00);
112
113             cell.b(false);
114             cell.f(false);
115             //cell.t(false, false, true);
116             cell.t(false, true, false);
117             cell.out(L3, true);
118             cell.wi(L3);
119
120             cell.yo(false);
121             cell.h(L0, false);
122             cell.h(L1, false);
123             cell.h(L2, false);
124             cell.h(L3, false);
125             cell.h(L4, false);
126
127             for(int i=3; i>=1; i--) {
128                 at40k.cell(i, 0x17).yi(EAST);
129                 at40k.cell(i, 0x17).ylut(0x55);
130                 at40k.cell(i, 0x17).yo(false);
131             }
132             */
133
134             //System.out.println("reading port status: " + Integer.toString(device.readBus() & 0xff, 16));
135
136
137             // blank these out
138             /*
139             at40k.cell(23, 8).ylut(0xff);
140             at40k.cell(23, 11).ylut(0xff);
141             at40k.iob_right(8, true).enableOutput();
142             at40k.iob_right(11, true).enableOutput();
143             */
144             //for(int x=4;  x<=22; x++) swap(at40k.cell(x, 22), NW, NORTH);
145
146
147             // entry cell: just copy X->X Y->Y
148             //at40k.cell(4,23).b(false);
149             //at40k.cell(4,23).yo(false);
150             //at40k.cell(4,23).ylut(at40k.cell(4,23).xlut());
151             //at40k.cell(4,23).xo(false);
152             /*
153             at40k.cell(4,23).xlut(0x55);
154             at40k.cell(4,23).ylut(0x55);
155             */
156             /*
157             at40k.cell(4,23).xlut(0x71);
158             at40k.cell(4,23).ylut(0x44);
159             at40k.cell(4,23).c(YLUT);
160             at40k.cell(4,23).f(false);
161             at40k.cell(4,23).t(false, false, true);
162             */
163
164             //for(int x=6;  x<=23; x++) copy(at40k.cell(x, 23), NW, WEST);  // top row copies to the right
165             /*
166             copy(at40k.cell(5, 22), NW, NORTH);
167             for(int x=6;  x<=22; x++) copy(at40k.cell(x, 22), NW, WEST);  // second top row copies to the right
168             //for(int y=22; y>=10; y--) copy(at40k.cell(23, y), NW, NORTH); // right edge copies down
169             for(int y=21; y>=9;  y--) copy(at40k.cell(22, y), NW, NORTH); // second right edge copies down
170             copy(at40k.cell(23, 9), NW, WEST);                            // second output
171             */
172             /*
173             handshaker(at40k.cell(4,23));
174             at40k.cell(4,23).xi(NW);
175             at40k.cell(4,23).yi(SOUTH);
176
177             //handshaker(at40k.cell(5,23));
178             //at40k.cell(5,23).yi(NORTH);
179
180             at40k.cell(5,23).yi(NORTH);
181             at40k.cell(5,23).xlut(0x55);
182             at40k.cell(5,23).xi(SW);
183             at40k.cell(5,23).ylut(0x55);
184             at40k.cell(5,22).yi(NORTH);
185             at40k.cell(5,22).xlut(0x55);
186
187             bounce(at40k.cell(4,22));
188
189             // cell southeast of entry cell
190             at40k.cell(3,22).xi(NE);      // NW->xin
191             at40k.cell(3,22).ylut(0x33);  // xin->y
192             at40k.cell(3,22).yo(false);   // y->yout
193             copy(at40k.cell(3, 21), NW, NORTH);  // second top row copies to the right
194             copy(at40k.cell(4, 21), NW, EAST);  // second top row copies to the right
195             copy(at40k.cell(5, 21), NW, EAST);  // second top row copies to the right
196             copy(at40k.cell(6, 21), NW, EAST);  // second top row copies to the right
197             copy(at40k.cell(6, 22), NW, SOUTH);  // second top row copies to the right
198             */
199             /*
200             at40k.cell(05,22).xlut(0xff);
201             at40k.cell(05,22).ylut(0xff);
202             at40k.cell(05,22).c(XLUT);
203             at40k.cell(05,22).f(false);
204             at40k.cell(05,22).b(false);
205             at40k.cell(05,22).oe(NONE);
206             at40k.cell(05,22).v(L3, true);
207             at40k.cell(05,22).out(L3, true);
208             */
209             at40k.cell(21,15).yi(WEST);
210             at40k.cell(21,15).ylut(0xAA);
211             at40k.cell(22,15).yi(WEST);
212             at40k.cell(22,15).ylut(0xAA);
213             at40k.cell(23,15).h(3, true);
214             at40k.cell(23,15).yi(L3);
215             at40k.cell(23,15).ylut(0xAA);
216             at40k.iob_right(15, true).enableOutput(WEST);
217             /*
218             at40k.cell(22,11).ylut(0xff);
219             at40k.cell(23,11).yi(L3);
220             //at40k.cell(23,11).yi(WEST);
221             //at40k.cell(23,11).xi(L1);
222             at40k.cell(23,11).ylut(0xAA);
223             at40k.iob_right(11, true).enableOutput(WEST);
224             at40k.cell(23,11).v(L3, true);
225             at40k.cell(23,11).yo(false);
226             //at40k.flush();
227             */
228             int vx=04;
229             int vv=23;
230             /*
231             System.out.println("correct: " + at40k.cell(19,15).hwire(L3) + " drives " + at40k.cell(20,15).hwire(L3));
232             System.out.println("correct: " + at40k.cell(15,15).hwire(L3) + " drives " + at40k.cell(19,15).hwire(L3));
233             System.out.println("correct: " + at40k.cell(11,15).hwire(L3) + " drives " + at40k.cell(15,15).hwire(L3));
234             System.out.println("correct: " + at40k.cell(07,15).hwire(L3) + " drives " + at40k.cell(11,15).hwire(L3));
235
236             at40k.cell(19,15).hwire(L3).drives(at40k.cell(20,15).hwire(L3), true);
237             at40k.cell(15,15).hwire(L3).drives(at40k.cell(19,15).hwire(L3), true);
238             at40k.cell(11,15).hwire(L3).drives(at40k.cell(15,15).hwire(L3), true);
239             at40k.cell(07,15).hwire(L3).drives(at40k.cell(11,15).hwire(L3), true);
240             */
241             //at40k.cell(05,vv).xlut(0xff);
242             //at40k.cell(05,vv).ylut(0xff);
243             /*
244             at40k.cell(vx,vv).c(YLUT);
245             at40k.cell(vx,vv).f(false);
246             at40k.cell(vx,vv).b(false);
247             at40k.cell(vx,vv).oe(NONE);
248             at40k.cell(vx,vv).v(L3, true);
249             at40k.cell(vx,vv).out(L3, true);
250             */
251             /*
252             at40k.cell(vx,15).v(L3, true);
253             at40k.cell(vx,15).h(L3, true);
254             at40k.cell(vx,19).vwire(L3).drives(at40k.cell(vx,15).vwire(L3), true);
255             at40k.cell(vx,23).vwire(L3).drives(at40k.cell(vx,19).vwire(L3), true);
256             */
257
258             //at40k.cell(5,23).ylut(0x00);
259             //at40k.cell(6,22).ylut(0xff);
260             //at40k.cell(22,11).ylut(0xff);
261             /*
262             At40k.Cell cell = at40k.cell(4, 16);
263             cell.xlut(0xff);
264             cell.ylut(0xff);
265             cell.b(false);
266             cell.f(false);
267             cell.c(XLUT);
268             cell.h(L3, true);
269             cell.v(L3, true);
270             cell.out(L3, true);
271             cell.oe(NONE);
272             */
273             //scan(at40k, cell, YLUT, true);
274             //scan(at40k, cell, YLUT, false);
275
276             device.scanFPGA();
277             Visualizer v = new Visualizer(at40k, device);
278             v.show();
279             v.setSize(1380, 1080);
280             At40k.Cell cell = at40k.cell(4, 23);
281             Image img = v.createImage(v.getWidth(), v.getHeight());
282             /*
283             int x = 1;
284             int y = 14;
285             cell = at40k.cell(x,y);
286             scan(at40k, cell, YLUT, true);
287             cell.c(YLUT);
288             cell.b(false);
289             cell.f(false);
290             cell.oe(NONE);
291             cell.ylut(0xff);
292             */
293             for(int x=0; x<10; x++)
294                 for(int y=20; y<23; y++) {
295                     scan(at40k, cell, YLUT, true);
296                     //v.paint(img.getGraphics());
297                     //v.getGraphics().drawImage(img, 0, 0, null);
298                     cell.ylut(0xff);
299                     cell.c(YLUT);
300                     cell.b(false);
301                     cell.f(false);
302                     cell.ylut(0xff);
303                     cell.ylut(0xff);
304                     cell.ylut(0xff);
305                     cell.ylut(0xff);
306                     //try { Thread.sleep(1000); } catch (Exception e) { }
307                     int a = device.readBus();
308                     cell.ylut(0x00);
309                     int b = device.readBus();
310                     scan(at40k, cell, YLUT, false);
311                     System.out.println("scan " + x+","+y+" => " + a + " " + b);
312                 }
313
314             for(int i=0; i<10000; i++) {
315                 v.refresh();
316                 try { Thread.sleep(100); } catch (Exception e) { }
317             }
318             //cell.ylut(0x09);
319
320             //at40k.cell(0x01, 0x17).h(0, false);
321             //at40k.cell(0x01, 0x17).xi(NE);
322             //at40k.cell(0x01, 0x17).ylut((byte)0x55);
323
324             //at40k.cell(0x04, 0x17).xlut((byte)0x10);
325             //at40k.cell(0x04, 0x17).ylut((byte)0x10);
326             //at40k.cell(0x04, 0x17).yo(false);
327             //at40k.cell(0x04, 0x17).xo();
328
329             /*
330             at40k.cell(0x01, 0x17).xi(L0);
331             at40k.cell(0x01, 0x17).h(L0, true);
332             */
333             /*
334             at40k.cell(0x03, 0x17).xlut((byte)0x55);
335             at40k.cell(0x03, 0x17).ylut((byte)0x55);
336             at40k.cell(0x03, 0x17).yi(EAST);
337             at40k.cell(0x03, 0x17).ylut((byte)0x55);
338             at40k.cell(0x03, 0x17).yo(true);
339
340             at40k.cell(0x03, 0x17).f(false);
341             at40k.cell(0x03, 0x17).c(XLUT);
342             at40k.cell(0x03, 0x17).oe(NONE);
343             at40k.cell(0x03, 0x17).out(L0, true);
344
345             at40k.cell(0x02, 0x17).yi(EAST);
346             at40k.cell(0x02, 0x17).ylut((byte)0x55);
347             at40k.cell(0x02, 0x17).yo(false);
348
349             at40k.cell(0x01, 0x17).yi(EAST);
350             at40k.cell(0x01, 0x17).ylut((byte)0x55);
351             at40k.cell(0x01, 0x17).yo(false);
352
353             at40k.cell(0x01, 0x17).h(L0, true);
354             at40k.cell(0x01, 0x17).v(L0, false);
355             */
356             //at40k.cell(0x01, 0x17).yi(L0);
357             //at40k.cell(0x01, 0x17).xi(L0);
358             //at40k.cell(0x01, 0x17).ylut((byte)0x33);
359
360             /*
361             at40k.cell(0x03, 0x17).h(L0, true);
362             at40k.cell(0x03, 0x17).out(L0, true);
363             at40k.cell(0x03, 0x17).c(XLUT);
364             at40k.cell(0x03, 0x17).f(false);
365             */
366             /*
367             at40k.cell(0x01, 0x17).xin(4);
368             at40k.cell(0x01, 0x17).yin(4);
369             at40k.cell(0x01, 0x16).ylut((byte)0x00);
370             device.mode4(2, 0x17, 0x01, 0);
371
372             for(int i=0; i<10; i++) {
373                 Thread.sleep(3000);
374                 System.out.println("tick");
375                 //at40k.cell(0x01, 0x17).xlut((byte)0xFF);
376                 at40k.cell(0x00, 0x17).ylut((byte)0x00);
377                 device.flush();
378                 Thread.sleep(3000);
379                 System.out.println("tick");
380                 //at40k.cell(0x01, 0x17).xlut((byte)0x00);
381                 at40k.cell(0x00, 0x17).ylut((byte)0xFF);
382                 device.flush();
383             }
384             */
385
386
387             /*
388             at40k.iob_top(0, true).output(0);
389             at40k.iob_top(0, true).oe(false);
390             at40k.iob_top(0, true).pullup();
391             device.flush();
392             Thread.sleep(3000);
393
394             Log.info(AtmelSerial.class, "issuing command");
395             at40k.iob_top(1, true).pulldown();
396             device.flush();
397             */
398             Log.info(AtmelSerial.class, "done");
399             System.exit(0);
400         } catch (Exception e) { e.printStackTrace(); }
401     }
402
403     public static void scan(At40k dev, At40k.Cell cell, int source, boolean setup) {
404         if (setup) {
405             cell.c(source);
406             cell.b(false);
407             cell.f(false);
408         }
409         cell.v(L3, setup);
410         cell.out(L3, setup);
411
412         At40k.SectorWire sw = cell.vwire(L3);
413         //System.out.println("wire is: " + sw);
414         while(sw.row > (12 & ~0x3) && sw.south() != null) {
415             //System.out.println(sw + " -> " + sw.south());
416             sw.drives(sw.south(), setup);
417             sw = sw.south();
418         }
419         while(sw.row < (12 & ~0x3) && sw.north() != null) {
420             //System.out.println(sw + " -> " + sw.north());
421             sw.drives(sw.north(), setup);
422             sw = sw.north();
423         }
424         //cell = dev.cell(19, 15);
425         cell = dev.cell(cell.col, 15);
426         /*
427         System.out.println("cell is " + cell);
428         cell.xlut(0xff);
429         cell.ylut(0xff);
430         cell.b(false);
431         cell.f(false);
432         cell.c(XLUT);
433         cell.out(L3, true);
434         cell.oe(NONE);
435         */
436         cell.h(L3, setup);
437         cell.v(L3, setup);
438         sw = cell.hwire(L3);
439         while(sw.east() != null) {
440             //System.out.println(sw + " -> " + sw.east());
441             sw.drives(sw.east(), setup);
442             sw = sw.east();
443         }
444     }
445
446     public static void copy(At40k.Cell cell, int xdir, int ydir) {
447         cell.xlut((byte)0x33);
448         cell.ylut((byte)0x55);
449         cell.xi(xdir);
450         cell.yi(ydir);
451         cell.xo(false);
452         cell.yo(false);
453     }
454     public static String hex(int x) {
455         return Long.toString(x & 0xffffffffL, 16);
456     }
457
458     public static void bounce(At40k.Cell cell) {
459         cell.xlut((byte)0x33);
460         cell.ylut((byte)0x55);
461         cell.xi(NE);
462         cell.yi(NORTH);
463         cell.xo(false);
464         cell.yo(false);
465     }
466     public static void handshaker(At40k.Cell cell) {
467         cell.xlut(0x22);
468         cell.ylut(0x71);
469         cell.c(XLUT);
470         cell.f(false);
471         cell.t(false, false, true);
472     }
473
474     public static class Visualizer extends Frame implements MouseMotionListener {
475         public static final int WIDTH = 40;
476         public static final int HEIGHT = 40;
477         public static final int LW = 15;
478         public static final int LH = 15;
479         public static final Color RED  = new Color(0xaa, 0x55, 0x55);
480         public static final Color BLUE = new Color(0x55, 0x55, 0xaa);
481         private final At40k dev;
482         private final AvrDrone drone;
483         int selx = -1;
484         int sely = -1;
485         public Visualizer(At40k dev, AvrDrone drone) {
486             this.dev = dev;
487             this.drone = drone;
488             show();
489             addMouseMotionListener(this);
490         }
491         public void mouseMoved(MouseEvent e) {
492             int x = e.getX();
493             int y = e.getY();
494             if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
495                 int cx = selx;
496                 int cy = sely;
497                 selx = -1;
498                 sely = -1;
499                 drawCell(getGraphics(), cx, cy);
500                 drawSector(getGraphics(), dev.cell(cx, cy).sector());
501             }
502             selx = (x-20)/(WIDTH+2);
503             sely = (23 - (y-20)/(HEIGHT+2))+1;
504             if (selx >= 0 && selx < 24 && sely >= 0 && sely < 24) {
505                 drawCell(getGraphics(), selx, sely);
506                 drawSector(getGraphics(), dev.cell(selx, sely).sector());
507             }
508         }
509         public void mouseDragged(MouseEvent e) {
510         }
511         public void paint(Graphics g) {
512             g.setColor(Color.white);
513             g.fillRect(0, 0, getWidth(), getHeight());
514             for(int x=0; x<24; x++)
515                 for(int y=0; y<24; y++)
516                     drawCell(g,x,y);
517             for(int x=0; x<=23; x+=4)
518                 for(int y=23; y>=0; y-=4) 
519                     drawSector(g, dev.cell(x, y).sector());
520             g.setColor(BLUE);
521             g.drawString("Ready", (5*(WIDTH+2))+20, 40);
522             g.setColor(RED);
523             g.drawString("Send",  (3*(WIDTH+2))+20, 40);
524             g.setColor(BLUE);
525             refresh();
526         }
527         public void refresh() {
528             Graphics g = getGraphics();
529             int data = drone.readBus() & 0xff;
530             for(int i=0; i<8; i++) {
531                 g.setColor((data & (1<<i))==0 ? Color.black : Color.green);
532                 g.drawString("D"+i,  (24*(WIDTH+2))+20, ((23-(i+7))*(HEIGHT+2))+60-HEIGHT/2);
533             }
534         }
535         public int left(At40k.Cell cell) { return (cell.col)   *(WIDTH+2)+20; }
536         public int top(At40k.Cell cell)  { return (23-cell.row)*(HEIGHT+2)+60; }
537         public void drawSector(Graphics g, At40k.Sector sector) {
538             g.setColor(Color.gray);
539             ((Graphics2D)g).setStroke(new BasicStroke(1));
540             int px = ((sector.col)*(WIDTH+2))+20-1;
541             int py = ((23-(sector.row+3))*(HEIGHT+2))+60-1;
542             g.drawRect(px, py, (WIDTH+2)*4+2, (HEIGHT+2)*4+2);
543             for(int dir=0; dir<2; dir++) {
544                 boolean h = dir==0;
545                 for(int y=h?sector.row:sector.col; y<(h?sector.row+4:sector.col+4); y++)
546                     for(int plane=0; plane<=4; plane++) {
547                         At40k.Cell cell      = h ? dev.cell(sector.col,   y) : dev.cell(y, sector.row);
548                         At40k.Cell cell_east = h ? dev.cell(sector.col-1, y) : dev.cell(y, sector.row-1);
549                         At40k.Cell cell_west = h ? dev.cell(sector.col+4, y) : dev.cell(y, sector.row+4);
550                         boolean draw = false;
551                         if (h) {
552                             if (cell_east!=null &&
553                                 (cell_east.hwire(plane).drives(cell.hwire(plane)) ||
554                                  cell_east.hwire(plane).drives(cell.hwire(plane))))
555                                 draw = true;
556                             if (cell_west!=null &&
557                                 (cell_west.hwire(plane).drives(cell.hwire(plane)) ||
558                                  cell_west.hwire(plane).drives(cell.hwire(plane))))
559                                 draw = true;
560                         } else {
561                             if (cell_east!=null &&
562                                 (cell_east.vwire(plane).drives(cell.vwire(plane)) ||
563                                  cell_east.vwire(plane).drives(cell.vwire(plane))))
564                                 draw = true;
565                             if (cell_west!=null &&
566                                 (cell_west.vwire(plane).drives(cell.vwire(plane)) ||
567                                  cell_west.vwire(plane).drives(cell.vwire(plane))))
568                                 draw = true;
569                         }
570                         if (!draw)
571                             for(int x=h?sector.col:sector.row; x<(h?sector.col+4:sector.row+4); x++)
572                                 if (((h ? dev.cell(x,y).hx(plane) : dev.cell(y,x).vx(plane))) ||
573                                     (h?dev.cell(x,y).out(plane):dev.cell(y,x).out(plane)))
574                                     draw = true;
575                         if (draw) {
576                             g.setColor(new Color(0xff, 0x00, 0xff));
577                             if (h) {
578                                 g.drawLine(left(cell),
579                                            top(cell)+3,
580                                            left(cell) + 4*(WIDTH+2),
581                                            top(cell)+3
582                                            );
583                             } else {
584                                 g.drawLine(left(cell)+3,
585                                            top(cell) + (HEIGHT+2),
586                                            left(cell)+3,
587                                            top(cell) - 3*(HEIGHT+2)
588                                            );
589                             }
590                         }
591                     }
592             }
593         }
594         public void drawCell(Graphics g, int cx, int cy) {
595             int x = (cx*(WIDTH+2))+20;
596             int y = ((23-cy)*(HEIGHT+2))+60;
597             if (g.getClipBounds() != null && !g.getClipBounds().intersects(new Rectangle(x, y, x+WIDTH, y+HEIGHT))) return;
598
599             At40k.Cell cell = dev.cell(cx, cy);
600             g.setColor(Color.white);
601             g.fillRect(x, y, WIDTH, HEIGHT);
602
603             g.setColor((selx==cx && sely==cy) ? Color.red : Color.black);
604             g.drawRect(x, y, WIDTH, HEIGHT);
605
606             g.setColor((selx==cx && sely==cy) ? Color.red : Color.gray);
607             g.drawRect(x+(WIDTH-(LW*2))/2-1,    y+(HEIGHT-LW)/2-1, LW*2+1, LH+1);
608
609             g.setColor(RED);
610             //g.fillRect(x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2, LW,   LH);
611             g.setColor(Color.white);
612             //g.drawString("1", x+(WIDTH-(LW*2))/2,    y+(HEIGHT-LW)/2);
613
614             g.setColor(BLUE);
615             //g.fillRect(x+(WIDTH-(LW*2))/2+LW, y+(HEIGHT-LW)/2, LW,   LH);
616             g.setColor(Color.white);
617             //g.drawString("0", x+(WIDTH-(LW*2))/2+LW,    y+(HEIGHT-LW)/2);
618
619             g.setColor(BLUE);
620             ((Graphics2D)g).setStroke(new BasicStroke((float)1.5));
621             switch(cell.yi()) {
622                 case NORTH: g.drawLine(x+WIDTH/2+5,  y-10,        x+WIDTH/2+5, y+HEIGHT/2); break;
623                 case SOUTH: g.drawLine(x+WIDTH/2-5,  y+HEIGHT+10, x+WIDTH/2-5, y+HEIGHT/2); break;
624                 case EAST:  g.drawLine(x+WIDTH+10, y+HEIGHT/2+5,  x+WIDTH/2, y+HEIGHT/2+5); break;
625                 case WEST:  g.drawLine(x-10,       y+HEIGHT/2-5,  x+WIDTH/2, y+HEIGHT/2-5); break;
626                 case NONE:  break;
627             }
628             g.setColor(RED);
629             ((Graphics2D)g).setStroke(new BasicStroke((float)1.5));
630             switch(cell.xi()) {
631                 case NW: g.drawLine(x-10+3,       y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
632                 case SW: g.drawLine(x-10-3,       y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
633                 case NE: g.drawLine(x+WIDTH+10+3, y-10,        x+WIDTH/2+3, y+HEIGHT/2); break;
634                 case SE: g.drawLine(x+WIDTH+10-3, y+HEIGHT+10, x+WIDTH/2-3, y+HEIGHT/2); break;
635                 case NONE:  break;
636             }
637             ((Graphics2D)g).setStroke(new BasicStroke(1));
638
639             if (selx==cx && sely==cy) {
640                 int xp = 23 * (WIDTH+2) + 100;
641                 int yp = 100;
642                 g.setColor(Color.white);
643                 g.fillRect(xp, yp, 300, 1000);
644                 g.setColor(Color.black);
645                 g.drawString("Cell " + cx + "," + cy,       xp, (yp+=15));
646                 //g.drawString("X-Lut: " + bin8(cell.xlut()), xp, (yp+=15));
647                 g.drawString("X-Lut: " + cell.printXLut(), xp, (yp+=15));
648                 //g.drawString("Y-Lut: " + bin8(cell.ylut()), xp, (yp+=15));
649                 g.drawString("Y-Lut: " + cell.printYLutX(), xp, (yp+=15));
650             }
651         }
652     }
653
654     private static String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
655     private static String bin8(byte b) {
656         int n = b & 0xff;
657         String ret = "";
658         for(int i=7; i>=0; i--)
659             ret += (n & (1<<i))==0 ? "0" : "1";
660         return ret;
661     }
662
663 }