b259d3e283d1bff8bc3203d3f5141a91164300fd
[fleet.git] / src / edu / berkeley / fleet / Main.java
1 package edu.berkeley.fleet;
2 import edu.berkeley.sbp.util.ANSI;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.fpga.*;
5 import edu.berkeley.fleet.interpreter.*;
6 import edu.berkeley.fleet.two.*;
7 import edu.berkeley.fleet.util.*;
8 import java.io.*;
9 import java.util.*;
10
11 public class Main {
12
13     static String command;
14     static HashMap<String,String> options = new HashMap<String,String>();
15     static ArrayList<String> args = new ArrayList<String>();
16
17     public static void main(String[] s) throws Exception {
18         if (s.length == 0) {
19             usage();
20             System.exit(-1);
21         }
22         boolean optionsDone = false;
23         for(int i=0; i<s.length; i++) {
24             if (!optionsDone && s[i].indexOf('=') != -1) {
25                 options.put(s[i].substring(0, s[i].indexOf('=')),
26                             s[i].substring(s[i].indexOf('=')+1));
27                
28             } else if (!optionsDone) {
29                 optionsDone = true;
30                 command = s[i];
31             } else {
32                 args.add(s[i]);
33             }
34         }
35
36         String target = options.get("target");
37         Fleet fleet;
38         if ("fpga".equals(target)) {
39             String bitfile = options.get("bitfile");
40             fleet = new Fpga();
41         } else if ("sim".equals(target) || "fleetsim".equals(target)) {
42             fleet = (Fleet)Class.forName("com.sunlabs.fleetsim.fleet.FleetDescription").newInstance();
43         } else {
44             fleet = new Interpreter();
45         }
46
47         if (!"yes".equals(options.get("verbose")))
48             Log.log = null;
49
50         if (command.equals("run")) {
51             InputStream is;
52             String filename = args.get(0);
53             ByteArrayOutputStream baos = new ByteArrayOutputStream();
54             Reader r = new InputStreamReader(new FileInputStream(args.get(0)));
55             run(fleet, edu.berkeley.fleet.assembler.Main.assemble(fleet, r));
56
57         } else if (command.equals("expand")) {
58             String name = new File(args.get(0)).getName();
59             if (name.endsWith(".ship"))
60                 name = name.substring(0, name.length() - ".ship".length());
61             if (fleet instanceof edu.berkeley.fleet.fpga.Fpga) {
62                 ((edu.berkeley.fleet.fpga.Fpga)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
63             } else {
64                 ((Interpreter)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
65             }
66
67         } else if (command.equals("doc")) {
68             if (!new File(".tmp").exists())
69                 new File(".tmp").mkdirs();
70             PrintWriter pw = new PrintWriter(new FileOutputStream(".tmp/FleetTwo.Manual.tex"));
71             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("doc/archman.tex")));
72             for(String ss = br.readLine(); ss!=null; ss = br.readLine())
73                 pw.println(ss);
74             for(String f : new File("ships").list()) {
75                 new ShipDescription(f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))).printTeX(pw);
76             }
77             pw.println("\\end{document}");
78             pw.close();
79
80         } else if (command.equals("test")) {
81             for(int i=0; i<args.size(); i++)
82                 test(fleet, new File(args.get(i)));
83         } else {
84             usage();
85             System.exit(-1);
86         }
87     }
88
89     static void runTest(Fleet fleet, Reader reader, String title) throws Exception {
90         Instruction[] instructions = null;
91         System.out.print("\r[    ] "  + ANSI.yellow(title)+": ");
92         try {
93             instructions = edu.berkeley.fleet.assembler.Main.assemble(fleet, reader);
94         } finally {
95             if (instructions==null)
96                 System.out.println();
97         }
98         if (edu.berkeley.fleet.assembler.Parser.skip) {
99             System.out.println("\r[" + ANSI.yellow("SKIP") +
100                                "] "  + ANSI.yellow(title));
101             edu.berkeley.fleet.assembler.Parser.skip = false;
102             return;
103         }
104         FleetProcess fp = fleet.run(instructions);
105         //long now = System.currentTimeMillis();
106         try {
107             ArrayList<Long> expect = edu.berkeley.fleet.assembler.Parser.expect;
108             String output = "";
109             // FIXME: check for extraneous stuff at the end
110             String verdict = "[    ]";
111             boolean failed = false;
112             while(true) {
113                 if (output.length() > 60 && !failed)
114                     output = "..."+output.substring(output.length()-57, output.length());
115                 if (!failed && expect.size() == 0) verdict = "["+ANSI.green("PASS")+"]";
116                 System.out.print("\r" + verdict + " " + ANSI.yellow(title) + ": " + output);
117                 if (failed) break;
118                 if (expect.size() == 0) break;
119                 //if (now!=0) { System.err.println(); System.err.println(System.currentTimeMillis()-now); now=0;}
120                 long l = unBitSet(fp.recvWord());
121                 long l2 = expect.remove(0);
122
123                 // FIXME, this is ugly and not size-independent
124                 if ((l & (1L << 36)) != 0) {
125                     l = l | (0xffffffffffffffffL << 37);
126                 }
127                 // FIXME, this is ugly and not size-independent
128                 if ((l2 & (1L << 36)) != 0) {
129                     l2 = l2 | (0xffffffffffffffffL << 37);
130                 }
131
132                 if (l!=l2) {
133                     verdict = "["+ANSI.red("FAIL")+"]";
134                     output += ANSI.red("0x"+Long.toString(l, 16)) +
135                         ANSI.yellow(" (expected ")+ANSI.green("0x"+Long.toString(l2, 16))+ANSI.yellow(")");
136                     failed = true;
137                     continue;
138                 } else {
139                     output += ("0x"+Long.toString(l2, 16) + " ");
140                 }
141             }
142             System.out.println();
143         } finally {
144             fp.terminate();
145         }
146     }
147
148     static void test(Fleet fleet, File f) throws Exception {
149         if (f.isDirectory()) {
150             for(String s : f.list())
151                 test(fleet, new File(f.getPath() + File.separatorChar + s));
152             return;
153         } else if (f.getPath().endsWith(".fleet") || f.getPath().endsWith(".test")) {
154             runTest(fleet, new InputStreamReader(new FileInputStream(f)), f.getPath());
155         } else if (f.getPath().endsWith(".ship")) {
156             ShipDescription sd = new ShipDescription(f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
157             String testsection = sd.getSection("test");
158             if (testsection == null)
159                 System.out.println("no test for " + sd.getName() + "!");
160             else if (fleet.getShip(sd.getName(),0)==null)
161                 System.out.println("specified Fleet does not have any ships of type " + sd.getName());
162             else
163                 runTest(fleet, new StringReader(testsection), sd.getName());
164         }
165     }
166
167     public static void run(Fleet fleet, Instruction[] instructions) throws IOException {
168         FleetProcess client = fleet.run(instructions);
169         while(true) {
170             long result = unBitSet(client.recvWord());
171             System.err.print(result);
172             System.err.print(" 0x");
173             System.err.print(Long.toString(result, 16));
174             System.err.println();
175         }
176     }
177
178     public static long unBitSet(BitVector bs) {
179         long val = 0;
180         for(int i=0; i<37; i++) {
181             if (bs.get(i))
182                 val |= (1L << i);
183         }
184         if ((val & (1L << 36)) != 0)
185             val = val | (0xffffffffffffffffL << 36);
186         return val;
187     }
188
189     static void usage() {
190         System.err.println(".........................................................................");
191         System.err.println("Fleet Framework                              UC Berkeley, Sun Labs / 2007");
192         System.err.println("");
193         System.err.println("usage:  java -jar fleet.jar [options] [command] [filename] [args]");
194         System.err.println("");
195         System.err.println("[options] is in the form key=val; supported keys are:");
196         System.err.println("   target={sim,fpga,interp}");
197         System.err.println("   bitfile=(hardware image for fpga)");
198         System.err.println("   verbose={yes,no}");
199         System.err.println("");
200         System.err.println("[command] is one of:");
201         System.err.println("   asm");
202         System.err.println("   disasm");
203         System.err.println("   typeset");
204         System.err.println("   extract [section]");
205         System.err.println("   run");
206         System.err.println("");
207         System.err.println("[filename] is one of:");
208         System.err.println("   *.fa          Fleet assembler");
209         System.err.println("   *.f0          F0 code (not yet supported)");
210         System.err.println("   *.fo          Fleet binary object");
211         System.err.println("   *.ship        Ship description");
212         System.err.println("");
213         System.err.println("All [args] not consumed by [command] are passed to target");
214         System.err.println("");
215     }
216
217 }