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 IOException {
28 private void init() throws IOException {
29 byte[] bytes = new byte[6];
35 // read any crap that might be left in the buffer
37 System.arraycopy(bytes, 1, bytes, 0, 5);
38 bytes[5] = in.readByte();
40 System.out.print("\rsignature: read \"" + new String(bytes) + "\" ");
41 if (bytes[0] == (byte)'O' &&
42 bytes[1] == (byte)'B' &&
43 bytes[2] == (byte)'I' &&
44 bytes[3] == (byte)'T' &&
45 bytes[4] == (byte)'S') {
46 System.out.println("\rsignature: got proper signature ");
53 public synchronized void scanFPGA(boolean on) throws IOException {
62 public static int retval = 0;
63 public synchronized int readCount() {
69 ByteCallback bc = new ByteCallback() {
70 public synchronized void call(byte b) throws Exception {
73 ((in.read() & 0xff) << 16) |
74 ((in.read() & 0xff) << 8) |
75 ((in.read() & 0xff) << 0);
86 } catch (Exception e) { throw new RuntimeException(e); }
89 public static interface ByteCallback {
90 public void call(byte b) throws Exception;
93 private Vector callbacks = new Vector();
95 private Thread reader = new Thread() {
97 System.out.println("*** reader thread begun");
100 byte b = in.readByte();
101 ByteCallback bc = (ByteCallback)callbacks.remove(0);
103 } catch (Exception e) {
110 public synchronized void readBus(ByteCallback bc) throws IOException {
114 if (reader != null) {
120 public synchronized void readInterrupts(ByteCallback bc) throws IOException {
124 if (reader != null) {
130 private byte[][][] cache = new byte[24][][];
131 public /*synchronized*/ byte mode4(int z, int y, int x) {
132 if (cache[x]==null) return 0;
133 if (cache[x][y]==null) return 0;
134 return cache[x][y][z];
140 public static int save = 0;
141 public static int saveof = 0;
142 public /*synchronized*/ void mode4(int z, int y, int x, int d) {
154 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
155 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
156 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
157 } catch (IOException e) {
158 throw new RuntimeException(e);
162 public /*synchronized*/ void flush() {
165 } catch (IOException e) {
166 throw new RuntimeException(e);
170 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }