1 package com.atmel.fpslic;
5 import org.ibex.util.Log;
6 import edu.berkeley.abits.*;
7 import static com.atmel.fpslic.FpslicConstants.*;
9 public abstract class FpslicDevice implements Device {
11 private final int width;
12 private final int height;
14 public FpslicDevice(int width, int height) {
19 public int getWidth() { return width; }
20 public int getHeight() { return height; }
22 public abstract void flush();
23 public abstract void mode4(int z, int y, int x, int d);
24 public abstract byte mode4(int z, int y, int x);
26 public byte mode4zyx(int zyx) { return mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff); }
27 public void mode4zyx(int zyx, int d, int invmask) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, d, invmask); }
28 public void mode4(int z, int y, int x, int d, int invmask) {
29 int old = mode4(z, y, x);
34 public void mode4zyx(int zyx, int bit, boolean set) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, bit, set); }
35 public void mode4(int z, int y, int x, int bit, boolean set) {
36 int old = mode4(z, y, x);
38 old |= set ? (1 << bit) : 0;
42 // Inner Classes ///////////////////////////////////////////////////////////////////////////////
44 public final class Sector {
47 public Sector(Cell c) { this((c.col/4)*4, (c.row/4)*4); }
48 private Sector(int col, int row) {
49 if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
50 if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
54 public Sector north() { return row+4>=getHeight() ? null : new Sector(col, row+4); }
55 public Sector south() { return row==0 ? null : new Sector(col, row-4); }
56 public Sector east() { return col+4>=getWidth() ? null : new Sector(col+4, row); }
57 public Sector west() { return col==0 ? null : new Sector(col-4, row); }
58 public Cell cell() { return FpslicDevice.this.cell(col, row); }
61 public final class SectorWire {
62 public final boolean global;
63 public final boolean horizontal;
64 public final int plane;
67 public SectorWire(boolean horizontal, int plane, int col, int row) {
68 this(horizontal, plane, col, row, false);
71 public void useColumnClock() {
72 // this is very poorly understood
73 if (horizontal) throw new RuntimeException();
74 mode4(0x25, (row>>2)+1, col, 0x80);
75 mode4(0x29, (row>>2), col, 0x40);
78 public SectorWire(boolean horizontal, int plane, int col, int row, boolean global) {
79 this.horizontal=horizontal;
82 this.col= horizontal ? (col & ~0x3) : col;
83 this.row=!horizontal ? (row & ~0x3) : row;
85 public SectorWire global() {
86 return new SectorWire(horizontal, plane, col, row, true);
88 public boolean isDriven() {
89 // FIXME: bridging connections (horiz-to-vert)
90 for(int i=0; i<4; i++)
91 if (cell(horizontal ? col+i : col,
92 horizontal ? row : row+i).out(plane)) return true;
93 // FIXME: sector switchbox drivers
96 private int z(int z) { return (horizontal ? 0x30 : 0x20) | z; }
97 public int code(boolean topleft) {
99 case 0: return z(8)+(topleft?0:1);
100 case 1: return z(6)+(topleft?0:1);
101 case 2: return z(2*(4-plane))+(topleft?0:1);
102 case 3: return z(2*(4-plane))+(topleft?0:1);
103 case 4: return z(2*(4-plane))+(topleft?0:1);
108 private final int fine() { return horizontal ? row : col; }
109 public final int coarse() { return horizontal ? col : row; }
110 private int _row() { return horizontal ? row : ((row)>>2); }
111 private int _col() { return horizontal ? ((col)>>2) : col; }
113 public SectorWire west() { return !horizontal ? null : col-4<0 ? null : new SectorWire(horizontal, plane, col-4, row); }
114 public SectorWire east() { return !horizontal ? null : col+4>=getWidth() ? null : new SectorWire(horizontal, plane, col+4, row); }
115 public SectorWire north() { return horizontal ? null : row+4>=getHeight() ? null : new SectorWire(horizontal, plane, col, row+4); }
116 public SectorWire south() { return horizontal ? null : row-4<0 ? null : new SectorWire(horizontal, plane, col, row-4); }
118 public String toString() {
120 (horizontal?(col+":"+(col+3)):(""+col))+","+
121 (horizontal?(row+"") :(row+":"+(row+3)))+
125 /** returns the ZYX0 coordinate of the byte controlling the switchbox that allows <tt>w</tt> to drive this wire */
126 public int switchbox(SectorWire w) {
127 if (w.horizontal==horizontal) {
128 if (w.plane!=plane) throw new Error();
129 if (Math.abs(w.coarse()-coarse())!=4) throw new Error(w.coarse() + " -- " + coarse());
130 boolean topleft = horizontal ? (w.coarse() < coarse()) : (w.coarse() > coarse());
131 int col = _col() + (( horizontal && !topleft) ? 1 : 0);
132 int row = _row() + ((!horizontal && topleft) ? 1 : 0);
133 return (code(topleft) << 24) | (row<<16) | (col<<8);
135 throw new Error("not implemented");
139 mode4zyx(switchbox(north()), (1<<6), (1<<6));
142 public void drives(SectorWire w, boolean enable) {
143 // FIXME: better error checks?
146 if (!global) val = 0x02;
150 if (w.global) { mask = mask << 3; val = val << 3; }
151 mode4zyx(switchbox(w), val, mask);
154 public boolean drives(SectorWire w) {
155 // FIXME: better error checks?
156 int connect = (mode4zyx(switchbox(w)) >> (global?3:0)) & 0x7;
157 return (connect & 0x2)!=0;
159 public SectorWire driverRight() {
160 //System.out.println("checking " + Integer.toString(code(true), 16) + " " +
161 //Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
162 int ret = mode4(z(code(true)), _row(), _col());
163 ret = (ret >> (global?3:0)) & 0x7;
166 case 1: return null; /* global wire on same side */
167 case 2: return new SectorWire(horizontal, plane, horizontal?(col+4):col, horizontal?row:(row+4));
168 case 4: return null; /* global wire on other side */
169 default: throw new Error("multiple drivers on " + this + "!");
173 public boolean touches(Cell c) {
176 ? (c.row==_row() && (_col()/4 == c.col/4))
177 : (c.col==_col() && (_row()/4 == c.row/4));
181 public final class SwitchBox {
182 public final boolean h;
183 public final int col;
184 public final int row;
185 public final int plane;
186 public SwitchBox(boolean h, int col, int row, int plane) { this.h = h; this.col = col; this.row = row; this.plane = plane; }
187 public SectorWire west(boolean global) { return !h ? null : global ? null : new SectorWire(h, col-4, row, plane); }
188 public SectorWire east(boolean global) { return !h ? null : global ? null : new SectorWire(h, col+4, row, plane); }
189 public SectorWire north(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row-4, plane); }
190 public SectorWire south(boolean global) { return !h ? null : global ? null : new SectorWire(h, col, row+4, plane); }
194 public Cell cell(int col, int row) {
195 if (col<0) return null;
196 if (row<0) return null;
197 if (col>=getWidth()) return null;
198 if (row>=getHeight()) return null;
199 return new Cell(col, row);
202 public final class Cell {
203 public final int col;
204 public final int row;
206 public void setColumnClock(int where) {
207 mode4(0x50, 0x00, col, where);
210 public String toString() { return "cell@("+col+","+row+")"; }
212 public Cell(int col, int row) {
217 public FpslicDevice fpslic() { return FpslicDevice.this; }
218 public int hashCode() { return col ^ row ^ FpslicDevice.this.hashCode(); }
219 public boolean equals(Object o) {
220 if (o==null || (!(o instanceof Cell))) return false;
222 return c.col == col && c.row == row && c.fpslic()==fpslic();
225 // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
227 public SectorWire hwire(int plane) { return new SectorWire(true, plane, col, row); }
228 public SectorWire vwire(int plane) { return new SectorWire(false, plane, col, row); }
229 public Cell east() { return cell(col+1, row); }
230 public Cell west() { return cell(col-1, row); }
231 public Cell north() { return cell(col, row+1); }
232 public Cell south() { return cell(col, row-1); }
233 public Cell ne() { return cell(col+1, row+1); }
234 public Cell nw() { return cell(col-1, row+1); }
235 public Cell se() { return cell(col+1, row-1); }
236 public Cell sw() { return cell(col-1, row-1); }
237 public Sector sector() { return new Sector(this); }
239 /* bit positions mean: [MSB] zxy z_y zx_ z__ _xy __y _x_ ___ [LSB] */
240 public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
241 public void xlut(int table) { mode4(7, row, col, (byte)(table & 0xff)); }
242 public byte xlut() { return (byte)(mode4(7, row, col) & 0xff); }
244 public String str(int x, String def) {
246 case NORTH: return "n";
247 case SOUTH: return "s";
248 case EAST: return "e";
249 case WEST: return "w";
250 case NW: return "nw";
251 case SE: return "se";
252 case NE: return "ne";
253 case SW: return "sw";
254 case FB: return "fb";
255 case L0: return (hx(0)&&vx(0))?"HV0":hx(0)?"H0":vx(0)?"V0":"L0";
256 case L1: return (hx(1)&&vx(1))?"HV1":hx(1)?"H1":vx(1)?"V1":"L1";
257 case L2: return (hx(2)&&vx(2))?"HV2":hx(2)?"H2":vx(2)?"V2":"L2";
258 case L3: return (hx(3)&&vx(3))?"HV3":hx(3)?"H3":vx(3)?"V3":"L3";
259 case L4: return (hx(4)&&vx(4))?"HV4":hx(4)?"H4":vx(4)?"V4":"L4";
264 /* bit positions mean: [MSB] zxy zx_ z_y z__ _xy _x_ __y ___ [LSB] */
265 public void ylut(int table) { mode4(6, row, col, (byte)(table & 0xff)); }
266 public byte ylut() { return (byte)(mode4(6, row, col) & 0xff); }
268 public void ff_reset_value(boolean value) {
269 //mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
272 public boolean ff_reset_value() { return false; }
273 public boolean columnClocked() {
277 public void out(int plane, boolean enable) {
279 case L0: mode4(0x00, row, col, 2, enable); return;
280 case L1: mode4(0x00, row, col, 3, enable); return;
281 case L2: mode4(0x00, row, col, 5, enable); return;
282 case L3: mode4(0x00, row, col, 4, enable); return;
283 case L4: mode4(0x00, row, col, 1, enable); return;
284 default: throw new RuntimeException("invalid argument");
288 public boolean out(int plane) {
290 case L0: return (mode4(0x00, row, col) & (1<<2)) != 0;
291 case L1: return (mode4(0x00, row, col) & (1<<3)) != 0;
292 case L2: return (mode4(0x00, row, col) & (1<<5)) != 0;
293 case L3: return (mode4(0x00, row, col) & (1<<4)) != 0;
294 case L4: return (mode4(0x00, row, col) & (1<<1)) != 0;
295 default: throw new RuntimeException("invalid argument");
299 public void h(int plane, boolean enable) {
301 case 0: mode4(0x08, row, col, 0, enable); return;
302 case 1: mode4(0x08, row, col, 2, enable); return;
303 case 2: mode4(0x08, row, col, 5, enable); return;
304 case 3: mode4(0x08, row, col, 6, enable); return;
305 case 4: mode4(0x00, row, col, 6, enable); return;
306 default: throw new RuntimeException("invalid argument");
310 public boolean hx(int plane) {
312 case 0: return (mode4(0x08, row, col) & (1<<0)) != 0;
313 case 1: return (mode4(0x08, row, col) & (1<<2)) != 0;
314 case 2: return (mode4(0x08, row, col) & (1<<5)) != 0;
315 case 3: return (mode4(0x08, row, col) & (1<<6)) != 0;
316 case 4: return (mode4(0x00, row, col) & (1<<6)) != 0;
317 default: throw new RuntimeException("invalid argument");
321 public void v(int plane, boolean enable) {
323 case 0: mode4(0x08, row, col, 1, enable); return;
324 case 1: mode4(0x08, row, col, 3, enable); return;
325 case 2: mode4(0x08, row, col, 4, enable); return;
326 case 3: mode4(0x08, row, col, 7, enable); return;
327 case 4: mode4(0x00, row, col, 7, enable); return;
328 default: throw new RuntimeException("invalid argument");
332 public boolean vx(int plane) {
334 case 0: return (mode4(0x08, row, col) & (1<<1)) != 0;
335 case 1: return (mode4(0x08, row, col) & (1<<3)) != 0;
336 case 2: return (mode4(0x08, row, col) & (1<<4)) != 0;
337 case 3: return (mode4(0x08, row, col) & (1<<7)) != 0;
338 case 4: return (mode4(0x00, row, col) & (1<<7)) != 0;
339 default: throw new RuntimeException("invalid argument");
344 public int ti_source() {
345 switch(mode4(1, row, col) & 0x30) {
346 case 0x20: return zi();
347 case 0x10: return FB;
348 case 0x00: return wi();
349 default: throw new Error("ack!");
354 switch(mode4(1, row, col) & 0x30) {
355 case 0x00: return TMUX_W;
356 case 0x10: return wi()==NONE ? TMUX_FB : TMUX_W_AND_FB;
357 case 0x20: return wi()==NONE ? TMUX_Z : TMUX_W_AND_Z;
358 case 0x30: throw new RuntimeException("illegal!");
359 default: return TMUX_W;
363 public void t(int code) {
366 case TMUX_W: result = 0x00; break;
367 case TMUX_Z: result = 0x20; break;
368 case TMUX_W_AND_Z: result = 0x20; break;
369 case TMUX_FB: result = 0x10; break;
370 case TMUX_W_AND_FB: result = 0x10; break;
371 default: result = 0x00; break;
373 mode4(1, row, col, result, 0x30);
376 private void fmux(int source) {
381 default: throw new Error("unknown argument to fmux()");
385 public boolean win_easable() {
390 return mode4(1, row, col) & 0x34;
393 public void t(boolean ignore_z_and_fb, boolean zm_drives_fb, boolean fb_drives_wm) {
394 // still not totally satisfied...
395 // need to find the bit that sets the w-mux off
396 // what does it mean for both bits (0x30) to be set to 1?
397 //if (fb && z) throw new RuntimeException("invalid combination");
405 // ttt => fb&w [0x34]
406 // ftt => fb&w [0x14]
407 // fft => fb&w [0x10]
411 // tft => fb&w [0x30]
412 if (ignore_z_and_fb) result |= 0x20;
413 if (zm_drives_fb) result |= 0x04;
414 if (fb_drives_wm) result |= 0x10;
415 mode4(1, row, col, result, 0x34);
419 public void c(int source) {
421 case XLUT: mode4(1, row, col, 0x00, 0xc0); break;
422 case YLUT: mode4(1, row, col, 0x40, 0xc0); break;
423 case ZMUX: mode4(1, row, col, 0x80, 0xc0); break;
424 default: throw new RuntimeException("Invalid Argument");
428 int cval = mode4(1, row, col) & 0xc0;
430 case 0x00: return XLUT;
431 case 0x40: return YLUT;
432 case 0x80: return ZMUX;
434 throw new Error("c() => " + cval);
436 public void b(boolean registered) { mode4(1, row, col, 3, !registered); }
437 public void f(boolean registered) { mode4(1, row, col, 2, !registered); }
438 public boolean xo() { return (mode4(1, row, col) & 0x01) != 0; }
439 public boolean yo() { return (mode4(1, row, col) & 0x02) != 0; }
440 public void xo(boolean center) { mode4(1, row, col, 0, center); }
441 public void yo(boolean center) { mode4(1, row, col, 1, center); }
443 public void xo(Cell c) {
444 if (c.row==row || c.col==col) { // use the y-input
454 public void yo(Cell c) {
455 if (!(c.row==row || c.col==col)) { // use the x-input
465 public boolean b() { return (mode4(1, row, col) & (1 << 3)) == 0; }
466 public boolean f() { return (mode4(1, row, col) & (1 << 2)) == 0; }
467 public boolean x() { return (mode4(1, row, col) & (1 << 1)) != 0; }
468 public boolean y() { return (mode4(1, row, col) & (1 << 0)) != 0; }
471 switch (mode4(0x02, row, col) & 0x3) {
475 default: throw new RuntimeException("invalid argument");
478 public void oe(int source) {
480 case NONE: mode4(0x02, row, col, 0, 0x3); break;
481 case H4: mode4(0x02, row, col, 1, 0x3); break;
482 case V4: mode4(0x02, row, col, 2, 0x3); break;
483 default: throw new RuntimeException("invalid argument");
488 // FIXME: can be multiple
489 if ((mode4(0x03, row, col) & (1<<4))!=0) return L4;
490 switch(mode4(0x05, row, col) & 0xff) {
491 case 0x80: return SW;
492 case (1<<6): return NE;
493 case (1<<5): return SE;
494 case (1<<4): return NW;
495 case (1<<3): return L0;
496 case (1<<2): return L1;
497 case (1<<1): return L2;
498 case (1<<0): return L3;
500 default: throw new Error();
504 public void xi(int source) {
506 case SW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<7); break;
507 case NE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<6); break;
508 case SE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<5); break;
509 case NW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<4); break;
511 case L0: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<3); break;
512 case L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
513 case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
514 case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
515 case L4: mode4(0x03, row, col, 4, true); mode4(0x05, row, col, 0); break;
517 case NONE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 0); break;
518 default: throw new RuntimeException("invalid argument");
522 public void xi(SectorWire sw) {
523 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
527 public void xi(Cell c) {
528 if (c.row==row-1 && c.col==col-1) xi(SW);
529 else if (c.row==row+1 && c.col==col-1) xi(NW);
530 else if (c.row==row-1 && c.col==col+1) xi(SE);
531 else if (c.row==row+1 && c.col==col+1) xi(NE);
532 else throw new RuntimeException("invalid argument");
536 if ((mode4(0x02, row, col) & (1<<6))!=0) return L4;
537 switch(mode4(0x04, row, col) & 0xff) {
538 case (1<<7): return NORTH;
539 case (1<<5): return SOUTH;
540 case (1<<6): return WEST;
541 case (1<<4): return EAST;
542 case (1<<3): return L0;
543 case (1<<2): return L1;
544 case (1<<1): return L2;
545 case (1<<0): return L3;
547 default: throw new Error();
551 public void yi(int source) {
553 case NORTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<7); break;
554 case SOUTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<5); break;
555 case WEST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<6); break;
556 case EAST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<4); break;
557 case L4: mode4(0x02, row, col, 6, true); mode4(0x04, row, col, 0); break;
558 case L3: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<0); break;
559 case L2: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<1); break;
560 case L1: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<2); break;
561 case L0: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<3); break;
562 case NONE: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 0); break;
563 default: throw new RuntimeException("invalid argument");
567 public void yi(SectorWire sw) {
568 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
572 public void yi(Cell c) {
573 if (c.row==row-1 && c.col==col) yi(SOUTH);
574 else if (c.row==row+1 && c.col==col) yi(NORTH);
575 else if (c.row==row && c.col==col-1) yi(WEST);
576 else if (c.row==row && c.col==col+1) yi(EAST);
577 else throw new RuntimeException("invalid argument");
580 public void wi(SectorWire sw) {
581 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
585 public void wi(int source) {
587 case L4: mode4(0x03, row, col, 1<<5, 0xEC); break;
588 case L3: mode4(0x03, row, col, 1<<6, 0xEC); break;
589 case L2: mode4(0x03, row, col, 1<<7, 0xEC); break;
590 case L1: mode4(0x03, row, col, 1<<3, 0xEC); break;
591 case L0: mode4(0x03, row, col, 1<<2, 0xEC); break;
592 case NONE: mode4(0x03, row, col, 0, 0xEC); break;
593 default: throw new RuntimeException("invalid argument");
598 int who = mode4(0x03, row, col) & 0xEC;
600 case (1<<5): return L4;
601 case (1<<6): return L3;
602 case (1<<7): return L2;
603 case (1<<3): return L1;
604 case (1<<2): return L0;
605 case (1<<0): return NONE; /* huh? */
606 case (0): return NONE;
607 default: throw new RuntimeException("invalid argument: " + who);
612 public void zi(SectorWire sw) {
613 if (!sw.touches(this)) throw new RuntimeException("invalid argument");
617 public void zi(int source) {
619 case L4: mode4(0x02, row, col, 1<<7, 0xDB); break;
620 case L3: mode4(0x02, row, col, 1<<5, 0xDB); break;
621 case L2: mode4(0x02, row, col, 1<<4, 0xDB); break;
622 case L1: mode4(0x02, row, col, 1<<3, 0xDB); break;
623 case L0: mode4(0x02, row, col, 1<<2, 0xDB); break;
624 case NONE: mode4(0x02, row, col, 0, 0xDB); break;
625 default: throw new RuntimeException("invalid argument");
630 switch(mode4(0x02, row, col) & 0x9B) {
631 case (1<<7): return L4;
632 case (1<<5): return L3;
633 case (1<<4): return L2;
634 case (1<<3): return L1;
635 case (1<<2): return L0;
636 case (1<<1): return NONE; /* huh? */
637 case (1<<0): return NONE; /* huh? */
639 default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0x9B));
643 public Cell dir(int i) {
645 case NORTH: return north();
646 case SOUTH: return south();
647 case EAST: return east();
648 case WEST: return west();
649 case NW: return nw();
650 case SW: return sw();
651 case SE: return se();
652 case NE: return ne();
656 public int dir(Cell c) {
657 if (c.row==row-1 && c.col==col-1) return SW;
658 else if (c.row==row+1 && c.col==col-1) return NW;
659 else if (c.row==row-1 && c.col==col+1) return SE;
660 else if (c.row==row+1 && c.col==col+1) return NE;
661 else if (c.row==row-1 && c.col==col) return SOUTH;
662 else if (c.row==row+1 && c.col==col) return NORTH;
663 else if (c.row==row && c.col==col-1) return WEST;
664 else if (c.row==row && c.col==col+1) return EAST;
668 public void generalized_c_element() {
671 ylut(LUT_SELF & (~LUT_OTHER));
672 xlut((~LUT_SELF) | LUT_OTHER);
682 ylut((LUT_SELF & ~LUT_OTHER) | (LUT_Z & ~LUT_OTHER) | (LUT_Z & LUT_SELF & LUT_OTHER));
687 t(false, false, true);
693 // Relevance //////////////////////////////////////////////////////////////////////////////
694 public boolean relevant() {
695 return xo_relevant() || yo_relevant() || out_relevant();
698 public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
699 public boolean xo_relevant(int direction) {
701 case NE: return ne() != null && ne().xi()==SW;
702 case NW: return nw() != null && nw().xi()==SE;
703 case SE: return se() != null && se().xi()==NW;
704 case SW: return sw() != null && sw().xi()==NE;
705 default: return false;
708 public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
709 public boolean yo_relevant(int direction) {
711 case NORTH: return north() != null && north().yi()==SOUTH /*&& north().yi_relevant()*/;
712 case EAST: return east() != null && east().yi()==WEST /*&& east().yi_relevant()*/;
713 case SOUTH: return south() != null && south().yi()==NORTH /*&& south().yi_relevant()*/;
714 case WEST: return west() != null && west().yi()==EAST /*&& west().yi_relevant()*/;
715 default: return false;
718 public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
719 public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
720 public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
721 public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
722 public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
723 public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
724 public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
725 public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
726 public boolean xlut_relevant() {
727 if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
728 if (xo()) return false;
729 return xo_relevant();
731 public boolean ylut_relevant() {
732 if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
733 if (yo()) return false;
734 return yo_relevant();
736 public boolean c_relevant() {
738 case 0x34: return true;
739 case 0x14: return true;
740 case 0x10: return true;
741 case 0x30: return true;
743 for(int i=0; i<5; i++)
746 if (xo() || yo()) return true;
750 public boolean register_relevant() {
751 if (!c_relevant()) return false;
752 if (f() && out_relevant()) return true;
753 if (f() && fb_relevant()) return true;
754 if (b() && xo()) return true;
755 if (b() && yo()) return true;
758 public boolean out_relevant() {
760 boolean connect = false;
761 for(int i=0; i<4; i++) {
766 if (out(L0+i)) out = true;
767 if (hx(L0+i)) connect = true;
768 if (vx(L0+i)) connect = true;
770 return out && connect;
773 public boolean fb_relevant() {
775 if (!(zi_to_xlut_relevant()) ||
776 !(zi_to_ylut_relevant())) return false;
778 case 0x34: return true;
779 case 0x14: return true;
780 case 0x10: return true;
781 case 0x30: return true;
791 public IOB iob_bot(int col, boolean primary) { return new IOB(col, 0, primary, true); }
792 public IOB iob_top(int col, boolean primary) { return new IOB(col, 1, primary, true); }
793 public IOB iob_left(int row, boolean primary) { return new IOB(0, row, primary, false); }
794 public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
796 public IOB fromPin(int pin) {
797 if (pin >= 4 && pin <= 11) return io(pin-3);
798 if (pin >= 15 && pin <= 18) return io(pin-2);
799 if (pin >= 19 && pin <= 24) return io(pin);
800 if (pin >= 27 && pin <= 30) return io(pin-2);
801 if (pin >= 33 && pin <= 36) return io(pin);
802 if (pin >= 38 && pin <= 47) return io(pin+1);
805 if (pin >= 33 && pin <= 36) return io(pin+36);
806 if (pin >= 38 && pin <= 41) return io(pin+43);
807 if (pin >= 42 && pin <= 43) return io(pin+47);
808 if (pin >= 44 && pin <= 47) return io(pin+49);
809 if (pin >= 57 && pin <= 62) return io(pin+40);
810 if (pin >= 63 && pin <= 66) return io(pin+46);
811 if (pin >= 68 && pin <= 71) return io(pin+53);
812 if (pin >= 72 && pin <= 73) return io(pin+53);
813 if (pin >= 74 && pin <= 75) return io(pin+63);
814 if (pin >= 76 && pin <= 77) return io(143+(pin-76));
815 if (pin >= 80 && pin <= 81) return io(145+(pin-80));
816 if (pin >= 82 && pin <= 85) return io(151+(pin-82));
817 if (pin >= 86 && pin <= 89) return io(165+(pin-86));
818 if (pin >= 91 && pin <= 94) return io(177+(pin-91));
819 if (pin >= 95 && pin <= 96) return io(183+(pin-95));
820 if (pin >= 97 && pin <= 100) return io(189+(pin-97));
821 if (pin >= 161 && pin <= 164) return io(289+(pin-161));
822 if (pin >= 165 && pin <= 166) return io(297+(pin-165));
823 if (pin >= 167 && pin <= 168) return io(303+(pin-167));
824 if (pin >= 169 && pin <= 170) return io(309+(pin-169));
825 if (pin >= 172 && pin <= 173) return io(313+(pin-172));
826 if (pin >= 174 && pin <= 175) return io(325+(pin-174));
827 if (pin >= 176 && pin <= 179) return io(327+(pin-176));
828 if (pin >= 180 && pin <= 181) return io(335+(pin-180));
829 if (pin >= 184 && pin <= 185) return io(337+(pin-184));
830 if (pin >= 186 && pin <= 191) return io(343+(pin-186));
831 if (pin >= 192 && pin <= 193) return io(359+(pin-192));
832 if (pin >= 195 && pin <= 196) return io(363+(pin-195));
833 if (pin >= 197 && pin <= 200) return io(369+(pin-197));
834 if (pin >= 201 && pin <= 204) return io(381+(pin-201));
836 public io(int ionum) {
838 int cell = (94 - pin) / 2;
839 boolean primary = cell * 2 == (94-pin);
843 public final class IOB {
844 public final int col;
845 public final int row;
846 public final boolean primary;
847 public final boolean northsouth;
848 public IOB(int col, int row, boolean primary, boolean northsouth) {
851 this.northsouth = northsouth;
852 this.primary = primary;
855 public String dump() {
856 System.out.println("[ "+
857 (schmitt()?"schmitt ":"")+
858 (slew()==3?"fast ":slew()==2?"med ":slew()==1?"slow ":"slew-unknown ")+
864 public void enableOutput(int direction) {
871 // note: east-side IOBs should have slew=med, others slew=fast
872 slew((!northsouth && col==1) ? MEDIUM : FAST);
874 public void enableInput() {
879 public void useoem(boolean use) { mode4(z(3), row, col, 6, use); }
880 public boolean useoem() { return (mode4(z(3), row, col) & (1<<6))!=0; }
881 public void schmitt(boolean use) { mode4(z(0), row, col, 7, use); }
882 public boolean schmitt() { return (mode4(z(0), row, col) & (1<<7))!=0; }
884 public void slew(int slew) {
886 case FAST: mode4(z(0), row, col, 3<<5, 0x60); return;
887 case MEDIUM: mode4(z(0), row, col, 2<<5, 0x60); return;
888 case SLOW: mode4(z(0), row, col, 1<<5, 0x60); return;
889 default: throw new Error();
893 public void oem(int source) {
895 case ALWAYS_ON: mode4(z(3), row, col, 1<<5, 0x3f); return;
896 case ALWAYS_OFF: mode4(z(3), row, col, 0, 0x3f); return;
897 default: throw new Error();
901 private int z(int code) { return (northsouth ? 0x70 : 0x60) | (primary ? 0x00 : 0x04) | (code & 0x7); }
902 public void pullup() { mode4(z(0), row, col, 0x00<<1, 0x06); }
903 public void pulldown() { mode4(z(0), row, col, 0x03<<1, 0x06); }
904 public void pullnone() { mode4(z(0), row, col, 0x01<<1, 0x06); }
905 public void oe(boolean oe) {
906 int old = mode4(z(1), row, col) & (~(1<<5));
907 old |= oe ? 0 : (1<<5);
908 mode4(z(1), row, col, old & 0xff);
911 public void output(int which) {
914 mode4(z(1), row, col, 0, 0x1f); return;
915 case WEST: case EAST: case NORTH: case SOUTH:
916 mode4(z(1), row, col, 1<<0, 0x1f); return;
917 case NW: case SW: case NE: case SE:
918 mode4(z(1), row, col, 1<<1, 0x1f); return;
919 default: throw new Error();
925 public void readMode4(Reader r) throws IOException {
927 BufferedReader br = new BufferedReader(r);
928 for(String str = br.readLine(); str != null; str = br.readLine()) {
929 long foo = Long.parseLong(str, 16);
930 mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >> 8), (int)(foo >> 0));
937 public void writeMode4(Writer w) throws IOException {
938 // FIXME: there are resources "above" the top of the device
939 for(int x=0; x<getWidth(); x++)
940 for(int y=0; y<getWidth(); y++)
941 for(int z=0; z<255; z++) {
942 if ((z > 0x09 && z < 0x10) ||
943 (z > 0x11 && z < 0x20) ||
944 (z > 0x29 && z < 0x30) ||
945 (z > 0x39 && z < 0x40) ||
946 (z > 0x41 && z < 0x60) ||
947 (z > 0x67 && z < 0x70) ||
948 (z > 0x77 && z < 0xD0) ||
954 w.write(hex2(mode4(z, y, x) & 0xff));
960 private static String hex2(int i) {
961 String ret = Integer.toString(i, 16);
962 while(ret.length() < 2) ret = "0"+ret;
963 return ret.toUpperCase();