rename slipway=>fpga
[fleet.git] / src / edu / berkeley / fleet / fpga / Client.java
1 package edu.berkeley.fleet.fpga;
2
3 import edu.berkeley.fleet.api.*;
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7 import java.util.concurrent.*;
8
9 public class Client extends FleetProcess {
10
11     private Socket s;
12     private BlockingQueue<Long> queue = new LinkedBlockingQueue<Long>();
13
14     public void invokeInstruction(Instruction i) {
15         throw new RuntimeException("not implemented");
16     }
17
18     public static long signExtend(long val) {
19         if ((val & (1L << 36)) != 0)
20             val = val | (0xffffffffffffffffL << 36);
21         return val;
22     }
23                                     
24     public long readWord() {
25         if (isTerminated())
26             throw new RuntimeException("this fleet has been terminated");
27         try {
28             return signExtend(queue.take());
29         } catch (InterruptedException e) { throw new RuntimeException(e); }
30     }
31
32     protected void _terminate() {
33         try {
34             s.close();
35         } catch (Exception e) { e.printStackTrace(); }
36     }
37
38     public Client(String bitfile, byte[] program) throws IOException {
39         s = new Socket(InetAddress.getByName("bee441.megacz.com"), 3133);
40         OutputStream os = s.getOutputStream();
41         PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
42         pw.print(Server.pass_string+" "+bitfile+"\n");
43         pw.flush();
44
45         int numinstrs = (program.length / 6);
46         os.write((numinstrs >> (5*8)) & 0xff);
47         os.write((numinstrs >> (4*8)) & 0xff);
48         os.write((numinstrs >> (3*8)) & 0xff);
49         os.write((numinstrs >> (2*8)) & 0xff);
50         os.write((numinstrs >> (1*8)) & 0xff);
51         os.write((numinstrs >> (0*8)) & 0xff);
52         os.write(program);
53         os.flush();
54
55         final InputStream is = s.getInputStream();
56         new Thread() {
57             public void run() {
58                 try {
59                     while(true) {
60                         long result = 0;
61                         int val = 0;
62                         for(int i=0; i<6; i++) {
63                             val = is.read();
64                             if (val==-1) break;
65                             long val2 = (val & 0xffL);
66                             val2 = val2 << (i * 8);
67                             result |= val2;
68                         }
69                         if (val==-1) break;
70                         queue.put(result);
71                     }
72                 } catch (SocketException e) {
73                 } catch (Exception e) { throw new RuntimeException(e);
74                 } finally { terminate(); }
75             }
76         }.start();
77     }
78
79     public static void main(String[] args) throws Exception {
80         ByteArrayOutputStream baos = new ByteArrayOutputStream();
81         byte[] buf = new byte[1024];
82         while(true) {
83             int numread = System.in.read(buf, 0, buf.length);
84             if (numread==-1) break;
85             baos.write(buf, 0, numread);
86         }
87         Client client = new Client(args.length==0 ? "main.bit" : args[0], baos.toByteArray());
88         while(true) {
89             long result = client.readWord();
90             System.err.print(result);
91             System.err.print(" 0x");
92             System.err.print(Long.toString(result, 16));
93             System.err.println();
94         }
95     }
96 }