+++ /dev/null
-package edu.berkeley.fleet;
-
-import java.lang.reflect.*;
-import edu.berkeley.sbp.chr.*;
-import edu.berkeley.sbp.misc.*;
-import edu.berkeley.sbp.meta.*;
-import edu.berkeley.sbp.bind.*;
-import edu.berkeley.sbp.util.*;
-import java.util.*;
-import java.io.*;
-import edu.berkeley.fleet.ships.*;
-
-public class Fleet {
-
- /** some "halt ship" can turn this on to stop the interpreter */
- public boolean halt = false;
-
- public int[] mem = new int[0];
- public ArrayList<String> imports = new ArrayList<String>();
-
- public ArrayList<Ship> shiplist = new ArrayList<Ship>();
- public HashMap<String,Ship> ships = new HashMap<String,Ship>();
-
- public void go() {
- while(!halt)
- for(Ship ship : ships.values())
- for(int j=0; j<10; j++)
- ship._service();
-
- // run the ships a bit longer for good measure
- for(int i=0; i<100; i++)
- for(Ship ship : ships.values())
- for(int j=0; j<10; j++)
- ship._service();
-
- // check the state of the ships
- for(Ship ship : ships.values())
- ship.shutdown();
-
- Log.println(Log.yellow(" DONE: ====== FLEET is halted. Have a nice day. ======"));
- }
-
- public void dumpMem() {
- Log.print(Log.cyan(" MEMORY: "));
- for(int i=0; i<mem.length; i++) {
- if ((i%10)==0 && i!=0) Log.print(Log.cyan(" "));
- Log.print(Log.cyan(mem[i] + " "));
- if ((i%10)==9 && i!=mem.length-1) Log.println("");
- }
- Log.println();
- }
-
- public void writeMem(int addr, int data) {
- if (addr >= mem.length) {
- int[] mem2 = new int[addr*2+1];
- System.arraycopy(mem, 0, mem2, 0, mem2.length);
- mem = mem2;
- }
- mem[addr] = data;
- }
-
- public Ship getShip(String name) {
- Ship s = ships.get(name);
- if (s == null) throw new RuntimeException("unknown ship \""+name+"\"");
- return s;
- }
-
- boolean tryCreate(String classname, String shipname) {
- try {
- Class c = Class.forName(classname);
- Constructor con = c.getConstructor(new Class[] { Fleet.class, String.class });
- con.newInstance(new Object[] { this, shipname });
- return true;
- } catch (Exception e) {
- return false;
- }
- }
-
- public void sendToken(Port source, Port dest) {
- Log.token(source, dest);
- dest.addTokenFromFabric();
- }
-
- public void sendData(Port source, int data, Port dest) {
- Log.data(data+"", source, dest);
- dest.addDataFromFabric(data);
- }
-
- public void dumpFabric(boolean quiet) {
- // FIXME: this is really ugly: the order of port declarations in
- // the XXXShip.java file must match the order in the .balsa file!
-
- ArrayList instructionports = new ArrayList<Port>();
- for(Ship ship : shiplist)
- for(Port port : ship.portlist)
- if (!port.special())
- instructionports.add(port);
- FabricTree instructions =
- new FabricTree((Port[])instructionports.toArray(new Port[0]),
- "ihorn",
- "instruction");
-
- ArrayList inputports = new ArrayList<Port>();
- for(Ship ship : shiplist)
- for(Port port : ship.portlist)
- if (!port.special())
- inputports.add(port);
- FabricTree inputs =
- new FabricTree((Port[])inputports.toArray(new Port[0]),
- "horn",
- "dest");
-
- ArrayList outputports = new ArrayList<Port>();
- for(Ship ship : shiplist)
- for(Port port : ship.portlist)
- if (!port.special())
- outputports.add(port);
- FabricTree outputs =
- new FabricTree((Port[])outputports.toArray(new Port[0]),
- "funnel",
- "source");
-
- if (quiet) return;
- System.out.println("`include \"macros.v\"");
- /*
- HashSet<Class> added = new HashSet<Class>();
- for(Ship ship : shiplist)
- if (!added.contains(ship.getClass())) {
- added.add(ship.getClass());
- System.out.println("import ["+ship.getBalsaName()+"]");
- }
- */
- System.out.println("module fabric(clk, top_r, top_a, top,");
- System.out.println(" data_debug_out_r, data_debug_out_a, data_debug_out);");
- System.out.println(" input clk;");
- System.out.println(" input top_r;");
- System.out.println(" output top_a;");
- System.out.println(" input [(`PACKET_WIDTH-1):0] top;");
- // System.out.println(" wire [(`PACKET_WIDTH-1):0] dest;");
- System.out.println(" output data_debug_out_r;");
- System.out.println(" input data_debug_out_a;");
- System.out.println(" output [(`PACKET_WIDTH-1):0] data_debug_out;");
- System.out.println(" wire [(`INSTRUCTION_WIDTH-1):0] data_command_out;");
- System.out.println();
-
- System.out.println();
-
- instructions.dumpChannels(true);
- outputs.dumpChannels(true);
- inputs.dumpChannels(true);
- for(Ship ship : shiplist)
- for(Port port : ship.portlist) {
- if (ship instanceof CommandShip && port instanceof Outbox) continue;
- System.out.println(" wire [(`PACKET_WIDTH-1):0] data_"+ship.getName()+"_"+port.getName()+";");
- }
-
- System.out.println("");
- instructions.dumpChannels(false);
- System.out.println("");
- outputs.dumpChannels(false);
- System.out.println("");
- inputs.dumpChannels(false);
- System.out.println("");
- for(Ship ship : shiplist) {
- System.out.print(ship.getBalsaName());
- System.out.print(" ");
- System.out.print("krunk"+(krunk++));
- System.out.print("(clk, ");
- boolean first = true;
- for(Port port : ship.portlist) {
- if (!first) System.out.print(", ");
- first = false;
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName()+"_r, ");
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName()+"_a, ");
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName());
- System.out.print(" ");
- }
- System.out.println(");");
-
- for(Port port : ship.portlist) {
- if (port.special()) continue;
- if (port instanceof Inbox) {
- if (port.noInbox())
- System.out.print("stupidinbox");
- else
- System.out.print("inbox");
- } else {
- System.out.print("outbox");
- }
- System.out.print(" krunk"+(krunk++)+"(clk, ");
- System.out.print("instruction_"+port.getShip().getName()+"_"+port.getName()+"_r, ");
- System.out.print("instruction_"+port.getShip().getName()+"_"+port.getName()+"_a, ");
- System.out.print("instruction_"+port.getShip().getName()+"_"+port.getName()+", ");
- System.out.print("dest_"+port.getShip().getName()+"_"+port.getName()+"_r, ");
- System.out.print("dest_"+port.getShip().getName()+"_"+port.getName()+"_a, ");
- System.out.print("dest_"+port.getShip().getName()+"_"+port.getName()+", ");
- System.out.print("source_"+port.getShip().getName()+"_"+port.getName()+"_r, ");
- System.out.print("source_"+port.getShip().getName()+"_"+port.getName()+"_a, ");
- System.out.print("source_"+port.getShip().getName()+"_"+port.getName()+", ");
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName()+"_r, ");
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName()+"_a, ");
- System.out.print("data_"+port.getShip().getName()+"_"+port.getName());
- System.out.print(");");
- System.out.println();
- }
-
- }
- System.out.println("funnel topfun(clk, dest_r, dest_a, dest, source_r, source_a, source, top_r, top_a, top);");
- System.out.println("");
- System.out.println(" assign instruction_r = data_command_out_r;");
- System.out.println(" assign data_command_out_a = instruction_a;");
- System.out.println(" assign instruction = data_command_out;");
- System.out.println("endmodule");
- }
-
- private static class FabricTree {
- int master_idx = 1;
- String prefix;
- Node root;
- public void dumpChannels(boolean decl) { root.dumpChannels(0, decl); }
- public FabricTree(Port[] ports, String component, String prefix) {
- this.prefix = prefix;
- root = (Node)mkNode("", component, ports, 0, ports.length, 0, 0);
- }
- private Object mkNode(String name, String component, Port[] ports, int start, int end, int addr, int bits) {
- if (end-start == 0) return null;
- if (end-start == 1) {
- Port p = ports[start];
- if (prefix.equals("instruction")) {
- p.instr_addr = addr;
- p.instr_bits = bits;
- } else {
- p.addr = addr;
- p.bits = bits;
- }
- return p;
- }
- int len = end-start;
- return new Node(name,
- component,
- mkNode(name+"_0", component, ports, start, start+len/2, addr, bits+1),
- mkNode(name+"_1", component, ports, start+len/2, end, addr | (1 << bits), bits+1),
- addr,
- bits);
- }
- private String describe(String prefix, Object o) {
- if (o==null) return null;
- if (o instanceof Port) {
- Port p = (Port)o;
- return prefix+"_"+p.getShip().getName()+"_"+p.getName();
- }
- if (o instanceof Node) {
- return ((Node)o).describe(prefix);
- }
- return null;
- }
- private class Node {
- Object left;
- Object right;
- String name;
- String component;
- int addr;
- int bits;
- public Node(String name, String component, Object left, Object right, int addr, int bits) {
- this.left = left;
- this.right = right;
- this.name = name;
- this.component = component;
- this.addr = addr;
- this.bits = bits;
- }
- public void dumpChannels(int indentamount, boolean decl) {
- String indent = "";
- for(int i=0; i<indentamount; i++) indent += " ";
- if (decl) {
- String n = describe(prefix).startsWith("instruction") ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
- System.out.println(" wire "+n+" "+indent+describe(prefix)+";");
- } else {
- System.out.println(" "+indent+
- component+" "+
- "krunk"+(krunk++)+"(clk, "+
- describe(prefix)+"_r, "+
- describe(prefix)+"_a, "+
- describe(prefix)+", "+
- FabricTree.this.describe(prefix, left)+"_r, "+
- FabricTree.this.describe(prefix, left)+"_a, "+
- FabricTree.this.describe(prefix, left)+", "+
- FabricTree.this.describe(prefix, right)+"_r, "+
- FabricTree.this.describe(prefix, right)+"_a, "+
- FabricTree.this.describe(prefix, right)+
- ");");
- }
- dumpChannels(left, indentamount+1, decl);
- dumpChannels(right, indentamount+1, decl);
- }
- public void dumpChannels(Object o, int indentamount, boolean decl) {
- if (o==null) return;
- if (o instanceof Node) {
- ((Node)o).dumpChannels(indentamount, decl);
- } else {
- String indent = "";
- for(int i=0; i<indentamount; i++) indent += " ";
- if (decl) {
- String n = FabricTree.this.describe(prefix,o).startsWith("instruction")
- ? "[(`INSTRUCTION_WIDTH-1):0]" : "[(`PACKET_WIDTH-1):0]";
- System.out.println(" wire "+n+" "+indent+FabricTree.this.describe(prefix,o)+";");
- }
- }
- }
- public String describe(String prefix) {
- return prefix+name;
- }
- }
- }
- public static int krunk=0;
-}