9a53410ec07d3541c80eeac719252bfcf4174719
[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 edu.berkeley.fleet.util.*;
8 import java.util.*;
9 import java.util.concurrent.*;
10 import static edu.berkeley.fleet.api.Instruction.Set.*;
11 import static edu.berkeley.fleet.api.Predicate.*;
12 import static edu.berkeley.fleet.api.Instruction.*;
13
14
15 public class Client extends FleetProcess {
16
17     private Socket s;
18     private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
19
20     public void invokeInstruction(Instruction i) {
21         throw new RuntimeException("not implemented");
22     }
23
24     public static long signExtend(long val) {
25         if ((val & (1L << 36)) != 0)
26             val = val | (0xffffffffffffffffL << 36);
27         return val;
28     }
29                                     
30     public Fleet getFleet() { return fpga; }
31     public Dock getDebugInputDock() {
32         throw new RuntimeException();
33     }
34     public BitVector readWord() {
35         if (isTerminated())
36             throw new RuntimeException("this fleet has been terminated");
37         try {
38             return queue.take();
39         } catch (InterruptedException e) { throw new RuntimeException(e); }
40     }
41
42     protected void _terminate() {
43         try {
44             s.close();
45         } catch (Exception e) { e.printStackTrace(); }
46     }
47
48     private Fpga fpga;
49     private DataOutputStream dos = null;
50
51     public void flush() {
52         try {
53             dos.flush();
54         } catch (Exception e) {
55             throw new RuntimeException(e);
56         }
57     }
58
59     public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
60         this.fpga = fpga;
61
62         s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
63         //s = new Socket(InetAddress.getByName("localhost"), 3133);
64         OutputStream os = new BufferedOutputStream(s.getOutputStream());
65         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
66         pw.print(Server.pass_string+" "+bitfile+"\n");
67         pw.flush();
68
69         this.dos = new DataOutputStream(os);
70         for(Instruction inst : instructions)
71             dispatchInstruction(inst);
72         flush();
73
74         final InputStream is = new BufferedInputStream(s.getInputStream());
75         Thread t = new Thread() {
76             public void run() {
77                 try {
78                     while(true) {
79                         long result = 0;
80                         int val = 0;
81                         for(int i=0; i<6; i++) {
82                             val = is.read();
83                             if (val==-1) break;
84                             long val2 = (val & 0xffL);
85                             val2 = val2 << (i * 8);
86                             result |= val2;
87                         }
88                         if (val==-1) break;
89                         BitVector bs = new BitVector(37);
90                         for(int i=0; i<37; i++)
91                             bs.set(i, ((result >> i) & 1L)!=0);
92                         queue.put(bs);
93                     }
94                 } catch (SocketException e) {
95                 } catch (Exception e) { throw new RuntimeException(e);
96                 } finally { terminate(); }
97             }
98             };
99         t.setDaemon(true);
100         t.start();
101     }
102
103     public void dispatchToken(Destination d) { dispatchWord(d, new BitVector(fpga.getWordWidth()), true); }
104     public void dispatchWord(Destination d, BitVector word) { dispatchWord(d, word, false); }
105     private void dispatchWord(Destination d, BitVector word, boolean token) {
106         try {
107             Dock dispatchFrom = fpga.debugShip.getDock("in");
108             long out = 0;
109             out = PACKET_DATA.setval(out, word);
110             out = PACKET_TOKEN.setval(out, token ? 1 : 0);
111             out = PACKET_SIGNAL.setval(out, 0);
112             out = PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
113             synchronized(this) {
114                 for(int i=7; i>=0; i--)
115                     dos.write(BitManipulations.getIntField(i*8+7, i*8, out));
116             }
117         } catch (Exception e) {
118             throw new RuntimeException(e);
119         }
120     }
121     public void dispatchInstruction(Instruction inst) {
122         Dock dispatchFrom = fpga.debugShip.getDock("in");
123         dispatchWord(inst.dock.getInstructionDestination(),
124                      new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
125     }
126
127     private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
128     private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
129     private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
130     private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
131 }