d9bc8c1f4c45d71dc2719537bdcc906c2c76cda5
[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         init();
36     }
37
38     public void reset() throws IOException {
39         chip.reset();
40     }
41
42     public void boot(Reader r) throws Exception {
43
44         chip.selfTest();
45
46         int total = 75090/9;
47         OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
48         BufferedReader br = new BufferedReader(r);
49
50         int bytes = 0;
51         while(true) {
52             String s = br.readLine();
53             if (s==null) break;
54             bytes++;
55             os.write((byte)Integer.parseInt(s, 2));
56             if ((bytes % 1000)==0) os.flush();
57         }
58         os.close();
59     }
60
61     public static String pad(String s, int i) {
62         if (s.length() >= i) return s;
63         return "0"+pad(s, i-1);
64     }
65
66
67     // AvrDrone leftovers //////////////////////////////////////////////////////////////////////////////
68
69     private void init() throws IOException {
70         byte[] bytes = new byte[6];
71         int i=0;
72
73         out.write(0);
74         out.flush();
75
76         // read any crap that might be left in the buffer
77         while(true) {
78             System.arraycopy(bytes, 1, bytes, 0, 5);
79             bytes[5] = in.readByte();
80             i++;
81             System.out.print("\rsignature: read \"" + new String(bytes) + "\"                   ");
82             if (bytes[0] == (byte)'O' &&
83                 bytes[1] == (byte)'B' &&
84                 bytes[2] == (byte)'I' &&
85                 bytes[3] == (byte)'T' &&
86                 bytes[4] == (byte)'S') {
87                 System.out.println("\rsignature: got proper signature                  ");
88                 break;
89             }
90         }
91
92         // FIXME: what if init() is called twice?
93         new Thread() {
94             public void run() {
95                 while(true) {
96                     try {
97                         byte b = in.readByte();
98                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
99                         bc.call(b);
100                     } catch (Exception e) {
101                         e.printStackTrace();
102                     }
103                 }
104             }
105         }.start();
106     }
107
108     public synchronized void scanFPGA(boolean on) throws IOException {
109         if (on) {
110             out.writeByte(3);
111             out.flush();
112         } else {
113             // FIXME
114         }
115     }
116     // fixme!
117     public static int retval = 0;
118     public synchronized int readCount() {
119         try {
120             ByteCallback bc = new ByteCallback() {
121                     public synchronized void call(byte b) throws Exception {
122                         retval =
123                             ((b & 0xff) << 24) |
124                             ((in.read() & 0xff) << 16) |
125                             ((in.read() & 0xff) << 8) |
126                             ((in.read() & 0xff) << 0);
127                         this.notify();
128                     }
129                 };
130             synchronized(bc) {
131                 callbacks.add(bc);
132                 out.writeByte(6);
133                 out.flush();
134                 bc.wait();
135             }
136             return retval;
137         } catch (Exception e) { throw new RuntimeException(e); }
138     }
139
140     public static interface ByteCallback {
141         public void call(byte b) throws Exception;
142     }
143
144     private Vector callbacks = new Vector();
145
146     public synchronized void readBus(ByteCallback bc) throws IOException {
147         callbacks.add(bc);
148         out.writeByte(2);
149         out.flush();
150     }
151
152     public synchronized void readInterrupts(ByteCallback bc) throws IOException {
153         callbacks.add(bc);
154         out.writeByte(6);
155         out.flush();
156     }
157
158     private byte[][][] cache = new byte[24][][];
159     public /*synchronized*/ byte mode4(int z, int y, int x) {
160         if (cache[x]==null) return 0;
161         if (cache[x][y]==null) return 0;
162         return cache[x][y][z];
163     }
164
165     int lastz = 0;
166     int lastx = 0;
167     int lasty = 0;
168     public static int save = 0;
169     public static int saveof = 0;
170     public /*synchronized*/ void mode4(int z, int y, int x, int d) {
171         try {
172             out.writeByte(1);
173             out.writeByte(z);
174             out.writeByte(y);
175             out.writeByte(x);
176             saveof++;
177             lastz = z;
178             lastx = x;
179             lasty = y;
180             out.writeByte(d);
181
182             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
183             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
184             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
185         } catch (IOException e) {
186             throw new RuntimeException(e);
187         }
188     }
189
190     public /*synchronized*/ void flush() {
191         try {
192             out.flush();
193         } catch (IOException e) {
194             throw new RuntimeException(e);
195         }
196     }
197
198     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
199
200 }