d1158ea7e363597b8165e67a1be339d5e98c7a6a
[fleet.git] / src / edu / berkeley / fleet / two / ShipDescription.java
1 package edu.berkeley.fleet.two;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.fpga.verilog.Verilog.PercolatedPort;
4 import java.io.*;
5 import java.util.*;
6
7 /** NOT YET FINALIZED: A description (specification) of a ship */
8 public class ShipDescription implements Iterable<DockDescription> {
9
10     private String name;
11     private LinkedHashMap<String,DockDescription> docks     = new LinkedHashMap<String,DockDescription>();
12     private HashMap<String,String>                sections  = new HashMap<String,String>();
13     private HashMap<String,Constant>              constants = new HashMap<String,Constant>();
14
15     public String getName() { return name; }
16     public String getSection(String sectionName) { return sections.get(sectionName); }
17     public DockDescription getDockDescription(String name) { return docks.get(name); }
18     public Iterator<DockDescription> iterator() { return docks.values().iterator(); }
19
20     public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
21
22     public ShipDescription(String name, BufferedReader r) throws IOException {
23         if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
24         this.name = name;
25         String sectionName = null;
26         StringBuffer sb = new StringBuffer();
27         while(true) {
28             String s = r.readLine();
29             if (s==null || s.startsWith("==")) {
30                 if (sectionName != null) sections.put(sectionName, sb.toString());
31                 if (s==null) break;
32                 sb = new StringBuffer();
33                 sectionName = s.trim();
34                 while(sectionName.startsWith("="))
35                     sectionName = sectionName.substring(1);
36                 while(sectionName.endsWith("="))
37                     sectionName = sectionName.substring(0, sectionName.length()-1);
38                 sectionName = sectionName.trim().toLowerCase();
39                 continue;
40             }
41             sb.append(s+"\n");
42         }
43         for(String s : sections.keySet())
44             processSection(s);
45     }
46
47     public Constant getConstant(String name) {
48         return constants.get(name);
49     }
50
51     private void processSection(String section) throws IOException {
52         if (section.equals("")) {
53             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
54             for(String s = br.readLine(); s != null; s = br.readLine()) {
55                 if (s.trim().length()==0) continue;
56                 String key = s.substring(0, s.indexOf(':')).trim();
57                 String val = s.substring(s.indexOf(':')+1).trim();
58                 if (key.toLowerCase().equals("ship"))
59                     name = val.trim();
60             }
61         } else if (section.equals("constants")) {
62             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
63             for(String s = br.readLine(); s != null; s = br.readLine()) {
64                 if (s.indexOf(':')==-1) continue;
65                 String key = s.substring(0, s.indexOf(':')).trim();
66                 if (key.startsWith("constant")) {
67                     String constname = key.substring("constant".length()+1).trim();
68                     String val       = s.substring(s.indexOf(':')+1).trim();
69                     constants.put(constname, new Constant(val));
70                 }
71             }
72         } else if (section.equals("ports")) {
73             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
74             boolean rightSide = false;
75             DockDescription p = null;
76             for(String s = br.readLine(); s != null; s = br.readLine()) {
77                 if (s.trim().length()==0) { rightSide = true; continue; }
78
79                 String key = s.substring(0, s.indexOf(':')).trim();
80                 boolean inbox = false;
81                 key = key.replaceAll("  +", " ");
82                 if      (key.equals("data in"))   { inbox = true;  }
83                 else if (key.equals("data out"))  { inbox = false; }
84                 else if (key.equals("in"))        { inbox = true;  }
85                 else if (key.equals("out"))       { inbox = false; }
86                 else if (key.startsWith("percolate")) { 
87                     key = s;
88                     key = key.substring("percolate".length()+1).trim();
89                     boolean up = key.startsWith("up");
90                     key = key.substring(key.indexOf(':')+1).trim();
91                     String name = key.substring(0, key.indexOf(' '));
92                     int width = Integer.parseInt(key.substring(key.indexOf(' ')).trim());
93                     percolatedPorts.add(new PercolatedPort(name, width, up));
94                     continue;
95                 }
96                 else if (key.startsWith("constant")) {
97                     String constname = key.substring("constant".length()+1).trim();
98                     String val       = s.substring(s.indexOf(':')+1).trim();
99                     p.addConstant(constname, new Constant(val));
100                     continue;
101                 } else if (key.startsWith("shortcut to")) {
102                     continue;
103                 }
104                 else throw new RuntimeException("unknown port type: \""+key+"\"");
105
106                 p = null;
107                 String val = s.substring(s.indexOf(':')+1).trim();
108                 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
109                 String dest    = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1)  : "";
110                 p = docks.get(boxname);
111                 if (p==null) {
112                     p = new DockDescription(this, boxname, !rightSide, inbox);
113                     docks.put(boxname, p);
114                 }
115             }
116         }
117     }
118
119     public void printTeX(PrintWriter pw) throws Exception {
120         ShipDescription sd = this;
121         pw.println("\\pagebreak");
122         pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
123         pw.println("\\addcontentsline{toc}{subsection}{"+sd.getName()+"}");
124         String tex = sd.getSection("tex");
125         /*
126         for(DockDescription bbd : sd) {
127             pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
128         }
129         */
130         int boxGap = 5;
131         int boxHeight = 25;
132         int boxWidth = 75;
133
134         int leftSize = 0;
135         int rightSize = 0;
136         for(DockDescription bbd : sd)
137             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
138             else              rightSize += (boxHeight+boxGap);
139
140         int totalHeight = Math.max(leftSize, rightSize);
141         int shipWidth = (int)(boxWidth * 1.5);
142         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
143
144         pw.println("");
145         pw.println("\\begin{center}");
146         pw.println("\\begin{empfile}["+sd.getName()+"]");
147         pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
148         pw.println("  beginfig(1)");
149         pw.println("      pickup pencircle scaled 1pt;");
150         pw.println("      draw "+
151                    "("+((totalWidth-shipWidth)/2)+","+0+")--"+
152                    "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
153                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
154                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
155                    "("+((totalWidth-shipWidth)/2)+","+0+");");
156         int left = 0;
157         int right = 0;
158         for(DockDescription bbd : sd) {
159             int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
160             int half = (totalWidth-shipWidth)/2;
161             int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
162             int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
163             if (bbd.isInputDock()) {
164                 int p1x = p1;
165                 p1 = p3;
166                 p3 = p1x;
167             }
168             boolean goo = ((bbd.isLeft() && bbd.isInputDock()) || (!bbd.isLeft() && bbd.isOutputDock()));
169             int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
170             if (bbd.isLeft()) left += (boxHeight+boxGap);
171             else              right += (boxHeight+boxGap);
172             if (goo) {
173                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
174             } else {
175                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
176             }
177             pw.println("      draw "+
178                        "  ("+p1+","+ypos+")--"+
179                        "  ("+p2+","+ypos+")--"+
180                        "  ("+p3+","+(ypos-(boxHeight/2))+")--"+
181                        "  ("+p2+","+(ypos-boxHeight)+")--"+
182                        "  ("+p1+","+(ypos-boxHeight)+")--"+
183                        "  ("+p1+","+ypos+");");
184             if (bbd.isLeft()) leftSize += boxHeight;
185             else              rightSize += boxHeight;
186         }
187         pw.println("  endfig;");
188         pw.println("\\end{emp}");
189         pw.println("\\end{empfile}");
190         pw.println("\\end{center}");
191         pw.println("");
192
193         if (tex!=null)
194             pw.println(tex);
195     }
196
197     // FIXME: merge with BitMask
198     public class Constant {
199         public long    setbits   = 0;
200         public long    clearbits = 0;
201         public boolean signExtend = false;
202         public int     numberOffset = 0;
203         public int     numberWidth = 0;
204         public Constant(String s) {
205             if (s.startsWith("0x")) {
206                 setbits = Long.parseLong(s.substring(2), 16);
207                 clearbits = ~setbits;
208             } else if (s.length() == 37) {
209                 for(int i=36; i>=0; i--) {
210                     char c = s.charAt(36-i);
211                     switch(c) {
212                         case '0': clearbits |= (1<<i); break;
213                         case '1': setbits   |= (1<<i); break;
214                         case '.': break;
215                         case 's': signExtend = true;  numberOffset = i; numberWidth++; break;
216                         case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
217                     }
218                 }
219             } else {
220                 setbits = Long.parseLong(s);
221                 clearbits = ~setbits;
222             }
223         }
224     }
225
226 }