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