7a92a3213e4b09a199abca450a5e9e4500a213d4
[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.net.*;
6 import edu.berkeley.fleet.two.*;
7 import edu.berkeley.fleet.fpga.*;
8 import edu.berkeley.fleet.api.*;
9 import edu.berkeley.fleet.api.Instruction.*;
10 import edu.berkeley.fleet.api.Instruction.Set;
11 import edu.berkeley.fleet.api.Instruction.Set.*;
12 import edu.berkeley.fleet.api.Instruction.Set.SetDest;
13 import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
14 import static edu.berkeley.fleet.api.Predicate.*;
15
16 public class MemoryUtils {
17
18     public static void readMem(FleetProcess fp,
19                                ShipPool pool,
20                                Ship memory,
21                                long offset,
22                                BitVector[] vals) throws RuntimeException {
23         doMem(true, fp, pool, memory, offset, vals);
24     }
25
26     public static void writeMem(FleetProcess fp,
27                                ShipPool pool,
28                                 Ship memory,
29                                 long offset,
30                                 long[] vals) throws RuntimeException {
31         doMem(false, fp, pool, memory, offset, long2bv(fp.getFleet(), vals));
32     }
33
34     public static void writeMem(FleetProcess fp,
35                                ShipPool pool,
36                                 Ship memory,
37                                 long offset,
38                                 BitVector[] vals) throws RuntimeException {
39         doMem(false, fp, pool, memory, offset, vals);
40     }
41
42     /*
43     public static void clearMem(FleetProcess fp,
44                                 Ship memory,
45                                 ShipPool pool,
46                                 long offset,
47                                 long count) throws RuntimeException {
48         if (fp.getFleet() != memory.getFleet())
49             throw new RuntimeException("Fleet mismatch");
50
51         Ship counter1 = pool.allocateShip("Counter");
52         Ship counter2 = pool.allocateShip("Counter");
53         //Ship alu      = pool.allocateShip("Alu");
54
55         final Dock debugIn     = fp.getDebugInputDock();
56
57         CodeBag ctx = new CodeBag(fp.getFleet());
58         LoopFactory lf;
59
60         lf = ctx.loopFactory(counter1.getDock("inOp"), 1);
61         lf.literal(counter1.getDock("inOp").getConstant("COUNT"));
62         lf.deliver();
63
64         lf = ctx.loopFactory(counter1.getDock("in1"), 1);
65         lf.literal(count);
66         lf.deliver();
67
68         lf = ctx.loopFactory(counter1.getDock("in2"), 1);
69         lf.literal(1);
70         lf.deliver();
71
72         lf = ctx.loopFactory(counter1.getDock("out"), 0);
73         lf.abortLoopIfTorpedoPresent();
74         lf.recvToken();
75         lf.collect();
76         lf.send(memory.getDock("inAddrWrite").getDataDestination());
77
78         lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
79         lf.literal(0);
80         lf.abortLoopIfTorpedoPresent();
81         lf.deliver();
82
83         lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 1);
84         lf.sendToken(counter1.getDock("out").getDataDestination());
85         lf = lf.makeNext(0);
86         lf.abortLoopIfTorpedoPresent();
87         lf.recvWord();
88         lf.deliver();
89         lf.sendToken(counter1.getDock("out").getDataDestination());
90
91         lf = ctx.loopFactory(memory.getDock("out"), 0);
92         lf.recvToken();
93         lf.recvWord();
94         lf.send(counter2.getDock("in2").getDataDestination());
95
96         lf = ctx.loopFactory(counter2.getDock("inOp"), 1);
97         lf.literal(counter2.getDock("inOp").getConstant("DROP_C1_V2"));
98         lf.deliver();
99         lf.literal(counter2.getDock("inOp").getConstant("PASS_C1_V2"));
100         lf.deliver();
101
102         lf = ctx.loopFactory(counter2.getDock("in2"), 0);
103         lf.recvWord();
104         lf.deliver();
105
106         fp.sendToken(counter1.getDock("out").getInstructionDestination());
107         fp.sendToken(memory.getDock("inDataWrite").getInstructionDestination());
108         fp.sendToken(memory.getDock("inAddrWrite").getInstructionDestination());
109         fp.sendToken(memory.getDock("out").getInstructionDestination());
110         fp.sendToken(counter2.getDock("in2").getInstructionDestination());
111         
112         lf = ctx.loopFactory(counter2.getDock("in1"), 1);
113         lf.literal(count-1);
114         lf.deliver();
115         lf.literal(1);
116         lf.deliver();
117         
118         lf = ctx.loopFactory(counter2.getDock("out"), 1);
119         lf.collect();
120         lf.send(debugIn.getDataDestination());
121
122         pool.releaseShip(counter1);
123         pool.releaseShip(counter2);
124         //pool.releaseShip(alu);
125     }
126     */
127
128     public static void doMem(final boolean read,
129                              final FleetProcess fp,
130                              final ShipPool pool,
131                              final Ship memory,
132                              final long offset,
133                              final BitVector[] vals) throws RuntimeException {
134         if (fp.getFleet() != memory.getFleet())
135             throw new RuntimeException("Fleet mismatch");
136
137         final Dock inAddrWrite = memory.getDock("inAddrWrite");
138         final Dock inDataWrite = memory.getDock("inDataWrite");
139         final Dock inAddrRead  = memory.getDock("inAddrRead");
140         final Dock out         = memory.getDock("out");
141         final Dock debugIn     = read ? fp.getDebugInputDock() : null;
142         if (debugIn!=null) pool.allocateShip(debugIn.getShip());
143
144         Ship counter = pool.allocateShip("Counter");
145         Dock counter_in1  = counter.getDock("in1");
146         Dock counter_in2  = counter.getDock("in2");
147         Dock counter_inOp = counter.getDock("inOp");
148         Dock counter_out  = counter.getDock("out");
149
150         Ship alu = pool.allocateShip("Alu");
151
152         CodeBag ctx = new CodeBag(fp.getFleet());
153         LoopFactory lf;
154         if (read) {
155             lf = ctx.loopFactory(inAddrRead, 0);
156             lf.abortLoopIfTorpedoPresent();
157             lf.sendToken(counter_out);
158             lf.recvWord();
159             lf.deliver();
160         } else {
161             lf = ctx.loopFactory(inAddrWrite, 0);
162             lf.sendToken(counter_out);
163             lf.abortLoopIfTorpedoPresent();
164             lf.recvWord();
165             lf.deliver();
166             lf = ctx.loopFactory(inDataWrite, 0);
167             lf.abortLoopIfTorpedoPresent();
168             lf.recvWord();
169             lf.deliver();
170         }
171
172         lf = ctx.loopFactory(counter_in1, 1);
173         lf.literal(vals.length);
174         lf.deliver();
175
176         lf = ctx.loopFactory(counter_in2, 1);
177         lf.literal(1);
178         lf.deliver();
179
180         lf = ctx.loopFactory(counter_inOp, 1);
181         lf.literal("COUNT");
182         lf.deliver();
183
184         lf = ctx.loopFactory(counter_out, 0);
185         lf.recvToken();
186         lf.abortLoopIfTorpedoPresent();
187         lf.collectWord();
188         lf.sendWord(alu.getDock("in1"));
189         lf.sendToken(alu.getDock("in2"));
190         lf.sendToken(alu.getDock("inOp"));
191
192         lf = ctx.loopFactory(alu.getDock("in1"), 0);
193         lf.abortLoopIfTorpedoPresent();
194         lf.recvWord();
195         lf.deliver();
196
197         lf = ctx.loopFactory(alu.getDock("in2"), 0);
198         lf.abortLoopIfTorpedoPresent();
199         lf.recvToken();
200         lf.literal(offset);
201         lf.deliver();
202
203         lf = ctx.loopFactory(alu.getDock("inOp"), 0);
204         lf.abortLoopIfTorpedoPresent();
205         lf.recvToken();
206         lf.literal("ADD");
207         lf.deliver();
208
209         lf = ctx.loopFactory(alu.getDock("out"), 0);
210         lf.abortLoopIfTorpedoPresent();
211         lf.collectWord();
212         lf.sendWord(read ? inAddrRead : inAddrWrite);
213
214         lf = ctx.loopFactory(out, 0);
215         if (read) lf.recvToken();
216         lf.abortLoopIfTorpedoPresent();
217         lf.collectWord();
218         if (read) lf.sendWord(debugIn.getDataDestination());
219
220         if (read) {
221             lf = ctx.loopFactory(debugIn, 0);
222             lf.sendToken(out);
223             lf.abortLoopIfTorpedoPresent();
224             lf.recvWord();
225             lf.deliver();
226         }
227
228         ctx.dispatch(fp);
229
230         for(int i=vals.length-1; i>=0; i--) {
231             if (!read)
232                 fp.sendWord(inDataWrite.getDataDestination(), vals[i]);
233             if (read) {
234                 BitVector outv = fp.recvWord();
235                 vals[i] = outv;
236             }
237             int pct = (int)Math.ceil(100.0*((double)(vals.length-1-i)/((double)(vals.length-1))));
238             String status = i + "/" + (vals.length-1) + "= " + pct + "%";
239             if (read) System.out.print("\rread from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
240             else      System.out.print("\rwrote to address: " + status +"           ");
241         }
242         fp.flush();
243
244         if (read) {
245             fp.sendToken(inAddrRead.getInstructionDestination());
246             fp.sendToken(debugIn.getInstructionDestination());
247         } else {
248             fp.sendToken(inAddrWrite.getInstructionDestination());
249             fp.sendToken(inDataWrite.getInstructionDestination());
250         }
251         fp.sendToken(alu.getDock("in1").getInstructionDestination());
252         fp.sendToken(alu.getDock("in2").getInstructionDestination());
253         fp.sendToken(alu.getDock("inOp").getInstructionDestination());
254         fp.sendToken(alu.getDock("out").getInstructionDestination());
255         fp.sendToken(counter_out.getInstructionDestination());
256         fp.sendToken(out.getInstructionDestination());
257         System.out.println();
258
259         pool.releaseShip(alu);
260         pool.releaseShip(counter);
261         if (read) pool.releaseShip(debugIn.getShip());
262     }
263
264     private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
265         BitVector[] bv = new BitVector[initialValues.length];
266         for(int i=0; i<initialValues.length; i++)
267             bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);
268         return bv;
269     }
270
271     public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
272         CodeBag ctx = new CodeBag(fp.getFleet());
273         LoopFactory lf;
274
275         lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
276         lf.abortLoopIfTorpedoPresent();
277         lf.collectPacket();
278         lf.sendPacket();
279
280         lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
281         lf.abortLoopIfTorpedoPresent();
282         lf.recvWord();
283         lf.deliver();
284
285         ctx.dispatch(fp);
286     }
287
288     public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
289         fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
290         fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
291     }
292
293     public static void main(String[] s) throws Exception {
294         Random random = new Random(System.currentTimeMillis());
295         Fleet fleet = new Fpga();
296         FleetProcess fp = fleet.run(new Instruction[0]);
297         //Ship memory = fleet.getShip("DDR2",0);
298         Ship memory = fleet.getShip("Dvi",0);
299         //Ship memory = fleet.getShip("Memory",0);
300
301         //int size = (548 * 478) / 2;
302         int size = 2048;
303
304         BitVector[] vals  = new BitVector[size];
305         BitVector[] vals2 = new BitVector[size];
306
307         for(int i=0; i<vals.length; i++) {
308             vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
309             for(int j=36; j<vals[i].length(); j++)
310                 vals[i].set(j, false);
311             vals[i].set(1, false);
312         }
313
314         BitVector bv = new BitVector(fleet.getWordWidth());
315         /*
316         for(int i=0; i<bv.length(); i++) bv.set(i, true);
317         for(int i=0; i<vals.length; i++)
318             vals[i] = bv;
319         */
320         ShipPool pool = new ShipPool(fleet);
321         writeMem(fp, pool, memory, 0, vals);
322         readMem(fp, pool, memory, 0, vals2);
323         int fails = 0;
324         for(int i=0; i<vals.length; i++)
325             if (!vals[i].equals(vals2[i])) {
326                 System.out.println("disagreement!  on index " + i + "\n  "+vals[i]+"\n  "+vals2[i]);
327                 fails++;
328             }
329         System.out.println("done! ("+fails+" failures)");
330     }
331 }