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