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);
37 public void reset() throws IOException {
41 public void boot(Reader r) throws Exception {
46 OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
47 BufferedReader br = new BufferedReader(r);
51 String s = br.readLine();
54 os.write((byte)Integer.parseInt(s, 2));
55 if ((bytes % 1000)==0) os.flush();
60 public static String pad(String s, int i) {
61 if (s.length() >= i) return s;
62 return "0"+pad(s, i-1);
66 // AvrDrone leftovers //////////////////////////////////////////////////////////////////////////////
68 private void init() throws IOException {
69 byte[] bytes = new byte[6];
75 // read any crap that might be left in the buffer
77 System.arraycopy(bytes, 1, bytes, 0, 5);
78 bytes[5] = in.readByte();
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 ");
93 public synchronized void scanFPGA(boolean on) throws IOException {
102 public static int retval = 0;
103 public synchronized int readCount() {
105 if (reader != null) {
109 ByteCallback bc = new ByteCallback() {
110 public synchronized void call(byte b) throws Exception {
113 ((in.read() & 0xff) << 16) |
114 ((in.read() & 0xff) << 8) |
115 ((in.read() & 0xff) << 0);
126 } catch (Exception e) { throw new RuntimeException(e); }
129 public static interface ByteCallback {
130 public void call(byte b) throws Exception;
133 private Vector callbacks = new Vector();
135 private Thread reader = new Thread() {
137 System.out.println("*** reader thread begun");
140 byte b = in.readByte();
141 ByteCallback bc = (ByteCallback)callbacks.remove(0);
143 } catch (Exception e) {
150 public synchronized void readBus(ByteCallback bc) throws IOException {
154 if (reader != null) {
160 public synchronized void readInterrupts(ByteCallback bc) throws IOException {
164 if (reader != null) {
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];
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) {
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);
202 public /*synchronized*/ void flush() {
205 } catch (IOException e) {
206 throw new RuntimeException(e);
210 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }