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