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 { chip.reset(); }
40 public void boot(Reader r) throws Exception {
44 OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
45 BufferedReader br = new BufferedReader(r);
49 String s = br.readLine();
52 os.write((byte)Integer.parseInt(s, 2));
53 if ((bytes % 1000)==0) os.flush();
59 // AvrDrone leftovers //////////////////////////////////////////////////////////////////////////////
61 private void init() throws IOException {
62 byte[] bytes = new byte[6];
68 // read any crap that might be left in the buffer
70 System.arraycopy(bytes, 1, bytes, 0, 5);
71 bytes[5] = in.readByte();
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 ");
84 // FIXME: what if init() is called twice?
89 while(callbacks.size() == 0) Thread.sleep(500);
90 byte b = in.readByte();
91 ByteCallback bc = (ByteCallback)callbacks.remove(0);
96 } catch (Exception e) {
105 // Programming ///////////////////////////////////////////////////////////////////////////////
107 private byte[][][] cache = new byte[24][][];
108 public byte mode4(int z, int y, int x) {
109 if (cache[x]==null) return 0;
110 if (cache[x][y]==null) return 0;
111 return cache[x][y][z];
114 public void mode4(int z, int y, int x, int d) {
116 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
117 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
118 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
125 } catch (IOException e) {
126 throw new RuntimeException(e);
130 public void flush() { try { out.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
133 // Callbacks //////////////////////////////////////////////////////////////////////////////
135 private Vector callbacks = new Vector();
137 private Object lock = new Object();
138 private static final int limit = 40;
140 private void enqueue(ByteCallback bcb) {
143 while (callbacks.size() >= limit) {
144 System.out.println("block");
146 System.out.println("unblock => " + callbacks.size());
148 } catch (Exception e) {
149 throw new RuntimeException(e);
155 public static abstract class ByteCallback {
157 public abstract void call(byte b) throws Exception;
160 public synchronized int readCount() {
162 ByteCallback bc = new ByteCallback() {
163 public synchronized void call(byte b) throws Exception {
166 ((in.read() & 0xff) << 16) |
167 ((in.read() & 0xff) << 8) |
168 ((in.read() & 0xff) << 0);
179 } catch (Exception e) { throw new RuntimeException(e); }
182 public synchronized void readBus(ByteCallback bc) throws IOException {
188 public synchronized void readInterrupts(ByteCallback bc) throws IOException {
195 // Util //////////////////////////////////////////////////////////////////////////////
197 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
198 public static String pad(String s, int i) {
199 if (s.length() >= i) return s;
200 return "0"+pad(s, i-1);