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