checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / At40k.java
index 6a98c5d..350f3fb 100644 (file)
@@ -1,5 +1,8 @@
 package edu.berkeley.obits.device.atmel;
-import static edu.berkeley.obits.device.atmel.AtmelDevice.Constants.*;
+
+import com.atmel.fpslic.*;
+import static com.atmel.fpslic.Fpslic.Constants.*;
+import static com.atmel.fpslic.Fpslic.Util.*;
 import edu.berkeley.obits.*;
 import org.ibex.util.*;
 import java.util.*;
@@ -7,18 +10,18 @@ import java.io.*;
 
 public class At40k {
 
-    /*private*/public final AtmelDevice dev;
+    /*private*/public final Fpslic dev;
     private final int width;
     private final int height;
 
-    public At40k(AtmelDevice dev, int width, int height) {
+    public At40k(Fpslic dev, int width, int height) {
         this.width = width;
         this.height = height;
         this.dev = dev;
     }
 
     public static class At40k10 extends At40k {
-        public At40k10(AtmelDevice dev) { super(dev, 24, 24); }
+        public At40k10(Fpslic dev) { super(dev, 24, 24); }
     }
 
     public final class Sector {
@@ -51,14 +54,22 @@ public class At40k {
             this.col= horizontal ? (col & ~0x3) : col;
             this.row=!horizontal ? (row & ~0x3) : row;
         }
+        public boolean isDriven() {
+            // FIXME: bridging connections (horiz-to-vert)
+            for(int i=0; i<4; i++)
+                if (cell(horizontal ? col+i : col,
+                         horizontal ? row   : row+i).out(plane)) return true;
+            // FIXME: sector switchbox drivers
+            return false;
+        }
         private int z(int z)       { return (horizontal ? 0x30 : 0x20) | z; }
-        public int code(boolean left) {
+        public int code(boolean topleft) {
             switch(plane) {
-                case 0: return z(6)+(left?0:1);
-                case 1: return z(8)+(left?0:1);
-                case 2: return z(2*(4-plane))+(left?0:1);
-                case 3: return z(2*(4-plane))+(left?0:1);
-                case 4: return z(2*(4-plane))+(left?0:1);
+                case 0: return z(6)+(topleft?0:1);
+                case 1: return z(8)+(topleft?0:1);
+                case 2: return z(2*(4-plane))+(topleft?0:1);
+                case 3: return z(2*(4-plane))+(topleft?0:1);
+                case 4: return z(2*(4-plane))+(topleft?0:1);
             }
             throw new Error();
         }
@@ -80,15 +91,14 @@ public class At40k {
                 "x"+plane;
         }
 
-        /** returns the ZYX0 coordinate of the byte controlling the switchbox that bridges this wire to <tt>w</tt> */
+        /** returns the ZYX0 coordinate of the byte controlling the switchbox that allows <tt>w</tt> to drive this wire */
         public int switchbox(SectorWire w) {
             if (w.horizontal==horizontal) {
                 if (w.plane!=plane) throw new Error();
                 if (Math.abs(w.coarse()-coarse())!=4) throw new Error(w.coarse() + " -- " + coarse());
-                if (w.coarse() < coarse()) return w.switchbox(this);
-                boolean topleft = w.coarse() < coarse();
+                boolean topleft = horizontal ? (w.coarse() < coarse()) : (w.coarse() > coarse());
                 int col = _col() + (( horizontal && !topleft) ? 1 : 0);
-                int row = _row() + ((!horizontal && !topleft) ? 1 : 0);
+                int row = _row() + ((!horizontal &&  topleft) ? 1 : 0);
                 return (code(topleft) << 24) | (row<<16) | (col<<8);
             }
             throw new Error("not implemented");
@@ -141,20 +151,31 @@ public class At40k {
         public final int col;
         public final int row;
 
-        public SectorWire hwire(int plane)  { return new SectorWire(true, plane, col, row); }
-        public SectorWire vwire(int plane)  { return new SectorWire(false, plane, col, row); }
-
-        public Sector sector() { return new Sector(this); }
         public Cell(int col, int row) {
             this.row = row;
             this.col = col;
         }
+        
+        // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
+
+        public SectorWire hwire(int plane)  { return new SectorWire(true, plane, col, row); }
+        public SectorWire vwire(int plane)  { return new SectorWire(false, plane, col, row); }
+        public Cell east() { return cell(col+1, row); }
+        public Cell west() { return cell(col-1, row); }
+        public Cell north() { return cell(col,   row+1); }
+        public Cell south() { return cell(col,   row-1); }
+        public Cell ne() { return cell(col+1, row+1); }
+        public Cell nw() { return cell(col-1, row+1); }
+        public Cell se() { return cell(col+1, row-1); }
+        public Cell sw() { return cell(col-1, row-1); }
+        public Sector sector() { return new Sector(this); }
 
         /* bit positions mean:  [MSB] zxy z_y zx_ z__ _xy __y _x_ ___ [LSB] */
+        public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
         public void xlut(int table)    { dev.mode4(7, row, col, (byte)(table & 0xff)); }
         public byte xlut()             { return (byte)(dev.mode4(7, row, col) & 0xff); }
-        public String printXLut()      { return printLut(xlut(), "y", "x", "t"); }
-        public String printXLutX()     { return printLut(xlut(), str(yi(), "y"), str(xi(), "x"), str(ti(), "t")); }
+        public String printXLut()      { return printLut(xlut(), "x", "y", "t"); }
+        public String printXLutX()     { return printLut(xlut(), str(xi(), "x"), str(yi(), "y"), str(ti_source(), "t")); }
 
         public String str(int x, String def) {
             switch(x) {
@@ -180,16 +201,21 @@ public class At40k {
         public void ylut(int table)    { dev.mode4(6, row, col, (byte)(table & 0xff)); }
         public byte ylut()             { return (byte)(dev.mode4(6, row, col) & 0xff); }
         public String printYLut()      { return printLut(ylut(), "y", "x", "t"); }
-        public String printYLutX()     { return printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti(), "t")); }
+        public String printYLutX()     { return printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti_source(), "t")) + Integer.toString(ylut() & 0xff, 16); }
 
         public void ff_reset_value(boolean value) {
             //dev.mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
         }
+        /** FIXME!!! */
+        public boolean ff_reset_value() { return false; }
+        public boolean columnClocked() {
+            return false;
+        }
 
         public void out(int plane, boolean enable) {
             switch(plane) {
-                case L0: dev.mode4(0x00, row, col, 3, enable); return;
-                case L1: dev.mode4(0x00, row, col, 2, enable); return;
+                case L0: dev.mode4(0x00, row, col, 2, enable); return;
+                case L1: dev.mode4(0x00, row, col, 3, enable); return;
                 case L2: dev.mode4(0x00, row, col, 5, enable); return;
                 case L3: dev.mode4(0x00, row, col, 4, enable); return;
                 case L4: dev.mode4(0x00, row, col, 1, enable); return;
@@ -199,8 +225,8 @@ public class At40k {
 
         public boolean out(int plane) {
             switch(plane) {
-                case L0: return (dev.mode4(0x00, row, col) & (1<<3)) != 0;
-                case L1: return (dev.mode4(0x00, row, col) & (1<<2)) != 0;
+                case L0: return (dev.mode4(0x00, row, col) & (1<<2)) != 0;
+                case L1: return (dev.mode4(0x00, row, col) & (1<<3)) != 0;
                 case L2: return (dev.mode4(0x00, row, col) & (1<<5)) != 0;
                 case L3: return (dev.mode4(0x00, row, col) & (1<<4)) != 0;
                 case L4: return (dev.mode4(0x00, row, col) & (1<<1)) != 0;
@@ -252,7 +278,8 @@ public class At40k {
             }
         }
         
-        public int ti() {
+
+        public int ti_source() {
             switch(dev.mode4(1, row, col) & 0x30) {
                 case 0x20: return zi();
                 case 0x10: return FB;
@@ -260,17 +287,77 @@ public class At40k {
                 default: throw new Error("ack!");
             }
         }
-        public void t(boolean z, boolean w, boolean fb) {
+
+        public int t() {
+            System.err.println("found " + (dev.mode4(1, row, col) & 0x34));
+            switch(dev.mode4(1, row, col) & 0x34) {
+                case 0x20: return TMUX_Z;
+                case 0x24: return TMUX_W_AND_Z;
+                case 0x34: return TMUX_FB;
+                case 0x14: return TMUX_W_AND_FB;
+                case 0x00: return TMUX_W;
+                    //default: throw new RuntimeException("unknown!");
+                default: return TMUX_W; 
+            }
+        }
+
+        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;
+            }
+            dev.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 dev.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...
-            //     how to we distinguish between z&w vs z or w&fb vs fb? ==> you can't!  this is a false connection in my diagram
+            //     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");
+            //if (fb && z) throw new RuntimeException("invalid combination");
             int result = 0;
-            if (z)  result |= 0x20;
-            if (fb) result |= 0x10;
-            dev.mode4(1, row, col, result, 0x30);
+            // 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;
+            dev.mode4(1, row, col, result, 0x34);
         }
 
+
         public void c(int source) {
             switch(source) {
                 case XLUT: dev.mode4(1, row, col, 0x00, 0xc0); break;
@@ -280,22 +367,33 @@ public class At40k {
             }
         }
         public int c() {
-            switch (dev.mode4(1, row, col) >> 6) {
+            int cval = dev.mode4(1, row, col) & 0xc0;
+            switch (cval) {
                 case 0x00: return XLUT;
-                case 0x01: return YLUT;
-                case 0x02: return ZMUX;
+                case 0x40: return YLUT;
+                case 0x80: return ZMUX;
             }
-            throw new Error();
+            throw new Error("c() => " + cval);
         }
         public void b(boolean registered) { dev.mode4(1, row, col, 3, !registered); }
         public void f(boolean registered) { dev.mode4(1, row, col, 2, !registered); }
-        public void xo(boolean center)    { dev.mode4(1, row, col, 1, center); }
-        public void yo(boolean center)    { dev.mode4(1, row, col, 0, center); }
-        public boolean b() { return (dev.mode4(1, row, col) >> 3)!=1; }
-        public boolean f() { return (dev.mode4(1, row, col) >> 2)!=1; }
-        public boolean x() { return (dev.mode4(1, row, col) >> 1)==1; }
-        public boolean y() { return (dev.mode4(1, row, col) >> 0)==1; }
-
+        public boolean xo()               { return (dev.mode4(1, row, col) & 0x01) != 0; }
+        public boolean yo()               { return (dev.mode4(1, row, col) & 0x02) != 0; }
+        public void xo(boolean center)    { dev.mode4(1, row, col, 0, center); }
+        public void yo(boolean center)    { dev.mode4(1, row, col, 1, center); }
+        public boolean b() { return (dev.mode4(1, row, col) & (1 << 3)) == 0; }
+        public boolean f() { return (dev.mode4(1, row, col) & (1 << 2)) == 0; }
+        public boolean x() { return (dev.mode4(1, row, col) & (1 << 1)) != 0; }
+        public boolean y() { return (dev.mode4(1, row, col) & (1 << 0)) != 0; }
+
+        public int oe() {
+            switch (dev.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: dev.mode4(0x02, row, col, 0, 0x3); break;
@@ -307,16 +405,16 @@ public class At40k {
 
         public int xi() {
             // FIXME: can be multiple
-            if ((dev.mode4(0x03, row, col) & (1<<4))!=0) return L3;
+            if ((dev.mode4(0x03, row, col) & (1<<4))!=0) return L4;
             switch(dev.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 L3;
-                case (1<<2): return L2;
-                case (1<<1): return L1;
-                case (1<<0): return L0;
+                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();
             }
@@ -333,6 +431,7 @@ public class At40k {
                 case L2: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<1); break;
                 case L1: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<2); break;
                 case L0: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<3); break;
+                case NONE: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 0); break;
                 default: throw new RuntimeException("invalid argument");
             }
         }
@@ -344,10 +443,10 @@ public class At40k {
                 case (1<<5): return SOUTH;
                 case (1<<6): return WEST;
                 case (1<<4): return EAST;
-                case (1<<3): return L3;
-                case (1<<2): return L2;
-                case (1<<1): return L1;
-                case (1<<0): return L0;
+                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();
             }
@@ -364,6 +463,7 @@ public class At40k {
                 case L2:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<1); break;
                 case L1:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<2); break;
                 case L0:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<3); break;
+                case NONE:  dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col,    0); break;
                 default: throw new RuntimeException("invalid argument");
             }
         }
@@ -375,22 +475,26 @@ public class At40k {
                 case L2:    dev.mode4(0x03, row, col, 1<<7, 0xEC); break;
                 case L1:    dev.mode4(0x03, row, col, 1<<3, 0xEC); break;
                 case L0:    dev.mode4(0x03, row, col, 1<<2, 0xEC); break;
+                case NONE:  dev.mode4(0x03, row, col,    0, 0xEC); break;
                 default: throw new RuntimeException("invalid argument");
             }
         }
 
         public int wi() {
-            switch(dev.mode4(0x03, row, col) & 0xff) {
+            int who = dev.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");
+                default: throw new RuntimeException("invalid argument: " + who);
             }
         }
 
+       
         public void zi(int source) {
             switch(source) {
                 case L4:    dev.mode4(0x02, row, col, 1<<7, 0xDB); break;
@@ -398,26 +502,115 @@ public class At40k {
                 case L2:    dev.mode4(0x02, row, col, 1<<4, 0xDB); break;
                 case L1:    dev.mode4(0x02, row, col, 1<<3, 0xDB); break;
                 case L0:    dev.mode4(0x02, row, col, 1<<2, 0xDB); break;
+                case NONE:  dev.mode4(0x02, row, col,    0, 0xDB); break;
                 default: throw new RuntimeException("invalid argument");
             }
         }
 
         public int zi() {
-            switch(dev.mode4(0x02, row, col) & 0xff) {
+            switch(dev.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");
+                default: throw new RuntimeException("invalid argument: zi=="+(dev.mode4(0x02, row, col) & 0xDB));
             }
         }
 
+
+        // Relevance //////////////////////////////////////////////////////////////////////////////
+
+        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 /*&& ne().xi_relevant()*/;
+                case NW: return nw() != null && nw().xi()==SE /*&& nw().xi_relevant()*/;
+                case SE: return se() != null && se().xi()==NW /*&& se().xi_relevant()*/;
+                case SW: return sw() != null && sw().xi()==NE /*&& sw().xi_relevant()*/;
+                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;
+        }
+
+
     }
 
-    public IOB iob_top(int col, boolean primary)   { return new IOB(col, 0, primary, true); }
-    public IOB iob_bot(int col, boolean primary)   { return new IOB(col, 1, primary, true); }
+    public IOB iob_bot(int col, boolean primary)   { return new IOB(col, 0, primary, true); }
+    public IOB iob_top(int col, boolean primary)   { return new IOB(col, 1, primary, true); }
     public IOB iob_left(int row, boolean primary)  { return new IOB(0, row, primary, false); }
     public IOB iob_right(int row, boolean primary) { return new IOB(1, row, primary, false); }
     /*