add Fleet.getDefaultImpl() and use it in Makefile
[fleet.git] / src / edu / berkeley / fleet / loops / MemoryUtils.java
1 package edu.berkeley.fleet.loops;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.concurrent.Semaphore;
4 import java.util.*;
5 import java.io.*;
6 import java.net.*;
7 import edu.berkeley.fleet.two.*;
8 import edu.berkeley.fleet.fpga.*;
9 import edu.berkeley.fleet.interpreter.*;
10 import edu.berkeley.fleet.api.*;
11 import edu.berkeley.fleet.api.Instruction.*;
12 import edu.berkeley.fleet.api.Instruction.Set;
13 import edu.berkeley.fleet.api.Instruction.Set.*;
14 import edu.berkeley.fleet.api.Instruction.Set.SetDest;
15 import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
16 import static edu.berkeley.fleet.api.Predicate.*;
17
18 public class MemoryUtils {
19
20     public static void readMem(FleetProcess fp,
21                                ShipPool pool,
22                                Ship memory,
23                                long offset,
24                                BitVector[] vals) throws RuntimeException {
25         try {
26             Ship alu = pool.allocateShip("Alu");
27             pool.assertAllocated(memory);
28             MemoryInputStream mos = new MemoryInputStream(fp, pool, memory, alu, offset, 1);
29             fp.flush();
30             for(int i=0; i<vals.length; i++) {
31                 int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
32                 String status = i + "/" + (vals.length-1) + "= " + pct + "%";
33                 System.out.print("\rreading from address: " + status + "...");
34                 vals[i] = mos.readWord();
35                 System.out.print("\rread    from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
36             }
37             mos.close();
38             pool.releaseShip(alu);
39         } catch (IOException e) {
40             throw new RuntimeException(e);
41         }
42     }
43
44     public static void writeMem(FleetProcess fp,
45                                 ShipPool pool,
46                                 Ship memory,
47                                 long offset,
48                                 BitVector[] vals) throws RuntimeException {
49         try {
50             Ship alu = pool.allocateShip("Alu");
51             pool.assertAllocated(memory);
52             MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, alu, offset);
53             for(int i=0; i<vals.length; i++) {
54                 int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
55                 String status = i + "/" + (vals.length-1) + "= " + pct + "%";
56                 System.out.print("\rwrote to address: " + status +"           ");
57                 mos.writeWord(vals[i]);
58             }
59             mos.close();
60             pool.releaseShip(alu);
61         } catch (IOException e) {
62             throw new RuntimeException(e);
63         }
64     }
65
66     public static class MemoryOutputStream extends DockOutputStream {
67
68         private FleetProcess fp;
69         private Ship memory;
70         private Ship alu;
71
72         public MemoryOutputStream(FleetProcess fp,
73                                   ShipPool pool,
74                                   Ship memory,
75                                   Ship alu,
76                                   long offset) throws IOException {
77
78             super(fp, pool, memory.getDock("inDataWrite"), alu.getDock("out").getDataDestination());
79             pool.assertAllocated(memory);
80             pool.assertAllocated(alu);
81
82             this.fp = fp;
83             this.memory = memory;
84             this.alu = alu;
85
86             buildIt(fp, memory, alu, memory.getDock("inAddrWrite"), offset);
87             CodeBag cb = new CodeBag(fp.getFleet());
88             LoopFactory lf;
89             lf = cb.loopFactory(memory.getDock("out"), 0);
90             lf.abortLoopIfTorpedoPresent();
91             lf.collectWord();
92             cb.dispatch(fp);
93         }
94
95         public void close() {
96             super.close();
97             fp.sendTorpedo(memory.getDock("inAddrWrite"));
98             fp.sendTorpedo(memory.getDock("out"));
99             fp.sendTorpedo(alu.getDock("in1"));
100             fp.sendTorpedo(alu.getDock("in2"));
101             fp.sendTorpedo(alu.getDock("inOp"));
102             fp.sendTorpedo(alu.getDock("out"));
103         }
104
105     }
106
107
108     public static class MemoryInputStream extends DockInputStream {
109
110         private FleetProcess fp;
111         private Ship memory;
112         private Ship alu;
113
114         public MemoryInputStream(FleetProcess fp,
115                                  ShipPool pool,
116                                  Ship memory,
117                                  Ship alu,
118                                  long offset,
119                                  int inflight) throws IOException {
120             super(fp, pool, memory.getDock("out"), alu.getDock("out").getDataDestination(), inflight);
121             pool.assertAllocated(memory);
122             pool.assertAllocated(alu);
123             this.fp = fp;
124             this.memory = memory;
125             this.alu = alu;
126             buildIt(fp, memory, alu, memory.getDock("inAddrRead"), offset);
127         }
128
129         public void close() {
130             super.close();
131             fp.sendTorpedo(memory.getDock("inAddrRead"));
132             fp.sendTorpedo(alu.getDock("in1"));
133             fp.sendTorpedo(alu.getDock("in2"));
134             fp.sendTorpedo(alu.getDock("inOp"));
135             fp.sendTorpedo(alu.getDock("out"));
136         }
137     }
138
139     private static void buildIt(FleetProcess fp, Ship memory, Ship alu, Dock dest, long offset) {
140
141         CodeBag ctx = new CodeBag(fp.getFleet());
142
143         LoopFactory lf;
144         // alu.in1: receive and deliver
145         lf = ctx.loopFactory(alu.getDock("in1"), 0);
146         lf.abortLoopIfTorpedoPresent();
147         lf.recvWord();
148         lf.deliver();
149             
150         // alu.in2: receive tokens, deliver 1's
151         lf = ctx.loopFactory(alu.getDock("in2"), 1);
152         lf.literal(1);
153         lf = lf.makeNext(0);
154         lf.abortLoopIfTorpedoPresent();
155         lf.recvToken();
156         lf.deliver();
157             
158         // alu.inOp: receive tokens, deliver ADD's
159         lf = ctx.loopFactory(alu.getDock("inOp"), 1);
160         lf.literal("ADD");
161         lf = lf.makeNext(0);
162         lf.abortLoopIfTorpedoPresent();
163         lf.recvToken();
164         lf.deliver();
165             
166         // alu.out: for each token, provide a word of count-data
167         lf = ctx.loopFactory(alu.getDock("out"), 1);
168         lf.literal(offset);
169         lf = lf.makeNext(0);
170         lf.abortLoopIfTorpedoPresent();
171         lf.recvToken();
172         lf.sendWord(dest);
173         lf.sendWord(alu.getDock("in1"));
174         lf.sendToken(alu.getDock("in2"));
175         lf.sendToken(alu.getDock("inOp"));
176         lf.collectWord();
177
178         lf = ctx.loopFactory(dest, 0);
179         lf.abortLoopIfTorpedoPresent();
180         lf.recvWord();
181         lf.deliver();
182
183         ctx.dispatch(fp);
184     }
185
186     public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
187         CodeBag ctx = new CodeBag(fp.getFleet());
188         LoopFactory lf;
189
190         lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
191         lf.abortLoopIfTorpedoPresent();
192         lf.collectPacket();
193         lf.sendPacket();
194
195         lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
196         lf.abortLoopIfTorpedoPresent();
197         lf.recvWord();
198         lf.deliver();
199
200         ctx.dispatch(fp);
201     }
202
203     public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
204         fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
205         fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
206     }
207
208     public static void main(String[] s) throws Exception {
209         Random random = new Random(System.currentTimeMillis());
210         Fleet fleet = Fleet.getDefaultImpl();
211         FleetProcess fp = fleet.run(new Instruction[0]);
212         Ship memory = fleet.getShip(s[0],0);
213
214         //int size = (548 * 478) / 2;
215         int size = 2048;
216
217         BitVector[] vals  = new BitVector[size];
218         BitVector[] vals2 = new BitVector[size];
219
220         for(int i=0; i<vals.length; i++) {
221             vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
222             for(int j=36; j<vals[i].length(); j++)
223                 vals[i].set(j, false);
224         }
225
226         ShipPool pool = new ShipPool(fleet);
227         pool.allocateShip(memory);
228         writeMem(fp, pool, memory, 0, vals);
229         readMem(fp, pool, memory, 0, vals2);
230         System.out.println();
231         int fails = 0;
232         for(int i=0; i<vals.length; i++)
233             if (!vals[i].equals(vals2[i])) {
234                 System.out.println("disagreement!  on index " + i + "\n  expected="+vals[i]+"\n       got="+vals2[i]);
235                 fails++;
236             }
237         System.out.println("done! ("+fails+" failures)");
238         if (fails>0) System.exit(-1);
239     }
240 }