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");
27 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
28 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
29 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
30 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
31 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
32 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
33 Log.info(this, "device correctly identified itself; ready for operation");
36 public synchronized void scanFPGA(boolean on) throws DeviceException {
44 } catch (IOException e) { throw new DeviceException(e); }
47 public static int retval = 0;
48 public synchronized int readCount() throws DeviceException {
54 ByteCallback bc = new ByteCallback() {
55 public synchronized void call(byte b) throws Exception {
58 ((in.read() & 0xff) << 16) |
59 ((in.read() & 0xff) << 8) |
60 ((in.read() & 0xff) << 0);
71 } catch (Exception e) { throw new DeviceException(e); }
74 public static interface ByteCallback {
75 public void call(byte b) throws Exception;
78 private Vector callbacks = new Vector();
80 private Thread reader = new Thread() {
82 System.out.println("*** reader thread begun");
85 byte b = in.readByte();
86 ByteCallback bc = (ByteCallback)callbacks.remove(0);
88 } catch (Exception e) {
95 public synchronized void readBus(ByteCallback bc) throws DeviceException {
100 if (reader != null) {
104 } catch (IOException e) { throw new DeviceException(e); }
107 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
112 if (reader != null) {
116 } catch (IOException e) { throw new DeviceException(e); }
119 private byte[][][] cache = new byte[24][][];
120 public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
121 if (cache[x]==null) return 0;
122 if (cache[x][y]==null) return 0;
123 return cache[x][y][z];
129 public static int save = 0;
130 public static int saveof = 0;
131 public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
134 Log.info(this, "writing configuration frame [zyxd]: " +
135 pad(1, Integer.toString(z&0xff, 16)) + " " +
136 pad(1, Integer.toString(y&0xff, 16)) + " " +
137 pad(1, Integer.toString(x&0xff, 16)) + " " +
138 pad(1, Integer.toString(d&0xff, 16))
141 boolean zchange = z!=lastz;
142 boolean ychange = y!=lasty;
143 boolean xchange = x!=lastx;
144 boolean zinc = z==lastz+1;
145 boolean yinc = y==lasty+1;
146 boolean xinc = x==lastx+1;
147 boolean zdec = z==lastz-1;
148 boolean ydec = y==lasty-1;
149 boolean xdec = x==lastx-1;
161 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
162 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
163 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
164 } catch (IOException e) { throw new DeviceException(e); }
167 public /*synchronized*/ void flush() throws DeviceException {
170 } catch (IOException e) { throw new DeviceException(e); }
173 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }