f32fecb2276698d70ed6b4c06048267d448262af
[fleet.git] / src / edu / berkeley / fleet / loops / Program.java
1 package edu.berkeley.fleet.loops;
2 import edu.berkeley.fleet.api.*;
3 import java.util.*;
4 import java.net.*;
5
6 /**
7  *  A program is a collection of CodeBags resident in a particular Memory.
8  */
9 public class Program {
10
11     private Ship memoryShip;
12     private Fleet fleet;
13     private long leastUnallocatedAddress;
14     private long startAddress;
15
16     private HashSet<CodeBag> codeBags = new HashSet<CodeBag>();
17     
18     public Program(Ship memoryShip) { this(memoryShip, 0); }
19     public Program(Ship memoryShip, long startAddress) {
20         this.fleet = memoryShip.getFleet();
21         this.memoryShip = memoryShip;
22         this.startAddress = startAddress;
23         this.leastUnallocatedAddress = startAddress;
24     }
25
26     public void run(FleetProcess fp, CodeBag run) {
27         System.out.println("invoking...");
28         Context ctx = new Context(fleet);
29         LoopFactory lf;
30         lf = new LoopFactory(ctx, memoryShip.getDock("inCBD"), 1);
31         lf.literal( (run.baseAddress<<6) | run.instructions.length );
32         lf.deliver();
33         ctx.dispatch(fp);
34         System.out.println("invoked.");
35     }
36
37     public void install(FleetProcess fp) {
38         BitVector[] bvs = new BitVector[(int)(leastUnallocatedAddress-startAddress)];
39         int j = 0;
40         for(CodeBag cb : codeBags)
41             for(int i=0; i<cb.instructions.length; i++)
42                 bvs[j++] = fleet.encodeInstruction(cb.instructions[i], memoryShip.getDock("out"));
43         System.out.println("writing... ("+bvs.length+" words)");
44         MemoryUtils.writeMem(fp, memoryShip, startAddress, bvs);
45         System.out.println("written.");
46     }
47
48     public CodeBag makeCodeBag(Context ctx) {
49         Instruction[] instructions = ctx.emit();
50         CodeBag codeBag = new CodeBag(this, instructions, leastUnallocatedAddress);
51         System.out.println("instructions.length="+instructions.length);
52         leastUnallocatedAddress += instructions.length;
53         codeBags.add(codeBag);
54         return codeBag;
55     }
56
57 }