cleaned up Client.java
authormegacz <adam@megacz.com>
Wed, 11 Mar 2009 22:53:59 +0000 (15:53 -0700)
committermegacz <adam@megacz.com>
Wed, 11 Mar 2009 22:53:59 +0000 (15:53 -0700)
src/edu/berkeley/fleet/fpga/Client.java

index 98c187c..1a2ec3c 100644 (file)
@@ -1,7 +1,6 @@
 package edu.berkeley.fleet.fpga;
 
 import static edu.berkeley.fleet.util.BitManipulations.*;
-import java.util.concurrent.Semaphore;
 import edu.berkeley.fleet.api.*;
 import java.io.*;
 import java.net.*;
@@ -19,12 +18,6 @@ public class Client extends FleetProcess {
     private Socket s;
     private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
 
-    public static long signExtend(long val) {
-        if ((val & (1L << 36)) != 0)
-            val = val | (0xffffffffffffffffL << 36);
-        return val;
-    }
-                                    
     public Fleet getFleet() { return fpga; }
     public Dock getDebugInputDock() {
         return fpga.getShip("Debug",0).getDock("in");
@@ -39,92 +32,63 @@ public class Client extends FleetProcess {
 
     protected void _terminate() {
         try {
-            closed = true;
             s.close();
         } catch (Exception e) { e.printStackTrace(); }
     }
 
     private Fpga fpga;
-    private DataOutputStream dos = null;
+    private OutputStream os = null;
 
     public void flush() {
         try {
-            synchronized(this) {
-                dos.flush();
-            }
+            os.flush();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
-    /** the 16550 has a 16-byte FIFO buffer */
-    private final Semaphore sem = new Semaphore(15);
-    private boolean closed = false;
-
     public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
         this.fpga = fpga;
 
         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
         //s = new Socket(InetAddress.getByName("localhost"), 3133);
-        OutputStream os = new BufferedOutputStream(s.getOutputStream());
+        this.os = new BufferedOutputStream(s.getOutputStream());
+        final InputStream is = new BufferedInputStream(s.getInputStream());
         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
         pw.print(Server.pass_string+" "+bitfile+"\n");
         pw.flush();
 
-        this.dos = new DataOutputStream(os);
-
-        dos.write( (3<<6) | 9);
-        dos.flush();
-        Thread.sleep(1000);
-
-        final InputStream is0 = s.getInputStream();
-        while(is0.available() > 0) {
-            int i = is0.read();
-            //System.out.println("reset code: " + (i & ~(-1 << 6)));
-        }
-
-        final InputStream is = new BufferedInputStream(is0);
-
-        dos.write( (1<<6) | 15);
-
-        Thread t;
-        /*
-          t = new Thread() {
+        // goofy reset sequence
+        new Thread() {
             public void run() {
                 try {
-                    while(true) {
-                        int count = sem.availablePermits();
-                        if (count >= (1<<6)) count = (1<<6)-1; 
-                        if (count < 1) count = 1;
-                        sem.acquire(count);
-                        if (closed) return;
-                        synchronized(this) {
-                            dos.write( (1<<6) | count );
-                            if (sem.availablePermits()==0) dos.flush();
-                        }
-                    }
+                    for(int i=0; i<255; i++) os.write( (3<<6) | 0);
+                    for(int i=0; i<60; i++) os.write( (3<<6) | i);
+                    os.flush();
                 } catch (Exception e) { throw new RuntimeException(e); }
             }
-            };
-        t.setDaemon(true);
-        t.start();
-        */
+        }.start();
+        int k = 1;
+        while(k<60) {
+            int i = is.read();
+            if ( (i & (3<<6)) != (3<<6) ) { k=1; continue; }
+            i = i & ~((-1)<<6);
+            if (i==k) k++; else k = 1;
+        }
+        
+        // initial flow-control credits
+        os.write( (1<<6) | 15);
 
-        t = new Thread() {
+        Thread t = new Thread() {
             public void run() {
                 try {
-                    while(true) {
+                    OUTER: while(true) {
                         long result = 0;
-                        int val = 0;
                         for(int i=0; i<8; i++) {
-                            val = is.read();
-                            if (val==-1) break;
-                            sem.release();
-                            long val2 = (val & 0xffL);
-                            val2 = val2 << (i * 6);
-                            result |= val2;
+                            int val = is.read();
+                            if (val==-1) break OUTER;
+                            result |= ((val & 0xffL) << (i * 6L));
                         }
-                        if (val==-1) break;
                         BitVector bs = new BitVector(37);
                         for(int i=0; i<37; i++)
                             bs.set(i, ((result >> i) & 1L)!=0);
@@ -140,7 +104,6 @@ public class Client extends FleetProcess {
 
         for(Instruction inst : instructions) sendInstruction(inst);
         flush();
-
     }
 
     public void sendToken(Destination d) { sendWord(d, new BitVector(fpga.getWordWidth()), true); }
@@ -155,8 +118,8 @@ public class Client extends FleetProcess {
             out = fpga.PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
             synchronized(this) {
                 for(int i=9; i>=0; i--)
-                    dos.write(BitManipulations.getIntField(i*6+5, i*6, out));
-                dos.flush();
+                    os.write(BitManipulations.getIntField(i*6+5, i*6, out));
+                os.flush();
             }
         } catch (Exception e) {
             throw new RuntimeException(e);