checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.obits.device.atmel;
2
3 import com.atmel.fpslic.*;
4 import edu.berkeley.slipway.*;
5 import edu.berkeley.obits.*;
6 import org.ibex.util.Log;
7 import java.io.*;
8 import java.util.*;
9 import gnu.io.*;
10
11 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
12 public class AvrDrone extends Fpslic {
13
14     private final DataInputStream in;
15     private final DataOutputStream out;
16     private final Board board;
17
18     public AvrDrone(Board b) throws IOException {
19         this.board = b;
20         this.out = new DataOutputStream(b.getOutputStream());
21         this.in = new DataInputStream(b.getInputStream());
22         init();
23     } 
24
25     public void reset() throws IOException {
26         board.reset();
27     }
28
29     private void init() throws IOException {
30         byte[] bytes = new byte[6];
31         int i=0;
32
33         out.write(0);
34         out.flush();
35
36         // read any crap that might be left in the buffer
37         while(true) {
38             System.arraycopy(bytes, 1, bytes, 0, 5);
39             bytes[5] = in.readByte();
40             i++;
41             System.out.print("\rsignature: read \"" + new String(bytes) + "\"                   ");
42             if (bytes[0] == (byte)'O' &&
43                 bytes[1] == (byte)'B' &&
44                 bytes[2] == (byte)'I' &&
45                 bytes[3] == (byte)'T' &&
46                 bytes[4] == (byte)'S') {
47                 System.out.println("\rsignature: got proper signature                  ");
48                 break;
49             }
50         }
51
52     }
53
54     public synchronized void scanFPGA(boolean on) throws IOException {
55         if (on) {
56             out.writeByte(3);
57             out.flush();
58         } else {
59             // FIXME
60         }
61     }
62     // fixme!
63     public static int retval = 0;
64     public synchronized int readCount() {
65         try {
66             if (reader != null) {
67                 reader.start();
68                 reader = null;
69             }
70             ByteCallback bc = new ByteCallback() {
71                     public synchronized void call(byte b) throws Exception {
72                         retval =
73                             ((b & 0xff) << 24) |
74                             ((in.read() & 0xff) << 16) |
75                             ((in.read() & 0xff) << 8) |
76                             ((in.read() & 0xff) << 0);
77                         this.notify();
78                     }
79                 };
80             synchronized(bc) {
81                 callbacks.add(bc);
82                 out.writeByte(6);
83                 out.flush();
84                 bc.wait();
85             }
86             return retval;
87         } catch (Exception e) { throw new RuntimeException(e); }
88     }
89
90     public static interface ByteCallback {
91         public void call(byte b) throws Exception;
92     }
93
94     private Vector callbacks = new Vector();
95
96     private Thread reader = new Thread() {
97             public void run() {
98                 System.out.println("*** reader thread begun");
99                 while(true) {
100                     try {
101                         byte b = in.readByte();
102                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
103                         bc.call(b);
104                     } catch (Exception e) {
105                         e.printStackTrace();
106                     }
107                 }
108             }
109         };
110
111     public synchronized void readBus(ByteCallback bc) throws IOException {
112         callbacks.add(bc);
113         out.writeByte(2);
114         out.flush();
115         if (reader != null) {
116             reader.start();
117             reader = null;
118         }
119     }
120
121     public synchronized void readInterrupts(ByteCallback bc) throws IOException {
122         callbacks.add(bc);
123         out.writeByte(6);
124         out.flush();
125         if (reader != null) {
126             reader.start();
127             reader = null;
128         }
129     }
130
131     private byte[][][] cache = new byte[24][][];
132     public /*synchronized*/ byte mode4(int z, int y, int x) {
133         if (cache[x]==null) return 0;
134         if (cache[x][y]==null) return 0;
135         return cache[x][y][z];
136     }
137
138     int lastz = 0;
139     int lastx = 0;
140     int lasty = 0;
141     public static int save = 0;
142     public static int saveof = 0;
143     public /*synchronized*/ void mode4(int z, int y, int x, int d) {
144         try {
145             out.writeByte(1);
146             out.writeByte(z);
147             out.writeByte(y);
148             out.writeByte(x);
149             saveof++;
150             lastz = z;
151             lastx = x;
152             lasty = y;
153             out.writeByte(d);
154
155             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
156             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
157             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
158         } catch (IOException e) {
159             throw new RuntimeException(e);
160         }
161     }
162
163     public /*synchronized*/ void flush() {
164         try {
165             out.flush();
166         } catch (IOException e) {
167             throw new RuntimeException(e);
168         }
169     }
170
171     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
172
173 }