1 package edu.berkeley.slipway;
4 import com.atmel.fpslic.*;
5 import edu.berkeley.obits.*;
6 import org.ibex.util.*;
11 public class FtdiBoard extends Fpslic implements Board {
14 System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath());
17 private final FpslicBoot chip;
18 private final DataInputStream in;
19 private final DataOutputStream out;
21 public InputStream getInputStream() { return in; }
22 public OutputStream getOutputStream() { return out; }
24 public FtdiBoard() throws Exception {
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);
38 public void reset() throws IOException {
42 public void boot(Reader r) throws Exception {
47 OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
48 BufferedReader br = new BufferedReader(r);
52 String s = br.readLine();
55 os.write((byte)Integer.parseInt(s, 2));
56 if ((bytes % 1000)==0) os.flush();
61 public static String pad(String s, int i) {
62 if (s.length() >= i) return s;
63 return "0"+pad(s, i-1);
67 // AvrDrone leftovers //////////////////////////////////////////////////////////////////////////////
69 private void init() throws IOException {
70 byte[] bytes = new byte[6];
76 // read any crap that might be left in the buffer
78 System.arraycopy(bytes, 1, bytes, 0, 5);
79 bytes[5] = in.readByte();
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 ");
92 // FIXME: what if init() is called twice?
97 byte b = in.readByte();
98 ByteCallback bc = (ByteCallback)callbacks.remove(0);
100 } catch (Exception e) {
108 public synchronized void scanFPGA(boolean on) throws IOException {
117 public static int retval = 0;
118 public synchronized int readCount() {
120 ByteCallback bc = new ByteCallback() {
121 public synchronized void call(byte b) throws Exception {
124 ((in.read() & 0xff) << 16) |
125 ((in.read() & 0xff) << 8) |
126 ((in.read() & 0xff) << 0);
137 } catch (Exception e) { throw new RuntimeException(e); }
140 public static interface ByteCallback {
141 public void call(byte b) throws Exception;
144 private Vector callbacks = new Vector();
146 public synchronized void readBus(ByteCallback bc) throws IOException {
152 public synchronized void readInterrupts(ByteCallback bc) throws IOException {
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];
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) {
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);
190 public /*synchronized*/ void flush() {
193 } catch (IOException e) {
194 throw new RuntimeException(e);
198 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }