1 package edu.berkeley.obits.device.atmel;
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
12 private final DataInputStream in;
13 private final DataOutputStream out;
14 private final Board board;
16 public AvrDrone(Board b) throws IOException {
18 this.out = new DataOutputStream(b.getOutputStream());
19 this.in = new DataInputStream(b.getInputStream());
23 public void reset() { board.reset(); }
25 private void init() throws IOException {
26 Log.debug(this, "waiting for device to identify itself");
28 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
29 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
30 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
31 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
32 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
33 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
36 byte[] bytes = new byte[6];
37 for(int i=0; i<6; i++) {
38 bytes[i] = in.readByte();
39 System.out.println("got " + (i+1) + " header bytes: " + (bytes[i] & 0xff) + " '" + ((char)bytes[i]) + "'");
42 Log.info(this, "device correctly identified itself; ready for operation");
45 public synchronized void scanFPGA(boolean on) throws DeviceException {
53 } catch (IOException e) { throw new DeviceException(e); }
56 public static int retval = 0;
57 public synchronized int readCount() throws DeviceException {
63 ByteCallback bc = new ByteCallback() {
64 public synchronized void call(byte b) throws Exception {
67 ((in.read() & 0xff) << 16) |
68 ((in.read() & 0xff) << 8) |
69 ((in.read() & 0xff) << 0);
80 } catch (Exception e) { throw new DeviceException(e); }
83 public static interface ByteCallback {
84 public void call(byte b) throws Exception;
87 private Vector callbacks = new Vector();
89 private Thread reader = new Thread() {
91 System.out.println("*** reader thread begun");
94 byte b = in.readByte();
95 ByteCallback bc = (ByteCallback)callbacks.remove(0);
97 } catch (Exception e) {
104 public synchronized void readBus(ByteCallback bc) throws DeviceException {
109 if (reader != null) {
113 } catch (IOException e) { throw new DeviceException(e); }
116 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
121 if (reader != null) {
125 } catch (IOException e) { throw new DeviceException(e); }
128 private byte[][][] cache = new byte[24][][];
129 public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
130 if (cache[x]==null) return 0;
131 if (cache[x][y]==null) return 0;
132 return cache[x][y][z];
138 public static int save = 0;
139 public static int saveof = 0;
140 public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
143 Log.info(this, "writing configuration frame [zyxd]: " +
144 pad(1, Integer.toString(z&0xff, 16)) + " " +
145 pad(1, Integer.toString(y&0xff, 16)) + " " +
146 pad(1, Integer.toString(x&0xff, 16)) + " " +
147 pad(1, Integer.toString(d&0xff, 16))
150 boolean zchange = z!=lastz;
151 boolean ychange = y!=lasty;
152 boolean xchange = x!=lastx;
153 boolean zinc = z==lastz+1;
154 boolean yinc = y==lasty+1;
155 boolean xinc = x==lastx+1;
156 boolean zdec = z==lastz-1;
157 boolean ydec = y==lasty-1;
158 boolean xdec = x==lastx-1;
170 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
171 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
172 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
173 } catch (IOException e) { throw new DeviceException(e); }
176 public /*synchronized*/ void flush() throws DeviceException {
179 } catch (IOException e) { throw new DeviceException(e); }
182 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }