--- /dev/null
+package edu.berkeley.fleet.loops;
+import edu.berkeley.fleet.api.*;
+import java.util.*;
+import java.net.*;
+
+/**
+ * A program is a collection of CodeBags resident in a particular Memory.
+ */
+public class Program {
+
+ private Ship memoryShip;
+ private Fleet fleet;
+ private long leastUnallocatedAddress;
+ private long startAddress;
+
+ private HashSet<CodeBag> codeBags = new HashSet<CodeBag>();
+
+ public Program(Ship memoryShip) { this(memoryShip, 0); }
+ public Program(Ship memoryShip, long startAddress) {
+ this.fleet = memoryShip.getFleet();
+ this.memoryShip = memoryShip;
+ this.startAddress = startAddress;
+ this.leastUnallocatedAddress = startAddress;
+ }
+
+ public void run(FleetProcess fp, CodeBag run) {
+ System.out.println("invoking...");
+ Context ctx = new Context(fleet);
+ LoopFactory lf;
+ lf = new LoopFactory(ctx, memoryShip.getDock("inCBD"), 1);
+ lf.literal( (run.baseAddress<<6) | run.instructions.length );
+ lf.deliver();
+ ctx.dispatch(fp);
+ System.out.println("invoked.");
+ }
+
+ public void install(FleetProcess fp) {
+ BitVector[] bvs = new BitVector[(int)(leastUnallocatedAddress-startAddress)];
+ int j = 0;
+ for(CodeBag cb : codeBags)
+ for(int i=0; i<cb.instructions.length; i++)
+ bvs[j++] = fleet.encodeInstruction(cb.instructions[i], memoryShip.getDock("out"));
+ System.out.println("writing... ("+bvs.length+" words)");
+ MemoryUtils.writeMem(fp, memoryShip, startAddress, bvs);
+ System.out.println("written.");
+ }
+
+ public CodeBag makeCodeBag(Context ctx) {
+ Instruction[] instructions = ctx.emit();
+ CodeBag codeBag = new CodeBag(this, instructions, leastUnallocatedAddress);
+ System.out.println("instructions.length="+instructions.length);
+ leastUnallocatedAddress += instructions.length;
+ codeBags.add(codeBag);
+ return codeBag;
+ }
+
+}
\ No newline at end of file