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 {
27 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");
34 Log.info(this, "device correctly identified itself; ready for operation");
37 public synchronized void scanFPGA(boolean on) throws DeviceException {
45 } catch (IOException e) { throw new DeviceException(e); }
48 public static int retval = 0;
49 public synchronized int readCount() throws DeviceException {
55 ByteCallback bc = new ByteCallback() {
56 public synchronized void call(byte b) throws Exception {
59 ((in.read() & 0xff) << 16) |
60 ((in.read() & 0xff) << 8) |
61 ((in.read() & 0xff) << 0);
72 } catch (Exception e) { throw new DeviceException(e); }
75 public static interface ByteCallback {
76 public void call(byte b) throws Exception;
79 private Vector callbacks = new Vector();
81 private Thread reader = new Thread() {
83 System.out.println("*** reader thread begun");
86 byte b = in.readByte();
87 ByteCallback bc = (ByteCallback)callbacks.remove(0);
89 } catch (Exception e) {
96 public synchronized void readBus(ByteCallback bc) throws DeviceException {
101 if (reader != null) {
105 } catch (IOException e) { throw new DeviceException(e); }
108 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
113 if (reader != null) {
117 } catch (IOException e) { throw new DeviceException(e); }
120 private byte[][][] cache = new byte[24][][];
121 public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
122 if (cache[x]==null) return 0;
123 if (cache[x][y]==null) return 0;
124 return cache[x][y][z];
130 public static int save = 0;
131 public static int saveof = 0;
132 public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
135 Log.info(this, "writing configuration frame [zyxd]: " +
136 pad(1, Integer.toString(z&0xff, 16)) + " " +
137 pad(1, Integer.toString(y&0xff, 16)) + " " +
138 pad(1, Integer.toString(x&0xff, 16)) + " " +
139 pad(1, Integer.toString(d&0xff, 16))
142 boolean zchange = z!=lastz;
143 boolean ychange = y!=lasty;
144 boolean xchange = x!=lastx;
145 boolean zinc = z==lastz+1;
146 boolean yinc = y==lasty+1;
147 boolean xinc = x==lastx+1;
148 boolean zdec = z==lastz-1;
149 boolean ydec = y==lasty-1;
150 boolean xdec = x==lastx-1;
162 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
163 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
164 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
165 } catch (IOException e) { throw new DeviceException(e); }
168 public /*synchronized*/ void flush() throws DeviceException {
171 } catch (IOException e) { throw new DeviceException(e); }
174 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }