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