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