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