#import edu.berkeley.fleet.ships
-#ship math : ArithmeticShip
+//#ship math : ArithmeticShip
#ship helper : FifoShip
#ship source : FifoShip
#ship dest : FifoShip
// NOTE: "accept" is a synonym for "move" it is less confusing in the case
// of inboxes, but is otherwise identical
-"foo" -> math.din
+//"foo" -> math.din
//////////////////////////////////////////////////////////////////////////////
// The following three instructions simply produce one hundred "3"s
}
public void dumpMem() {
+ Log.print(Log.cyan(" MEMORY: "));
for(int i=0; i<mem.length; i++) {
- if ((i%10)==0) Log.println(" ");
- Log.print(mem[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+"\"");
import java.io.*;
import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
+/**
+ * @author Adam Megacz <megacz@cs.berkeley.edu>
+ * @author Thomas Kho <tkho@eecs.berkeley.edu>
+ */
public class FleetParser {
public static void main(String[] s) throws Exception {
DataInbox.defaultInstruction = null;
continue;
}
+ } else if (s[i].startsWith("--memory=")) {
+ String val = s[i].substring(s[i].indexOf('=')+1);
+ if (val.equals("hide")) {
+ debugMemory = false;
+ continue;
+ } else if (val.equals("show")) {
+ debugMemory = true;
+ continue;
+ }
}
System.out.println("Fleeterpreter usage:");
System.out.println("");
System.out.println(" --color={on|off}");
System.out.println(" --inboxes={configured|unconfigured}");
+ System.out.println(" --memory={hide|show}");
System.exit(-1);
}
go(new InputStreamReader(System.in));
private Fleet fleet;
private ArrayList<String> imports = new ArrayList<String>();
private CodeBag rootCodeBag;
+ private static boolean debugMemory = true;
public FleetParser(Fleet fleet) {
this.fleet = fleet;
public void done() {
if (rootCodeBag != null) {
+ if (debugMemory) { fleet.dumpMem(); }
+ System.out.println(rootCodeBag);
rootCodeBag.dispatch(fleet);
fleet.go();
+ if (debugMemory) { fleet.dumpMem(); }
}
}
CodeBag cb = new CodeBag(null, null);
for(Tree<String> statement : t.child(1))
fillCodeBag(statement, cb);
- System.out.println(cb);
rootCodeBag = cb;
} else if (head.equals("Import")) {
}
if (!good)
throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
+
+ } else if (head.equals("Memory")) {
+ if (fleet.mem.length != 0)
+ throw new RuntimeException("multiple memory directives found");
+ Tree<String> m = t.child(0);
+ int[] mem = new int[m.size()];
+ for(int i=0; i<mem.length; i++)
+ mem[i] = Integer.parseInt(string(m.child(i)));
+ fleet.mem = mem;
}
}
public static String red(Object o) { if (!ansi_color) return o+""; return "\033[31m"+o+"\033[0m"; }
public static String green(Object o) { if (!ansi_color) return o+""; return "\033[32m"+o+"\033[0m"; }
public static String yellow(Object o) { if (!ansi_color) return o+""; return "\033[33m"+o+"\033[0m"; }
- public static String blue(Object o) { if (!ansi_color) return o+""; return o+""; }
+ public static String blue(Object o) { if (!ansi_color) return o+""; return "\033[34m"+o+"\033[0m"; }
public static String purple(Object o) { if (!ansi_color) return o+""; return "\033[35m"+o+"\033[0m"; }
public static String cyan(Object o) { if (!ansi_color) return o+""; return "\033[36m"+o+"\033[0m"; }
public static String invert(Object o) { if (!ansi_color) return o+""; return "\033[7m"+o+"\033[0m"; }
--- /dev/null
+package edu.berkeley.fleet.ships;
+
+import edu.berkeley.fleet.*;
+import java.util.*;
+import java.io.*;
+
+/**
+ * @author Thomas Kho <tkho@eecs.berkeley.edu>
+ */
+public class MemoryWriteShip extends Ship {
+
+ int _count = 0;
+ int _stride = 0;
+ int _addr = 0;
+
+ DataInbox addr = new DataInbox(this, "addr");
+ DataInbox stride = new DataInbox(this, "stride");
+ DataInbox count = new DataInbox(this, "count");
+ DataInbox data = new DataInbox(this, "data");
+ TokenOutbox done = new TokenOutbox(this, "done");
+
+ public MemoryWriteShip(Fleet fleet, String name) {
+ super(fleet, name);
+ }
+
+ public void service() {
+ if (_count > 0) {
+ if (!data.dataReadyForShip()) return;
+ if (_addr<=getFleet().mem.length)
+ getFleet().writeMem(_addr, data.removeDataForShip());
+ _count--;
+ _addr += _stride;
+ if (_count==0)
+ done.addTokenFromShip();
+ } else {
+ if (count.dataReadyForShip() &&
+ addr.dataReadyForShip() &&
+ stride.dataReadyForShip() &&
+ done.readyForTokenFromShip() &&
+ data.dataReadyForShip()) {
+
+ _count = count.removeDataForShip();
+ _addr = addr.removeDataForShip();
+ _stride = stride.removeDataForShip();
+ }
+ }
+ }
+
+}