1 package edu.berkeley.fleet;
3 import java.lang.reflect.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.bind.*;
8 import edu.berkeley.sbp.util.*;
11 import edu.berkeley.fleet.ships.*;
15 /** some "halt ship" can turn this on to stop the interpreter */
16 public boolean halt = false;
18 public int[] mem = new int[0];
19 public ArrayList<String> imports = new ArrayList<String>();
21 public ArrayList<Ship> shiplist = new ArrayList<Ship>();
22 public HashMap<String,Ship> ships = new HashMap<String,Ship>();
26 for(Ship ship : ships.values())
27 for(int j=0; j<10; j++)
30 // run the ships a bit longer for good measure
31 for(int i=0; i<100; i++)
32 for(Ship ship : ships.values())
33 for(int j=0; j<10; j++)
36 // check the state of the ships
37 for(Ship ship : ships.values())
40 Log.println(Log.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======"));
43 public void dumpMem() {
44 Log.print(Log.cyan(" MEMORY: "));
45 for(int i=0; i<mem.length; i++) {
46 if ((i%10)==0 && i!=0) Log.print(Log.cyan(" "));
47 Log.print(Log.cyan(mem[i] + " "));
48 if ((i%10)==9 && i!=mem.length-1) Log.println("");
53 public void writeMem(int addr, int data) {
54 if (addr >= mem.length) {
55 int[] mem2 = new int[addr*2+1];
56 System.arraycopy(mem, 0, mem2, 0, mem2.length);
62 public Ship getShip(String name) {
63 Ship s = ships.get(name);
64 if (s == null) throw new RuntimeException("unknown ship \""+name+"\"");
68 boolean tryCreate(String classname, String shipname) {
70 Class c = Class.forName(classname);
71 Constructor con = c.getConstructor(new Class[] { Fleet.class, String.class });
72 con.newInstance(new Object[] { this, shipname });
74 } catch (Exception e) {
79 public void sendToken(Port source, Port dest) {
80 Log.token(source, dest);
81 dest.addTokenFromFabric();
84 public void sendData(Port source, int data, Port dest) {
85 Log.data(data+"", source, dest);
86 dest.addDataFromFabric(data);
89 public void dumpFabric(boolean quiet) {
90 // FIXME: this is really ugly: the order of port declarations in
91 // the XXXShip.java file must match the order in the .balsa file!
93 ArrayList instructionports = new ArrayList<Port>();
94 for(Ship ship : shiplist)
95 for(Port port : ship.portlist)
96 if (!(port.getName().equals("out") && port.getShip() instanceof DebugShip))
97 instructionports.add(port);
98 FabricTree instructions =
99 new FabricTree((Port[])instructionports.toArray(new Port[0]),
103 ArrayList inputports = new ArrayList<Port>();
104 for(Ship ship : shiplist)
105 for(Port port : ship.portlist)
106 if (!(port.getName().equals("out") && port.getShip() instanceof DebugShip))
107 inputports.add(port);
109 new FabricTree((Port[])inputports.toArray(new Port[0]),
113 ArrayList outputports = new ArrayList<Port>();
114 for(Ship ship : shiplist)
115 for(Port port : ship.portlist)
116 if (!(port.getName().equals("out") && port.getShip() instanceof DebugShip))
117 outputports.add(port);
119 new FabricTree((Port[])outputports.toArray(new Port[0]),
124 System.out.println("import [balsa.sim.portio]");
125 System.out.println("import [xbit]");
126 System.out.println("import [funnel]");
127 System.out.println("import [horn]");
128 System.out.println("import [par2ser]");
129 System.out.println("import [ser2par]");
131 HashSet<Class> added = new HashSet<Class>();
132 for(Ship ship : shiplist)
133 if (!added.contains(ship.getClass())) {
134 added.add(ship.getClass());
135 System.out.println("import ["+ship.getBalsaName()+"]");
138 System.out.println("procedure fabric(input top_in:XBit; output top_out:XBit)");
139 System.out.println("is");
140 instructions.dumpChannels(true);
141 outputs.dumpChannels(true);
142 inputs.dumpChannels(true);
143 for(Ship ship : shiplist)
144 for(Port port : ship.portlist)
145 System.out.println(" channel data_"+ship.getName()+"_"+port.getName()+" : XBit");
146 System.out.println("begin");
147 System.out.println(" loop source -> dest end");
148 System.out.println(" || loop top_in -> instruction end");
149 System.out.println(" || loop data_debug_out -> top_out end");
150 System.out.println("");
151 instructions.dumpChannels(false);
152 System.out.println("");
153 outputs.dumpChannels(false);
154 System.out.println("");
155 inputs.dumpChannels(false);
156 System.out.println("");
157 for(Ship ship : shiplist) {
158 System.out.print(" || ");
159 System.out.print(ship.getBalsaName());
160 System.out.print("(");
161 boolean first = true;
162 for(Port port : ship.portlist) {
163 if (!first) System.out.print(", ");
165 System.out.print("data_"+port.getShip().getName()+"_"+port.getName());
166 System.out.print(" ");
168 System.out.println(")");
170 for(Port port : ship.portlist) {
171 if (ship instanceof DebugShip && port instanceof Outbox)
173 System.out.print(" || ");
174 if (port instanceof Inbox)
175 System.out.print("inbox(");
177 System.out.print("outbox(");
178 System.out.print("instruction_"+port.getShip().getName()+"_"+port.getName());
179 System.out.print(", ");
180 System.out.print("dest_"+port.getShip().getName()+"_"+port.getName());
181 System.out.print(", ");
182 System.out.print("source_"+port.getShip().getName()+"_"+port.getName());
183 System.out.print(", ");
184 System.out.print("data_"+port.getShip().getName()+"_"+port.getName());
185 System.out.print(")");
186 System.out.println();
190 System.out.println("end");
193 private static class FabricTree {
197 public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
198 public FabricTree(Port[] ports, String component, String prefix) {
199 this.prefix = prefix;
200 root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
202 private Object mkNode(String name, String component, Port[] ports, int start, int end, int addr, int bits) {
203 if (end-start == 0) return null;
204 if (end-start == 1) {
205 Port p = ports[start];
206 if (prefix.equals("instruction")) {
216 return new Node(name,
218 mkNode(name+"_0", component, ports, start, start+len/2, (addr<<1)|0, bits+1),
219 mkNode(name+"_1", component, ports, start+len/2, end, (addr<<1)|1, bits+1),
223 private String describe(String prefix, Object o) {
224 if (o==null) return null;
225 if (o instanceof Port) {
227 return prefix+"_"+p.getShip().getName()+"_"+p.getName();
229 if (o instanceof Node) {
230 return ((Node)o).describe(prefix);
241 public Node(String name, String component, Object left, Object right, int addr, int bits) {
245 this.component = component;
249 public void dumpChannels(int indentamount, boolean decl) {
251 for(int i=0; i<indentamount; i++) indent += " ";
253 System.out.println(" channel "+indent+describe(prefix)+" : XBit");
255 System.out.println(" || "+indent+
256 component+"("+describe(prefix)+
257 ", "+FabricTree.this.describe(prefix, left)+
258 ", "+FabricTree.this.describe(prefix, right)+")");
260 dumpChannels(left, indentamount+1, decl);
261 dumpChannels(right, indentamount+1, decl);
263 public void dumpChannels(Object o, int indentamount, boolean decl) {
265 if (o instanceof Node) {
266 ((Node)o).dumpChannels(indentamount, decl);
269 for(int i=0; i<indentamount; i++) indent += " ";
271 System.out.println(" channel "+indent+FabricTree.this.describe(prefix,o)+" :XBit");
274 public String describe(String prefix) {