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