if (name.endsWith(".ship"))
name = name.substring(0, name.length() - ".ship".length());
if (fleet instanceof edu.berkeley.fleet.fpga.Fpga) {
- ((edu.berkeley.fleet.fpga.Fpga)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
+ ((edu.berkeley.fleet.fpga.Fpga)fleet).expand(new ShipDescription(fleet, name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
} else {
- ((Interpreter)fleet).expand(new ShipDescription(name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
+ ((Interpreter)fleet).expand(new ShipDescription(fleet, name, new BufferedReader(new InputStreamReader(new FileInputStream(args.get(0))))));
}
} else if (command.equals("doc")) {
for(String ss = br.readLine(); ss!=null; ss = br.readLine())
pw.println(ss);
for(String f : new File("ships").list()) {
- new ShipDescription(f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))).printTeX(pw);
+ new ShipDescription(fleet, f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))).printTeX(pw);
}
pw.println("\\end{document}");
pw.close();
} else if (f.getPath().endsWith(".fleet") || f.getPath().endsWith(".test")) {
runTest(fleet, new InputStreamReader(new FileInputStream(f)), f.getPath());
} else if (f.getPath().endsWith(".ship")) {
- ShipDescription sd = new ShipDescription(f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
+ ShipDescription sd = new ShipDescription(fleet, f.getName(), new BufferedReader(new InputStreamReader(new FileInputStream(f))));
String testsection = sd.getSection("test");
if (testsection == null)
System.out.println("no test for " + sd.getName() + "!");
/** get a constant associated with a ship; returns null if none found */
public BitVector getConstant(String constantName) {
- return null;
+ throw new RuntimeException("unknown constant \""+constantName+"\" on ship " + this);
}
public String toString() {
}
}
- private long parseSSL(Tree t) {
+ private BitVector parseSSL(Tree t) {
String shipType = name(t.child(0));
String portName = name(t.child(1));
Ship chosenship = null;
Dock chosenport = chosenship.getDock(portName);
Tree specs = t.child(2);
long literal = 0;
- for(int i=0; i<specs.size(); i++) {
- Tree tt = specs.child(i);
- literal |= resolveLiteral(chosenport, stringBody(tt));
- }
- return literal;
+ if (specs.size() != 1) throw new RuntimeException("multiple constants not supported");
+ Tree tt = specs.child(0);
+ return resolveLiteral(chosenport, stringBody(tt));
}
- private static long resolveLiteral(Dock dd, String s) {
+ private static BitVector resolveLiteral(Dock dd, String s) {
long val = 0;
long ret = 0;
boolean hasval = false;
s = s.substring(0, s.indexOf('='));
hasval = true;
}
- ShipDescription.Constant c = ((FleetTwoDock)dd).getDockConstant(s);
- if (c==null) throw new RuntimeException("no constant " + s + " on dock " + dd);
- ret |= c.setbits;
- ret &= ~c.clearbits;
- if (hasval)
- ret |= ((~(0xffffffffffffffffL << c.numberWidth)) & val) << c.numberOffset;
- return ret;
+ return ((FleetTwoDock)dd).getDockConstant(s);
}
private static FlagFunction parseFlags(Tree<String> t) {
cb.add(new Abort(dock, predicate));
continue;
} else if ("word".equals(tt.head())) {
- long literal = 0;
+ BitVector literal = null;
if (tt.child(0).head().equals("CodeBagBody")) {
Tree<String> tq = tt;
CodeBag cb2 = getCodeBag("anon"+(anoncount++));
} else if (tt.child(0).head().equals("[")) {
literal = parseSSL(tt.child(0));
} else {
- literal = number(tt.child(0));
+ literal = new BitVector(dock.getShip().getFleet().getWordWidth()).set(number(tt.child(0)));
}
count = 1;
- /*
- if ("int".equals(tt.child(1).head())) {
- count = (int)number(tt.child(1));
- } else if ("forever".equals(tt.child(1).head())) {
- count = 0;
- }
- */
-
if (((FleetTwoFleet)fleet).isSmallEnoughToFit(literal)) {
- cb.add(new Set(dock, predicate, SetDest.DataLatch, (literal)));
+ cb.add(new Set(dock, predicate, SetDest.DataLatch, literal.toLong()));
} else {
int counter = 0;
while(counter < dock.getShip().getFleet().getWordWidth()) counter += fleet.getShiftWidth();
- while(counter > 0) {
- cb.add(new Shift(dock, predicate,
- new BitVector(dock.getShip().getFleet().getShiftWidth())
- .set(getField(counter-1, counter-fleet.getShiftWidth(), literal))));
+ while(true) {
counter -= fleet.getShiftWidth();
+ if (counter < 0) break;
+ BitVector bv = new BitVector(dock.getShip().getFleet().getShiftWidth());
+ for(int i=0; i<fleet.getShiftWidth(); i++)
+ bv.set(i, literal.get(counter+i));
+ cb.add(new Shift(dock, predicate, bv));
}
}
// Setup //////////////////////////////////////////////////////////////////////////////
Ship createShip(String type) throws IOException {
- ShipDescription sd = new ShipDescription(type, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
+ ShipDescription sd = new ShipDescription(this, type, new BufferedReader(new InputStreamReader(new FileInputStream("ships/"+type+".ship"))));
int count = 0;
for(Ship ship : ships.values()) if (ship.getType().equals(type)) count++;
String name = type+count;
this.top = top;
debugShip = createShip("Debug");
- boolean small = true;
- //boolean small = false;
+ //boolean small = true;
+ boolean small = false;
if (small) {
for(int i=0; i<2; i++) createShip("Alu");
createShip("Timer");
createShip("DDR2");
createShip("Dvi");
- createShip("ZBT");
+ //createShip("ZBT");
}
// for FifoShip
String src = "/ships/" + shipType + ".ship";
InputStream is = getClass().getResourceAsStream(src);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
- ShipDescription sd = new ShipDescription(shipType, br);
+ ShipDescription sd = new ShipDescription(this, shipType, br);
InterpreterShip ret = (InterpreterShip)con.newInstance(new Object[] { this, shipname, sd });
ships.put(shipname, ret);
if (shipType.equals("Debug") && debugShip == null)
}
public String getName() { return "fakedock"; }
- public ShipDescription.Constant getDockConstant(String s) { return null; }
+ public BitVector getDockConstant(String s) {
+ throw new RuntimeException("not supported");
+ }
}
private final boolean inbox;
private final boolean left;
private final boolean isDockless;
- private final HashMap<String,ShipDescription.Constant> constants = new HashMap<String,ShipDescription.Constant>();
+ private final HashMap<String,BitVector> constants = new HashMap<String,BitVector>();
DockDescription(ShipDescription ship, String name, boolean left, boolean inbox) {
this(ship, name, left, inbox, false);
boolean isLeft() { return left; }
/** Searches the dock-specific constants first, then ship-wide constants */
- public ShipDescription.Constant getConstant(String name) {
- ShipDescription.Constant ret = constants.get(name);
+ public BitVector getConstant(String name) {
+ BitVector ret = constants.get(name);
if (ret == null) ret = ship.getConstant(name);
return ret;
}
- void addConstant(String s, ShipDescription.Constant c) { constants.put(s, c); }
+ void addConstant(String s, BitVector c) { constants.put(s, c); }
}
public String getName() { return dockDescription.getName(); }
- public ShipDescription.Constant getDockConstant(String s) { return dockDescription.getConstant(s); }
+ public BitVector getDockConstant(String s) { return dockDescription.getConstant(s); }
public boolean isInputDock() { return dockDescription.isInputDock(); }
public final int WIDTH_WORD;
public final int WIDTH_PACKET;
- public final long DataLatch_WIDTH;
+ public final int DataLatch_WIDTH;
private final long mask;
public FleetTwoFleet() { this(false); }
return false;
}
+ public boolean isSmallEnoughToFit(BitVector immediate) {
+ boolean b = immediate.get((int)DataLatch_WIDTH);
+ for(int i=DataLatch_WIDTH+1; i<immediate.length(); i++)
+ if (immediate.get(i) != b) return false;
+ return true;
+ }
+
public int getWordWidth() { return 37; }
//////////////////////////////////////////////////////////////////////////////
/** NOT YET FINALIZED: A description (specification) of a ship */
public class ShipDescription implements Iterable<DockDescription> {
+ private Fleet fleet;
private String name;
private LinkedHashMap<String,DockDescription> docks = new LinkedHashMap<String,DockDescription>();
private LinkedHashMap<String,DockDescription> ports = new LinkedHashMap<String,DockDescription>();
private HashMap<String,String> sections = new HashMap<String,String>();
- private HashMap<String,Constant> constants = new HashMap<String,Constant>();
+ private HashMap<String,BitVector> constants = new HashMap<String,BitVector>();
public String getName() { return name; }
public String getSection(String sectionName) { return sections.get(sectionName); }
public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
- public ShipDescription(String name, BufferedReader r) throws IOException {
+ public ShipDescription(Fleet fleet, String name, BufferedReader r) throws IOException {
if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
this.name = name;
+ this.fleet = fleet;
String sectionName = null;
StringBuffer sb = new StringBuffer();
while(true) {
processSection(s);
}
- public Constant getConstant(String name) {
- return constants.get(name);
+ public BitVector getConstant(String name) {
+ BitVector c = constants.get(name);
+ if (c==null)
+ throw new RuntimeException("unknown constant " + name);
+ return c;
}
private void processSection(String section) throws IOException {
if (key.startsWith("constant")) {
String constname = key.substring("constant".length()+1).trim();
String val = s.substring(s.indexOf(':')+1).trim();
- constants.put(constname, new Constant(val));
+ constants.put(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
}
}
} else if (section.equals("ports")) {
else if (key.startsWith("constant")) {
String constname = key.substring("constant".length()+1).trim();
String val = s.substring(s.indexOf(':')+1).trim();
- p.addConstant(constname, new Constant(val));
+ p.addConstant(constname, new BitVector(fleet.getWordWidth()).set(Integer.parseInt(val)));
continue;
} else if (key.startsWith("shortcut to")) {
continue;
pw.println(tex);
}
- // FIXME: merge with BitMask
- public class Constant {
- public long setbits = 0;
- public long clearbits = 0;
- public boolean signExtend = false;
- public int numberOffset = 0;
- public int numberWidth = 0;
- public Constant(String s) {
- if (s.startsWith("0x")) {
- setbits = Long.parseLong(s.substring(2), 16);
- clearbits = ~setbits;
- } else if (s.length() == 37) {
- for(int i=36; i>=0; i--) {
- char c = s.charAt(36-i);
- switch(c) {
- case '0': clearbits |= (1<<i); break;
- case '1': setbits |= (1<<i); break;
- case '.': break;
- case 's': signExtend = true; numberOffset = i; numberWidth++; break;
- case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
- }
- }
- } else {
- setbits = Long.parseLong(s);
- clearbits = ~setbits;
- }
- }
- }
-
}