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 Fleet getFleet() { return fpga; }
22 public Dock getDebugInputDock() {
23 return fpga.getShip("Debug",0).getDock("in");
25 public BitVector recvWord() {
27 throw new RuntimeException("this fleet has been terminated");
30 } catch (InterruptedException e) { throw new RuntimeException(e); }
33 protected void _terminate() {
36 } catch (Exception e) { e.printStackTrace(); }
40 private OutputStream os = null;
45 } catch (Exception e) {
46 throw new RuntimeException(e);
50 public Client(Fpga fpga, String bitfile, Instruction[] instructions) throws Exception {
53 s = new Socket(InetAddress.getByName("goliath.megacz.com"), 3133);
54 //s = new Socket(InetAddress.getByName("localhost"), 3133);
55 this.os = new BufferedOutputStream(s.getOutputStream());
56 final InputStream is = new BufferedInputStream(s.getInputStream());
57 PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
58 pw.print(Server.pass_string+" "+bitfile+"\n");
61 // goofy reset sequence
65 for(int i=0; i<255; i++) os.write( (3<<6) | 0);
66 for(int i=0; i<60; i++) os.write( (3<<6) | i);
68 } catch (Exception e) { throw new RuntimeException(e); }
74 if ( (i & (3<<6)) != (3<<6) ) { k=1; continue; }
76 if (i==k) k++; else k = 1;
79 // initial flow-control credits
80 os.write( (1<<6) | 15);
82 Thread t = new Thread() {
87 for(int i=0; i<8; i++) {
89 if (val==-1) break OUTER;
90 result |= ((val & 0xffL) << (i * 6L));
92 BitVector bs = new BitVector(37);
93 for(int i=0; i<37; i++)
94 bs.set(i, ((result >> i) & 1L)!=0);
97 } catch (SocketException e) {
98 } catch (Exception e) { throw new RuntimeException(e);
99 } finally { terminate(); }
105 for(Instruction inst : instructions) sendInstruction(inst);
109 public void sendToken(Destination d) { sendWord(d, new BitVector(fpga.getWordWidth()), null, true); }
110 public void sendWord(Destination d, BitVector word) { sendWord(d, word, null, false); }
111 public void sendWord(Destination d, BitVector word, BitVector signal) { sendWord(d, word, signal, false); }
112 private void sendWord(Destination d, BitVector word, BitVector signal, boolean token) {
114 Dock dispatchFrom = fpga.debugShip.getDock("in");
116 out = fpga.PACKET_DATA.setval(out, word);
117 out = fpga.PACKET_TOKEN.setval(out, token ? 1 : 0);
119 out = fpga.PACKET_SIGNAL.setval(out, 0);
121 out = fpga.PACKET_SIGNAL.setval(out, signal);
122 out = fpga.PACKET_DEST.setval(out, ((FpgaPath)dispatchFrom.getPath(d, null)).toLong());
124 for(int i=9; i>=0; i--)
125 os.write(BitManipulations.getIntField(i*6+5, i*6, out));
128 } catch (Exception e) {
129 throw new RuntimeException(e);
132 public void sendInstruction(Instruction inst) {
133 Dock dispatchFrom = fpga.debugShip.getDock("in");
134 sendWord(inst.dock.getInstructionDestination(),
135 new BitVector(fpga.getWordWidth()).set(fpga.writeInstruction(inst, dispatchFrom)));
138 private static Move discard(Dock dock) { return new Move(dock, IgnoreFlagD, false, null, false, true, false, false, false, false); }
139 private static Move deliver(Dock dock) { return new Move(dock, IgnoreFlagD, false, null, false, false, false, false, true, false); }
140 private static Move wait(Dock dock) { return new Move(dock, IgnoreFlagD, false, null, true, false, false, false, false, false); }
141 private static Move sendto(Dock dock, Path path) { return new Move(dock, IgnoreFlagD, false, path, false, false, false, false, true, false); }