Bee2 branch landing: step 1
[fleet.git] / src / edu / berkeley / fleet / fpga / Bee2.java
1 package edu.berkeley.fleet.fpga;
2 import edu.berkeley.fleet.fpga.*;
3 import edu.berkeley.fleet.api.*;
4 import java.io.*;
5 import gnu.io.*;
6 import java.io.*;
7 import java.net.*;
8 import java.util.*;
9 import java.util.concurrent.Semaphore;
10
11 public class Bee2 extends Fpga {
12
13     public Bee2() throws IOException {
14         for(int i=0; i<2; i++) createShip("Alu");
15         for(int i=0; i<1; i++) createShip("Memory");
16         for(int i=0; i<2; i++) createShip("Fifo");
17         createShip("Random");
18         createShip("Counter");
19         //createShip("CarrySaveAdder");
20         createShip("Rotator");
21         createShip("Lut3");
22         /*
23           for(int i=0; i<2; i++)  createShip("Memory");
24           for(int i=0; i<6; i++)  createShip("Alu");
25           for(int i=0; i<1; i++)  createShip("Fifo");
26           for(int i=0; i<12; i++) createShip("Counter");
27         
28           //createShip("CarrySaveAdder");
29           //createShip("Rotator");
30           createShip("Random");
31           createShip("Button");
32           createShip("Timer");
33         */
34         init();
35     }
36
37
38     // Server //////////////////////////////////////////////////////////////////////////////
39
40     private static class Gobbler extends Thread {
41         private final BufferedReader br;
42         public Gobbler(InputStream is) {
43             this.br = new BufferedReader(new InputStreamReader(is));
44         }
45         public void run() {
46             try {
47                 for(String s = br.readLine(); s!=null; s=br.readLine())
48                     System.err.println(s);
49             } catch (Exception e) { throw new RuntimeException(e); }
50         }
51     }
52
53     private static RandomAccessFile raf;
54     private static OutputStream fos;
55     private static InputStream fis;
56
57     public static String pass_string = "password=security_is_for_wimps ";
58
59     public static void main(String[] args) throws Exception {
60         int idx = Integer.parseInt(args[0]);
61
62         Process proc;
63         System.err.println("== unprogramming fpga " + idx);
64         proc = Runtime.getRuntime().exec("user_unprogram "+idx);
65         new Gobbler(proc.getInputStream()).start();
66         proc.waitFor();
67         System.err.println("== programming fpga " + idx);
68         proc = Runtime.getRuntime().exec("user_program "+idx+" main.bit");
69         new Gobbler(proc.getInputStream()).start();
70         int ret = proc.waitFor();
71         if (ret!=0) System.exit(ret);
72
73         raf = new RandomAccessFile(new File("/dev/selectmap"+idx), "rw");
74         fos = new FileOutputStream(raf.getFD());
75         fis = new BufferedInputStream(new FileInputStream(raf.getFD()));
76         ServerSocket ss = new ServerSocket(3133);
77
78         while(true) {
79             System.out.println("listening...");
80             Socket socket = ss.accept();
81             try {
82                 socket.setKeepAlive(true);
83                 System.out.println("accept!");
84                 
85                 //final InputStream is = new BufferedInputStream(socket.getInputStream());
86                 final InputStream is = socket.getInputStream();
87                 final OutputStream os = socket.getOutputStream(); //new BufferedOutputStream(socket.getOutputStream());
88                 
89                 // read login string
90                 byte[] buf = new byte[1024];
91                 StringBuffer sb = new StringBuffer();
92                 while(true) {
93                     int i = is.read();
94                     if (i==-1) return;
95                     if (((char)i)=='\n') break;
96                     sb.append((char)i);
97                 }
98                 System.err.println("login string: " + sb.toString());
99                 if (!sb.toString().startsWith(pass_string)) return;
100
101                 socket.setSoTimeout(10);
102                 while(true) {
103                     boolean ok = false;
104                     while (fis.available() > 0) {
105                         ok = true;
106                         int val = fis.read();
107                         if (val==-1) break;
108                         System.err.println("fpga->host: 0x"+Integer.toString(val & 0xff, 16));
109                         if ((val & (3<<6)) == 0)
110                             fos.write( (1<<6) | 1);
111                         os.write((byte)val);
112                         os.flush();
113                     }
114                     try {
115                         int r = is.read();
116                         if (r == -1) break;
117                         ok = true;
118                         System.err.println("host->fpga: 0x"+Integer.toString(r & 0xff, 16));
119                         fos.write(r);
120                     } catch (SocketTimeoutException _) { }
121                     if (!ok) {
122                         if (socket.isOutputShutdown() || socket.isInputShutdown() || socket.isClosed() || !socket.isConnected()) {
123                             socket.close();
124                             break;
125                         }
126                         System.err.println("flushing os...");
127                         os.flush();
128                         System.err.println("flushing fos...");
129                         fos.flush();
130                         System.err.println("sleeping...");
131                         Thread.sleep(10);                    
132                     }
133                 }
134             } catch (IOException e) {
135                 e.printStackTrace();
136             }
137         }
138     }
139 }