Bee2 branch landing: step 1
[fleet.git] / src / edu / berkeley / fleet / fpga / Bee2.java
index 701f262..e0af6b6 100644 (file)
@@ -2,22 +2,138 @@ package edu.berkeley.fleet.fpga;
 import edu.berkeley.fleet.fpga.*;
 import edu.berkeley.fleet.api.*;
 import java.io.*;
+import gnu.io.*;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.concurrent.Semaphore;
 
 public class Bee2 extends Fpga {
 
     public Bee2() throws IOException {
-        for(int i=0; i<2; i++)  createShip("Memory");
-        for(int i=0; i<8; i++)  createShip("Alu");
-        for(int i=0; i<1; i++)  createShip("Fifo");
-        for(int i=0; i<12; i++) createShip("Counter");
-        
-        //createShip("CarrySaveAdder");
-        //createShip("Rotator");
+        for(int i=0; i<2; i++) createShip("Alu");
+        for(int i=0; i<1; i++) createShip("Memory");
+        for(int i=0; i<2; i++) createShip("Fifo");
         createShip("Random");
-        createShip("Button");
-        createShip("Timer");
+        createShip("Counter");
+        //createShip("CarrySaveAdder");
+        createShip("Rotator");
+        createShip("Lut3");
+        /*
+          for(int i=0; i<2; i++)  createShip("Memory");
+          for(int i=0; i<6; i++)  createShip("Alu");
+          for(int i=0; i<1; i++)  createShip("Fifo");
+          for(int i=0; i<12; i++) createShip("Counter");
         
+          //createShip("CarrySaveAdder");
+          //createShip("Rotator");
+          createShip("Random");
+          createShip("Button");
+          createShip("Timer");
+        */
         init();
     }
 
+
+    // Server //////////////////////////////////////////////////////////////////////////////
+
+    private static class Gobbler extends Thread {
+        private final BufferedReader br;
+        public Gobbler(InputStream is) {
+            this.br = new BufferedReader(new InputStreamReader(is));
+        }
+        public void run() {
+            try {
+                for(String s = br.readLine(); s!=null; s=br.readLine())
+                    System.err.println(s);
+            } catch (Exception e) { throw new RuntimeException(e); }
+        }
+    }
+
+    private static RandomAccessFile raf;
+    private static OutputStream fos;
+    private static InputStream fis;
+
+    public static String pass_string = "password=security_is_for_wimps ";
+
+    public static void main(String[] args) throws Exception {
+        int idx = Integer.parseInt(args[0]);
+
+        Process proc;
+        System.err.println("== unprogramming fpga " + idx);
+        proc = Runtime.getRuntime().exec("user_unprogram "+idx);
+        new Gobbler(proc.getInputStream()).start();
+        proc.waitFor();
+        System.err.println("== programming fpga " + idx);
+        proc = Runtime.getRuntime().exec("user_program "+idx+" main.bit");
+        new Gobbler(proc.getInputStream()).start();
+        int ret = proc.waitFor();
+        if (ret!=0) System.exit(ret);
+
+        raf = new RandomAccessFile(new File("/dev/selectmap"+idx), "rw");
+        fos = new FileOutputStream(raf.getFD());
+        fis = new BufferedInputStream(new FileInputStream(raf.getFD()));
+        ServerSocket ss = new ServerSocket(3133);
+
+        while(true) {
+            System.out.println("listening...");
+            Socket socket = ss.accept();
+            try {
+                socket.setKeepAlive(true);
+                System.out.println("accept!");
+                
+                //final InputStream is = new BufferedInputStream(socket.getInputStream());
+                final InputStream is = socket.getInputStream();
+                final OutputStream os = socket.getOutputStream(); //new BufferedOutputStream(socket.getOutputStream());
+                
+                // read login string
+                byte[] buf = new byte[1024];
+                StringBuffer sb = new StringBuffer();
+                while(true) {
+                    int i = is.read();
+                    if (i==-1) return;
+                    if (((char)i)=='\n') break;
+                    sb.append((char)i);
+                }
+                System.err.println("login string: " + sb.toString());
+                if (!sb.toString().startsWith(pass_string)) return;
+
+                socket.setSoTimeout(10);
+                while(true) {
+                    boolean ok = false;
+                    while (fis.available() > 0) {
+                        ok = true;
+                        int val = fis.read();
+                        if (val==-1) break;
+                        System.err.println("fpga->host: 0x"+Integer.toString(val & 0xff, 16));
+                        if ((val & (3<<6)) == 0)
+                            fos.write( (1<<6) | 1);
+                        os.write((byte)val);
+                        os.flush();
+                    }
+                    try {
+                        int r = is.read();
+                        if (r == -1) break;
+                        ok = true;
+                        System.err.println("host->fpga: 0x"+Integer.toString(r & 0xff, 16));
+                        fos.write(r);
+                    } catch (SocketTimeoutException _) { }
+                    if (!ok) {
+                        if (socket.isOutputShutdown() || socket.isInputShutdown() || socket.isClosed() || !socket.isConnected()) {
+                            socket.close();
+                            break;
+                        }
+                        System.err.println("flushing os...");
+                        os.flush();
+                        System.err.println("flushing fos...");
+                        fos.flush();
+                        System.err.println("sleeping...");
+                        Thread.sleep(10);                    
+                    }
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
 }