bd30f3025e5a6d67c2667cfbc4887c8b6b507c4b
[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 import java.io.*;
6
7 /**
8  *  A program is a collection of CodeBags resident in a particular Memory.
9  *
10  *  FIXME: should every Program have an associated ShipPool?
11  */
12 public class Program {
13
14     public final Fleet fleet;
15     public final Ship memoryShip;
16
17     private CodeBag end = null;
18
19     private long leastUnallocatedAddress;
20     private long startAddress;
21
22     private ArrayList<CodeBag> codeBags = new ArrayList<CodeBag>();
23     private ArrayList<Instruction> all_instructions = new ArrayList<Instruction>();
24     
25     public Program(Ship memoryShip) { this(memoryShip, 0); }
26     public Program(Ship memoryShip, long startAddress) {
27         this.fleet = memoryShip.getFleet();
28         this.memoryShip = memoryShip;
29         this.startAddress = startAddress;
30         this.leastUnallocatedAddress = startAddress;
31     }
32
33     public Destination getCBDDestination() {
34         return memoryShip.getDock("inCBD").getDataDestination();
35     }
36
37     public long run(FleetProcess fp, CodeBag run, ShipPool pool) {
38         Ship timer    = pool.allocateShip("Timer");
39         Ship debug    = pool.allocateShip("Debug");
40         Dock debugIn  = debug.getDock("in");
41         
42         LoopFactory lf;
43
44         CodeBag start = new CodeBag(fleet, this);
45         lf = start.loopFactory(timer.getDock("out"), 1);
46         lf.collectWord();
47         lf.sendWord(debugIn.getDataDestination());
48         lf = start.loopFactory(debugIn, 1);
49         lf.recvWord();
50         lf.deliver();
51         lf.recvWord();
52         lf.deliver();
53         start.sendWord(run.getDescriptor(), getCBDDestination());
54
55         CodeBag done  = end;
56         lf = done.loopFactory(timer.getDock("out"), 1);
57         lf.collectWord();
58         lf.sendWord(debugIn.getDataDestination());
59        
60         start.seal();
61         done.seal();
62         
63         install(fp, pool);
64         
65         MemoryUtils.putMemoryShipInDispatchMode(fp, memoryShip);
66         fp.sendWord(getCBDDestination(), start.getDescriptor().getBitVector());
67         fp.flush();
68         long now = System.currentTimeMillis();
69         System.out.println("waiting for timestamps to come back...");
70         BitVector bv1 = fp.recvWord();
71         System.out.println();
72         System.out.println("got start time (bv="+bv1+" = "+bv1.toLong()+")");
73         BitVector bv2 = fp.recvWord();
74         System.out.println("got end time (bv="+bv2+" = "+bv2.toLong()+")");
75         long ret = (bv2.toLong()-bv1.toLong());
76         long simtime = (System.currentTimeMillis()-now)/1000;
77         System.out.println("total run time = " + ret + " gate delays, " + (ret/40) + "ns (simulation time="+simtime+"sec)");
78         MemoryUtils.removeMemoryShipFromDispatchMode(fp, memoryShip);
79
80         return ret;
81     }
82
83     public void dump(String file) {
84         try {
85             FileOutputStream fos = new FileOutputStream(file);
86             PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
87             for(int i=0; i<codeBags.size(); i++)
88                 for(Instruction in : codeBags.get(i).emit())
89                     pw.println(in);
90             pw.flush();
91             pw.close();
92         } catch (Exception e) { throw new RuntimeException(e); }
93     }
94
95     public CodeBag getEndProgramCodeBag() {
96         if (end != null) return end;
97         end = new CodeBag(fleet, this);
98         return end;
99     }
100
101     /** the provided pool should be a sibling of the one which holds this Program's instructions */
102     public void install(FleetProcess fp, ShipPool pool) {
103         for(int i=0; i<codeBags.size(); i++)
104             codeBags.get(i).emit();
105         BitVector[] bvs = new BitVector[(int)(leastUnallocatedAddress-startAddress)];
106         for(int i=0; i<bvs.length; i++) {
107             bvs[i] = fleet.encodeInstruction(all_instructions.get(i), memoryShip.getDock("out"));
108         }
109         MemoryUtils.writeMem(fp, pool, memoryShip, startAddress, bvs);
110     }
111
112     BitVector addInstructions(Instruction[] instructions) {
113
114         // CodeBags are "daisy chained" to eliminate any possibility
115         // of cloggage at the inCBD port as a result of more CBD's
116         // being dispatched than the maximum number that fit in the
117         // data fifo at that dock.
118
119         int MAX_BAG_SIZE = (1<<6)-1;
120
121         // FUZZ is an estimate of the number of instructions required
122         // to dispatch a code bag descriptor
123         int FUZZ = 4;
124
125         if (instructions.length <= MAX_BAG_SIZE) {
126             BitVector descriptor = new BitVector(fleet.getWordWidth());
127             descriptor.set( (leastUnallocatedAddress<<6) | instructions.length );
128             leastUnallocatedAddress += instructions.length;
129             for(Instruction i : instructions)
130                 all_instructions.add(i);
131             return descriptor;
132         }
133
134         System.out.println("warning: code bag size is "+instructions.length+
135                            ", which exceeds maximum of "+MAX_BAG_SIZE+
136                            "; breaking into multiple bags");
137         
138         Instruction[] rest = new Instruction[instructions.length - (MAX_BAG_SIZE-FUZZ)];
139         System.arraycopy(instructions, MAX_BAG_SIZE-FUZZ, rest, 0, rest.length);
140         
141         DeferredBitVector restbag = addInstructions(rest);
142         CodeBag ctx = new CodeBag(fleet);
143         ctx.sendWord(restbag, getCBDDestination());
144         Instruction[] fuzz = ctx.emit();
145         if (fuzz.length > FUZZ) throw new RuntimeException("FUZZ="+FUZZ+", fuzz.length="+fuzz.length);
146         Instruction[] self = new Instruction[fuzz.length+MAX_BAG_SIZE-FUZZ];
147         System.arraycopy(instructions, 0, self, 0, MAX_BAG_SIZE-FUZZ);
148         System.arraycopy(fuzz, 0, self, MAX_BAG_SIZE-FUZZ, fuzz.length);
149         return addInstructions(self);
150     }
151
152 }