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=horizontal;
66 this.col= horizontal ? (col & ~0x3) : col;
67 this.row=!horizontal ? (row & ~0x3) : row;
69 public boolean isDriven() {
70 // FIXME: bridging connections (horiz-to-vert)
71 for(int i=0; i<4; i++)
72 if (cell(horizontal ? col+i : col,
73 horizontal ? row : row+i).out(plane)) return true;
74 // FIXME: sector switchbox drivers
77 private int z(int z) { return (horizontal ? 0x30 : 0x20) | z; }
78 public int code(boolean topleft) {
80 case 0: return z(6)+(topleft?0:1);
81 case 1: return z(8)+(topleft?0:1);
82 case 2: return z(2*(4-plane))+(topleft?0:1);
83 case 3: return z(2*(4-plane))+(topleft?0:1);
84 case 4: return z(2*(4-plane))+(topleft?0:1);
89 private final int fine() { return horizontal ? row : col; }
90 public final int coarse() { return horizontal ? col : row; }
91 private int _row() { return horizontal ? row : ((row)>>2); }
92 private int _col() { return horizontal ? ((col)>>2) : col; }
94 public SectorWire west() { return !horizontal ? null : col-4<0 ? null : new SectorWire(horizontal, plane, col-4, row); }
95 public SectorWire east() { return !horizontal ? null : col+4>=getWidth() ? null : new SectorWire(horizontal, plane, col+4, row); }
96 public SectorWire north() { return horizontal ? null : row+4>=getHeight() ? null : new SectorWire(horizontal, plane, col, row+4); }
97 public SectorWire south() { return horizontal ? null : row-4<0 ? null : new SectorWire(horizontal, plane, col, row-4); }
99 public String toString() {
101 (horizontal?(col+":"+(col+3)):(""+col))+","+
102 (horizontal?(row+"") :(row+":"+(row+3)))+
106 /** returns the ZYX0 coordinate of the byte controlling the switchbox that allows <tt>w</tt> to drive this wire */
107 public int switchbox(SectorWire w) {
108 if (w.horizontal==horizontal) {
109 if (w.plane!=plane) throw new Error();
110 if (Math.abs(w.coarse()-coarse())!=4) throw new Error(w.coarse() + " -- " + coarse());
111 boolean topleft = horizontal ? (w.coarse() < coarse()) : (w.coarse() > coarse());
112 int col = _col() + (( horizontal && !topleft) ? 1 : 0);
113 int row = _row() + ((!horizontal && topleft) ? 1 : 0);
114 return (code(topleft) << 24) | (row<<16) | (col<<8);
116 throw new Error("not implemented");
119 public void drives(SectorWire w, boolean enable) {
120 mode4zyx(switchbox(w), enable?0x02:0x00, 0x07);
123 public boolean drives(SectorWire w) {
124 int connect = (mode4zyx(switchbox(w)) >> (global?3:0)) & 0x7;
125 return (connect & 0x2)!=0;
127 public SectorWire driverRight() {
128 //System.out.println("checking " + Integer.toString(code(true), 16) + " " +
129 //Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
130 int ret = mode4(z(code(true)), _row(), _col());
131 ret = (ret >> (global?3:0)) & 0x7;
134 case 1: return null; /* global wire on same side */
135 case 2: return new SectorWire(horizontal, plane, horizontal?(col+4):col, horizontal?row:(row+4));
136 case 4: return null; /* global wire on other side */
137 default: throw new Error("multiple drivers on " + this + "!");
141 public boolean touches(Cell c) {
144 ? (c.row==_row() && (_col()/4 == c.col/4))
145 : (c.col==_col() && (_row()/4 == c.row/4));
149 public final class SwitchBox {
150 public final boolean h;
151 public final int col;
152 public final int row;
153 public final int plane;
154 public SwitchBox(boolean h, int col, int row, int plane) { this.h = h; this.col = col; this.row = row; this.plane = plane; }
155 public SectorWire west(boolean global) { return !h ? null : global ? null : new SectorWire(h, col-4, row, plane); }
156 public SectorWire east(boolean global) { return !h ? null : global ? null : new SectorWire(h, col+4, row, plane); }
157 public SectorWire north(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row-4, plane); }
158 public SectorWire south(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row+4, plane); }
162 public Cell cell(int col, int row) {
163 if (col<0) return null;
164 if (row<0) return null;
165 if (col>=getWidth()) return null;
166 if (row>=getHeight()) return null;
167 return new Cell(col, row);
170 public final class Cell {
171 public final int col;
172 public final int row;
174 public Cell(int col, int row) {
179 public Fpslic fpslic() { return Fpslic.this; }
181 // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
183 public SectorWire hwire(int plane) { return new SectorWire(true, plane, col, row); }
184 public SectorWire vwire(int plane) { return new SectorWire(false, plane, col, row); }
185 public Cell east() { return cell(col+1, row); }
186 public Cell west() { return cell(col-1, row); }
187 public Cell north() { return cell(col, row+1); }
188 public Cell south() { return cell(col, row-1); }
189 public Cell ne() { return cell(col+1, row+1); }
190 public Cell nw() { return cell(col-1, row+1); }
191 public Cell se() { return cell(col+1, row-1); }
192 public Cell sw() { return cell(col-1, row-1); }
193 public Sector sector() { return new Sector(this); }
195 /* bit positions mean: [MSB] zxy z_y zx_ z__ _xy __y _x_ ___ [LSB] */
196 public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
197 public void xlut(int table) { mode4(7, row, col, (byte)(table & 0xff)); }
198 public byte xlut() { return (byte)(mode4(7, row, col) & 0xff); }
199 public String printXLut() { return FpslicUtil.printLut(xlut(), "x", "y", "t"); }
200 public String printXLutX() { return FpslicUtil.printLut(xlut(), str(xi(), "x"), str(yi(), "y"), str(ti_source(), "t")); }
202 public String str(int x, String def) {
204 case NORTH: return "n";
205 case SOUTH: return "s";
206 case EAST: return "e";
207 case WEST: return "w";
208 case NW: return "nw";
209 case SE: return "se";
210 case NE: return "ne";
211 case SW: return "sw";
212 case FB: return "fb";
213 case L0: return (hx(0)&&vx(0))?"HV0":hx(0)?"H0":vx(0)?"V0":"L0";
214 case L1: return (hx(1)&&vx(1))?"HV1":hx(1)?"H1":vx(1)?"V1":"L1";
215 case L2: return (hx(2)&&vx(2))?"HV2":hx(2)?"H2":vx(2)?"V2":"L2";
216 case L3: return (hx(3)&&vx(3))?"HV3":hx(3)?"H3":vx(3)?"V3":"L3";
217 case L4: return (hx(4)&&vx(4))?"HV4":hx(4)?"H4":vx(4)?"V4":"L4";
222 /* bit positions mean: [MSB] zxy zx_ z_y z__ _xy _x_ __y ___ [LSB] */
223 public void ylut(int table) { mode4(6, row, col, (byte)(table & 0xff)); }
224 public byte ylut() { return (byte)(mode4(6, row, col) & 0xff); }
225 public String printYLut() { return FpslicUtil.printLut(ylut(), "y", "x", "t"); }
226 public String printYLutX() { return FpslicUtil.printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti_source(), "t")) + Integer.toString(ylut() & 0xff, 16); }
228 public void ff_reset_value(boolean value) {
229 //mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
232 public boolean ff_reset_value() { return false; }
233 public boolean columnClocked() {
237 public void out(int plane, boolean enable) {
239 case L0: mode4(0x00, row, col, 2, enable); return;
240 case L1: mode4(0x00, row, col, 3, enable); return;
241 case L2: mode4(0x00, row, col, 5, enable); return;
242 case L3: mode4(0x00, row, col, 4, enable); return;
243 case L4: mode4(0x00, row, col, 1, enable); return;
244 default: throw new RuntimeException("invalid argument");
248 public boolean out(int plane) {
250 case L0: return (mode4(0x00, row, col) & (1<<2)) != 0;
251 case L1: return (mode4(0x00, row, col) & (1<<3)) != 0;
252 case L2: return (mode4(0x00, row, col) & (1<<5)) != 0;
253 case L3: return (mode4(0x00, row, col) & (1<<4)) != 0;
254 case L4: return (mode4(0x00, row, col) & (1<<1)) != 0;
255 default: throw new RuntimeException("invalid argument");
259 public void h(int plane, boolean enable) {
261 case 0: mode4(0x08, row, col, 0, enable); return;
262 case 1: mode4(0x08, row, col, 2, enable); return;
263 case 2: mode4(0x08, row, col, 5, enable); return;
264 case 3: mode4(0x08, row, col, 6, enable); return;
265 case 4: mode4(0x00, row, col, 6, enable); return;
266 default: throw new RuntimeException("invalid argument");
270 public boolean hx(int plane) {
272 case 0: return (mode4(0x08, row, col) & (1<<0)) != 0;
273 case 1: return (mode4(0x08, row, col) & (1<<2)) != 0;
274 case 2: return (mode4(0x08, row, col) & (1<<5)) != 0;
275 case 3: return (mode4(0x08, row, col) & (1<<6)) != 0;
276 case 4: return (mode4(0x00, row, col) & (1<<6)) != 0;
277 default: throw new RuntimeException("invalid argument");
281 public void v(int plane, boolean enable) {
283 case 0: mode4(0x08, row, col, 1, enable); return;
284 case 1: mode4(0x08, row, col, 3, enable); return;
285 case 2: mode4(0x08, row, col, 4, enable); return;
286 case 3: mode4(0x08, row, col, 7, enable); return;
287 case 4: mode4(0x00, row, col, 7, enable); return;
288 default: throw new RuntimeException("invalid argument");
292 public boolean vx(int plane) {
294 case 0: return (mode4(0x08, row, col) & (1<<1)) != 0;
295 case 1: return (mode4(0x08, row, col) & (1<<3)) != 0;
296 case 2: return (mode4(0x08, row, col) & (1<<4)) != 0;
297 case 3: return (mode4(0x08, row, col) & (1<<7)) != 0;
298 case 4: return (mode4(0x00, row, col) & (1<<7)) != 0;
299 default: throw new RuntimeException("invalid argument");
304 public int ti_source() {
305 switch(mode4(1, row, col) & 0x30) {
306 case 0x20: return zi();
307 case 0x10: return FB;
308 case 0x00: return wi();
309 default: throw new Error("ack!");
314 switch(mode4(1, row, col) & 0x30) {
315 case 0x00: return TMUX_W;
316 case 0x10: return wi()==NONE ? TMUX_FB : TMUX_W_AND_FB;
317 case 0x20: return wi()==NONE ? TMUX_Z : TMUX_W_AND_Z;
318 case 0x30: throw new RuntimeException("illegal!");
319 default: return TMUX_W;
323 public void t(int code) {
326 case TMUX_W: result = 0x00; break;
327 case TMUX_Z: result = 0x20; break;
328 case TMUX_W_AND_Z: result = 0x20; break;
329 case TMUX_FB: result = 0x10; break;
330 case TMUX_W_AND_FB: result = 0x10; break;
331 default: result = 0x00; break;
333 mode4(1, row, col, result, 0x30);
336 private void fmux(int source) {
341 default: throw new Error("unknown argument to fmux()");
345 public boolean win_easable() {
350 return mode4(1, row, col) & 0x34;
353 public void t(boolean ignore_z_and_fb, boolean zm_drives_fb, boolean fb_drives_wm) {
354 // still not totally satisfied...
355 // need to find the bit that sets the w-mux off
356 // what does it mean for both bits (0x30) to be set to 1?
357 //if (fb && z) throw new RuntimeException("invalid combination");
365 // ttt => fb&w [0x34]
366 // ftt => fb&w [0x14]
367 // fft => fb&w [0x10]
371 // tft => fb&w [0x30]
372 if (ignore_z_and_fb) result |= 0x20;
373 if (zm_drives_fb) result |= 0x04;
374 if (fb_drives_wm) result |= 0x10;
375 mode4(1, row, col, result, 0x34);
379 public void c(int source) {
381 case XLUT: mode4(1, row, col, 0x00, 0xc0); break;
382 case YLUT: mode4(1, row, col, 0x40, 0xc0); break;
383 case ZMUX: mode4(1, row, col, 0x80, 0xc0); break;
384 default: throw new RuntimeException("Invalid Argument");
388 int cval = mode4(1, row, col) & 0xc0;
390 case 0x00: return XLUT;
391 case 0x40: return YLUT;
392 case 0x80: return ZMUX;
394 throw new Error("c() => " + cval);
396 public void b(boolean registered) { mode4(1, row, col, 3, !registered); }
397 public void f(boolean registered) { mode4(1, row, col, 2, !registered); }
398 public boolean xo() { return (mode4(1, row, col) & 0x01) != 0; }
399 public boolean yo() { return (mode4(1, row, col) & 0x02) != 0; }
400 public void xo(boolean center) { mode4(1, row, col, 0, center); }
401 public void yo(boolean center) { mode4(1, row, col, 1, center); }
403 public void xo(Cell c) {
404 if (c.row==row || c.col==col) { // use the y-input
414 public void yo(Cell c) {
415 if (!(c.row==row || c.col==col)) { // use the x-input
425 public boolean b() { return (mode4(1, row, col) & (1 << 3)) == 0; }
426 public boolean f() { return (mode4(1, row, col) & (1 << 2)) == 0; }
427 public boolean x() { return (mode4(1, row, col) & (1 << 1)) != 0; }
428 public boolean y() { return (mode4(1, row, col) & (1 << 0)) != 0; }
431 switch (mode4(0x02, row, col) & 0x3) {
435 default: throw new RuntimeException("invalid argument");
438 public void oe(int source) {
440 case NONE: mode4(0x02, row, col, 0, 0x3); break;
441 case H4: mode4(0x02, row, col, 1, 0x3); break;
442 case V4: mode4(0x02, row, col, 2, 0x3); break;
443 default: throw new RuntimeException("invalid argument");
448 // FIXME: can be multiple
449 if ((mode4(0x03, row, col) & (1<<4))!=0) return L4;
450 switch(mode4(0x05, row, col) & 0xff) {
451 case 0x80: return SW;
452 case (1<<6): return NE;
453 case (1<<5): return SE;
454 case (1<<4): return NW;
455 case (1<<3): return L0;
456 case (1<<2): return L1;
457 case (1<<1): return L2;
458 case (1<<0): return L3;
460 default: throw new Error();
464 public void xi(int source) {
466 case SW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<7); break;
467 case NE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<6); break;
468 case SE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<5); break;
469 case NW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<4); break;
471 case L0: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<3); break;
472 case L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
473 case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
474 case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
475 case L4: mode4(0x03, row, col, 4, true); mode4(0x05, row, col, 0); break;
477 case NONE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 0); break;
478 default: throw new RuntimeException("invalid argument");
482 public void xi(SectorWire sw) {
483 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
487 public void xi(Cell c) {
488 if (c.row==row-1 && c.col==col-1) xi(SW);
489 else if (c.row==row+1 && c.col==col-1) xi(NW);
490 else if (c.row==row-1 && c.col==col+1) xi(SE);
491 else if (c.row==row+1 && c.col==col+1) xi(NE);
492 else throw new RuntimeException("invalid argument");
496 if ((mode4(0x02, row, col) & (1<<6))!=0) return L4;
497 switch(mode4(0x04, row, col) & 0xff) {
498 case 0x80: return NORTH;
499 case (1<<5): return SOUTH;
500 case (1<<6): return WEST;
501 case (1<<4): return EAST;
502 case (1<<3): return L0;
503 case (1<<2): return L1;
504 case (1<<1): return L2;
505 case (1<<0): return L3;
507 default: throw new Error();
511 public void yi(int source) {
513 case NORTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<7); break;
514 case SOUTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<5); break;
515 case WEST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<6); break;
516 case EAST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<4); break;
517 case L4: mode4(0x02, row, col, 6, true); mode4(0x04, row, col, 0); break;
518 case L3: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<0); break;
519 case L2: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<1); break;
520 case L1: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<2); break;
521 case L0: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<3); break;
522 case NONE: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 0); break;
523 default: throw new RuntimeException("invalid argument");
527 public void yi(SectorWire sw) {
528 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
532 public void yi(Cell c) {
533 if (c.row==row-1 && c.col==col) yi(SOUTH);
534 else if (c.row==row+1 && c.col==col) yi(NORTH);
535 else if (c.row==row && c.col==col-1) yi(WEST);
536 else if (c.row==row && c.col==col+1) yi(EAST);
537 else throw new RuntimeException("invalid argument");
540 public void wi(SectorWire sw) {
541 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
545 public void wi(int source) {
547 case L4: mode4(0x03, row, col, 1<<5, 0xEC); break;
548 case L3: mode4(0x03, row, col, 1<<6, 0xEC); break;
549 case L2: mode4(0x03, row, col, 1<<7, 0xEC); break;
550 case L1: mode4(0x03, row, col, 1<<3, 0xEC); break;
551 case L0: mode4(0x03, row, col, 1<<2, 0xEC); break;
552 case NONE: mode4(0x03, row, col, 0, 0xEC); break;
553 default: throw new RuntimeException("invalid argument");
558 int who = mode4(0x03, row, col) & 0xEC;
560 case (1<<5): return L4;
561 case (1<<6): return L3;
562 case (1<<7): return L2;
563 case (1<<3): return L1;
564 case (1<<2): return L0;
565 case (1<<0): return NONE; /* huh? */
566 case (0): return NONE;
567 default: throw new RuntimeException("invalid argument: " + who);
572 public void zi(SectorWire sw) {
573 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
577 public void zi(int source) {
579 case L4: mode4(0x02, row, col, 1<<7, 0xDB); break;
580 case L3: mode4(0x02, row, col, 1<<5, 0xDB); break;
581 case L2: mode4(0x02, row, col, 1<<4, 0xDB); break;
582 case L1: mode4(0x02, row, col, 1<<3, 0xDB); break;
583 case L0: mode4(0x02, row, col, 1<<2, 0xDB); break;
584 case NONE: mode4(0x02, row, col, 0, 0xDB); break;
585 default: throw new RuntimeException("invalid argument");
590 switch(mode4(0x02, row, col) & 0x9B) {
591 case (1<<7): return L4;
592 case (1<<5): return L3;
593 case (1<<4): return L2;
594 case (1<<3): return L1;
595 case (1<<2): return L0;
596 case (1<<1): return NONE; /* huh? */
597 case (1<<0): return NONE; /* huh? */
599 default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0x9B));
603 public Cell dir(int i) {
605 case NORTH: return north();
606 case SOUTH: return south();
607 case EAST: return east();
608 case WEST: return west();
609 case NW: return nw();
610 case SW: return sw();
611 case SE: return se();
612 case NE: return ne();
616 public int dir(Cell c) {
617 if (c.row==row-1 && c.col==col-1) return SW;
618 else if (c.row==row+1 && c.col==col-1) return NW;
619 else if (c.row==row-1 && c.col==col+1) return SE;
620 else if (c.row==row+1 && c.col==col+1) return NE;
621 else if (c.row==row-1 && c.col==col) return SOUTH;
622 else if (c.row==row+1 && c.col==col) return NORTH;
623 else if (c.row==row && c.col==col-1) return WEST;
624 else if (c.row==row && c.col==col+1) return EAST;
628 public void generalized_c_element() {
631 ylut(LUT_SELF & (~LUT_OTHER));
632 xlut((~LUT_SELF) | LUT_OTHER);
642 ylut((LUT_SELF & ~LUT_OTHER) | (LUT_Z & ~LUT_OTHER) | (LUT_Z & LUT_SELF & LUT_OTHER));
647 t(false, false, true);
653 // Relevance //////////////////////////////////////////////////////////////////////////////
654 public boolean relevant() {
655 return xo_relevant() || yo_relevant() || out_relevant();
658 public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
659 public boolean xo_relevant(int direction) {
661 case NE: return ne() != null && ne().xi()==SW;
662 case NW: return nw() != null && nw().xi()==SE;
663 case SE: return se() != null && se().xi()==NW;
664 case SW: return sw() != null && sw().xi()==NE;
665 default: return false;
668 public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
669 public boolean yo_relevant(int direction) {
671 case NORTH: return north() != null && north().yi()==SOUTH /*&& north().yi_relevant()*/;
672 case EAST: return east() != null && east().yi()==WEST /*&& east().yi_relevant()*/;
673 case SOUTH: return south() != null && south().yi()==NORTH /*&& south().yi_relevant()*/;
674 case WEST: return west() != null && west().yi()==EAST /*&& west().yi_relevant()*/;
675 default: return false;
678 public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
679 public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
680 public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
681 public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
682 public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
683 public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
684 public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
685 public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
686 public boolean xlut_relevant() {
687 if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
688 if (xo()) return false;
689 return xo_relevant();
691 public boolean ylut_relevant() {
692 if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
693 if (yo()) return false;
694 return yo_relevant();
696 public boolean c_relevant() {
698 case 0x34: return true;
699 case 0x14: return true;
700 case 0x10: return true;
701 case 0x30: return true;
703 for(int i=0; i<5; i++)
706 if (xo() || yo()) return true;
710 public boolean register_relevant() {
711 if (!c_relevant()) return false;
712 if (f() && out_relevant()) return true;
713 if (f() && fb_relevant()) return true;
714 if (b() && xo()) return true;
715 if (b() && yo()) return true;
718 public boolean out_relevant() {
720 boolean connect = false;
721 for(int i=0; i<4; i++) {
726 if (out(L0+i)) out = true;
727 if (hx(L0+i)) connect = true;
728 if (vx(L0+i)) connect = true;
730 return out && connect;
733 public boolean fb_relevant() {
735 if (!(zi_to_xlut_relevant()) ||
736 !(zi_to_ylut_relevant())) return false;
738 case 0x34: return true;
739 case 0x14: return true;
740 case 0x10: return true;
741 case 0x30: return true;
751 public IOB iob_bot(int col, boolean primary) { return new IOB(col, 0, primary, true); }
752 public IOB iob_top(int col, boolean primary) { return new IOB(col, 1, primary, true); }
753 public IOB iob_left(int row, boolean primary) { return new IOB(0, row, primary, false); }
754 public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
756 public IOB fromPin(int pin) {
757 if (pin >= 4 && pin <= 11) return io(pin-3);
758 if (pin >= 15 && pin <= 18) return io(pin-2);
759 if (pin >= 19 && pin <= 24) return io(pin);
760 if (pin >= 27 && pin <= 30) return io(pin-2);
761 if (pin >= 33 && pin <= 36) return io(pin);
762 if (pin >= 38 && pin <= 47) return io(pin+1);
765 if (pin >= 33 && pin <= 36) return io(pin+36);
766 if (pin >= 38 && pin <= 41) return io(pin+43);
767 if (pin >= 42 && pin <= 43) return io(pin+47);
768 if (pin >= 44 && pin <= 47) return io(pin+49);
769 if (pin >= 57 && pin <= 62) return io(pin+40);
770 if (pin >= 63 && pin <= 66) return io(pin+46);
771 if (pin >= 68 && pin <= 71) return io(pin+53);
772 if (pin >= 72 && pin <= 73) return io(pin+53);
773 if (pin >= 74 && pin <= 75) return io(pin+63);
774 if (pin >= 76 && pin <= 77) return io(143+(pin-76));
775 if (pin >= 80 && pin <= 81) return io(145+(pin-80));
776 if (pin >= 82 && pin <= 85) return io(151+(pin-82));
777 if (pin >= 86 && pin <= 89) return io(165+(pin-86));
778 if (pin >= 91 && pin <= 94) return io(177+(pin-91));
779 if (pin >= 95 && pin <= 96) return io(183+(pin-95));
780 if (pin >= 97 && pin <= 100) return io(189+(pin-97));
781 if (pin >= 161 && pin <= 164) return io(289+(pin-161));
782 if (pin >= 165 && pin <= 166) return io(297+(pin-165));
783 if (pin >= 167 && pin <= 168) return io(303+(pin-167));
784 if (pin >= 169 && pin <= 170) return io(309+(pin-169));
785 if (pin >= 172 && pin <= 173) return io(313+(pin-172));
786 if (pin >= 174 && pin <= 175) return io(325+(pin-174));
787 if (pin >= 176 && pin <= 179) return io(327+(pin-176));
788 if (pin >= 180 && pin <= 181) return io(335+(pin-180));
789 if (pin >= 184 && pin <= 185) return io(337+(pin-184));
790 if (pin >= 186 && pin <= 191) return io(343+(pin-186));
791 if (pin >= 192 && pin <= 193) return io(359+(pin-192));
792 if (pin >= 195 && pin <= 196) return io(363+(pin-195));
793 if (pin >= 197 && pin <= 200) return io(369+(pin-197));
794 if (pin >= 201 && pin <= 204) return io(381+(pin-201));
796 public io(int ionum) {
798 int cell = (94 - pin) / 2;
799 boolean primary = cell * 2 == (94-pin);
803 public final class IOB {
804 public final int col;
805 public final int row;
806 public final boolean primary;
807 public final boolean northsouth;
808 public IOB(int col, int row, boolean primary, boolean northsouth) {
811 this.northsouth = northsouth;
812 this.primary = primary;
815 public String dump() {
816 System.out.println("[ "+
817 (schmitt()?"schmitt ":"")+
818 (slew()==3?"fast ":slew()==2?"med ":slew()==1?"slow ":"slew-unknown ")+
824 public void enableOutput(int direction) {
831 // note: east-side IOBs should have slew=med, others slew=fast
832 slew((!northsouth && col==1) ? MEDIUM : FAST);
834 public void enableInput() {
839 public void useoem(boolean use) { mode4(z(3), row, col, 6, use); }
840 public boolean useoem() { return (mode4(z(3), row, col) & (1<<6))!=0; }
841 public void schmitt(boolean use) { mode4(z(0), row, col, 7, use); }
842 public boolean schmitt() { return (mode4(z(0), row, col) & (1<<7))!=0; }
844 public void slew(int slew) {
846 case FAST: mode4(z(0), row, col, 3<<5, 0x60); return;
847 case MEDIUM: mode4(z(0), row, col, 2<<5, 0x60); return;
848 case SLOW: mode4(z(0), row, col, 1<<5, 0x60); return;
849 default: throw new Error();
853 public void oem(int source) {
855 case ALWAYS_ON: mode4(z(3), row, col, 1<<5, 0x3f); return;
856 case ALWAYS_OFF: mode4(z(3), row, col, 0, 0x3f); return;
857 default: throw new Error();
861 private int z(int code) { return (northsouth ? 0x70 : 0x60) | (primary ? 0x00 : 0x04) | (code & 0x7); }
862 public void pullup() { mode4(z(0), row, col, 0x00<<1, 0x06); }
863 public void pulldown() { mode4(z(0), row, col, 0x03<<1, 0x06); }
864 public void pullnone() { mode4(z(0), row, col, 0x01<<1, 0x06); }
865 public void oe(boolean oe) {
866 int old = mode4(z(1), row, col) & (~(1<<5));
867 old |= oe ? 0 : (1<<5);
868 mode4(z(1), row, col, old & 0xff);
871 public void output(int which) {
874 mode4(z(1), row, col, 0, 0x1f); return;
875 case WEST: case EAST: case NORTH: case SOUTH:
876 mode4(z(1), row, col, 1<<0, 0x1f); return;
877 case NW: case SW: case NE: case SE:
878 mode4(z(1), row, col, 1<<1, 0x1f); return;
879 default: throw new Error();