+ public void t(int code) {
+ int result = 0;
+ switch(code) {
+ case TMUX_Z: result = 0x20; break; // TOTALLYBOGUS throw new Error("not implemented, but should be possible");
+ case TMUX_W_AND_Z: result = 0x24; break;
+ case TMUX_FB: result = 0x34; break; /* I think this is actually W_AND_FB, sadly */
+ case TMUX_W_AND_FB: result = 0x14; break;
+ case TMUX_W: result = 0x00; break;
+ //default: throw new RuntimeException("unknown code! " + code);
+ default: result = 0x00; break;
+ }
+ mode4(1, row, col, result, 0x34);
+ }
+ /*
+ private void fmux(int source) {
+ switch(source) {
+ case ZMUX:
+ case FB:
+ case ALWAYS_ON:
+ default: throw new Error("unknown argument to fmux()");
+ }
+ }
+
+ public boolean win_easable() {
+ }
+ */
+
+ public int ti() {
+ return mode4(1, row, col) & 0x34;
+ }
+
+ public void t(boolean ignore_z_and_fb, boolean zm_drives_fb, boolean fb_drives_wm) {
+ // still not totally satisfied...
+ // need to find the bit that sets the w-mux off
+ // what does it mean for both bits (0x30) to be set to 1?
+ //if (fb && z) throw new RuntimeException("invalid combination");
+ int result = 0;
+ // ZM->FB = 0x04
+ // FB->WM = 0x10
+ // WZ->WM = 0x20
+
+ // tff => w&z [0x20]
+ // fff => w [0x00]
+ // ttt => fb&w [0x34]
+ // ftt => fb&w [0x14]
+ // fft => fb&w [0x10]
+
+ // ttf => w&z [0x24]
+ // ftf => w [0x04]
+ // tft => fb&w [0x30]
+ if (ignore_z_and_fb) result |= 0x20;
+ if (zm_drives_fb) result |= 0x04;
+ if (fb_drives_wm) result |= 0x10;
+ mode4(1, row, col, result, 0x34);
+ }
+
+
+ public void c(int source) {
+ switch(source) {
+ case XLUT: mode4(1, row, col, 0x00, 0xc0); break;
+ case YLUT: mode4(1, row, col, 0x40, 0xc0); break;
+ case ZMUX: mode4(1, row, col, 0x80, 0xc0); break;
+ default: throw new RuntimeException("Invalid Argument");
+ }
+ }
+ public int c() {
+ int cval = mode4(1, row, col) & 0xc0;
+ switch (cval) {
+ case 0x00: return XLUT;
+ case 0x40: return YLUT;
+ case 0x80: return ZMUX;
+ }
+ throw new Error("c() => " + cval);
+ }
+ public void b(boolean registered) { mode4(1, row, col, 3, !registered); }
+ public void f(boolean registered) { mode4(1, row, col, 2, !registered); }
+ public boolean xo() { return (mode4(1, row, col) & 0x01) != 0; }
+ public boolean yo() { return (mode4(1, row, col) & 0x02) != 0; }
+ public void xo(boolean center) { mode4(1, row, col, 0, center); }
+ public void yo(boolean center) { mode4(1, row, col, 1, center); }
+
+ public void xo(Cell c) {
+ if (c.row==row || c.col==col) { // use the y-input
+ xlut(LUT_OTHER);
+ yi(c);
+ } else {
+ xlut(LUT_SELF);
+ xi(c);
+ }
+ xo(false);
+ }
+
+ public void yo(Cell c) {
+ if (!(c.row==row || c.col==col)) { // use the x-input
+ ylut(LUT_OTHER);
+ xi(c);
+ } else {
+ ylut(LUT_SELF);
+ yi(c);
+ }
+ yo(false);
+ }
+
+ public boolean b() { return (mode4(1, row, col) & (1 << 3)) == 0; }
+ public boolean f() { return (mode4(1, row, col) & (1 << 2)) == 0; }
+ public boolean x() { return (mode4(1, row, col) & (1 << 1)) != 0; }
+ public boolean y() { return (mode4(1, row, col) & (1 << 0)) != 0; }
+
+ public int oe() {
+ switch (mode4(0x02, row, col) & 0x3) {
+ case 0: return NONE;
+ case 1: return H4;
+ case 2: return V4;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+ public void oe(int source) {
+ switch(source) {
+ case NONE: mode4(0x02, row, col, 0, 0x3); break;
+ case H4: mode4(0x02, row, col, 1, 0x3); break;
+ case V4: mode4(0x02, row, col, 2, 0x3); break;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+
+ public int xi() {
+ // FIXME: can be multiple
+ if ((mode4(0x03, row, col) & (1<<4))!=0) return L4;
+ switch(mode4(0x05, row, col) & 0xff) {
+ case 0x80: return SW;
+ case (1<<6): return NE;
+ case (1<<5): return SE;
+ case (1<<4): return NW;
+ case (1<<3): return L0;
+ case (1<<2): return L1;
+ case (1<<1): return L2;
+ case (1<<0): return L3;
+ case 0: return NONE;
+ default: throw new Error();
+ }
+ }
+
+ public void xi(int source) {
+ switch(source) {
+ case SW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<7); break;
+ case NE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<6); break;
+ case SE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<5); break;
+ case NW: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<4); break;
+ case L4: mode4(0x03, row, col, 4, true); mode4(0x05, row, col, 0); break;
+ case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
+ case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
+ case L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
+ case L0: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<3); break;
+ case NONE: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 0); break;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+
+ public void xi(SectorWire sw) {
+ if (!sw.touches(this)) throw new RuntimeException("invalid argument");
+ xi(sw.plane);
+ }
+
+ public void xi(Cell c) {
+ if (c.row==row-1 && c.col==col-1) xi(SW);
+ else if (c.row==row+1 && c.col==col-1) xi(NW);
+ else if (c.row==row-1 && c.col==col+1) xi(SE);
+ else if (c.row==row+1 && c.col==col+1) xi(NE);
+ else throw new RuntimeException("invalid argument");
+ }
+
+ public int yi() {
+ if ((mode4(0x02, row, col) & (1<<6))!=0) return L4;
+ switch(mode4(0x04, row, col) & 0xff) {
+ case 0x80: return NORTH;
+ case (1<<5): return SOUTH;
+ case (1<<6): return WEST;
+ case (1<<4): return EAST;
+ case (1<<3): return L0;
+ case (1<<2): return L1;
+ case (1<<1): return L2;
+ case (1<<0): return L3;
+ case 0: return NONE;
+ default: throw new Error();
+ }
+ }
+
+ public void yi(int source) {
+ switch(source) {
+ case NORTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<7); break;
+ case SOUTH: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<5); break;
+ case WEST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<6); break;
+ case EAST: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<4); break;
+ case L4: mode4(0x02, row, col, 6, true); mode4(0x04, row, col, 0); break;
+ case L3: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<0); break;
+ case L2: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<1); break;
+ case L1: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<2); break;
+ case L0: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 1<<3); break;
+ case NONE: mode4(0x02, row, col, 6, false); mode4(0x04, row, col, 0); break;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+
+ public void yi(SectorWire sw) {
+ if (!sw.touches(this)) throw new RuntimeException("invalid argument");
+ yi(sw.plane);
+ }
+
+ public void yi(Cell c) {
+ if (c.row==row-1 && c.col==col) yi(SOUTH);
+ else if (c.row==row+1 && c.col==col) yi(NORTH);
+ else if (c.row==row && c.col==col-1) yi(WEST);
+ else if (c.row==row && c.col==col+1) yi(EAST);
+ else throw new RuntimeException("invalid argument");
+ }
+
+ public void wi(SectorWire sw) {
+ if (!sw.touches(this)) throw new RuntimeException("invalid argument");
+ wi(sw.plane);
+ }
+
+ public void wi(int source) {
+ switch(source) {
+ case L4: mode4(0x03, row, col, 1<<5, 0xEC); break;
+ case L3: mode4(0x03, row, col, 1<<6, 0xEC); break;
+ case L2: mode4(0x03, row, col, 1<<7, 0xEC); break;
+ case L1: mode4(0x03, row, col, 1<<3, 0xEC); break;
+ case L0: mode4(0x03, row, col, 1<<2, 0xEC); break;
+ case NONE: mode4(0x03, row, col, 0, 0xEC); break;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+
+ public int wi() {
+ int who = mode4(0x03, row, col) & 0xEC;
+ switch(who) {
+ case (1<<5): return L4;
+ case (1<<6): return L3;
+ case (1<<7): return L2;
+ case (1<<3): return L1;
+ case (1<<2): return L0;
+ case (1<<0): return NONE; /* huh? */
+ case (0): return NONE;
+ default: throw new RuntimeException("invalid argument: " + who);
+ }
+ }
+
+
+ public void zi(SectorWire sw) {
+ if (!sw.touches(this)) throw new RuntimeException("invalid argument");
+ zi(sw.plane);
+ }
+
+ public void zi(int source) {
+ switch(source) {
+ case L4: mode4(0x02, row, col, 1<<7, 0xDB); break;
+ case L3: mode4(0x02, row, col, 1<<5, 0xDB); break;
+ case L2: mode4(0x02, row, col, 1<<4, 0xDB); break;
+ case L1: mode4(0x02, row, col, 1<<3, 0xDB); break;
+ case L0: mode4(0x02, row, col, 1<<2, 0xDB); break;
+ case NONE: mode4(0x02, row, col, 0, 0xDB); break;
+ default: throw new RuntimeException("invalid argument");
+ }
+ }
+
+ public int zi() {
+ switch(mode4(0x02, row, col) & 0xDB) {
+ case (1<<7): return L4;
+ case (1<<5): return L3;
+ case (1<<4): return L2;
+ case (1<<3): return L1;
+ case (1<<2): return L0;
+ case (1<<1): return NONE; /* huh? */
+ case (1<<0): return NONE; /* huh? */
+ case 0: return NONE;
+ default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0xDB));
+ }
+ }
+
+
+ // Relevance //////////////////////////////////////////////////////////////////////////////
+ public boolean relevant() {
+ return xo_relevant() || yo_relevant() || out_relevant();
+ }
+
+ public boolean xo_relevant() { return xo_relevant(NE) || xo_relevant(SE) || xo_relevant(NW) || xo_relevant(SW); }
+ public boolean xo_relevant(int direction) {
+ switch(direction) {
+ case NE: return ne() != null && ne().xi()==SW;
+ case NW: return nw() != null && nw().xi()==SE;
+ case SE: return se() != null && se().xi()==NW;
+ case SW: return sw() != null && sw().xi()==NE;
+ default: return false;
+ }
+ }
+ public boolean yo_relevant() { return yo_relevant(NORTH) || yo_relevant(SOUTH) || yo_relevant(EAST) || yo_relevant(WEST); }
+ public boolean yo_relevant(int direction) {
+ switch(direction) {
+ case NORTH: return north() != null && north().yi()==SOUTH /*&& north().yi_relevant()*/;
+ case EAST: return east() != null && east().yi()==WEST /*&& east().yi_relevant()*/;
+ case SOUTH: return south() != null && south().yi()==NORTH /*&& south().yi_relevant()*/;
+ case WEST: return west() != null && west().yi()==EAST /*&& west().yi_relevant()*/;
+ default: return false;
+ }
+ }
+ public boolean xi_relevant() { return xi_to_xlut_relevant() || xi_to_ylut_relevant(); }
+ public boolean yi_relevant() { return yi_to_xlut_relevant() || yi_to_ylut_relevant(); }
+ public boolean xi_to_ylut_relevant() { return (((ylut() & 0xcc) >> 2) != (ylut() & 0x33)); }
+ public boolean yi_to_xlut_relevant() { return (((xlut() & 0xcc) >> 2) != (xlut() & 0x33)); }
+ public boolean zi_to_xlut_relevant() { return (((xlut() & LUT_Z) >> 4) != (xlut() & LUT_Z)); }
+ public boolean zi_to_ylut_relevant() { return (((ylut() & LUT_Z) >> 4) != (ylut() & LUT_Z)); }
+ public boolean xi_to_xlut_relevant() { return (((xlut() & LUT_SELF) >> 1) != (xlut() & (LUT_SELF >> 1))); }
+ public boolean yi_to_ylut_relevant() { return (((ylut() & LUT_SELF) >> 1) != (ylut() & (LUT_SELF >> 1))); }
+ public boolean xlut_relevant() {
+ if ((c()==XLUT || c()==ZMUX) && c_relevant()) return true;
+ if (xo()) return false;
+ return xo_relevant();
+ }
+ public boolean ylut_relevant() {
+ if ((c()==YLUT || c()==ZMUX) && c_relevant()) return true;
+ if (yo()) return false;
+ return yo_relevant();
+ }
+ public boolean c_relevant() {
+ switch(ti()) {
+ case 0x34: return true;
+ case 0x14: return true;
+ case 0x10: return true;
+ case 0x30: return true;
+ }
+ for(int i=0; i<5; i++)
+ if (out(i))
+ return true;
+ if (xo() || yo()) return true;
+ return false;
+ }
+
+ public boolean register_relevant() {
+ if (!c_relevant()) return false;
+ if (f() && out_relevant()) return true;
+ if (f() && fb_relevant()) return true;
+ if (b() && xo()) return true;
+ if (b() && yo()) return true;
+ return false;
+ }
+ public boolean out_relevant() {
+ boolean out = false;
+ boolean connect = false;
+ for(int i=0; i<4; i++) {
+ if (out(L0+i)) out = true;
+ if (hx(L0+i)) connect = true;
+ if (vx(L0+i)) connect = true;
+ }
+ return out && connect;
+ }
+ public boolean fb_relevant() {
+ if (!(zi_to_xlut_relevant()) ||
+ !(zi_to_ylut_relevant())) return false;
+ switch(ti()) {
+ case 0x34: return true;
+ case 0x14: return true;
+ case 0x10: return true;
+ case 0x30: return true;
+ }
+ return false;