1 package com.atmel.fpslic;
5 import org.ibex.util.Log;
6 import static com.atmel.fpslic.FpslicConstants.*;
8 public abstract class Fpslic {
10 public Fpslic(int width, int height) { this.width = width; this.height = height; }
14 public int getWidth() { return width; }
15 public int getHeight() { return height; }
17 public abstract void flush();
18 public abstract void mode4(int z, int y, int x, int d);
19 public abstract byte mode4(int z, int y, int x);
21 public byte mode4zyx(int zyx) { return mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff); }
22 public void mode4zyx(int zyx, int d, int invmask) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, d, invmask); }
23 public void mode4(int z, int y, int x, int d, int invmask) {
24 int old = mode4(z, y, x);
29 public void mode4zyx(int zyx, int bit, boolean set) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, bit, set); }
30 public void mode4(int z, int y, int x, int bit, boolean set) {
31 int old = mode4(z, y, x);
33 old |= set ? (1 << bit) : 0;
37 // Inner Classes ///////////////////////////////////////////////////////////////////////////////
39 public final class Sector {
42 public Sector(Cell c) { this((c.col/4)*4, (c.row/4)*4); }
43 private Sector(int col, int row) {
44 if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
45 if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
49 public Sector north() { return row+4>=getHeight() ? null : new Sector(col, row+4); }
50 public Sector south() { return row==0 ? null : new Sector(col, row-4); }
51 public Sector east() { return col+4>=getWidth() ? null : new Sector(col+4, row); }
52 public Sector west() { return col==0 ? null : new Sector(col-4, row); }
53 public Cell cell() { return Fpslic.this.cell(col, row); }
56 public final class SectorWire {
57 public final boolean global;
58 public final boolean horizontal;
59 public final int plane;
62 public SectorWire(boolean horizontal, int plane, int col, int row) {
63 this(horizontal, plane, col, row, false);
66 public SectorWire(boolean horizontal, int plane, int col, int row, boolean global) {
67 this.horizontal=horizontal;
70 this.col= horizontal ? (col & ~0x3) : col;
71 this.row=!horizontal ? (row & ~0x3) : row;
73 public SectorWire global() {
74 return new SectorWire(horizontal, plane, col, row, true);
76 public boolean isDriven() {
77 // FIXME: bridging connections (horiz-to-vert)
78 for(int i=0; i<4; i++)
79 if (cell(horizontal ? col+i : col,
80 horizontal ? row : row+i).out(plane)) return true;
81 // FIXME: sector switchbox drivers
84 private int z(int z) { return (horizontal ? 0x30 : 0x20) | z; }
85 public int code(boolean topleft) {
87 case 0: return z(8)+(topleft?0:1);
88 case 1: return z(6)+(topleft?0:1);
89 case 2: return z(2*(4-plane))+(topleft?0:1);
90 case 3: return z(2*(4-plane))+(topleft?0:1);
91 case 4: return z(2*(4-plane))+(topleft?0:1);
96 private final int fine() { return horizontal ? row : col; }
97 public final int coarse() { return horizontal ? col : row; }
98 private int _row() { return horizontal ? row : ((row)>>2); }
99 private int _col() { return horizontal ? ((col)>>2) : col; }
101 public SectorWire west() { return !horizontal ? null : col-4<0 ? null : new SectorWire(horizontal, plane, col-4, row); }
102 public SectorWire east() { return !horizontal ? null : col+4>=getWidth() ? null : new SectorWire(horizontal, plane, col+4, row); }
103 public SectorWire north() { return horizontal ? null : row+4>=getHeight() ? null : new SectorWire(horizontal, plane, col, row+4); }
104 public SectorWire south() { return horizontal ? null : row-4<0 ? null : new SectorWire(horizontal, plane, col, row-4); }
106 public String toString() {
108 (horizontal?(col+":"+(col+3)):(""+col))+","+
109 (horizontal?(row+"") :(row+":"+(row+3)))+
113 /** returns the ZYX0 coordinate of the byte controlling the switchbox that allows <tt>w</tt> to drive this wire */
114 public int switchbox(SectorWire w) {
115 if (w.horizontal==horizontal) {
116 if (w.plane!=plane) throw new Error();
117 if (Math.abs(w.coarse()-coarse())!=4) throw new Error(w.coarse() + " -- " + coarse());
118 boolean topleft = horizontal ? (w.coarse() < coarse()) : (w.coarse() > coarse());
119 int col = _col() + (( horizontal && !topleft) ? 1 : 0);
120 int row = _row() + ((!horizontal && topleft) ? 1 : 0);
121 return (code(topleft) << 24) | (row<<16) | (col<<8);
123 throw new Error("not implemented");
127 mode4zyx(switchbox(north()), (1<<6), (1<<6));
130 public void drives(SectorWire w, boolean enable) {
131 // FIXME: better error checks?
134 if (!global) val = 0x02;
138 if (w.global) { mask = mask << 3; val = val << 3; }
139 mode4zyx(switchbox(w), val, mask);
142 public boolean drives(SectorWire w) {
143 // FIXME: better error checks?
144 int connect = (mode4zyx(switchbox(w)) >> (global?3:0)) & 0x7;
145 return (connect & 0x2)!=0;
147 public SectorWire driverRight() {
148 //System.out.println("checking " + Integer.toString(code(true), 16) + " " +
149 //Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
150 int ret = mode4(z(code(true)), _row(), _col());
151 ret = (ret >> (global?3:0)) & 0x7;
154 case 1: return null; /* global wire on same side */
155 case 2: return new SectorWire(horizontal, plane, horizontal?(col+4):col, horizontal?row:(row+4));
156 case 4: return null; /* global wire on other side */
157 default: throw new Error("multiple drivers on " + this + "!");
161 public boolean touches(Cell c) {
164 ? (c.row==_row() && (_col()/4 == c.col/4))
165 : (c.col==_col() && (_row()/4 == c.row/4));
169 public final class SwitchBox {
170 public final boolean h;
171 public final int col;
172 public final int row;
173 public final int plane;
174 public SwitchBox(boolean h, int col, int row, int plane) { this.h = h; this.col = col; this.row = row; this.plane = plane; }
175 public SectorWire west(boolean global) { return !h ? null : global ? null : new SectorWire(h, col-4, row, plane); }
176 public SectorWire east(boolean global) { return !h ? null : global ? null : new SectorWire(h, col+4, row, plane); }
177 public SectorWire north(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row-4, plane); }
178 public SectorWire south(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row+4, plane); }
182 public Cell cell(int col, int row) {
183 if (col<0) return null;
184 if (row<0) return null;
185 if (col>=getWidth()) return null;
186 if (row>=getHeight()) return null;
187 return new Cell(col, row);
190 public final class Cell {
191 public final int col;
192 public final int row;
194 public String toString() { return "cell@("+col+","+row+")"; }
196 public Cell(int col, int row) {
201 public Fpslic fpslic() { return Fpslic.this; }
202 public int hashCode() { return col ^ row ^ Fpslic.this.hashCode(); }
203 public boolean equals(Object o) {
204 if (o==null || (!(o instanceof Cell))) return false;
206 return c.col == col && c.row == row && c.fpslic()==fpslic();
209 // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
211 public SectorWire hwire(int plane) { return new SectorWire(true, plane, col, row); }
212 public SectorWire vwire(int plane) { return new SectorWire(false, plane, col, row); }
213 public Cell east() { return cell(col+1, row); }
214 public Cell west() { return cell(col-1, row); }
215 public Cell north() { return cell(col, row+1); }
216 public Cell south() { return cell(col, row-1); }
217 public Cell ne() { return cell(col+1, row+1); }
218 public Cell nw() { return cell(col-1, row+1); }
219 public Cell se() { return cell(col+1, row-1); }
220 public Cell sw() { return cell(col-1, row-1); }
221 public Sector sector() { return new Sector(this); }
223 /* bit positions mean: [MSB] zxy z_y zx_ z__ _xy __y _x_ ___ [LSB] */
224 public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
225 public void xlut(int table) { mode4(7, row, col, (byte)(table & 0xff)); }
226 public byte xlut() { return (byte)(mode4(7, row, col) & 0xff); }
227 public String printXLut() { return FpslicUtil.printLut(xlut(), "x", "y", "t"); }
228 public String printXLutX() { return FpslicUtil.printLut(xlut(), str(xi(), "x"), str(yi(), "y"), str(ti_source(), "t")); }
230 public String str(int x, String def) {
232 case NORTH: return "n";
233 case SOUTH: return "s";
234 case EAST: return "e";
235 case WEST: return "w";
236 case NW: return "nw";
237 case SE: return "se";
238 case NE: return "ne";
239 case SW: return "sw";
240 case FB: return "fb";
241 case L0: return (hx(0)&&vx(0))?"HV0":hx(0)?"H0":vx(0)?"V0":"L0";
242 case L1: return (hx(1)&&vx(1))?"HV1":hx(1)?"H1":vx(1)?"V1":"L1";
243 case L2: return (hx(2)&&vx(2))?"HV2":hx(2)?"H2":vx(2)?"V2":"L2";
244 case L3: return (hx(3)&&vx(3))?"HV3":hx(3)?"H3":vx(3)?"V3":"L3";
245 case L4: return (hx(4)&&vx(4))?"HV4":hx(4)?"H4":vx(4)?"V4":"L4";
250 /* bit positions mean: [MSB] zxy zx_ z_y z__ _xy _x_ __y ___ [LSB] */
251 public void ylut(int table) { mode4(6, row, col, (byte)(table & 0xff)); }
252 public byte ylut() { return (byte)(mode4(6, row, col) & 0xff); }
253 public String printYLut() { return FpslicUtil.printLut(ylut(), "y", "x", "t"); }
254 public String printYLutX() { return FpslicUtil.printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti_source(), "t")) + Integer.toString(ylut() & 0xff, 16); }
256 public void ff_reset_value(boolean value) {
257 //mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
260 public boolean ff_reset_value() { return false; }
261 public boolean columnClocked() {
265 public void out(int plane, boolean enable) {
267 case L0: mode4(0x00, row, col, 2, enable); return;
268 case L1: mode4(0x00, row, col, 3, enable); return;
269 case L2: mode4(0x00, row, col, 5, enable); return;
270 case L3: mode4(0x00, row, col, 4, enable); return;
271 case L4: mode4(0x00, row, col, 1, enable); return;
272 default: throw new RuntimeException("invalid argument");
276 public boolean out(int plane) {
278 case L0: return (mode4(0x00, row, col) & (1<<2)) != 0;
279 case L1: return (mode4(0x00, row, col) & (1<<3)) != 0;
280 case L2: return (mode4(0x00, row, col) & (1<<5)) != 0;
281 case L3: return (mode4(0x00, row, col) & (1<<4)) != 0;
282 case L4: return (mode4(0x00, row, col) & (1<<1)) != 0;
283 default: throw new RuntimeException("invalid argument");
287 public void h(int plane, boolean enable) {
289 case 0: mode4(0x08, row, col, 0, enable); return;
290 case 1: mode4(0x08, row, col, 2, enable); return;
291 case 2: mode4(0x08, row, col, 5, enable); return;
292 case 3: mode4(0x08, row, col, 6, enable); return;
293 case 4: mode4(0x00, row, col, 6, enable); return;
294 default: throw new RuntimeException("invalid argument");
298 public boolean hx(int plane) {
300 case 0: return (mode4(0x08, row, col) & (1<<0)) != 0;
301 case 1: return (mode4(0x08, row, col) & (1<<2)) != 0;
302 case 2: return (mode4(0x08, row, col) & (1<<5)) != 0;
303 case 3: return (mode4(0x08, row, col) & (1<<6)) != 0;
304 case 4: return (mode4(0x00, row, col) & (1<<6)) != 0;
305 default: throw new RuntimeException("invalid argument");
309 public void v(int plane, boolean enable) {
311 case 0: mode4(0x08, row, col, 1, enable); return;
312 case 1: mode4(0x08, row, col, 3, enable); return;
313 case 2: mode4(0x08, row, col, 4, enable); return;
314 case 3: mode4(0x08, row, col, 7, enable); return;
315 case 4: mode4(0x00, row, col, 7, enable); return;
316 default: throw new RuntimeException("invalid argument");
320 public boolean vx(int plane) {
322 case 0: return (mode4(0x08, row, col) & (1<<1)) != 0;
323 case 1: return (mode4(0x08, row, col) & (1<<3)) != 0;
324 case 2: return (mode4(0x08, row, col) & (1<<4)) != 0;
325 case 3: return (mode4(0x08, row, col) & (1<<7)) != 0;
326 case 4: return (mode4(0x00, row, col) & (1<<7)) != 0;
327 default: throw new RuntimeException("invalid argument");
332 public int ti_source() {
333 switch(mode4(1, row, col) & 0x30) {
334 case 0x20: return zi();
335 case 0x10: return FB;
336 case 0x00: return wi();
337 default: throw new Error("ack!");
342 switch(mode4(1, row, col) & 0x30) {
343 case 0x00: return TMUX_W;
344 case 0x10: return wi()==NONE ? TMUX_FB : TMUX_W_AND_FB;
345 case 0x20: return wi()==NONE ? TMUX_Z : TMUX_W_AND_Z;
346 case 0x30: throw new RuntimeException("illegal!");
347 default: return TMUX_W;
351 public void t(int code) {
354 case TMUX_W: result = 0x00; break;
355 case TMUX_Z: result = 0x20; break;
356 case TMUX_W_AND_Z: result = 0x20; break;
357 case TMUX_FB: result = 0x10; break;
358 case TMUX_W_AND_FB: result = 0x10; break;
359 default: result = 0x00; break;
361 mode4(1, row, col, result, 0x30);
364 private void fmux(int source) {
369 default: throw new Error("unknown argument to fmux()");
373 public boolean win_easable() {
378 return mode4(1, row, col) & 0x34;
381 public void t(boolean ignore_z_and_fb, boolean zm_drives_fb, boolean fb_drives_wm) {
382 // still not totally satisfied...
383 // need to find the bit that sets the w-mux off
384 // what does it mean for both bits (0x30) to be set to 1?
385 //if (fb && z) throw new RuntimeException("invalid combination");
393 // ttt => fb&w [0x34]
394 // ftt => fb&w [0x14]
395 // fft => fb&w [0x10]
399 // tft => fb&w [0x30]
400 if (ignore_z_and_fb) result |= 0x20;
401 if (zm_drives_fb) result |= 0x04;
402 if (fb_drives_wm) result |= 0x10;
403 mode4(1, row, col, result, 0x34);
407 public void c(int source) {
409 case XLUT: mode4(1, row, col, 0x00, 0xc0); break;
410 case YLUT: mode4(1, row, col, 0x40, 0xc0); break;
411 case ZMUX: mode4(1, row, col, 0x80, 0xc0); break;
412 default: throw new RuntimeException("Invalid Argument");
416 int cval = mode4(1, row, col) & 0xc0;
418 case 0x00: return XLUT;
419 case 0x40: return YLUT;
420 case 0x80: return ZMUX;
422 throw new Error("c() => " + cval);
424 public void b(boolean registered) { mode4(1, row, col, 3, !registered); }
425 public void f(boolean registered) { mode4(1, row, col, 2, !registered); }
426 public boolean xo() { return (mode4(1, row, col) & 0x01) != 0; }
427 public boolean yo() { return (mode4(1, row, col) & 0x02) != 0; }
428 public void xo(boolean center) { mode4(1, row, col, 0, center); }
429 public void yo(boolean center) { mode4(1, row, col, 1, center); }
431 public void xo(Cell c) {
432 if (c.row==row || c.col==col) { // use the y-input
442 public void yo(Cell c) {
443 if (!(c.row==row || c.col==col)) { // use the x-input
453 public boolean b() { return (mode4(1, row, col) & (1 << 3)) == 0; }
454 public boolean f() { return (mode4(1, row, col) & (1 << 2)) == 0; }
455 public boolean x() { return (mode4(1, row, col) & (1 << 1)) != 0; }
456 public boolean y() { return (mode4(1, row, col) & (1 << 0)) != 0; }
459 switch (mode4(0x02, row, col) & 0x3) {
463 default: throw new RuntimeException("invalid argument");
466 public void oe(int source) {
468 case NONE: mode4(0x02, row, col, 0, 0x3); break;
469 case H4: mode4(0x02, row, col, 1, 0x3); break;
470 case V4: mode4(0x02, row, col, 2, 0x3); break;
471 default: throw new RuntimeException("invalid argument");
476 // FIXME: can be multiple
477 if ((mode4(0x03, row, col) & (1<<4))!=0) return L4;
478 switch(mode4(0x05, row, col) & 0xff) {
479 case 0x80: return SW;
480 case (1<<6): return NE;
481 case (1<<5): return SE;
482 case (1<<4): return NW;
483 case (1<<3): return L0;
484 case (1<<2): return L1;
485 case (1<<1): return L2;
486 case (1<<0): return L3;
488 default: throw new Error();
492 public void xi(int source) {
494 case SW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<7); break;
495 case NE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<6); break;
496 case SE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<5); break;
497 case NW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<4); break;
499 case L0: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<3); break;
500 case L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
501 case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
502 case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
503 case L4: mode4(0x03, row, col, 4, true); mode4(0x05, row, col, 0); break;
505 case NONE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 0); break;
506 default: throw new RuntimeException("invalid argument");
510 public void xi(SectorWire sw) {
511 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
515 public void xi(Cell c) {
516 if (c.row==row-1 && c.col==col-1) xi(SW);
517 else if (c.row==row+1 && c.col==col-1) xi(NW);
518 else if (c.row==row-1 && c.col==col+1) xi(SE);
519 else if (c.row==row+1 && c.col==col+1) xi(NE);
520 else throw new RuntimeException("invalid argument");
524 if ((mode4(0x02, row, col) & (1<<6))!=0) return L4;
525 switch(mode4(0x04, row, col) & 0xff) {
526 case (1<<7): return NORTH;
527 case (1<<5): return SOUTH;
528 case (1<<6): return WEST;
529 case (1<<4): return EAST;
530 case (1<<3): return L0;
531 case (1<<2): return L1;
532 case (1<<1): return L2;
533 case (1<<0): return L3;
535 default: throw new Error();
539 public void yi(int source) {
541 case NORTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<7); break;
542 case SOUTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<5); break;
543 case WEST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<6); break;
544 case EAST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<4); break;
545 case L4: mode4(0x02, row, col, 6, true); mode4(0x04, row, col, 0); break;
546 case L3: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<0); break;
547 case L2: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<1); break;
548 case L1: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<2); break;
549 case L0: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<3); break;
550 case NONE: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 0); break;
551 default: throw new RuntimeException("invalid argument");
555 public void yi(SectorWire sw) {
556 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
560 public void yi(Cell c) {
561 if (c.row==row-1 && c.col==col) yi(SOUTH);
562 else if (c.row==row+1 && c.col==col) yi(NORTH);
563 else if (c.row==row && c.col==col-1) yi(WEST);
564 else if (c.row==row && c.col==col+1) yi(EAST);
565 else throw new RuntimeException("invalid argument");
568 public void wi(SectorWire sw) {
569 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
573 public void wi(int source) {
575 case L4: mode4(0x03, row, col, 1<<5, 0xEC); break;
576 case L3: mode4(0x03, row, col, 1<<6, 0xEC); break;
577 case L2: mode4(0x03, row, col, 1<<7, 0xEC); break;
578 case L1: mode4(0x03, row, col, 1<<3, 0xEC); break;
579 case L0: mode4(0x03, row, col, 1<<2, 0xEC); break;
580 case NONE: mode4(0x03, row, col, 0, 0xEC); break;
581 default: throw new RuntimeException("invalid argument");
586 int who = mode4(0x03, row, col) & 0xEC;
588 case (1<<5): return L4;
589 case (1<<6): return L3;
590 case (1<<7): return L2;
591 case (1<<3): return L1;
592 case (1<<2): return L0;
593 case (1<<0): return NONE; /* huh? */
594 case (0): return NONE;
595 default: throw new RuntimeException("invalid argument: " + who);
600 public void zi(SectorWire sw) {
601 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
605 public void zi(int source) {
607 case L4: mode4(0x02, row, col, 1<<7, 0xDB); break;
608 case L3: mode4(0x02, row, col, 1<<5, 0xDB); break;
609 case L2: mode4(0x02, row, col, 1<<4, 0xDB); break;
610 case L1: mode4(0x02, row, col, 1<<3, 0xDB); break;
611 case L0: mode4(0x02, row, col, 1<<2, 0xDB); break;
612 case NONE: mode4(0x02, row, col, 0, 0xDB); break;
613 default: throw new RuntimeException("invalid argument");
618 switch(mode4(0x02, row, col) & 0x9B) {
619 case (1<<7): return L4;
620 case (1<<5): return L3;
621 case (1<<4): return L2;
622 case (1<<3): return L1;
623 case (1<<2): return L0;
624 case (1<<1): return NONE; /* huh? */
625 case (1<<0): return NONE; /* huh? */
627 default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0x9B));
631 public Cell dir(int i) {
633 case NORTH: return north();
634 case SOUTH: return south();
635 case EAST: return east();
636 case WEST: return west();
637 case NW: return nw();
638 case SW: return sw();
639 case SE: return se();
640 case NE: return ne();
644 public int dir(Cell c) {
645 if (c.row==row-1 && c.col==col-1) return SW;
646 else if (c.row==row+1 && c.col==col-1) return NW;
647 else if (c.row==row-1 && c.col==col+1) return SE;
648 else if (c.row==row+1 && c.col==col+1) return NE;
649 else if (c.row==row-1 && c.col==col) return SOUTH;
650 else if (c.row==row+1 && c.col==col) return NORTH;
651 else if (c.row==row && c.col==col-1) return WEST;
652 else if (c.row==row && c.col==col+1) return EAST;
656 public void generalized_c_element() {
659 ylut(LUT_SELF & (~LUT_OTHER));
660 xlut((~LUT_SELF) | LUT_OTHER);
670 ylut((LUT_SELF & ~LUT_OTHER) | (LUT_Z & ~LUT_OTHER) | (LUT_Z & LUT_SELF & LUT_OTHER));
675 t(false, false, true);
681 // Relevance //////////////////////////////////////////////////////////////////////////////
682 public boolean relevant() {
683 return xo_relevant() || yo_relevant() || out_relevant();
686 public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
687 public boolean xo_relevant(int direction) {
689 case NE: return ne() != null && ne().xi()==SW;
690 case NW: return nw() != null && nw().xi()==SE;
691 case SE: return se() != null && se().xi()==NW;
692 case SW: return sw() != null && sw().xi()==NE;
693 default: return false;
696 public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
697 public boolean yo_relevant(int direction) {
699 case NORTH: return north() != null && north().yi()==SOUTH /*&& north().yi_relevant()*/;
700 case EAST: return east() != null && east().yi()==WEST /*&& east().yi_relevant()*/;
701 case SOUTH: return south() != null && south().yi()==NORTH /*&& south().yi_relevant()*/;
702 case WEST: return west() != null && west().yi()==EAST /*&& west().yi_relevant()*/;
703 default: return false;
706 public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
707 public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
708 public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
709 public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
710 public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
711 public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
712 public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
713 public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
714 public boolean xlut_relevant() {
715 if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
716 if (xo()) return false;
717 return xo_relevant();
719 public boolean ylut_relevant() {
720 if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
721 if (yo()) return false;
722 return yo_relevant();
724 public boolean c_relevant() {
726 case 0x34: return true;
727 case 0x14: return true;
728 case 0x10: return true;
729 case 0x30: return true;
731 for(int i=0; i<5; i++)
734 if (xo() || yo()) return true;
738 public boolean register_relevant() {
739 if (!c_relevant()) return false;
740 if (f() && out_relevant()) return true;
741 if (f() && fb_relevant()) return true;
742 if (b() && xo()) return true;
743 if (b() && yo()) return true;
746 public boolean out_relevant() {
748 boolean connect = false;
749 for(int i=0; i<4; i++) {
754 if (out(L0+i)) out = true;
755 if (hx(L0+i)) connect = true;
756 if (vx(L0+i)) connect = true;
758 return out && connect;
761 public boolean fb_relevant() {
763 if (!(zi_to_xlut_relevant()) ||
764 !(zi_to_ylut_relevant())) return false;
766 case 0x34: return true;
767 case 0x14: return true;
768 case 0x10: return true;
769 case 0x30: return true;
779 public IOB iob_bot(int col, boolean primary) { return new IOB(col, 0, primary, true); }
780 public IOB iob_top(int col, boolean primary) { return new IOB(col, 1, primary, true); }
781 public IOB iob_left(int row, boolean primary) { return new IOB(0, row, primary, false); }
782 public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
784 public IOB fromPin(int pin) {
785 if (pin >= 4 && pin <= 11) return io(pin-3);
786 if (pin >= 15 && pin <= 18) return io(pin-2);
787 if (pin >= 19 && pin <= 24) return io(pin);
788 if (pin >= 27 && pin <= 30) return io(pin-2);
789 if (pin >= 33 && pin <= 36) return io(pin);
790 if (pin >= 38 && pin <= 47) return io(pin+1);
793 if (pin >= 33 && pin <= 36) return io(pin+36);
794 if (pin >= 38 && pin <= 41) return io(pin+43);
795 if (pin >= 42 && pin <= 43) return io(pin+47);
796 if (pin >= 44 && pin <= 47) return io(pin+49);
797 if (pin >= 57 && pin <= 62) return io(pin+40);
798 if (pin >= 63 && pin <= 66) return io(pin+46);
799 if (pin >= 68 && pin <= 71) return io(pin+53);
800 if (pin >= 72 && pin <= 73) return io(pin+53);
801 if (pin >= 74 && pin <= 75) return io(pin+63);
802 if (pin >= 76 && pin <= 77) return io(143+(pin-76));
803 if (pin >= 80 && pin <= 81) return io(145+(pin-80));
804 if (pin >= 82 && pin <= 85) return io(151+(pin-82));
805 if (pin >= 86 && pin <= 89) return io(165+(pin-86));
806 if (pin >= 91 && pin <= 94) return io(177+(pin-91));
807 if (pin >= 95 && pin <= 96) return io(183+(pin-95));
808 if (pin >= 97 && pin <= 100) return io(189+(pin-97));
809 if (pin >= 161 && pin <= 164) return io(289+(pin-161));
810 if (pin >= 165 && pin <= 166) return io(297+(pin-165));
811 if (pin >= 167 && pin <= 168) return io(303+(pin-167));
812 if (pin >= 169 && pin <= 170) return io(309+(pin-169));
813 if (pin >= 172 && pin <= 173) return io(313+(pin-172));
814 if (pin >= 174 && pin <= 175) return io(325+(pin-174));
815 if (pin >= 176 && pin <= 179) return io(327+(pin-176));
816 if (pin >= 180 && pin <= 181) return io(335+(pin-180));
817 if (pin >= 184 && pin <= 185) return io(337+(pin-184));
818 if (pin >= 186 && pin <= 191) return io(343+(pin-186));
819 if (pin >= 192 && pin <= 193) return io(359+(pin-192));
820 if (pin >= 195 && pin <= 196) return io(363+(pin-195));
821 if (pin >= 197 && pin <= 200) return io(369+(pin-197));
822 if (pin >= 201 && pin <= 204) return io(381+(pin-201));
824 public io(int ionum) {
826 int cell = (94 - pin) / 2;
827 boolean primary = cell * 2 == (94-pin);
831 public final class IOB {
832 public final int col;
833 public final int row;
834 public final boolean primary;
835 public final boolean northsouth;
836 public IOB(int col, int row, boolean primary, boolean northsouth) {
839 this.northsouth = northsouth;
840 this.primary = primary;
843 public String dump() {
844 System.out.println("[ "+
845 (schmitt()?"schmitt ":"")+
846 (slew()==3?"fast ":slew()==2?"med ":slew()==1?"slow ":"slew-unknown ")+
852 public void enableOutput(int direction) {
859 // note: east-side IOBs should have slew=med, others slew=fast
860 slew((!northsouth && col==1) ? MEDIUM : FAST);
862 public void enableInput() {
867 public void useoem(boolean use) { mode4(z(3), row, col, 6, use); }
868 public boolean useoem() { return (mode4(z(3), row, col) & (1<<6))!=0; }
869 public void schmitt(boolean use) { mode4(z(0), row, col, 7, use); }
870 public boolean schmitt() { return (mode4(z(0), row, col) & (1<<7))!=0; }
872 public void slew(int slew) {
874 case FAST: mode4(z(0), row, col, 3<<5, 0x60); return;
875 case MEDIUM: mode4(z(0), row, col, 2<<5, 0x60); return;
876 case SLOW: mode4(z(0), row, col, 1<<5, 0x60); return;
877 default: throw new Error();
881 public void oem(int source) {
883 case ALWAYS_ON: mode4(z(3), row, col, 1<<5, 0x3f); return;
884 case ALWAYS_OFF: mode4(z(3), row, col, 0, 0x3f); return;
885 default: throw new Error();
889 private int z(int code) { return (northsouth ? 0x70 : 0x60) | (primary ? 0x00 : 0x04) | (code & 0x7); }
890 public void pullup() { mode4(z(0), row, col, 0x00<<1, 0x06); }
891 public void pulldown() { mode4(z(0), row, col, 0x03<<1, 0x06); }
892 public void pullnone() { mode4(z(0), row, col, 0x01<<1, 0x06); }
893 public void oe(boolean oe) {
894 int old = mode4(z(1), row, col) & (~(1<<5));
895 old |= oe ? 0 : (1<<5);
896 mode4(z(1), row, col, old & 0xff);
899 public void output(int which) {
902 mode4(z(1), row, col, 0, 0x1f); return;
903 case WEST: case EAST: case NORTH: case SOUTH:
904 mode4(z(1), row, col, 1<<0, 0x1f); return;
905 case NW: case SW: case NE: case SE:
906 mode4(z(1), row, col, 1<<1, 0x1f); return;
907 default: throw new Error();