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