add new urjtag-based code, fjmem
[fleet.git] / src / edu / berkeley / fleet / fpga / Server.java
index fb9049d..eb017ba 100644 (file)
@@ -4,18 +4,18 @@ import gnu.io.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import java.util.concurrent.Semaphore;
 
 // FIXME: accept connections, but stall, during programming
 public class Server {
     static boolean sign = false;
 
-
     static long jarFileTime = 0;
     static long bitFileTime = 0;
     static {
         try {
             jarFileTime = new File("fleet.jar").lastModified();
-            bitFileTime = new File("build/fpga/main.bit").lastModified();
+            bitFileTime = new File("./main.bit").lastModified();
         } catch (Exception e) { throw new RuntimeException(e); }
     }
 
@@ -23,8 +23,8 @@ public class Server {
     public static void main(String[] args) throws Exception {
         System.err.println("programming...");
         Process proc = Runtime.getRuntime().exec(new String[] {
-                "misc/program.sh",
-                "build/fpga/main.bit"
+                "./misc/program.sh",
+                "./main.bit"
             });
         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
         String str = null;
@@ -46,7 +46,7 @@ public class Server {
                             System.err.println("jarfile modified; exiting...");
                             System.exit(0);
                         }
-                        if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
+                        if (bitFileTime != new File("./main.bit").lastModified()) {
                             System.err.println("bitfile modified; exiting...");
                             System.exit(0);
                         }
@@ -73,13 +73,20 @@ public class Server {
         private SerialPort sp;
         public Handler(Socket s) { this.socket = s; this.sp = sp; }
         public void run() {
+            System.err.println("waiting for Server.class lock...");
             synchronized(Server.class) {
+                System.err.println("   (got it)");
                 try {
-                    this.sp = new RXTXPort("/dev/ttyS0");
+                    if (new File("/dev/cu.usbserial-FTCBWI2P").exists())
+                        this.sp = new RXTXPort("/dev/cu.usbserial-FTCBWI2P");
+                    else
+                        this.sp = new RXTXPort("/dev/ttyS0");
                     sp.setInputBufferSize(0);
                     sp.setOutputBufferSize(0);
-                    sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
-                    sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
+                    //sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    //sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
+                    sp.setFlowControlMode(0);
                     try {
                         _run();
                     } finally {
@@ -91,6 +98,13 @@ public class Server {
         public void _run() {
             try {
                 final InputStream is = new BufferedInputStream(socket.getInputStream());
+                final OutputStream os = socket.getOutputStream();
+                final OutputStream fos = sp.getOutputStream();
+                final InputStream fis = new BufferedInputStream(sp.getInputStream());
+
+                sp.sendBreak(100);
+
+                // read login string
                 byte[] buf = new byte[1024];
                 StringBuffer sb = new StringBuffer();
                 while(true) {
@@ -101,13 +115,6 @@ public class Server {
                 }
                 System.err.println("login string: " + sb.toString());
                 if (!sb.toString().startsWith(pass_string)) return;
-                final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
-                System.err.println("sending breaks...");
-                sp.sendBreak(100);
-
-                final OutputStream fos = sp.getOutputStream();
-                final InputStream fis = new BufferedInputStream(sp.getInputStream());
-                sign = false;
 
                 System.err.println("sending instructions...");
                 new Thread() {
@@ -116,14 +123,15 @@ public class Server {
                             while(true) {
                                 int r = is.read();
                                 if (r == -1) break;
-                                fos.write(r);
-                                if (!sign) {
-                                    fos.flush();
-                                    Thread.sleep(100);
-                                    sign = true;
+                                System.err.println("write: 0x"+Integer.toString(r & 0xff, 16));
+                                synchronized(fos) {
+                                    fos.write(r);
+                                    if (is.available()==0) fos.flush();
                                 }
                             }
-                            fos.flush();
+                            synchronized(fos) {
+                                fos.flush();
+                            }
                         } catch (Exception e) { throw new RuntimeException(e);
                         } finally {
                             System.err.println("closing...");
@@ -133,40 +141,26 @@ public class Server {
                     }
                 }.start();
 
-
                 System.err.println("reading back...");
                 while(true) {
-                    long result = 0;
                     int val = 0;
-                    boolean hit=false;
                     for(int i=0; i<6; i++) {
                         int k = 0;
-                        while(!closed && fis.available()==0) {
+                        while (fis.available()==0) {
                             if (closed) return;
-                            k++;
-                            if (k >= 100) {
-                                System.err.println("sleep");
-                                k = 0;
-                            }
-                            if (k==10) os.flush();
                             Thread.sleep(10);
                         }
-                        if (closed) { os.flush(); return; }
                         val = fis.read();
-                        if (!sign) {
-                            System.err.println("leader byte: 0x" + Integer.toString(val, 16) + " '"+((char)val)+"'");
-                            continue;
-                        }
                         if (val==-1) break;
-                        System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
+                        if ((val & (3<<6)) == 0) {
+                            synchronized(fos) {
+                                fos.write( (1<<6) | 1);
+                            }
+                        }
+                        System.err.println("read: 0x"+Integer.toString(val & 0xff, 16));
                         os.write((byte)val);
-                        result |= ((long)val) << (i * 8);
                     }
                     if (val==-1) break;
-                    System.err.print(result);
-                    System.err.print(" 0x");
-                    System.err.print(Long.toString(result, 16));
-                    System.err.println();
                 }
                 os.flush();
                 System.err.println("done.");
@@ -175,10 +169,6 @@ public class Server {
             } finally {
                 try {
                     System.err.println("closing...");
-                    /*
-                    fis.close();
-                    if (fis != null) fis.close();
-                    */
                 } catch (Throwable t) { t.printStackTrace(); }
             }
         }