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