add FleetProcess.sendWord() that takes a signal
[fleet.git] / src / edu / berkeley / fleet / fpga / Client.java
index 4d83c65..4c223fd 100644 (file)
@@ -18,17 +18,11 @@ 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");
     }
-    public BitVector readWord() {
+    public BitVector recvWord() {
         if (isTerminated())
             throw new RuntimeException("this fleet has been terminated");
         try {
@@ -43,11 +37,11 @@ public class Client extends FleetProcess {
     }
 
     private Fpga fpga;
-    private DataOutputStream dos = null;
+    private OutputStream os = null;
 
     public void flush() {
         try {
-            dos.flush();
+            os.flush();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
@@ -58,31 +52,43 @@ public class Client extends FleetProcess {
 
         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);
-        for(Instruction inst : instructions)
-            dispatchInstruction(inst);
-        flush();
+        // goofy reset sequence
+        new Thread() {
+            public void run() {
+                try {
+                    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); }
+            }
+        }.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);
 
-        final InputStream is = new BufferedInputStream(s.getInputStream());
         Thread t = new Thread() {
             public void run() {
                 try {
-                    while(true) {
+                    OUTER: while(true) {
                         long result = 0;
-                        int val = 0;
-                        for(int i=0; i<6; i++) {
-                            val = is.read();
-                            if (val==-1) break;
-                            long val2 = (val & 0xffL);
-                            val2 = val2 << (i * 8);
-                            result |= val2;
+                        for(int i=0; i<8; i++) {
+                            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);
@@ -95,34 +101,42 @@ public class Client extends FleetProcess {
             };
         t.setDaemon(true);
         t.start();
+
+        for(Instruction inst : instructions) sendInstruction(inst);
+        flush();
     }
 
-    public void dispatchToken(Destination d) { dispatchWord(d, new BitVector(fpga.getWordWidth()), true); }
-    public void dispatchWord(Destination d, BitVector word) { dispatchWord(d, word, false); }
-    private void dispatchWord(Destination d, BitVector word, boolean token) {
+    public void sendToken(Destination d) { sendWord(d, new BitVector(fpga.getWordWidth()), null, true); }
+    public void sendWord(Destination d, BitVector word) { sendWord(d, word, null, false); }
+    public void sendWord(Destination d, BitVector word, BitVector signal) { sendWord(d, word, signal, false); }
+    private void sendWord(Destination d, BitVector word, BitVector signal, boolean token) {
         try {
             Dock dispatchFrom = fpga.debugShip.getDock("in");
             long out = 0;
-            out = PACKET_DATA.setval(out, word);
-            out = PACKET_TOKEN.setval(out, token ? 1 : 0);
-            out = PACKET_SIGNAL.setval(out, 0);
-            out = PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
+            out = fpga.PACKET_DATA.setval(out, word);
+            out = fpga.PACKET_TOKEN.setval(out, token ? 1 : 0);
+            if (signal==null)
+                out = fpga.PACKET_SIGNAL.setval(out, 0);
+            else 
+                out = fpga.PACKET_SIGNAL.setval(out, signal);
+            out = fpga.PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
             synchronized(this) {
-                for(int i=7; i>=0; i--)
-                    dos.write(BitManipulations.getIntField(i*8+7, i*8, out));
+                for(int i=9; i>=0; i--)
+                    os.write(BitManipulations.getIntField(i*6+5, i*6, out));
+                os.flush();
             }
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
-    public void dispatchInstruction(Instruction inst) {
+    public void sendInstruction(Instruction inst) {
         Dock dispatchFrom = fpga.debugShip.getDock("in");
-        dispatchWord(inst.dock.getInstructionDestination(),
-                     new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
+        sendWord(inst.dock.getInstructionDestination(),
+                 new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
     }
 
-    private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
-    private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
-    private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
-    private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
+    private static Move discard(Dock dock)           { return new Move(dock, IgnoreFlagD, false, null, false, true,  false, false, false, false); }
+    private static Move deliver(Dock dock)           { return new Move(dock, IgnoreFlagD, false, null, false, false, false, false, true,  false); }
+    private static Move wait(Dock dock)              { return new Move(dock, IgnoreFlagD, false, null, true,  false, false, false, false, false); }
+    private static Move sendto(Dock dock, Path path) { return new Move(dock, IgnoreFlagD, false, path, false, false, false, false, true,  false); }
 }