1 package edu.berkeley.fleet.loops;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.concurrent.Semaphore;
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.*;
16 public class MemoryUtils {
18 public static void readMem(FleetProcess fp,
22 BitVector[] vals) throws RuntimeException {
23 doMem(true, fp, pool, memory, offset, vals);
26 public static void writeMem(FleetProcess fp,
30 long[] vals) throws RuntimeException {
31 doMem(false, fp, pool, memory, offset, long2bv(fp.getFleet(), vals));
34 public static void writeMem(FleetProcess fp,
38 BitVector[] vals) throws RuntimeException {
39 doMem(false, fp, pool, memory, offset, vals);
43 public static void clearMem(FleetProcess fp,
47 long count) throws RuntimeException {
48 if (fp.getFleet() != memory.getFleet())
49 throw new RuntimeException("Fleet mismatch");
51 Ship counter1 = pool.allocateShip("Counter");
52 Ship counter2 = pool.allocateShip("Counter");
53 //Ship alu = pool.allocateShip("Alu");
55 final Dock debugIn = fp.getDebugInputDock();
57 CodeBag ctx = new CodeBag(fp.getFleet());
60 lf = ctx.loopFactory(counter1.getDock("inOp"), 1);
61 lf.literal(counter1.getDock("inOp").getConstant("COUNT"));
64 lf = ctx.loopFactory(counter1.getDock("in1"), 1);
68 lf = ctx.loopFactory(counter1.getDock("in2"), 1);
72 lf = ctx.loopFactory(counter1.getDock("out"), 0);
73 lf.abortLoopIfTorpedoPresent();
76 lf.send(memory.getDock("inAddrWrite").getDataDestination());
78 lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
80 lf.abortLoopIfTorpedoPresent();
83 lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 1);
84 lf.sendToken(counter1.getDock("out").getDataDestination());
86 lf.abortLoopIfTorpedoPresent();
89 lf.sendToken(counter1.getDock("out").getDataDestination());
91 lf = ctx.loopFactory(memory.getDock("out"), 0);
94 lf.send(counter2.getDock("in2").getDataDestination());
96 lf = ctx.loopFactory(counter2.getDock("inOp"), 1);
97 lf.literal(counter2.getDock("inOp").getConstant("DROP_C1_V2"));
99 lf.literal(counter2.getDock("inOp").getConstant("PASS_C1_V2"));
102 lf = ctx.loopFactory(counter2.getDock("in2"), 0);
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());
112 lf = ctx.loopFactory(counter2.getDock("in1"), 1);
118 lf = ctx.loopFactory(counter2.getDock("out"), 1);
120 lf.send(debugIn.getDataDestination());
122 pool.releaseShip(counter1);
123 pool.releaseShip(counter2);
124 //pool.releaseShip(alu);
128 public static void doMem(final boolean read,
129 final FleetProcess fp,
133 final BitVector[] vals) throws RuntimeException {
134 if (fp.getFleet() != memory.getFleet())
135 throw new RuntimeException("Fleet mismatch");
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());
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");
150 Ship alu = pool.allocateShip("Alu");
152 CodeBag ctx = new CodeBag(fp.getFleet());
155 lf = ctx.loopFactory(inAddrRead, 0);
156 lf.abortLoopIfTorpedoPresent();
157 lf.sendToken(counter_out);
161 lf = ctx.loopFactory(inAddrWrite, 0);
162 lf.sendToken(counter_out);
163 lf.abortLoopIfTorpedoPresent();
166 lf = ctx.loopFactory(inDataWrite, 0);
167 lf.abortLoopIfTorpedoPresent();
172 lf = ctx.loopFactory(counter_in1, 1);
173 lf.literal(vals.length);
176 lf = ctx.loopFactory(counter_in2, 1);
180 lf = ctx.loopFactory(counter_inOp, 1);
184 lf = ctx.loopFactory(counter_out, 0);
186 lf.abortLoopIfTorpedoPresent();
188 lf.sendWord(alu.getDock("in1"));
189 lf.sendToken(alu.getDock("in2"));
190 lf.sendToken(alu.getDock("inOp"));
192 lf = ctx.loopFactory(alu.getDock("in1"), 0);
193 lf.abortLoopIfTorpedoPresent();
197 lf = ctx.loopFactory(alu.getDock("in2"), 0);
198 lf.abortLoopIfTorpedoPresent();
203 lf = ctx.loopFactory(alu.getDock("inOp"), 0);
204 lf.abortLoopIfTorpedoPresent();
209 lf = ctx.loopFactory(alu.getDock("out"), 0);
210 lf.abortLoopIfTorpedoPresent();
212 lf.sendWord(read ? inAddrRead : inAddrWrite);
214 lf = ctx.loopFactory(out, 0);
215 if (read) lf.recvToken();
216 lf.abortLoopIfTorpedoPresent();
218 if (read) lf.sendWord(debugIn.getDataDestination());
221 lf = ctx.loopFactory(debugIn, 0);
223 lf.abortLoopIfTorpedoPresent();
230 for(int i=vals.length-1; i>=0; i--) {
232 fp.sendWord(inDataWrite.getDataDestination(), vals[i]);
234 BitVector outv = fp.recvWord();
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 +" ");
245 fp.sendToken(inAddrRead.getInstructionDestination());
246 fp.sendToken(debugIn.getInstructionDestination());
248 fp.sendToken(inAddrWrite.getInstructionDestination());
249 fp.sendToken(inDataWrite.getInstructionDestination());
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();
259 pool.releaseShip(alu);
260 pool.releaseShip(counter);
261 if (read) pool.releaseShip(debugIn.getShip());
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]);
271 public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
272 CodeBag ctx = new CodeBag(fp.getFleet());
275 lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
276 lf.abortLoopIfTorpedoPresent();
280 lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
281 lf.abortLoopIfTorpedoPresent();
288 public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
289 fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
290 fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
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);
301 //int size = (548 * 478) / 2;
304 BitVector[] vals = new BitVector[size];
305 BitVector[] vals2 = new BitVector[size];
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);
314 BitVector bv = new BitVector(fleet.getWordWidth());
316 for(int i=0; i<bv.length(); i++) bv.set(i, true);
317 for(int i=0; i<vals.length; i++)
320 ShipPool pool = new ShipPool(fleet);
321 writeMem(fp, pool, memory, 0, vals);
322 readMem(fp, pool, memory, 0, vals2);
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]);
329 System.out.println("done! ("+fails+" failures)");