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