checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
index 31f1e62..2854ece 100644 (file)
@@ -1,5 +1,7 @@
 package edu.berkeley.obits.device.atmel;
 
+import com.atmel.fpslic.*;
+import edu.berkeley.slipway.*;
 import edu.berkeley.obits.*;
 import org.ibex.util.Log;
 import java.io.*;
@@ -7,7 +9,7 @@ import java.util.*;
 import gnu.io.*;
 
 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
-public class AvrDrone extends AtmelDevice {
+public class AvrDrone extends Fpslic {
 
     private final DataInputStream in;
     private final DataOutputStream out;
@@ -20,33 +22,46 @@ public class AvrDrone extends AtmelDevice {
         init();
     } 
 
-    public void reset() { board.reset(); }
+    public void reset() throws IOException {
+        board.reset();
+    }
 
     private void init() throws IOException {
-        //board.reset();
-        Log.debug(this, "waiting for device to identify itself");
-        if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
-        if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
-        if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
-        if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
-        if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
-        if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
-        Log.info(this, "device correctly identified itself; ready for operation");
+        byte[] bytes = new byte[6];
+        int i=0;
+
+        out.write(0);
+        out.flush();
+
+        // read any crap that might be left in the buffer
+        while(true) {
+            System.arraycopy(bytes, 1, bytes, 0, 5);
+            bytes[5] = in.readByte();
+            i++;
+            System.out.print("\rsignature: read \"" + new String(bytes) + "\"                   ");
+            if (bytes[0] == (byte)'O' &&
+                bytes[1] == (byte)'B' &&
+                bytes[2] == (byte)'I' &&
+                bytes[3] == (byte)'T' &&
+                bytes[4] == (byte)'S') {
+                System.out.println("\rsignature: got proper signature                  ");
+                break;
+            }
+        }
+
     }
 
-    public synchronized void scanFPGA(boolean on) throws DeviceException {
-        try {
-            if (on) {
-                out.writeByte(3);
-                out.flush();
-            } else {
-                // FIXME
-            }
-        } catch (IOException e) { throw new DeviceException(e); }
+    public synchronized void scanFPGA(boolean on) throws IOException {
+        if (on) {
+            out.writeByte(3);
+            out.flush();
+        } else {
+            // FIXME
+        }
     }
     // fixme!
     public static int retval = 0;
-    public synchronized int readCount() throws DeviceException {
+    public synchronized int readCount() {
         try {
             if (reader != null) {
                 reader.start();
@@ -69,7 +84,7 @@ public class AvrDrone extends AtmelDevice {
                 bc.wait();
             }
             return retval;
-        } catch (Exception e) { throw new DeviceException(e); }
+        } catch (Exception e) { throw new RuntimeException(e); }
     }
 
     public static interface ByteCallback {
@@ -93,32 +108,28 @@ public class AvrDrone extends AtmelDevice {
             }
         };
 
-    public synchronized void readBus(ByteCallback bc) throws DeviceException {
-        try {
-            callbacks.add(bc);
-            out.writeByte(2);
-            out.flush();
-            if (reader != null) {
-                reader.start();
-                reader = null;
-            }
-        } catch (IOException e) { throw new DeviceException(e); }
+    public synchronized void readBus(ByteCallback bc) throws IOException {
+        callbacks.add(bc);
+        out.writeByte(2);
+        out.flush();
+        if (reader != null) {
+            reader.start();
+            reader = null;
+        }
     }
 
-    public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
-        try {
-            callbacks.add(bc);
-            out.writeByte(6);
-            out.flush();
-            if (reader != null) {
-                reader.start();
-                reader = null;
-            }
-        } catch (IOException e) { throw new DeviceException(e); }
+    public synchronized void readInterrupts(ByteCallback bc) throws IOException {
+        callbacks.add(bc);
+        out.writeByte(6);
+        out.flush();
+        if (reader != null) {
+            reader.start();
+            reader = null;
+        }
     }
 
     private byte[][][] cache = new byte[24][][];
-    public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
+    public /*synchronized*/ byte mode4(int z, int y, int x) {
         if (cache[x]==null) return 0;
         if (cache[x][y]==null) return 0;
         return cache[x][y][z];
@@ -129,26 +140,8 @@ public class AvrDrone extends AtmelDevice {
     int lasty = 0;
     public static int save = 0;
     public static int saveof = 0;
-    public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
+    public /*synchronized*/ void mode4(int z, int y, int x, int d) {
         try {
-            /*
-            Log.info(this, "writing configuration frame [zyxd]: " +
-                      pad(1, Integer.toString(z&0xff, 16)) + " " +
-                      pad(1, Integer.toString(y&0xff, 16)) + " " +
-                      pad(1, Integer.toString(x&0xff, 16)) + " " +
-                      pad(1, Integer.toString(d&0xff, 16))
-                      );
-            */
-            boolean zchange = z!=lastz;
-            boolean ychange = y!=lasty;
-            boolean xchange = x!=lastx;
-            boolean zinc    = z==lastz+1;
-            boolean yinc    = y==lasty+1;
-            boolean xinc    = x==lastx+1;
-            boolean zdec    = z==lastz-1;
-            boolean ydec    = y==lasty-1;
-            boolean xdec    = x==lastx-1;
-            
             out.writeByte(1);
             out.writeByte(z);
             out.writeByte(y);
@@ -162,13 +155,17 @@ public class AvrDrone extends AtmelDevice {
             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
-        } catch (IOException e) { throw new DeviceException(e); }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
-    public /*synchronized*/ void flush() throws DeviceException {
+    public /*synchronized*/ void flush() {
         try {
             out.flush();
-        } catch (IOException e) { throw new DeviceException(e); }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }