fixed heinous bug in switchbox routing
[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, 0, enable); return;
260                 case 1: mode4(0x08, row, col, 2, 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<<0)) != 0;
271                 case 1: return (mode4(0x08, row, col) & (1<<2)) != 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_W:        result = 0x34; break;
328                 case TMUX_Z:        result = 0x20; break; // TOTALLYBOGUS throw new Error("not implemented, but should be possible");
329                 case TMUX_W_AND_Z:  result = 0x00; break;
330                 case TMUX_FB:       result = 0x34; break; /* I think this is actually W_AND_FB, sadly */
331                 case TMUX_W_AND_FB: result = 0x14; 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
473                 case L0: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<3); break;
474                 case L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
475                 case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
476                 case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
477                 case L4: mode4(0x03, row, col, 4, true);  mode4(0x05, row, col,    0); break;
478
479                 case NONE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 0); break;
480                 default: throw new RuntimeException("invalid argument");
481             }
482         }
483
484         public void xi(SectorWire sw) {
485             if (!sw.touches(this)) throw new RuntimeException("invalid argument");
486             xi(sw.plane);
487         }
488
489         public void xi(Cell c) {
490             if      (c.row==row-1 && c.col==col-1) xi(SW);
491             else if (c.row==row+1 && c.col==col-1) xi(NW);
492             else if (c.row==row-1 && c.col==col+1) xi(SE);
493             else if (c.row==row+1 && c.col==col+1) xi(NE);
494             else throw new RuntimeException("invalid argument");
495         }
496
497         public int yi() {
498             if ((mode4(0x02, row, col) & (1<<6))!=0) return L4;
499             switch(mode4(0x04, row, col) & 0xff) {
500                 case 0x80: return NORTH;
501                 case (1<<5): return SOUTH;
502                 case (1<<6): return WEST;
503                 case (1<<4): return EAST;
504                 case (1<<3): return L0;
505                 case (1<<2): return L1;
506                 case (1<<1): return L2;
507                 case (1<<0): return L3;
508                 case 0: return NONE;
509                 default: throw new Error();
510             }
511         }
512
513         public void yi(int source) {
514             switch(source) {
515                 case NORTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<7); break;
516                 case SOUTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<5); break;
517                 case WEST:  mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<6); break;
518                 case EAST:  mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<4); break;
519                 case L4:    mode4(0x02, row, col, 6, true);  mode4(0x04, row, col,    0); break;
520                 case L3:    mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<0); break;
521                 case L2:    mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<1); break;
522                 case L1:    mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<2); break;
523                 case L0:    mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<3); break;
524                 case NONE:  mode4(0x02, row, col, 6, false); mode4(0x04, row, col,    0); break;
525                 default: throw new RuntimeException("invalid argument");
526             }
527         }
528
529         public void yi(SectorWire sw) {
530             if (!sw.touches(this)) throw new RuntimeException("invalid argument");
531             yi(sw.plane);
532         }
533
534         public void yi(Cell c) {
535             if      (c.row==row-1 && c.col==col)   yi(SOUTH);
536             else if (c.row==row+1 && c.col==col)   yi(NORTH);
537             else if (c.row==row   && c.col==col-1) yi(WEST);
538             else if (c.row==row   && c.col==col+1) yi(EAST);
539             else throw new RuntimeException("invalid argument");
540         }
541
542         public void wi(SectorWire sw) {
543             if (!sw.touches(this)) throw new RuntimeException("invalid argument");
544             wi(sw.plane);
545         }
546
547         public void wi(int source) {
548             switch(source) {
549                 case L4:    mode4(0x03, row, col, 1<<5, 0xEC); break;
550                 case L3:    mode4(0x03, row, col, 1<<6, 0xEC); break;
551                 case L2:    mode4(0x03, row, col, 1<<7, 0xEC); break;
552                 case L1:    mode4(0x03, row, col, 1<<3, 0xEC); break;
553                 case L0:    mode4(0x03, row, col, 1<<2, 0xEC); break;
554                 case NONE:  mode4(0x03, row, col,    0, 0xEC); break;
555                 default: throw new RuntimeException("invalid argument");
556             }
557         }
558
559         public int wi() {
560             int who = mode4(0x03, row, col) & 0xEC;
561             switch(who) {
562                 case (1<<5): return L4;
563                 case (1<<6): return L3;
564                 case (1<<7): return L2;
565                 case (1<<3): return L1;
566                 case (1<<2): return L0;
567                 case (1<<0): return NONE;  /* huh? */
568                 case (0):    return NONE;
569                 default: throw new RuntimeException("invalid argument: " + who);
570             }
571         }
572
573        
574         public void zi(SectorWire sw) {
575             if (!sw.touches(this)) throw new RuntimeException("invalid argument");
576             zi(sw.plane);
577         }
578
579         public void zi(int source) {
580             switch(source) {
581                 case L4:    mode4(0x02, row, col, 1<<7, 0xDB); break;
582                 case L3:    mode4(0x02, row, col, 1<<5, 0xDB); break;
583                 case L2:    mode4(0x02, row, col, 1<<4, 0xDB); break;
584                 case L1:    mode4(0x02, row, col, 1<<3, 0xDB); break;
585                 case L0:    mode4(0x02, row, col, 1<<2, 0xDB); break;
586                 case NONE:  mode4(0x02, row, col,    0, 0xDB); break;
587                 default: throw new RuntimeException("invalid argument");
588             }
589         }
590
591         public int zi() {
592             switch(mode4(0x02, row, col) & 0x9B) {
593                 case (1<<7): return L4;
594                 case (1<<5): return L3;
595                 case (1<<4): return L2;
596                 case (1<<3): return L1;
597                 case (1<<2): return L0;
598                 case (1<<1): return NONE;  /* huh? */
599                 case (1<<0): return NONE;  /* huh? */
600                 case 0:      return NONE;
601                 default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0x9B));
602             }
603         }
604
605
606         public void generalized_c_element() {
607
608             /*
609             ylut(LUT_SELF & (~LUT_OTHER));
610             xlut((~LUT_SELF) | LUT_OTHER);
611             c(ZMUX);
612             zi(L2);
613             //h(L2, true);
614             out(L2, true);
615             xo(true);
616             yo(true);
617             */
618
619             //ylut(0xB2);
620             ylut((LUT_SELF & ~LUT_OTHER) | (LUT_Z & ~LUT_OTHER) | (LUT_Z & LUT_SELF & LUT_OTHER));
621             xlut(LUT_Z);
622             c(YLUT);
623             f(false);
624             b(false);
625             t(false, false, true);
626             yo(false);
627             xo(false);
628         }
629
630
631         // Relevance //////////////////////////////////////////////////////////////////////////////
632         public boolean relevant() {
633             return xo_relevant() || yo_relevant() || out_relevant();
634         }
635
636         public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
637         public boolean xo_relevant(int direction) {
638             switch(direction) {
639                 case NE: return ne() != null && ne().xi()==SW;
640                 case NW: return nw() != null && nw().xi()==SE;
641                 case SE: return se() != null && se().xi()==NW;
642                 case SW: return sw() != null && sw().xi()==NE;
643                 default: return false;
644             }
645         }
646         public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
647         public boolean yo_relevant(int direction) {
648             switch(direction) {
649                 case NORTH: return north() != null && north().yi()==SOUTH  /*&& north().yi_relevant()*/;
650                 case EAST: return east() != null  && east().yi()==WEST     /*&& east().yi_relevant()*/;
651                 case SOUTH: return south() != null && south().yi()==NORTH  /*&& south().yi_relevant()*/;
652                 case WEST: return west() != null  && west().yi()==EAST     /*&& west().yi_relevant()*/;
653                 default: return false;
654             }
655         }
656         public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
657         public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
658         public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
659         public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
660         public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
661         public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
662         public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
663         public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
664         public boolean xlut_relevant() {
665             if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
666             if (xo()) return false;
667             return xo_relevant();
668         }
669         public boolean ylut_relevant() {
670             if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
671             if (yo()) return false;
672             return yo_relevant();
673         }
674         public boolean c_relevant() {
675             switch(ti()) {
676                 case 0x34: return true;
677                 case 0x14: return true;
678                 case 0x10: return true;
679                 case 0x30: return true;
680             }
681             for(int i=0; i<5; i++)
682                 if (out(i))
683                     return true;
684             if (xo() || yo()) return true;
685             return false;
686         }
687
688         public boolean register_relevant() {
689             if (!c_relevant()) return false;
690             if (f() && out_relevant()) return true;
691             if (f() && fb_relevant()) return true;
692             if (b() && xo()) return true;
693             if (b() && yo()) return true;
694             return false;
695         }
696         public boolean out_relevant() {
697             boolean out = false;
698             boolean connect = false;
699             for(int i=0; i<4; i++) {
700
701                 // FIXME FIXME FIXME
702                 if (i==3) continue;
703
704                 if (out(L0+i)) out = true;
705                 if (hx(L0+i)) connect = true;
706                 if (vx(L0+i)) connect = true;
707             }
708             return out && connect;
709         }
710         public boolean fb_relevant() {
711             if (!(zi_to_xlut_relevant()) ||
712                 !(zi_to_ylut_relevant())) return false;
713             switch(ti()) {
714                 case 0x34: return true;
715                 case 0x14: return true;
716                 case 0x10: return true;
717                 case 0x30: return true;
718             }
719             return false;
720         }
721
722
723     }
724
725     public IOB iob_bot(int col, boolean primary)   { return new IOB(col, 0, primary, true); }
726     public IOB iob_top(int col, boolean primary)   { return new IOB(col, 1, primary, true); }
727     public IOB iob_left(int row, boolean primary)  { return new IOB(0, row, primary, false); }
728     public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
729     /*
730     public IOB fromPin(int pin) {
731         if (pin >=  4 && pin <= 11) return io(pin-3);
732         if (pin >= 15 && pin <= 18) return io(pin-2);
733         if (pin >= 19 && pin <= 24) return io(pin);
734         if (pin >= 27 && pin <= 30) return io(pin-2);
735         if (pin >= 33 && pin <= 36) return io(pin);
736         if (pin >= 38 && pin <= 47) return io(pin+1);
737
738
739         if (pin >= 33 && pin <= 36) return io(pin+36);
740         if (pin >= 38 && pin <= 41) return io(pin+43);
741         if (pin >= 42 && pin <= 43) return io(pin+47);
742         if (pin >= 44 && pin <= 47) return io(pin+49);
743         if (pin >= 57 && pin <= 62) return io(pin+40);
744         if (pin >= 63 && pin <= 66) return io(pin+46);
745         if (pin >= 68 && pin <= 71) return io(pin+53);
746         if (pin >= 72 && pin <= 73) return io(pin+53);
747         if (pin >= 74 && pin <= 75) return io(pin+63);
748         if (pin >= 76 && pin <= 77) return io(143+(pin-76));
749         if (pin >= 80 && pin <= 81) return io(145+(pin-80));
750         if (pin >= 82 && pin <= 85) return io(151+(pin-82));
751         if (pin >= 86 && pin <= 89) return io(165+(pin-86));
752         if (pin >= 91 && pin <= 94) return io(177+(pin-91));
753         if (pin >= 95 && pin <= 96) return io(183+(pin-95));
754         if (pin >= 97 && pin <= 100) return io(189+(pin-97));
755         if (pin >= 161 && pin <= 164) return io(289+(pin-161));
756         if (pin >= 165 && pin <= 166) return io(297+(pin-165));
757         if (pin >= 167 && pin <= 168) return io(303+(pin-167));
758         if (pin >= 169 && pin <= 170) return io(309+(pin-169));
759         if (pin >= 172 && pin <= 173) return io(313+(pin-172));
760         if (pin >= 174 && pin <= 175) return io(325+(pin-174));
761         if (pin >= 176 && pin <= 179) return io(327+(pin-176));
762         if (pin >= 180 && pin <= 181) return io(335+(pin-180));
763         if (pin >= 184 && pin <= 185) return io(337+(pin-184));
764         if (pin >= 186 && pin <= 191) return io(343+(pin-186));
765         if (pin >= 192 && pin <= 193) return io(359+(pin-192));
766         if (pin >= 195 && pin <= 196) return io(363+(pin-195));
767         if (pin >= 197 && pin <= 200) return io(369+(pin-197));
768         if (pin >= 201 && pin <= 204) return io(381+(pin-201));
769     }
770     public io(int ionum) {
771         if (ionum <= 94) {
772             int cell = (94 - pin) / 2;
773             boolean primary = cell * 2 == (94-pin);
774         }
775     }
776     */
777     public final class IOB {
778         public final int col;
779         public final int row;
780         public final boolean primary;
781         public final boolean northsouth;
782         public IOB(int col, int row, boolean primary, boolean northsouth) {
783             this.col = col;
784             this.row = row;
785             this.northsouth = northsouth;
786             this.primary = primary;
787         }
788         /*
789         public String dump() {
790             System.out.println("[ "+
791                                (schmitt()?"schmitt ":"")+
792                                (slew()==3?"fast ":slew()==2?"med ":slew()==1?"slow ":"slew-unknown ")+
793                                (cr()?"cr ":"")+
794                                (reg()?"reg ":"")+
795                                
796         }
797         */
798         public void enableOutput(int direction) {
799             useoem(true);
800             output(direction);
801             pullnone();
802             useoem(true);
803             oem(ALWAYS_ON);
804             oe(true);
805             // note: east-side IOBs should have slew=med, others slew=fast
806             slew((!northsouth && col==1) ? MEDIUM : FAST);
807         }
808         public void enableInput() {
809             schmitt(true);
810             pullnone();
811         }
812
813         public void    useoem(boolean use)  { mode4(z(3), row, col, 6, use); }
814         public boolean useoem()             { return (mode4(z(3), row, col) & (1<<6))!=0; }
815         public void    schmitt(boolean use) { mode4(z(0), row, col, 7, use); }
816         public boolean schmitt()            { return (mode4(z(0), row, col) & (1<<7))!=0; }
817
818         public void    slew(int slew) {
819             switch(slew) {
820                 case FAST:   mode4(z(0), row, col, 3<<5, 0x60); return;
821                 case MEDIUM: mode4(z(0), row, col, 2<<5, 0x60); return;
822                 case SLOW:   mode4(z(0), row, col, 1<<5, 0x60); return;
823                 default: throw new Error();
824             }
825         }
826
827         public void    oem(int source) {
828             switch(source) {
829                 case ALWAYS_ON:  mode4(z(3), row, col, 1<<5, 0x3f); return;
830                 case ALWAYS_OFF: mode4(z(3), row, col,    0, 0x3f); return;
831                 default: throw new Error();
832             }
833         }
834
835         private int z(int code) { return (northsouth ? 0x70 : 0x60) | (primary ? 0x00 : 0x04) | (code & 0x7); }
836         public void pullup()   { mode4(z(0), row, col, 0x00<<1, 0x06); }
837         public void pulldown() { mode4(z(0), row, col, 0x03<<1, 0x06); }
838         public void pullnone() { mode4(z(0), row, col, 0x01<<1, 0x06); }
839         public void oe(boolean oe) {
840             int old =  mode4(z(1), row, col) & (~(1<<5));
841             old     |= oe ? 0 : (1<<5);
842             mode4(z(1), row, col, old & 0xff);
843         }
844
845         public void output(int which) {
846             switch(which) {
847                 case NONE:
848                     mode4(z(1), row, col, 0, 0x1f); return;
849                 case WEST: case EAST: case NORTH: case SOUTH:
850                     mode4(z(1), row, col, 1<<0, 0x1f); return;
851                 case NW: case SW: case NE: case SE:
852                     mode4(z(1), row, col, 1<<1, 0x1f); return;
853                 default: throw new Error();
854             }
855         }
856
857     }
858
859
860 }