add Program, a collection of CodeBags
authormegacz <adam@megacz.com>
Sun, 15 Mar 2009 04:15:37 +0000 (21:15 -0700)
committermegacz <adam@megacz.com>
Sun, 15 Mar 2009 04:15:37 +0000 (21:15 -0700)
src/edu/berkeley/fleet/loops/Program.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/fleet/loops/Program.java b/src/edu/berkeley/fleet/loops/Program.java
new file mode 100644 (file)
index 0000000..f32fecb
--- /dev/null
@@ -0,0 +1,57 @@
+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