checkpoint
[slipway.git] / src / com / atmel / fpslic / Fpslic.java
index 6538a6a..5056ba2 100644 (file)
@@ -7,69 +7,17 @@ import static com.atmel.fpslic.FpslicConstants.*;
 
 public abstract class Fpslic {
 
-    public static class Util {
-        public static int lutSwap(int x) {
-            return
-                (x & 0x80)        |
-                ((x & 0x20) << 1) |
-                ((x & 0x40) >> 1) |
-                (x & 0x10) |
-                (x & 0x08)        |
-                ((x & 0x02) << 1) |
-                ((x & 0x04) >> 1) |
-                (x & 0x01);
-        }
-    }
-    
-    /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */
-    public int getWidth() { return 24; }
-    public int getHeight() { return 24; }
-
-    private static String hex2(int i) {
-        String ret = Integer.toString(i, 16);
-        while(ret.length() < 2) ret = "0"+ret;
-        return ret.toUpperCase();
-    }
+    public Fpslic(int width, int height) { this.width = width; this.height = height; }
 
-    public void readMode4(InputStream in) throws IOException {
-        int count = 0;
-        BufferedReader br = new BufferedReader(new InputStreamReader(in));
-        for(String str = br.readLine(); str != null; str = br.readLine()) {
-            long foo = Long.parseLong(str, 16);
-            mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
-            count++;
-        }
-        flush();
-        in.close();
-    }
+    private int width;
+    private int height;
+    public int getWidth() { return width; }
+    public int getHeight() { return height; }
 
     public abstract void flush();
-
-    public void writeMode4(Writer w) throws IOException {
-        for(int x=0; x<getWidth(); x++)
-            for(int y=0; y<getWidth(); y++)
-                for(int z=0; z<255; z++) {
-                    if ((z > 0x09 && z < 0x10) ||
-                        (z > 0x11 && z < 0x20) ||
-                        (z > 0x29 && z < 0x30) ||
-                        (z > 0x39 && z < 0x40) ||
-                        (z > 0x41 && z < 0x60) ||
-                        (z > 0x67 && z < 0x70) ||
-                        (z > 0x77 && z < 0xD0) ||
-                        (z > 0xD3))
-                        continue;
-                    w.write(hex2(z));
-                    w.write(hex2(y));
-                    w.write(hex2(x));
-                    w.write(hex2(mode4(z, y, x) & 0xff));
-                    w.write('\n');
-                }
-        w.flush();
-    }
-
-
     public abstract void mode4(int z, int y, int x, int d);
     public abstract byte mode4(int z, int y, int x);
+
     public          byte mode4zyx(int zyx) { return mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff); }
     public          void mode4zyx(int zyx, int d, int invmask) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, d, invmask); }
     public          void mode4(int z, int y, int x, int d, int invmask) {
@@ -86,9 +34,7 @@ public abstract class Fpslic {
         mode4(z, y, x, old);
     }
 
-    // Fpslic ///////////////////////////////////////////////////////////////////////////////
-
-
+    // Inner Classes ///////////////////////////////////////////////////////////////////////////////
 
     public final class Sector {
         public final int col;
@@ -104,7 +50,7 @@ public abstract class Fpslic {
         public Sector south() { return row==0 ?             null : new Sector(col, row-4); }
         public Sector east()  { return col+4>=getWidth() ?  null : new Sector(col+4, row); }
         public Sector west()  { return col==0 ?             null : new Sector(col-4, row); }
-        public Cell cell() { return Fpslic.this.cell(col, row); }
+        public Cell cell()    { return Fpslic.this.cell(col, row); }
     }
 
     public final class SectorWire {
@@ -179,7 +125,8 @@ public abstract class Fpslic {
             return (connect & 0x2)!=0;
         }
         public SectorWire driverRight() {
-            System.out.println("checking " + Integer.toString(code(true), 16) + " " + Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
+            //System.out.println("checking " + Integer.toString(code(true), 16) + " " +
+            //Integer.toString(_row(), 16) + " " + Integer.toString(_col(), 16));
             int ret = mode4(z(code(true)), _row(), _col());
             ret = (ret >> (global?3:0)) & 0x7;
             switch(ret) {
@@ -190,6 +137,13 @@ public abstract class Fpslic {
                 default: throw new Error("multiple drivers on " + this + "!");
             }
         }
+
+        public boolean touches(Cell c) {
+            return
+                horizontal
+                ? (c.row==_row() && (_col()/4 == c.col/4))
+                : (c.col==_col() && (_row()/4 == c.row/4));
+        }
     }
     /*    
     public final class SwitchBox {
@@ -222,6 +176,8 @@ public abstract class Fpslic {
             this.col = col;
         }
         
+        public Fpslic fpslic() { return Fpslic.this; }
+
         // Accessors for Neighbors //////////////////////////////////////////////////////////////////////////////
 
         public SectorWire hwire(int plane)  { return new SectorWire(true, plane, col, row); }
@@ -240,8 +196,8 @@ public abstract class Fpslic {
         public void lut(int xlut, int ylut) { xlut(xlut); ylut(ylut); }
         public void xlut(int table)    { mode4(7, row, col, (byte)(table & 0xff)); }
         public byte xlut()             { return (byte)(mode4(7, row, col) & 0xff); }
-        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 printXLut()      { return FpslicUtil.printLut(xlut(), "x", "y", "t"); }
+        public String printXLutX()     { return FpslicUtil.printLut(xlut(), str(xi(), "x"), str(yi(), "y"), str(ti_source(), "t")); }
 
         public String str(int x, String def) {
             switch(x) {
@@ -266,8 +222,8 @@ public abstract class Fpslic {
         /* bit positions mean:  [MSB] zxy zx_ z_y z__ _xy _x_ __y ___ [LSB] */
         public void ylut(int table)    { mode4(6, row, col, (byte)(table & 0xff)); }
         public byte ylut()             { return (byte)(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_source(), "t")) + Integer.toString(ylut() & 0xff, 16); }
+        public String printYLut()      { return FpslicUtil.printLut(ylut(), "y", "x", "t"); }
+        public String printYLutX()     { return FpslicUtil.printLut(ylut(), str(yi(), "y"), str(xi(), "x"), str(ti_source(), "t")) + Integer.toString(ylut() & 0xff, 16); }
 
         public void ff_reset_value(boolean value) {
             //mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
@@ -302,8 +258,8 @@ public abstract class Fpslic {
 
         public void h(int plane, boolean enable) {
             switch(plane) {
-                case 0: mode4(0x08, row, col, 2, enable); return;
-                case 1: mode4(0x08, row, col, 0, enable); return;
+                case 0: mode4(0x08, row, col, 0, enable); return;
+                case 1: mode4(0x08, row, col, 2, enable); return;
                 case 2: mode4(0x08, row, col, 5, enable); return;
                 case 3: mode4(0x08, row, col, 6, enable); return;
                 case 4: mode4(0x00, row, col, 6, enable); return;
@@ -313,8 +269,8 @@ public abstract class Fpslic {
         
         public boolean hx(int plane) {
             switch(plane) {
-                case 0: return (mode4(0x08, row, col) & (1<<2)) != 0;
-                case 1: return (mode4(0x08, row, col) & (1<<0)) != 0;
+                case 0: return (mode4(0x08, row, col) & (1<<0)) != 0;
+                case 1: return (mode4(0x08, row, col) & (1<<2)) != 0;
                 case 2: return (mode4(0x08, row, col) & (1<<5)) != 0;
                 case 3: return (mode4(0x08, row, col) & (1<<6)) != 0;
                 case 4: return (mode4(0x00, row, col) & (1<<6)) != 0;
@@ -355,14 +311,11 @@ public abstract class Fpslic {
         }
 
         public int t() {
-            System.err.println("found " + (mode4(1, row, col) & 0x34));
-            switch(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;
+            switch(mode4(1, row, col) & 0x30) {
                 case 0x00: return TMUX_W;
-                    //default: throw new RuntimeException("unknown!");
+                case 0x10: return wi()==NONE ? TMUX_FB : TMUX_W_AND_FB;
+                case 0x20: return wi()==NONE ? TMUX_Z  : TMUX_W_AND_Z;
+                case 0x30: throw new RuntimeException("illegal!");
                 default: return TMUX_W; 
             }
         }
@@ -370,15 +323,14 @@ public abstract class Fpslic {
         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);
+                case TMUX_Z:        result = 0x20; break;
+                case TMUX_W_AND_Z:  result = 0x20; break;
+                case TMUX_FB:       result = 0x10; break;
+                case TMUX_W_AND_FB: result = 0x10; break;
                 default: result = 0x00; break;
             }
-            mode4(1, row, col, result, 0x34);
+            mode4(1, row, col, result, 0x30);
         }
         /*
         private void fmux(int source) {
@@ -447,6 +399,29 @@ public abstract class Fpslic {
         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; }
@@ -492,16 +467,31 @@ public abstract class Fpslic {
                 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 L1: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<2); break;
+                case L2: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<1); break;
+                case L3: mode4(0x03, row, col, 4, false); mode4(0x05, row, col, 1<<0); break;
+                case L4: mode4(0x03, row, col, 4, true);  mode4(0x05, row, col,    0); 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) {
@@ -534,6 +524,24 @@ public abstract class Fpslic {
             }
         }
 
+        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;
@@ -561,6 +569,11 @@ public abstract class Fpslic {
         }
 
        
+        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;
@@ -574,7 +587,7 @@ public abstract class Fpslic {
         }
 
         public int zi() {
-            switch(mode4(0x02, row, col) & 0xDB) {
+            switch(mode4(0x02, row, col) & 0x9B) {
                 case (1<<7): return L4;
                 case (1<<5): return L3;
                 case (1<<4): return L2;
@@ -583,20 +596,72 @@ public abstract class Fpslic {
                 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));
-            }
+                default: throw new RuntimeException("invalid argument: zi=="+(mode4(0x02, row, col) & 0x9B));
+            }
+        }
+
+        public Cell dir(int i) {
+            switch(i) {
+                case NORTH: return north();
+                case SOUTH: return south();
+                case EAST: return east();
+                case WEST: return west();
+                case NW: return nw();
+                case SW: return sw();
+                case SE: return se();
+                case NE: return ne();
+            }
+            return null;
+        }
+        public int dir(Cell c) {
+            if      (c.row==row-1 && c.col==col-1) return SW;
+            else if (c.row==row+1 && c.col==col-1) return NW;
+            else if (c.row==row-1 && c.col==col+1) return SE;
+            else if (c.row==row+1 && c.col==col+1) return NE;
+            else if (c.row==row-1 && c.col==col)   return SOUTH;
+            else if (c.row==row+1 && c.col==col)   return NORTH;
+            else if (c.row==row   && c.col==col-1) return WEST;
+            else if (c.row==row   && c.col==col+1) return EAST;
+            return -1;
+        }
+
+        public void generalized_c_element() {
+
+            /*
+            ylut(LUT_SELF & (~LUT_OTHER));
+            xlut((~LUT_SELF) | LUT_OTHER);
+            c(ZMUX);
+            zi(L2);
+            //h(L2, true);
+            out(L2, true);
+            xo(true);
+            yo(true);
+            */
+
+            //ylut(0xB2);
+            ylut((LUT_SELF & ~LUT_OTHER) | (LUT_Z & ~LUT_OTHER) | (LUT_Z & LUT_SELF & LUT_OTHER));
+            xlut(LUT_Z);
+            c(YLUT);
+            f(false);
+            b(false);
+            t(false, false, true);
+            yo(false);
+            xo(false);
         }
 
 
         // 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 /*&& 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()*/;
+                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;
             }
         }
@@ -654,13 +719,19 @@ public abstract class Fpslic {
             boolean out = false;
             boolean connect = false;
             for(int i=0; i<4; i++) {
+
+                // FIXME FIXME FIXME
+                if (i==3) continue;
+
                 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()) {
@@ -670,6 +741,8 @@ public abstract class Fpslic {
                 case 0x30: return true;
             }
             return false;
+            */
+            return true;
         }
 
 
@@ -809,63 +882,5 @@ public abstract class Fpslic {
 
     }
 
-    public static void main(String[] s) throws Exception {
-        System.out.println(printLut(0x39, "se", "n", "L0"));
-    }
-    public static synchronized String printLut(int lut, String xn, String yn, String zn) {
-        try {
-            File f = File.createTempFile("mvsis", ".mvs");
-            f.deleteOnExit();
-
-            FileOutputStream fos = new FileOutputStream(f);
-            PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
-            pw.println(".model clb");
-            pw.println(".inputs "+xn+" "+yn+" "+zn);
-            pw.println(".outputs O");
-            pw.println(".table "+xn+" "+yn+" "+zn+/*("X_xor_Y X_xor_Z Y_xor_Z")+*/ " -> O");
-            for(int i=8; i>=0; i--) {
-                int x = ((i & 0x01)!=0 ? 1 : 0);
-                int y = ((i & 0x02)!=0 ? 1 : 0);
-                int z = ((i & 0x04)!=0 ? 1 : 0);
-                pw.print(" "+x+" ");
-                pw.print(" "+y+" ");
-                pw.print(" "+z+" ");
-                //pw.print(" "+(x ^ y)+" ");
-                //pw.print(" "+(y ^ z)+" ");
-                //pw.print(" "+(z ^ y)+" ");
-                pw.print((lut & (1<<i))==0 ? 0 : 1);
-                pw.println();
-            }
-            pw.println(".end");
-            pw.flush();
-            pw.close();
-            Process p = Runtime.getRuntime().exec(new String[] { "mvsis", "-c", "simplify;print_factor", f.getAbsolutePath() });
-            new Gobble("mvsis: ", p.getErrorStream()).start();
-            //new Gobble("mvsis: ", p.getInputStream()).start();
-            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
-            String ret = br.readLine();
-            //f.delete();
-            return ret.trim();
-        } catch (Exception e) {
-            e.printStackTrace();
-            return "*mvsis_error*";
-        }
-    }
-
-    public static class Gobble extends Thread {
-        private final String header;
-        private final BufferedReader br;
-        public Gobble(String header, BufferedReader br) { this.br = br; this.header = header; }
-        public Gobble(String header, Reader r)          { this(header, new BufferedReader(r)); }
-        public Gobble(String header, InputStream is)    { this(header, new InputStreamReader(is)); }
-        public void run() {
-            try {
-                for(String s = br.readLine(); s!=null; s=br.readLine())
-                    System.err.println(header + s);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
 
 }