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