major overhaul of FPGA code to support both ML509 and Bee2 at the same time
[fleet.git] / src / edu / berkeley / fleet / fpga / Bee2.java
1 package edu.berkeley.fleet.fpga;
2 import edu.berkeley.fleet.fpga.*;
3 import edu.berkeley.fleet.api.*;
4 import java.io.*;
5 import gnu.io.*;
6 import java.io.*;
7 import java.net.*;
8 import java.util.*;
9 import java.util.concurrent.Semaphore;
10
11 public class Bee2 extends Fpga {
12
13     /*
14       Secondly, here is the EDK core:
15
16       http://repository.eecs.berkeley.edu/viewvc/Projects/BEE/trunk/2/hardware/pcores/interchip_block_v1_00_a/
17
18       This core generates the links for both control-user and user-user using some crazy scripts possibly written by Yury. Some EDK system examples are here:
19
20       http://repository.eecs.berkeley.edu/viewvc/Projects/BEE/trunk/2/applications/reference/MPI_demo/XPS_Ctrlfpga/
21       http://repository.eecs.berkeley.edu/viewvc/Projects/BEE/trunk/2/applications/reference/MPI_demo/XPS_Userfpga/
22
23       You can either attempt to execute these scripts in order to generate output for your desired configuration or use already generated output for a 100 Mhz configuration as we did when we converted RAMP Blue to RDL:
24
25       http://repository.eecs.berkeley.edu/viewvc/Projects/RAMP/tags/RAMPBlue_Final/Blue/UserFPGA/Infrastructure/Interchip/
26
27       You can see how we used the generated output here:
28
29       http://repository.eecs.berkeley.edu/viewvc/Projects/RAMP/tags/RAMPBlue_Final/Blue/UserFPGA/
30
31       I may be able to assist you further if you run into difficulties, but my memory of how this is supposed to work is hazy at best--and I never understood the interchip core scripts anyway.
32
33       Alex
34
35     */
36     protected String getDirName() { return "bee2"; }
37
38     public Bee2() throws IOException {
39         for(int i=0; i<2; i++) createShip("Alu");
40         for(int i=0; i<1; i++) createShip("Memory");
41         for(int i=0; i<2; i++) createShip("Fifo");
42         //createShip("Random");
43         //createShip("CarrySaveAdder");
44         createShip("Rotator");
45         createShip("Lut3");
46         createShip("Counter");
47         /*
48           for(int i=0; i<2; i++)  createShip("Memory");
49           for(int i=0; i<6; i++)  createShip("Alu");
50           for(int i=0; i<1; i++)  createShip("Fifo");
51           for(int i=0; i<12; i++) createShip("Counter");
52         
53           //createShip("CarrySaveAdder");
54           //createShip("Rotator");
55           createShip("Random");
56           createShip("Button");
57           createShip("Timer");
58         */
59         init();
60     }
61
62
63     // Server //////////////////////////////////////////////////////////////////////////////
64
65     private static class Gobbler extends Thread {
66         private final BufferedReader br;
67         public Gobbler(InputStream is) {
68             this.br = new BufferedReader(new InputStreamReader(is));
69         }
70         public void run() {
71             try {
72                 for(String s = br.readLine(); s!=null; s=br.readLine())
73                     System.err.println("  > " + s);
74             } catch (Exception e) { throw new RuntimeException(e); }
75         }
76     }
77
78     private static RandomAccessFile raf;
79     private static OutputStream fos;
80     private static InputStream fis;
81
82     public static String pass_string = "password=security_is_for_wimps ";
83
84     public static void main(String[] args) throws Exception {
85         if (args.length != 2) {
86             System.err.println("usage: java " + Bee2.class.getName() + " {-client|-server} <fpganum>");
87             System.exit(-1);
88         }
89
90         int idx = Integer.parseInt(args[1]);
91         String host = "bee2";
92         Process proc;
93         int res;
94
95         if (args[0].equals("-client")) {
96
97             System.err.println("== rsyncing");
98             proc = Runtime.getRuntime().exec("rsync -are ssh --progress --verbose fleet.jar build/bee2/main.bit misc/bicat.c "+host+":");
99             new Gobbler(proc.getInputStream()).start();
100             res = proc.waitFor();
101             if (res != 0) throw new RuntimeException("nonzero exit code");
102
103             System.err.println("== (un)programming fpga " + idx);
104             proc = Runtime.getRuntime().exec("ssh "+host+" -- user_unprogram "+idx+"; user_program "+idx+" main.bit");
105             new Gobbler(proc.getInputStream()).start();
106             res = proc.waitFor();
107             if (res != 0) throw new RuntimeException("nonzero exit code");
108
109             System.err.println("== launching java");
110             proc = Runtime.getRuntime().exec("ssh "+host+" -- gcc -o bicat bicat.c && ./bicat /dev/selectmap"+idx);
111             //return proc.getInputStream();
112             
113         } else if (args[0].equals("-server")) {
114
115             raf = new RandomAccessFile(new File("/dev/selectmap"+idx), "rw");
116             fos = new FileOutputStream(raf.getFD());
117             fis = new BufferedInputStream(new FileInputStream(raf.getFD()));
118
119             //socket.setKeepAlive(true);
120             //final InputStream is = new BufferedInputStream(socket.getInputStream());
121             //final InputStream is = socket.getInputStream();
122             //final OutputStream os = socket.getOutputStream(); //new BufferedOutputStream(socket.getOutputStream());
123
124             final InputStream is = System.in;
125             final OutputStream os = System.out;
126             PrintStream log = new PrintStream(new FileOutputStream("log"));
127                 
128             //socket.setSoTimeout(10);
129             while(true) {
130                 boolean ok = false;
131                 while (fis.available() > 0) {
132                     ok = true;
133                     int val = fis.read();
134                     if (val==-1) break;
135                     log.println("fpga->host: 0x"+Integer.toString(val & 0xff, 16));
136                     if ((val & (3<<6)) == 0)
137                         fos.write( (1<<6) | 1);
138                     os.write((byte)val);
139                     os.flush();
140                 }
141                 while(is.available() > 0) {
142                     int r = is.read();
143                     if (r == -1) break;
144                     ok = true;
145                     log.println("host->fpga: 0x"+Integer.toString(r & 0xff, 16));
146                     fos.write(r);
147                 }
148                 if (!ok) {
149                     log.println("flushing os...");
150                     os.flush();
151                     log.println("flushing fos...");
152                     fos.flush();
153                     log.println("sleeping...");
154                     Thread.sleep(10);                    
155                 }
156             }
157         }
158     }
159 }