checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
1 package edu.berkeley.slipway;
2
3 import com.ftdi.usb.*;
4 import com.atmel.fpslic.*;
5 import edu.berkeley.obits.*;
6 import org.ibex.util.*;
7 import java.io.*;
8 import java.util.*;
9 import gnu.io.*;
10
11 public class FtdiBoard extends Fpslic implements Board {
12
13     static {
14         System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath());
15     }
16
17     private final FpslicBoot chip;
18     private final DataInputStream in;
19     private final DataOutputStream out;
20
21     public InputStream getInputStream() { return in; }
22     public OutputStream getOutputStream() { return out; }
23
24     public FtdiBoard() throws Exception {
25         super(24, 24);
26         chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000)));
27         String bstFile = this.getClass().getName();
28         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
29         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
30         boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile)));
31         in = new DataInputStream(chip.getInputStream());
32         out = new DataOutputStream(chip.getOutputStream());
33         for(int i=0; i<255; i++) out.write(0);
34         out.flush();
35     }
36
37     public void reset() throws IOException {
38         chip.reset();
39     }
40
41     public void boot(Reader r) throws Exception {
42
43         chip.selfTest();
44
45         int total = 75090/9;
46         OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
47         BufferedReader br = new BufferedReader(r);
48
49         int bytes = 0;
50         while(true) {
51             String s = br.readLine();
52             if (s==null) break;
53             bytes++;
54             os.write((byte)Integer.parseInt(s, 2));
55             if ((bytes % 1000)==0) os.flush();
56         }
57         os.close();
58     }
59
60     public static String pad(String s, int i) {
61         if (s.length() >= i) return s;
62         return "0"+pad(s, i-1);
63     }
64
65
66     // AvrDrone leftovers //////////////////////////////////////////////////////////////////////////////
67
68     private void init() throws IOException {
69         byte[] bytes = new byte[6];
70         int i=0;
71
72         out.write(0);
73         out.flush();
74
75         // read any crap that might be left in the buffer
76         while(true) {
77             System.arraycopy(bytes, 1, bytes, 0, 5);
78             bytes[5] = in.readByte();
79             i++;
80             System.out.print("\rsignature: read \"" + new String(bytes) + "\"                   ");
81             if (bytes[0] == (byte)'O' &&
82                 bytes[1] == (byte)'B' &&
83                 bytes[2] == (byte)'I' &&
84                 bytes[3] == (byte)'T' &&
85                 bytes[4] == (byte)'S') {
86                 System.out.println("\rsignature: got proper signature                  ");
87                 break;
88             }
89         }
90
91     }
92
93     public synchronized void scanFPGA(boolean on) throws IOException {
94         if (on) {
95             out.writeByte(3);
96             out.flush();
97         } else {
98             // FIXME
99         }
100     }
101     // fixme!
102     public static int retval = 0;
103     public synchronized int readCount() {
104         try {
105             if (reader != null) {
106                 reader.start();
107                 reader = null;
108             }
109             ByteCallback bc = new ByteCallback() {
110                     public synchronized void call(byte b) throws Exception {
111                         retval =
112                             ((b & 0xff) << 24) |
113                             ((in.read() & 0xff) << 16) |
114                             ((in.read() & 0xff) << 8) |
115                             ((in.read() & 0xff) << 0);
116                         this.notify();
117                     }
118                 };
119             synchronized(bc) {
120                 callbacks.add(bc);
121                 out.writeByte(6);
122                 out.flush();
123                 bc.wait();
124             }
125             return retval;
126         } catch (Exception e) { throw new RuntimeException(e); }
127     }
128
129     public static interface ByteCallback {
130         public void call(byte b) throws Exception;
131     }
132
133     private Vector callbacks = new Vector();
134
135     private Thread reader = new Thread() {
136             public void run() {
137                 System.out.println("*** reader thread begun");
138                 while(true) {
139                     try {
140                         byte b = in.readByte();
141                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
142                         bc.call(b);
143                     } catch (Exception e) {
144                         e.printStackTrace();
145                     }
146                 }
147             }
148         };
149
150     public synchronized void readBus(ByteCallback bc) throws IOException {
151         callbacks.add(bc);
152         out.writeByte(2);
153         out.flush();
154         if (reader != null) {
155             reader.start();
156             reader = null;
157         }
158     }
159
160     public synchronized void readInterrupts(ByteCallback bc) throws IOException {
161         callbacks.add(bc);
162         out.writeByte(6);
163         out.flush();
164         if (reader != null) {
165             reader.start();
166             reader = null;
167         }
168     }
169
170     private byte[][][] cache = new byte[24][][];
171     public /*synchronized*/ byte mode4(int z, int y, int x) {
172         if (cache[x]==null) return 0;
173         if (cache[x][y]==null) return 0;
174         return cache[x][y][z];
175     }
176
177     int lastz = 0;
178     int lastx = 0;
179     int lasty = 0;
180     public static int save = 0;
181     public static int saveof = 0;
182     public /*synchronized*/ void mode4(int z, int y, int x, int d) {
183         try {
184             out.writeByte(1);
185             out.writeByte(z);
186             out.writeByte(y);
187             out.writeByte(x);
188             saveof++;
189             lastz = z;
190             lastx = x;
191             lasty = y;
192             out.writeByte(d);
193
194             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
195             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
196             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
197         } catch (IOException e) {
198             throw new RuntimeException(e);
199         }
200     }
201
202     public /*synchronized*/ void flush() {
203         try {
204             out.flush();
205         } catch (IOException e) {
206             throw new RuntimeException(e);
207         }
208     }
209
210     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
211
212 }