massive overhaul of fpga code
[fleet.git] / src / edu / berkeley / fleet / fpga / Client.java
1 package edu.berkeley.fleet.fpga;
2
3 import static edu.berkeley.fleet.util.BitManipulations.*;
4 import edu.berkeley.fleet.api.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import java.util.concurrent.*;
9 import static edu.berkeley.fleet.api.Instruction.Set.*;
10 import static edu.berkeley.fleet.api.Predicate.*;
11 import static edu.berkeley.fleet.api.Instruction.*;
12
13
14 public class Client extends FleetProcess {
15
16     private Socket s;
17     private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
18
19     public void invokeInstruction(Instruction i) {
20         throw new RuntimeException("not implemented");
21     }
22
23     public static long signExtend(long val) {
24         if ((val & (1L << 36)) != 0)
25             val = val | (0xffffffffffffffffL << 36);
26         return val;
27     }
28                                     
29     public Dock getDebugInputDock() {
30         throw new RuntimeException();
31     }
32     public BitVector readWord() {
33         if (isTerminated())
34             throw new RuntimeException("this fleet has been terminated");
35         try {
36             return queue.take();
37         } catch (InterruptedException e) { throw new RuntimeException(e); }
38     }
39
40     protected void _terminate() {
41         try {
42             s.close();
43         } catch (Exception e) { e.printStackTrace(); }
44     }
45
46     private Fpga fpga;
47
48     public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
49         this.fpga = fpga;
50
51         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
52         //s = new Socket(InetAddress.getByName("localhost"), 3133);
53         OutputStream os = new BufferedOutputStream(s.getOutputStream());
54         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
55         pw.print(Server.pass_string+" "+bitfile+"\n");
56         pw.flush();
57
58         DataOutputStream dos = new DataOutputStream(os);
59         for(Instruction inst : instructions)
60             fpga.writeInstruction(dos, fpga.debugShip.getDock("in"), inst);
61         dos.flush();
62
63         final InputStream is = new BufferedInputStream(s.getInputStream());
64         new Thread() {
65             public void run() {
66                 try {
67                     while(true) {
68                         long result = 0;
69                         int val = 0;
70                         for(int i=0; i<6; i++) {
71                             val = is.read();
72                             if (val==-1) break;
73                             long val2 = (val & 0xffL);
74                             val2 = val2 << (i * 8);
75                             result |= val2;
76                         }
77                         if (val==-1) break;
78                         BitVector bs = new BitVector(37);
79                         for(int i=0; i<37; i++)
80                             bs.set(i, ((result >> i) & 1L)!=0);
81                         queue.put(bs);
82                     }
83                 } catch (SocketException e) {
84                 } catch (Exception e) { throw new RuntimeException(e);
85                 } finally { terminate(); }
86             }
87         }.start();
88     }
89
90     public void dispatchInstruction(Instruction i) { throw new RuntimeException(); }
91     private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
92     private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
93     private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
94     private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
95 }