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 static long signExtend(long val) {
22 if ((val & (1L << 36)) != 0)
23 val = val | (0xffffffffffffffffL << 36);
27 public Fleet getFleet() { return fpga; }
28 public Dock getDebugInputDock() {
29 return fpga.getShip("Debug",0).getDock("in");
31 public BitVector readWord() {
33 throw new RuntimeException("this fleet has been terminated");
36 } catch (InterruptedException e) { throw new RuntimeException(e); }
39 protected void _terminate() {
42 } catch (Exception e) { e.printStackTrace(); }
46 private DataOutputStream dos = null;
51 } catch (Exception e) {
52 throw new RuntimeException(e);
56 public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
59 s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
60 //s = new Socket(InetAddress.getByName("localhost"), 3133);
61 OutputStream os = new BufferedOutputStream(s.getOutputStream());
62 PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
63 pw.print(Server.pass_string+" "+bitfile+"\n");
66 this.dos = new DataOutputStream(os);
67 for(Instruction inst : instructions)
68 dispatchInstruction(inst);
71 final InputStream is = new BufferedInputStream(s.getInputStream());
72 Thread t = new Thread() {
78 for(int i=0; i<6; i++) {
81 long val2 = (val & 0xffL);
82 val2 = val2 << (i * 8);
86 BitVector bs = new BitVector(37);
87 for(int i=0; i<37; i++)
88 bs.set(i, ((result >> i) & 1L)!=0);
91 } catch (SocketException e) {
92 } catch (Exception e) { throw new RuntimeException(e);
93 } finally { terminate(); }
100 public void dispatchToken(Destination d) { dispatchWord(d, new BitVector(fpga.getWordWidth()), true); }
101 public void dispatchWord(Destination d, BitVector word) { dispatchWord(d, word, false); }
102 private void dispatchWord(Destination d, BitVector word, boolean token) {
104 Dock dispatchFrom = fpga.debugShip.getDock("in");
106 out = PACKET_DATA.setval(out, word);
107 out = PACKET_TOKEN.setval(out, token ? 1 : 0);
108 out = PACKET_SIGNAL.setval(out, 0);
109 out = PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
111 for(int i=7; i>=0; i--)
112 dos.write(BitManipulations.getIntField(i*8+7, i*8, out));
114 } catch (Exception e) {
115 throw new RuntimeException(e);
118 public void dispatchInstruction(Instruction inst) {
119 Dock dispatchFrom = fpga.debugShip.getDock("in");
120 dispatchWord(inst.dock.getInstructionDestination(),
121 new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
124 private static Move discard(Dock dock) { return new Move(dock, false, IgnoreOLC, false, null, false, true, false, false, false, false); }
125 private static Move deliver(Dock dock) { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true, false); }
126 private static Move wait(Dock dock) { return new Move(dock, false, IgnoreOLC, false, null, true, false, false, false, false, false); }
127 private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true, false); }