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