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