1 package edu.berkeley.obits.device.atmel;
3 import edu.berkeley.slipway.*;
4 import edu.berkeley.obits.*;
5 import org.ibex.util.Log;
10 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
11 public class AvrDrone extends AtmelDevice {
13 private final DataInputStream in;
14 private final DataOutputStream out;
15 private final Board board;
17 public AvrDrone(Board b) throws IOException {
19 this.out = new DataOutputStream(b.getOutputStream());
20 this.in = new DataInputStream(b.getInputStream());
24 public void reset() throws DeviceException {
27 } catch (IOException e) {
28 throw new DeviceException(e);
32 private void init() throws IOException {
33 //Log.debug(this, "waiting for device to identify itself");
35 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
36 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
37 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
38 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
39 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
40 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
43 byte[] bytes = new byte[6];
44 for(int i=0; i<6; i++) {
45 bytes[i] = in.readByte();
46 //System.out.println("got " + (i+1) + " header bytes: " + (bytes[i] & 0xff) + " '" + ((char)bytes[i]) + "'");
49 //Log.info(this, "device correctly identified itself; ready for operation");
52 public synchronized void scanFPGA(boolean on) throws DeviceException {
60 } catch (IOException e) { throw new DeviceException(e); }
63 public static int retval = 0;
64 public synchronized int readCount() throws DeviceException {
70 ByteCallback bc = new ByteCallback() {
71 public synchronized void call(byte b) throws Exception {
74 ((in.read() & 0xff) << 16) |
75 ((in.read() & 0xff) << 8) |
76 ((in.read() & 0xff) << 0);
87 } catch (Exception e) { throw new DeviceException(e); }
90 public static interface ByteCallback {
91 public void call(byte b) throws Exception;
94 private Vector callbacks = new Vector();
96 private Thread reader = new Thread() {
98 System.out.println("*** reader thread begun");
101 byte b = in.readByte();
102 ByteCallback bc = (ByteCallback)callbacks.remove(0);
104 } catch (Exception e) {
111 public synchronized void readBus(ByteCallback bc) throws DeviceException {
116 if (reader != null) {
120 } catch (IOException e) { throw new DeviceException(e); }
123 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
128 if (reader != null) {
132 } catch (IOException e) { throw new DeviceException(e); }
135 private byte[][][] cache = new byte[24][][];
136 public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
137 if (cache[x]==null) return 0;
138 if (cache[x][y]==null) return 0;
139 return cache[x][y][z];
145 public static int save = 0;
146 public static int saveof = 0;
147 public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
150 Log.info(this, "writing configuration frame [zyxd]: " +
151 pad(1, Integer.toString(z&0xff, 16)) + " " +
152 pad(1, Integer.toString(y&0xff, 16)) + " " +
153 pad(1, Integer.toString(x&0xff, 16)) + " " +
154 pad(1, Integer.toString(d&0xff, 16))
157 boolean zchange = z!=lastz;
158 boolean ychange = y!=lasty;
159 boolean xchange = x!=lastx;
160 boolean zinc = z==lastz+1;
161 boolean yinc = y==lasty+1;
162 boolean xinc = x==lastx+1;
163 boolean zdec = z==lastz-1;
164 boolean ydec = y==lasty-1;
165 boolean xdec = x==lastx-1;
177 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
178 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
179 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
180 } catch (IOException e) { throw new DeviceException(e); }
183 public /*synchronized*/ void flush() throws DeviceException {
186 } catch (IOException e) { throw new DeviceException(e); }
189 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }