1 package edu.berkeley.fleet.fpga;
3 import static edu.berkeley.fleet.util.BitManipulations.*;
4 import java.util.concurrent.Semaphore;
5 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.util.*;
10 import java.util.concurrent.*;
11 import static edu.berkeley.fleet.two.FleetTwoFleet.*;
12 import static edu.berkeley.fleet.api.Instruction.Set.*;
13 import static edu.berkeley.fleet.api.Predicate.*;
14 import static edu.berkeley.fleet.api.Instruction.*;
17 public class Client extends FleetProcess {
20 private BlockingQueue<BitVector> queue = new LinkedBlockingQueue<BitVector>();
22 public static long signExtend(long val) {
23 if ((val & (1L << 36)) != 0)
24 val = val | (0xffffffffffffffffL << 36);
28 public Fleet getFleet() { return fpga; }
29 public Dock getDebugInputDock() {
30 return fpga.getShip("Debug",0).getDock("in");
32 public BitVector recvWord() {
34 throw new RuntimeException("this fleet has been terminated");
37 } catch (InterruptedException e) { throw new RuntimeException(e); }
40 protected void _terminate() {
44 } catch (Exception e) { e.printStackTrace(); }
48 private DataOutputStream dos = null;
55 } catch (Exception e) {
56 throw new RuntimeException(e);
60 /** the 16550 has a 16-byte FIFO buffer */
61 private final Semaphore sem = new Semaphore(15);
62 private boolean closed = false;
64 public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
67 s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
68 //s = new Socket(InetAddress.getByName("localhost"), 3133);
69 OutputStream os = new BufferedOutputStream(s.getOutputStream());
70 PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
71 pw.print(Server.pass_string+" "+bitfile+"\n");
74 this.dos = new DataOutputStream(os);
76 dos.write( (3<<6) | 9);
80 final InputStream is0 = s.getInputStream();
81 while(is0.available() > 0) {
83 //System.out.println("reset code: " + (i & ~(-1 << 6)));
86 final InputStream is = new BufferedInputStream(is0);
88 Thread t = new Thread() {
92 int count = sem.availablePermits();
93 if (count >= (1<<6)) count = (1<<6)-1;
94 if (count < 1) count = 1;
98 dos.write( (1<<6) | count );
99 if (sem.availablePermits()==0) dos.flush();
102 } catch (Exception e) { throw new RuntimeException(e); }
114 for(int i=0; i<8; i++) {
118 long val2 = (val & 0xffL);
119 val2 = val2 << (i * 6);
123 BitVector bs = new BitVector(37);
124 for(int i=0; i<37; i++)
125 bs.set(i, ((result >> i) & 1L)!=0);
128 } catch (SocketException e) {
129 } catch (Exception e) { throw new RuntimeException(e);
130 } finally { terminate(); }
136 for(Instruction inst : instructions) sendInstruction(inst);
141 public void sendToken(Destination d) { sendWord(d, new BitVector(fpga.getWordWidth()), true); }
142 public void sendWord(Destination d, BitVector word) { sendWord(d, word, false); }
143 private void sendWord(Destination d, BitVector word, boolean token) {
145 Dock dispatchFrom = fpga.debugShip.getDock("in");
147 out = fpga.PACKET_DATA.setval(out, word);
148 out = fpga.PACKET_TOKEN.setval(out, token ? 1 : 0);
149 out = fpga.PACKET_SIGNAL.setval(out, 0);
150 out = fpga.PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
152 for(int i=9; i>=0; i--)
153 dos.write(BitManipulations.getIntField(i*6+5, i*6, out));
156 } catch (Exception e) {
157 throw new RuntimeException(e);
160 public void sendInstruction(Instruction inst) {
161 Dock dispatchFrom = fpga.debugShip.getDock("in");
162 sendWord(inst.dock.getInstructionDestination(),
163 new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
166 private static Move discard(Dock dock) { return new Move(dock, false, IgnoreFlagD, false, null, false, true, false, false, false, false); }
167 private static Move deliver(Dock dock) { return new Move(dock, false, IgnoreFlagD, false, null, false, false, false, false, true, false); }
168 private static Move wait(Dock dock) { return new Move(dock, false, IgnoreFlagD, false, null, true, false, false, false, false, false); }
169 private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreFlagD, false, path, false, false, false, false, true, false); }