checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
1 package edu.berkeley.slipway;
2
3 import com.atmel.fpslic.*;
4 import edu.berkeley.obits.*;
5 import org.ibex.util.Log;
6 import java.io.*;
7 import java.util.*;
8 import gnu.io.*;
9
10 public class FtdiBoard extends Board {
11
12     static {
13         System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath());
14     }
15
16     private final Chip chip;
17     private final InputStream in;
18     private final OutputStream out;
19
20     public InputStream getInputStream() { return in; }
21     public OutputStream getOutputStream() { return out; }
22
23     public FtdiBoard() throws Exception {
24         chip = new ChipImpl();
25         String bstFile = this.getClass().getName();
26         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
27         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
28         boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile)));
29         in = chip.getInputStream();
30         out = chip.getOutputStream();
31         for(int i=0; i<255; i++) out.write(0);
32         out.flush();
33     }
34
35     public void reset() throws IOException {
36         chip.doReset();
37     }
38
39     public void boot(Reader r) throws Exception {
40         Chip d = chip;
41
42         d.selfTest();
43
44         OutputStream os = d.getConfigStream();
45
46         BufferedReader br = new BufferedReader(r);
47         br.readLine();
48
49         int bytes = 0;
50         while(true) {
51             String s = br.readLine();
52             if (s==null) break;
53             int in = Integer.parseInt(s, 2);
54             bytes++;
55             os.write((byte)in);
56             if ((bytes % 1000)==0) {
57                 os.flush();
58                 System.out.print("wrote " + bytes + " bytes\r");
59             }
60         }
61         os.close();
62     }
63
64     public static String pad(String s, int i) {
65         if (s.length() >= i) return s;
66         return "0"+pad(s, i-1);
67     }
68 }