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