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