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