e36e86362884c4e388b0ff505361c889a19e462a
[slipway.git] / src / edu / berkeley / obits / device / atmel / At40k.java
1 package edu.berkeley.obits.device.atmel;
2 import static edu.berkeley.obits.device.atmel.AtmelDevice.Constants.*;
3 import edu.berkeley.obits.*;
4 import org.ibex.util.*;
5 import java.util.*;
6 import java.io.*;
7
8 public class At40k {
9
10     /*private*/public final AtmelDevice dev;
11     private final int width;
12     private final int height;
13
14     public At40k(AtmelDevice dev, int width, int height) {
15         this.width = width;
16         this.height = height;
17         this.dev = dev;
18     }
19
20     public static class At40k10 extends At40k {
21         public At40k10(AtmelDevice dev) { super(dev, 24, 24); }
22     }
23
24     public final class Sector {
25         public final int col;
26         public final int row;
27         public Sector(Cell c) { this((c.col/4)*4, (c.row/4)*4); }
28         private Sector(int col, int row) {
29             if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
30             if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
31             this.row = row;
32             this.col = col;
33         }
34         public Sector north() { return row+4>=height ? null : new Sector(col, row+4); }
35         public Sector south() { return row==0 ?        null : new Sector(col, row-4); }
36         public Sector east()  { return col+4>=width ?  null : new Sector(col+4, row); }
37         public Sector west()  { return col==0 ?        null : new Sector(col-4, row); }
38         public Cell cell() { return At40k.this.cell(col, row); }
39     }
40
41     public final class SectorWire {
42         public final boolean global;
43         public final boolean horizontal;
44         public final int     plane;
45         public final int     row;
46         public final int     col;
47         public SectorWire(boolean horizontal, int plane, int col, int row) {
48             this.horizontal=horizontal;
49             this.global = false;
50             this.plane=plane;
51             this.col= horizontal ? (col & ~0x3) : col;
52             this.row=!horizontal ? (row & ~0x3) : row;
53         }
54         public boolean isDriven() {
55             // FIXME: bridging connections (horiz-to-vert)
56             for(int i=0; i<4; i++)
57                 if (cell(horizontal ? col+i : col,
58                          horizontal ? row   : row+i).out(plane)) return true;
59             // FIXME: sector switchbox drivers
60             return false;
61         }
62         private int z(int z)       { return (horizontal ? 0x30 : 0x20) | z; }
63         public int code(boolean topleft) {
64             switch(plane) {
65                 case 0: return z(6)+(topleft?0:1);
66                 case 1: return z(8)+(topleft?0:1);
67                 case 2: return z(2*(4-plane))+(topleft?0:1);
68                 case 3: return z(2*(4-plane))+(topleft?0:1);
69                 case 4: return z(2*(4-plane))+(topleft?0:1);
70             }
71             throw new Error();
72         }
73
74         private final int fine()   { return horizontal ? row : col; }
75         public  final int coarse() { return horizontal ? col : row; }
76         private int _row()  { return horizontal ? row          : ((row)>>2); }
77         private int _col()  { return horizontal ? ((col)>>2) : col;          }
78
79         public SectorWire west()  { return !horizontal ? null : col-4<0       ? null : new SectorWire(horizontal, plane, col-4, row); }
80         public SectorWire east()  { return !horizontal ? null : col+4>=width  ? null : new SectorWire(horizontal, plane, col+4, row); }
81         public SectorWire north() { return  horizontal ? null : row+4>=height ? null : new SectorWire(horizontal, plane, col,   row+4); }
82         public SectorWire south() { return  horizontal ? null : row-4<0       ? null : new SectorWire(horizontal, plane, col,   row-4); }
83
84         public String toString() {
85             return
86                 (horizontal?(col+":"+(col+3)):(""+col))+","+
87                 (horizontal?(row+"")         :(row+":"+(row+3)))+
88                 "x"+plane;
89         }
90
91         /** returns the ZYX0 coordinate of the byte controlling the switchbox that allows <tt>w</tt> to drive this wire */
92         public int switchbox(SectorWire w) {
93             if (w.horizontal==horizontal) {
94                 if (w.plane!=plane) throw new Error();
95                 if (Math.abs(w.coarse()-coarse())!=4) throw new Error(w.coarse() + " -- " + coarse());
96                 boolean topleft = horizontal ? (w.coarse() < coarse()) : (w.coarse() > coarse());
97                 int col = _col() + (( horizontal && !topleft) ? 1 : 0);
98                 int row = _row() + ((!horizontal &&  topleft) ? 1 : 0);
99                 return (code(topleft) << 24) | (row<<16) | (col<<8);
100             }
101             throw new Error("not implemented");
102         }
103
104         public void drives(SectorWire w, boolean enable) {
105             dev.mode4zyx(switchbox(w), enable?0x02:0x00, 0x07);
106         }
107
108         public boolean drives(SectorWire w) {
109             int connect = (dev.mode4zyx(switchbox(w)) >> (global?3:0)) & 0x7;
110             return (connect & 0x2)!=0;
111         }
112         public SectorWire driverRight() {
113             System.out.println("checking " + Integer.toString(code(true), 16) + " " + Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
114             int ret = dev.mode4(z(code(true)), _row(), _col());
115             ret = (ret >> (global?3:0)) & 0x7;
116             switch(ret) {
117                 case 0: return null;
118                 case 1: return null;  /* global wire on same side */
119                 case 2: return new SectorWire(horizontal, plane, horizontal?(col+4):col, horizontal?row:(row+4));
120                 case 4: return null;  /* global wire on other side */
121                 default: throw new Error("multiple drivers on " + this + "!");
122             }
123         }
124     }
125     /*    
126     public final class SwitchBox {
127         public final boolean h;
128         public final int col;
129         public final int row;
130         public final int plane;
131         public SwitchBox(boolean h, int col, int row, int plane) { this.h = h; this.col = col; this.row = row; this.plane = plane; }
132         public SectorWire west(boolean global)  { return !h ? null : global ? null : new SectorWire(h, col-4, row,   plane); }
133         public SectorWire east(boolean global)  { return !h ? null : global ? null : new SectorWire(h, col+4, row,   plane); }
134         public SectorWire north(boolean global) { return !h ? null : global ? null : new SectorWire(h, col,   row-4, plane); }
135         public SectorWire south(boolean global) { return !h ? null : global ? null : new SectorWire(h, col,   row+4, plane); }
136     }
137     */
138
139     public Cell cell(int col, int row) {
140         if (col<0) return null;
141         if (row<0) return null;
142         if (col>=width) return null;
143         if (row>=height) return null;
144         return new Cell(col, row);
145     }
146
147     public final class Cell {
148         public final int col;
149         public final int row;
150
151         public Cell(int col, int row) {
152             this.row = row;
153             this.col = col;
154         }
155         
156         // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
157
158         public SectorWire hwire(int plane)  { return new SectorWire(true, plane, col, row); }
159         public SectorWire vwire(int plane)  { return new SectorWire(false, plane, col, row); }
160         public Cell east() { return cell(col+1, row); }
161         public Cell west() { return cell(col-1, row); }
162         public Cell north() { return cell(col,   row+1); }
163         public Cell south() { return cell(col,   row-1); }
164         public Cell ne() { return cell(col+1, row+1); }
165         public Cell nw() { return cell(col-1, row+1); }
166         public Cell se() { return cell(col+1, row-1); }
167         public Cell sw() { return cell(col-1, row-1); }
168         public Sector sector() { return new Sector(this); }
169
170         /* bit positions mean:  [MSB] zxy z_y zx_ z__ _xy __y _x_ ___ [LSB] */
171         public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
172         public void xlut(int table)    { dev.mode4(7, row, col, (byte)(table & 0xff)); }
173         public byte xlut()             { return (byte)(dev.mode4(7, row, col) & 0xff); }
174         public String printXLut()      { return printLut(xlut(), "x", "y", "t"); }
175         public String printXLutX()     { return printLut(xlut(), str(xi(), "x"), str(yi(), "y"), str(ti(), "t")); }
176
177         public String str(int x, String def) {
178             switch(x) {
179                 case NORTH: return "n";
180                 case SOUTH: return "s";
181                 case EAST:  return "e";
182                 case WEST:  return "w";
183                 case NW:    return "nw";
184                 case SE:    return "se";
185                 case NE:    return "ne";
186                 case SW:    return "sw";
187                 case FB:    return "fb";
188                 case L0:    return (hx(0)&&vx(0))?"HV0":hx(0)?"H0":vx(0)?"V0":"L0";
189                 case L1:    return (hx(1)&&vx(1))?"HV1":hx(1)?"H1":vx(1)?"V1":"L1";
190                 case L2:    return (hx(2)&&vx(2))?"HV2":hx(2)?"H2":vx(2)?"V2":"L2";
191                 case L3:    return (hx(3)&&vx(3))?"HV3":hx(3)?"H3":vx(3)?"V3":"L3";
192                 case L4:    return (hx(4)&&vx(4))?"HV4":hx(4)?"H4":vx(4)?"V4":"L4";
193                 default: return def;
194             }
195         }
196
197         /* bit positions mean:  [MSB] zxy zx_ z_y z__ _xy _x_ __y ___ [LSB] */
198         public void ylut(int table)    { dev.mode4(6, row, col, (byte)(table & 0xff)); }
199         public byte ylut()             { return (byte)(dev.mode4(6, row, col) & 0xff); }
200         public String printYLut()      { return printLut(ylut(), "y", "x", "t"); }
201         public String printYLutX()     { return printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti(), "t")) + Integer.toString(ylut() & 0xff, 16); }
202
203         public void ff_reset_value(boolean value) {
204             //dev.mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
205         }
206         /** FIXME!!! */
207         public boolean ff_reset_value() { return false; }
208         public boolean columnClocked() {
209             return false;
210         }
211
212         public void out(int plane, boolean enable) {
213             switch(plane) {
214                 case L0: dev.mode4(0x00, row, col, 2, enable); return;
215                 case L1: dev.mode4(0x00, row, col, 3, enable); return;
216                 case L2: dev.mode4(0x00, row, col, 5, enable); return;
217                 case L3: dev.mode4(0x00, row, col, 4, enable); return;
218                 case L4: dev.mode4(0x00, row, col, 1, enable); return;
219                 default: throw new RuntimeException("invalid argument");
220             }
221         }
222
223         public boolean out(int plane) {
224             switch(plane) {
225                 case L0: return (dev.mode4(0x00, row, col) & (1<<2)) != 0;
226                 case L1: return (dev.mode4(0x00, row, col) & (1<<3)) != 0;
227                 case L2: return (dev.mode4(0x00, row, col) & (1<<5)) != 0;
228                 case L3: return (dev.mode4(0x00, row, col) & (1<<4)) != 0;
229                 case L4: return (dev.mode4(0x00, row, col) & (1<<1)) != 0;
230                 default: throw new RuntimeException("invalid argument");
231             }
232         }
233
234         public void h(int plane, boolean enable) {
235             switch(plane) {
236                 case 0: dev.mode4(0x08, row, col, 2, enable); return;
237                 case 1: dev.mode4(0x08, row, col, 0, enable); return;
238                 case 2: dev.mode4(0x08, row, col, 5, enable); return;
239                 case 3: dev.mode4(0x08, row, col, 6, enable); return;
240                 case 4: dev.mode4(0x00, row, col, 6, enable); return;
241                 default: throw new RuntimeException("invalid argument");
242             }
243         }
244         
245         public boolean hx(int plane) {
246             switch(plane) {
247                 case 0: return (dev.mode4(0x08, row, col) & (1<<2)) != 0;
248                 case 1: return (dev.mode4(0x08, row, col) & (1<<0)) != 0;
249                 case 2: return (dev.mode4(0x08, row, col) & (1<<5)) != 0;
250                 case 3: return (dev.mode4(0x08, row, col) & (1<<6)) != 0;
251                 case 4: return (dev.mode4(0x00, row, col) & (1<<6)) != 0;
252                 default: throw new RuntimeException("invalid argument");
253             }
254         }
255         
256         public void v(int plane, boolean enable) {
257             switch(plane) {
258                 case 0: dev.mode4(0x08, row, col, 1, enable); return;
259                 case 1: dev.mode4(0x08, row, col, 3, enable); return;
260                 case 2: dev.mode4(0x08, row, col, 4, enable); return;
261                 case 3: dev.mode4(0x08, row, col, 7, enable); return;
262                 case 4: dev.mode4(0x00, row, col, 7, enable); return;
263                 default: throw new RuntimeException("invalid argument");
264             }
265         }
266         
267         public boolean vx(int plane) {
268             switch(plane) {
269                 case 0: return (dev.mode4(0x08, row, col) & (1<<1)) != 0;
270                 case 1: return (dev.mode4(0x08, row, col) & (1<<3)) != 0;
271                 case 2: return (dev.mode4(0x08, row, col) & (1<<4)) != 0;
272                 case 3: return (dev.mode4(0x08, row, col) & (1<<7)) != 0;
273                 case 4: return (dev.mode4(0x00, row, col) & (1<<7)) != 0;
274                 default: throw new RuntimeException("invalid argument");
275             }
276         }
277         
278         public int ti() {
279             switch(dev.mode4(1, row, col) & 0x30) {
280                 case 0x20: return zi();
281                 case 0x10: return FB;
282                 case 0x00: return wi();
283                 default: throw new Error("ack!");
284             }
285         }
286         public void t(int code) {
287             int result = 0;
288             switch(code) {
289                 case TMUX_Z: throw new Error("not implemented, but should be possible");
290                 case TMUX_W_AND_Z: result = 0x20; break;
291
292                 case TMUX_FB: result = 0x34; break; /* I think this is actually W_AND_FB, sadly */
293                 case TMUX_W_AND_FB: result = 0x14; break;
294                 case TMUX_W: result = 0x00; break;
295             }
296             dev.mode4(1, row, col, result, 0x34);
297         }
298         /*
299         private void fmux(int source) {
300             switch(source) {
301                 case ZMUX:      
302                 case FB:        
303                 case ALWAYS_ON: 
304                 default: throw new Error("unknown argument to fmux()");
305             }
306         }
307
308         public boolean win_easable() {
309         }
310         */
311         public int t() {
312             return dev.mode4(1, row, col) & 0x34;
313         }
314         public void t(boolean ignore_z_and_fb, boolean zm_drives_fb, boolean fb_drives_wm) {
315             // still not totally satisfied...
316             //     need to find the bit that sets the w-mux off
317             //     what does it mean for both bits (0x30) to be set to 1?
318             //if (fb && z) throw new RuntimeException("invalid combination");
319             int result = 0;
320             // ZM->FB = 0x04
321             // FB->WM = 0x10
322             // WZ->WM = 0x20
323
324             // tff => w&z      [0x20]
325             // fff => w        [0x00]
326             // ttt => fb&w     [0x34]
327             // ftt => fb&w     [0x14]
328             // fft => fb&w     [0x10]
329
330             // ttf => w&z      [0x24]
331             // ftf => w        [0x04]
332             // tft => fb&w     [0x30]
333             if (ignore_z_and_fb) result |= 0x20;
334             if (zm_drives_fb) result |= 0x04;
335             if (fb_drives_wm) result |= 0x10;
336             dev.mode4(1, row, col, result, 0x34);
337         }
338
339
340         public void c(int source) {
341             switch(source) {
342                 case XLUT: dev.mode4(1, row, col, 0x00, 0xc0); break;
343                 case YLUT: dev.mode4(1, row, col, 0x40, 0xc0); break;
344                 case ZMUX: dev.mode4(1, row, col, 0x80, 0xc0); break;
345                 default:   throw new RuntimeException("Invalid Argument");
346             }
347         }
348         public int c() {
349             int cval = dev.mode4(1, row, col) & 0xc0;
350             switch (cval) {
351                 case 0x00: return XLUT;
352                 case 0x40: return YLUT;
353                 case 0x80: return ZMUX;
354             }
355             throw new Error("c() => " + cval);
356         }
357         public void b(boolean registered) { dev.mode4(1, row, col, 3, !registered); }
358         public void f(boolean registered) { dev.mode4(1, row, col, 2, !registered); }
359         public boolean xo()               { return (dev.mode4(1, row, col) & 0x01) != 0; }
360         public boolean yo()               { return (dev.mode4(1, row, col) & 0x02) != 0; }
361         public void xo(boolean center)    { dev.mode4(1, row, col, 0, center); }
362         public void yo(boolean center)    { dev.mode4(1, row, col, 1, center); }
363         public boolean b() { return (dev.mode4(1, row, col) & (1 << 3)) == 0; }
364         public boolean f() { return (dev.mode4(1, row, col) & (1 << 2)) == 0; }
365         public boolean x() { return (dev.mode4(1, row, col) & (1 << 1)) != 0; }
366         public boolean y() { return (dev.mode4(1, row, col) & (1 << 0)) != 0; }
367
368         public int oe() {
369             switch (dev.mode4(0x02, row, col) & 0x3) {
370                 case 0: return NONE;
371                 case 1: return H4;
372                 case 2: return V4;
373                 default: throw new RuntimeException("invalid argument");                    
374             }
375         }
376         public void oe(int source) {
377             switch(source) {
378                 case NONE: dev.mode4(0x02, row, col, 0, 0x3); break;
379                 case H4:   dev.mode4(0x02, row, col, 1, 0x3); break;
380                 case V4:   dev.mode4(0x02, row, col, 2, 0x3); break;
381                 default: throw new RuntimeException("invalid argument");
382             }
383         }
384
385         public int xi() {
386             // FIXME: can be multiple
387             if ((dev.mode4(0x03, row, col) & (1<<4))!=0) return L4;
388             switch(dev.mode4(0x05, row, col) & 0xff) {
389                 case 0x80: return SW;
390                 case (1<<6): return NE;
391                 case (1<<5): return SE;
392                 case (1<<4): return NW;
393                 case (1<<3): return L0;
394                 case (1<<2): return L1;
395                 case (1<<1): return L2;
396                 case (1<<0): return L3;
397                 case 0: return NONE;
398                 default: throw new Error();
399             }
400         }
401
402         public void xi(int source) {
403             switch(source) {
404                 case SW: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<7); break;
405                 case NE: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<6); break;
406                 case SE: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<5); break;
407                 case NW: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<4); break;
408                 case L4: dev.mode4(0x03, row, col, 4, true);  dev.mode4(0x05, row, col,    0); break;
409                 case L3: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<0); break;
410                 case L2: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<1); break;
411                 case L1: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<2); break;
412                 case L0: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<3); break;
413                 default: throw new RuntimeException("invalid argument");
414             }
415         }
416
417         public int yi() {
418             if ((dev.mode4(0x02, row, col) & (1<<6))!=0) return L4;
419             switch(dev.mode4(0x04, row, col) & 0xff) {
420                 case 0x80: return NORTH;
421                 case (1<<5): return SOUTH;
422                 case (1<<6): return WEST;
423                 case (1<<4): return EAST;
424                 case (1<<3): return L0;
425                 case (1<<2): return L1;
426                 case (1<<1): return L2;
427                 case (1<<0): return L3;
428                 case 0: return NONE;
429                 default: throw new Error();
430             }
431         }
432
433         public void yi(int source) {
434             switch(source) {
435                 case NORTH: dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<7); break;
436                 case SOUTH: dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<5); break;
437                 case WEST:  dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<6); break;
438                 case EAST:  dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<4); break;
439                 case L4:    dev.mode4(0x02, row, col, 6, true);  dev.mode4(0x04, row, col,    0); break;
440                 case L3:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<0); break;
441                 case L2:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<1); break;
442                 case L1:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<2); break;
443                 case L0:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<3); break;
444                 default: throw new RuntimeException("invalid argument");
445             }
446         }
447
448         public void wi(int source) {
449             switch(source) {
450                 case L4:    dev.mode4(0x03, row, col, 1<<5, 0xEC); break;
451                 case L3:    dev.mode4(0x03, row, col, 1<<6, 0xEC); break;
452                 case L2:    dev.mode4(0x03, row, col, 1<<7, 0xEC); break;
453                 case L1:    dev.mode4(0x03, row, col, 1<<3, 0xEC); break;
454                 case L0:    dev.mode4(0x03, row, col, 1<<2, 0xEC); break;
455                 default: throw new RuntimeException("invalid argument");
456             }
457         }
458
459         public int wi() {
460             int who = dev.mode4(0x03, row, col) & 0xEC;
461             switch(who) {
462                 case (1<<5): return L4;
463                 case (1<<6): return L3;
464                 case (1<<7): return L2;
465                 case (1<<3): return L1;
466                 case (1<<2): return L0;
467                 case (1<<0): return NONE;  /* huh? */
468                 case (0):    return NONE;
469                 default: throw new RuntimeException("invalid argument: " + who);
470             }
471         }
472
473        
474         public void zi(int source) {
475             switch(source) {
476                 case L4:    dev.mode4(0x02, row, col, 1<<7, 0xDB); break;
477                 case L3:    dev.mode4(0x02, row, col, 1<<5, 0xDB); break;
478                 case L2:    dev.mode4(0x02, row, col, 1<<4, 0xDB); break;
479                 case L1:    dev.mode4(0x02, row, col, 1<<3, 0xDB); break;
480                 case L0:    dev.mode4(0x02, row, col, 1<<2, 0xDB); break;
481                 default: throw new RuntimeException("invalid argument");
482             }
483         }
484
485         public int zi() {
486             switch(dev.mode4(0x02, row, col) & 0xDB) {
487                 case (1<<7): return L4;
488                 case (1<<5): return L3;
489                 case (1<<4): return L2;
490                 case (1<<3): return L1;
491                 case (1<<2): return L0;
492                 case (1<<1): return NONE;  /* huh? */
493                 case (1<<0): return NONE;  /* huh? */
494                 case 0:      return NONE;
495                 default: throw new RuntimeException("invalid argument: zi=="+(dev.mode4(0x02, row, col) & 0xDB));
496             }
497         }
498
499
500         // Relevance //////////////////////////////////////////////////////////////////////////////
501
502         public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
503         public boolean xo_relevant(int direction) {
504             switch(direction) {
505                 case NE: return ne() != null && ne().xi()==SW /*&& ne().xi_relevant()*/;
506                 case NW: return nw() != null && nw().xi()==SE /*&& nw().xi_relevant()*/;
507                 case SE: return se() != null && se().xi()==NW /*&& se().xi_relevant()*/;
508                 case SW: return sw() != null && sw().xi()==NE /*&& sw().xi_relevant()*/;
509                 default: return false;
510             }
511         }
512         public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
513         public boolean yo_relevant(int direction) {
514             switch(direction) {
515                 case NORTH: return north() != null && north().yi()==SOUTH  /*&& north().yi_relevant()*/;
516                 case EAST: return east() != null  && east().yi()==WEST     /*&& east().yi_relevant()*/;
517                 case SOUTH: return south() != null && south().yi()==NORTH  /*&& south().yi_relevant()*/;
518                 case WEST: return west() != null  && west().yi()==EAST     /*&& west().yi_relevant()*/;
519                 default: return false;
520             }
521         }
522         public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
523         public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
524         public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
525         public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
526         public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
527         public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
528         public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
529         public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
530         public boolean xlut_relevant() {
531             if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
532             if (xo()) return false;
533             return xo_relevant();
534         }
535         public boolean ylut_relevant() {
536             if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
537             if (yo()) return false;
538             return yo_relevant();
539         }
540         public boolean c_relevant() {
541             switch(t()) {
542                 case 0x34: return true;
543                 case 0x14: return true;
544                 case 0x10: return true;
545                 case 0x30: return true;
546             }
547             for(int i=0; i<5; i++)
548                 if (out(i))
549                     return true;
550             if (xo() || yo()) return true;
551             return false;
552         }
553
554         public boolean register_relevant() {
555             if (!c_relevant()) return false;
556             if (f() && out_relevant()) return true;
557             if (f() && fb_relevant()) return true;
558             if (b() && xo()) return true;
559             if (b() && yo()) return true;
560             return false;
561         }
562         public boolean out_relevant() {
563             boolean out = false;
564             boolean connect = false;
565             for(int i=0; i<4; i++) {
566                 if (out(L0+i)) out = true;
567                 if (hx(L0+i)) connect = true;
568                 if (vx(L0+i)) connect = true;
569             }
570             return out && connect;
571         }
572         public boolean fb_relevant() {
573             if (!(zi_to_xlut_relevant()) ||
574                 !(zi_to_ylut_relevant())) return false;
575             switch(t()) {
576                 case 0x34: return true;
577                 case 0x14: return true;
578                 case 0x10: return true;
579                 case 0x30: return true;
580             }
581             return false;
582         }
583
584
585     }
586
587     public IOB iob_bot(int col, boolean primary)   { return new IOB(col, 0, primary, true); }
588     public IOB iob_top(int col, boolean primary)   { return new IOB(col, 1, primary, true); }
589     public IOB iob_left(int row, boolean primary)  { return new IOB(0, row, primary, false); }
590     public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
591     /*
592     public IOB fromPin(int pin) {
593         if (pin >=  4 && pin <= 11) return io(pin-3);
594         if (pin >= 15 && pin <= 18) return io(pin-2);
595         if (pin >= 19 && pin <= 24) return io(pin);
596         if (pin >= 27 && pin <= 30) return io(pin-2);
597         if (pin >= 33 && pin <= 36) return io(pin);
598         if (pin >= 38 && pin <= 47) return io(pin+1);
599
600
601         if (pin >= 33 && pin <= 36) return io(pin+36);
602         if (pin >= 38 && pin <= 41) return io(pin+43);
603         if (pin >= 42 && pin <= 43) return io(pin+47);
604         if (pin >= 44 && pin <= 47) return io(pin+49);
605         if (pin >= 57 && pin <= 62) return io(pin+40);
606         if (pin >= 63 && pin <= 66) return io(pin+46);
607         if (pin >= 68 && pin <= 71) return io(pin+53);
608         if (pin >= 72 && pin <= 73) return io(pin+53);
609         if (pin >= 74 && pin <= 75) return io(pin+63);
610         if (pin >= 76 && pin <= 77) return io(143+(pin-76));
611         if (pin >= 80 && pin <= 81) return io(145+(pin-80));
612         if (pin >= 82 && pin <= 85) return io(151+(pin-82));
613         if (pin >= 86 && pin <= 89) return io(165+(pin-86));
614         if (pin >= 91 && pin <= 94) return io(177+(pin-91));
615         if (pin >= 95 && pin <= 96) return io(183+(pin-95));
616         if (pin >= 97 && pin <= 100) return io(189+(pin-97));
617         if (pin >= 161 && pin <= 164) return io(289+(pin-161));
618         if (pin >= 165 && pin <= 166) return io(297+(pin-165));
619         if (pin >= 167 && pin <= 168) return io(303+(pin-167));
620         if (pin >= 169 && pin <= 170) return io(309+(pin-169));
621         if (pin >= 172 && pin <= 173) return io(313+(pin-172));
622         if (pin >= 174 && pin <= 175) return io(325+(pin-174));
623         if (pin >= 176 && pin <= 179) return io(327+(pin-176));
624         if (pin >= 180 && pin <= 181) return io(335+(pin-180));
625         if (pin >= 184 && pin <= 185) return io(337+(pin-184));
626         if (pin >= 186 && pin <= 191) return io(343+(pin-186));
627         if (pin >= 192 && pin <= 193) return io(359+(pin-192));
628         if (pin >= 195 && pin <= 196) return io(363+(pin-195));
629         if (pin >= 197 && pin <= 200) return io(369+(pin-197));
630         if (pin >= 201 && pin <= 204) return io(381+(pin-201));
631     }
632     public io(int ionum) {
633         if (ionum <= 94) {
634             int cell = (94 - pin) / 2;
635             boolean primary = cell * 2 == (94-pin);
636         }
637     }
638     */
639     public final class IOB {
640         public final int col;
641         public final int row;
642         public final boolean primary;
643         public final boolean northsouth;
644         public IOB(int col, int row, boolean primary, boolean northsouth) {
645             this.col = col;
646             this.row = row;
647             this.northsouth = northsouth;
648             this.primary = primary;
649         }
650         /*
651         public String dump() {
652             System.out.println("[ "+
653                                (schmitt()?"schmitt ":"")+
654                                (slew()==3?"fast ":slew()==2?"med ":slew()==1?"slow ":"slew-unknown ")+
655                                (cr()?"cr ":"")+
656                                (reg()?"reg ":"")+
657                                
658         }
659         */
660         public void enableOutput(int direction) {
661             useoem(true);
662             output(direction);
663             pullnone();
664             useoem(true);
665             oem(ALWAYS_ON);
666             oe(true);
667             // note: east-side IOBs should have slew=med, others slew=fast
668             slew((!northsouth && col==1) ? MEDIUM : FAST);
669         }
670         public void enableInput() {
671             schmitt(true);
672             pullnone();
673         }
674
675         public void    useoem(boolean use)  { dev.mode4(z(3), row, col, 6, use); }
676         public boolean useoem()             { return (dev.mode4(z(3), row, col) & (1<<6))!=0; }
677         public void    schmitt(boolean use) { dev.mode4(z(0), row, col, 7, use); }
678         public boolean schmitt()            { return (dev.mode4(z(0), row, col) & (1<<7))!=0; }
679
680         public void    slew(int slew) {
681             switch(slew) {
682                 case FAST:   dev.mode4(z(0), row, col, 3<<5, 0x60); return;
683                 case MEDIUM: dev.mode4(z(0), row, col, 2<<5, 0x60); return;
684                 case SLOW:   dev.mode4(z(0), row, col, 1<<5, 0x60); return;
685                 default: throw new Error();
686             }
687         }
688
689         public void    oem(int source) {
690             switch(source) {
691                 case ALWAYS_ON:  dev.mode4(z(3), row, col, 1<<5, 0x3f); return;
692                 case ALWAYS_OFF: dev.mode4(z(3), row, col,    0, 0x3f); return;
693                 default: throw new Error();
694             }
695         }
696
697         private int z(int code) { return (northsouth ? 0x70 : 0x60) | (primary ? 0x00 : 0x04) | (code & 0x7); }
698         public void pullup()   { dev.mode4(z(0), row, col, 0x00<<1, 0x06); }
699         public void pulldown() { dev.mode4(z(0), row, col, 0x03<<1, 0x06); }
700         public void pullnone() { dev.mode4(z(0), row, col, 0x01<<1, 0x06); }
701         public void oe(boolean oe) {
702             int old =  dev.mode4(z(1), row, col) & (~(1<<5));
703             old     |= oe ? 0 : (1<<5);
704             dev.mode4(z(1), row, col, old & 0xff);
705         }
706
707         public void output(int which) {
708             switch(which) {
709                 case NONE:
710                     dev.mode4(z(1), row, col, 0, 0x1f); return;
711                 case WEST: case EAST: case NORTH: case SOUTH:
712                     dev.mode4(z(1), row, col, 1<<0, 0x1f); return;
713                 case NW: case SW: case NE: case SE:
714                     dev.mode4(z(1), row, col, 1<<1, 0x1f); return;
715                 default: throw new Error();
716             }
717         }
718
719     }
720
721     public static void main(String[] s) throws Exception {
722         System.out.println(printLut(0x39, "se", "n", "L0"));
723     }
724     public static synchronized String printLut(int lut, String xn, String yn, String zn) {
725         try {
726             File f = File.createTempFile("mvsis", ".mvs");
727             f.deleteOnExit();
728
729             FileOutputStream fos = new FileOutputStream(f);
730             PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
731             pw.println(".model clb");
732             pw.println(".inputs "+xn+" "+yn+" "+zn);
733             pw.println(".outputs O");
734             pw.println(".table "+xn+" "+yn+" "+zn+/*("X_xor_Y X_xor_Z Y_xor_Z")+*/ " -> O");
735             for(int i=8; i>=0; i--) {
736                 int x = ((i & 0x01)!=0 ? 1 : 0);
737                 int y = ((i & 0x02)!=0 ? 1 : 0);
738                 int z = ((i & 0x04)!=0 ? 1 : 0);
739                 pw.print(" "+x+" ");
740                 pw.print(" "+y+" ");
741                 pw.print(" "+z+" ");
742                 //pw.print(" "+(x ^ y)+" ");
743                 //pw.print(" "+(y ^ z)+" ");
744                 //pw.print(" "+(z ^ y)+" ");
745                 pw.print((lut & (1<<i))==0 ? 0 : 1);
746                 pw.println();
747             }
748             pw.println(".end");
749             pw.flush();
750             pw.close();
751             Process p = Runtime.getRuntime().exec(new String[] { "mvsis", "-c", "simplify;print_factor", f.getAbsolutePath() });
752             new Gobble("mvsis: ", p.getErrorStream()).start();
753             //new Gobble("mvsis: ", p.getInputStream()).start();
754             BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
755             String ret = br.readLine();
756             //f.delete();
757             return ret.trim();
758         } catch (Exception e) {
759             e.printStackTrace();
760             return "*mvsis_error*";
761         }
762     }
763
764     public static class Gobble extends Thread {
765         private final String header;
766         private final BufferedReader br;
767         public Gobble(String header, BufferedReader br) { this.br = br; this.header = header; }
768         public Gobble(String header, Reader r)          { this(header, new BufferedReader(r)); }
769         public Gobble(String header, InputStream is)    { this(header, new InputStreamReader(is)); }
770         public void run() {
771             try {
772                 for(String s = br.readLine(); s!=null; s=br.readLine())
773                     System.err.println(header + s);
774             } catch (Exception e) {
775                 e.printStackTrace();
776             }
777         }
778     }
779 }