refactor codebag-memory-block creation code
[fleet.git] / src / edu / berkeley / fleet / fpga / Client.java
index c07442a..793c987 100644 (file)
@@ -43,93 +43,21 @@ public class Client extends FleetProcess {
         } catch (Exception e) { e.printStackTrace(); }
     }
 
-    public Client(String bitfile, byte[] program) throws Exception {
+    private Fpga fpga;
+
+    public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
+        this.fpga = fpga;
+
         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
         OutputStream os = new BufferedOutputStream(s.getOutputStream());
         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
         pw.print(Server.pass_string+" "+bitfile+"\n");
         pw.flush();
 
-
-        Fpga fpga = new Fpga();
-        ByteArrayOutputStream newp = new ByteArrayOutputStream();
-        DataOutputStream newpd = new DataOutputStream(newp);
-        long startcbd = 0;
-
-        Dock inAddrWrite = null;
-        Dock inDataWrite = null;
-        Dock inCBD       = null;
-        Dock out         = null;
-        Dock debugIn     = null;
-        Dock ihorn       = null;
-
-        for(Ship ship : fpga) {
-            if ("Memory".equals(ship.getType()) && ship.getOrdinal()==0) {
-                inAddrWrite = ship.getDock("inAddrWrite");
-                inDataWrite = ship.getDock("inDataWrite");
-                inCBD = ship.getDock("inCBD");
-                out = ship.getDock("out");
-                ihorn = ship.getDock("outIhorn");
-            }
-            if ("Debug".equals(ship.getType()) && ship.getOrdinal()==0) {
-                debugIn = ship.getDock("in");
-            }
-        }
-
-        for(int i=0; i<program.length; i+=6) {
-            long lit = 0
-                | ((program[i+0] & 0xffL) << 40)
-                | ((program[i+1] & 0xffL) << 32)
-                | ((program[i+2] & 0xffL) << 24)
-                | ((program[i+3] & 0xffL) << 16)
-                | ((program[i+4] & 0xffL) << 8)
-                | ((program[i+5] & 0xffL) << 0);
-            Instruction inst = fpga.readInstruction(lit, fpga.getUniversalSource());
-            if (i==0) {
-                long offset = (lit >> 6) & ~(-1L << 10);
-                long size   = (lit >> 0) & ~(-1L << 6);
-                startcbd = (offset << 6) | size;
-                size = 0;
-                offset = 0;
-                inst = new Instruction.Shift(fpga.getUniversalSource(), false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(0));
-            } else {
-                if (fpga.writeInstruction(inst, fpga.getUniversalSource()) != lit)
-                    throw new RuntimeException("no match: " + inst + " @"+(i/6)+"\n"+Long.toString(lit,16)+
-                                               "\n"+Long.toString(fpga.writeInstruction(inst, fpga.getUniversalSource()),16));
-            }
-            //fpga.writeInstruction(newpd, fpga.getUniversalSource(), inst);
-        }
-
-        for(int i=0; i<program.length; i+=6) {
-            long lit = 0
-                | ((program[i+0] & 0xffL) << 40)
-                | ((program[i+1] & 0xffL) << 32)
-                | ((program[i+2] & 0xffL) << 24)
-                | ((program[i+3] & 0xffL) << 16)
-                | ((program[i+4] & 0xffL) << 8)
-                | ((program[i+5] & 0xffL) << 0);
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), discard(out));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inDataWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, lit))));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inDataWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, lit))));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), deliver(inDataWrite));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inAddrWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, i/6))));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inAddrWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, i/6))));
-            fpga.writeInstruction(newpd, fpga.getUniversalSource(), deliver(inAddrWrite));
-        }
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inCBD, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, startcbd))));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Shift(inCBD, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, startcbd))));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), wait(inCBD));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), deliver(inCBD));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), sendto(out, out.getPath(inCBD.getDataDestination(),null)));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(), new Instruction.Set(ihorn, false, IgnoreOLC, SetDest.InnerLoopCounter, SetSource.Infinity));
-        fpga.writeInstruction(newpd, fpga.getUniversalSource(),
-                              new Instruction.Move(ihorn, false, IgnoreOLC, false, null,false,true,true,true,true,false));
-
-        newpd.flush();
-        program = newp.toByteArray();
-
-        os.write(program);
-        os.flush();
+        DataOutputStream dos = new DataOutputStream(os);
+        for(Instruction inst : instructions)
+            fpga.writeInstruction(dos, fpga.getUniversalSource(), inst);
+        dos.flush();
 
         final InputStream is = new BufferedInputStream(s.getInputStream());
         new Thread() {
@@ -157,25 +85,7 @@ public class Client extends FleetProcess {
             }
         }.start();
     }
-    /*
-    public static void main(String[] args) throws Exception {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buf = new byte[1024];
-        while(true) {
-            int numread = System.in.read(buf, 0, buf.length);
-            if (numread==-1) break;
-            baos.write(buf, 0, numread);
-        }
-        Client client = new Client(args.length==0 ? "main.bit" : args[0], baos.toByteArray());
-        while(true) {
-            long result = client.readWord();
-            System.err.print(result);
-            System.err.print(" 0x");
-            System.err.print(Long.toString(result, 16));
-            System.err.println();
-        }
-    }
-    */
+
     public void dispatchInstruction(Instruction i) { throw new RuntimeException(); }
     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); }