1 package edu.berkeley.fleet.two;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.fpga.verilog.Verilog.PercolatedPort;
7 /** NOT YET FINALIZED: A description (specification) of a ship */
8 public class ShipDescription implements Iterable<DockDescription> {
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>();
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(); }
20 public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
22 public ShipDescription(String name, BufferedReader r) throws IOException {
23 if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
25 String sectionName = null;
26 StringBuffer sb = new StringBuffer();
28 String s = r.readLine();
29 if (s==null || s.startsWith("==")) {
30 if (sectionName != null) sections.put(sectionName, sb.toString());
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();
43 for(String s : sections.keySet())
47 public Constant getConstant(String name) {
48 return constants.get(name);
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"))
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));
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; }
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")) {
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));
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));
101 } else if (key.startsWith("shortcut to")) {
104 else throw new RuntimeException("unknown port type: \""+key+"\"");
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);
112 p = new DockDescription(this, boxname, !rightSide, inbox);
113 docks.put(boxname, p);
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");
126 for(DockDescription bbd : sd) {
127 pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
136 for(DockDescription bbd : sd)
137 if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
138 else rightSize += (boxHeight+boxGap);
140 int totalHeight = Math.max(leftSize, rightSize);
141 int shipWidth = (int)(boxWidth * 1.5);
142 int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
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;");
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+");");
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()) {
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);
173 pw.println(" label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
175 pw.println(" label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
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;
187 pw.println(" endfig;");
188 pw.println("\\end{emp}");
189 pw.println("\\end{empfile}");
190 pw.println("\\end{center}");
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);
212 case '0': clearbits |= (1<<i); break;
213 case '1': setbits |= (1<<i); break;
215 case 's': signExtend = true; numberOffset = i; numberWidth++; break;
216 case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
220 setbits = Long.parseLong(s);
221 clearbits = ~setbits;