1 package edu.berkeley.fleet.fpga;
3 import static edu.berkeley.fleet.util.BitManipulations.*;
4 import edu.berkeley.fleet.api.*;
7 import edu.berkeley.fleet.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.*;
16 public class Client extends FleetProcess {
19 private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
21 public void invokeInstruction(Instruction i) {
22 throw new RuntimeException("not implemented");
25 public static long signExtend(long val) {
26 if ((val & (1L << 36)) != 0)
27 val = val | (0xffffffffffffffffL << 36);
31 public Fleet getFleet() { return fpga; }
32 public Dock getDebugInputDock() {
33 return fpga.getShip("Debug",0).getDock("in");
35 public BitVector readWord() {
37 throw new RuntimeException("this fleet has been terminated");
40 } catch (InterruptedException e) { throw new RuntimeException(e); }
43 protected void _terminate() {
46 } catch (Exception e) { e.printStackTrace(); }
50 private DataOutputStream dos = null;
55 } catch (Exception e) {
56 throw new RuntimeException(e);
60 public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
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");
70 this.dos = new DataOutputStream(os);
71 for(Instruction inst : instructions)
72 dispatchInstruction(inst);
75 final InputStream is = new BufferedInputStream(s.getInputStream());
76 Thread t = new Thread() {
82 for(int i=0; i<6; i++) {
85 long val2 = (val & 0xffL);
86 val2 = val2 << (i * 8);
90 BitVector bs = new BitVector(37);
91 for(int i=0; i<37; i++)
92 bs.set(i, ((result >> i) & 1L)!=0);
95 } catch (SocketException e) {
96 } catch (Exception e) { throw new RuntimeException(e);
97 } finally { terminate(); }
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) {
108 Dock dispatchFrom = fpga.debugShip.getDock("in");
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());
115 for(int i=7; i>=0; i--)
116 dos.write(BitManipulations.getIntField(i*8+7, i*8, out));
118 } catch (Exception e) {
119 throw new RuntimeException(e);
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)));
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); }