1 package edu.berkeley.fleet.loops;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.concurrent.Semaphore;
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.*;
17 public class MemoryUtils {
19 public static void readMem(FleetProcess fp,
23 BitVector[] vals) throws RuntimeException {
24 doMem(true, fp, pool, memory, offset, vals);
27 public static void writeMem(FleetProcess fp,
31 long[] vals) throws RuntimeException {
32 doMem(false, fp, pool, memory, offset, long2bv(fp.getFleet(), vals));
35 public static void writeMem(FleetProcess fp,
39 BitVector[] vals) throws RuntimeException {
40 doMem(false, fp, pool, memory, offset, vals);
44 public static void clearMem(FleetProcess fp,
48 long count) throws RuntimeException {
49 if (fp.getFleet() != memory.getFleet())
50 throw new RuntimeException("Fleet mismatch");
52 Ship counter1 = pool.allocateShip("Counter");
53 Ship counter2 = pool.allocateShip("Counter");
54 //Ship alu = pool.allocateShip("Alu");
56 final Dock debugIn = fp.getDebugInputDock();
58 CodeBag ctx = new CodeBag(fp.getFleet());
61 lf = ctx.loopFactory(counter1.getDock("inOp"), 1);
62 lf.literal(counter1.getDock("inOp").getConstant("COUNT"));
65 lf = ctx.loopFactory(counter1.getDock("in1"), 1);
69 lf = ctx.loopFactory(counter1.getDock("in2"), 1);
73 lf = ctx.loopFactory(counter1.getDock("out"), 0);
74 lf.abortLoopIfTorpedoPresent();
77 lf.send(memory.getDock("inAddrWrite").getDataDestination());
79 lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
81 lf.abortLoopIfTorpedoPresent();
84 lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 1);
85 lf.sendToken(counter1.getDock("out").getDataDestination());
87 lf.abortLoopIfTorpedoPresent();
90 lf.sendToken(counter1.getDock("out").getDataDestination());
92 lf = ctx.loopFactory(memory.getDock("out"), 0);
95 lf.send(counter2.getDock("in2").getDataDestination());
97 lf = ctx.loopFactory(counter2.getDock("inOp"), 1);
98 lf.literal(counter2.getDock("inOp").getConstant("DROP_C1_V2"));
100 lf.literal(counter2.getDock("inOp").getConstant("PASS_C1_V2"));
103 lf = ctx.loopFactory(counter2.getDock("in2"), 0);
107 fp.sendToken(counter1.getDock("out").getInstructionDestination());
108 fp.sendToken(memory.getDock("inDataWrite").getInstructionDestination());
109 fp.sendToken(memory.getDock("inAddrWrite").getInstructionDestination());
110 fp.sendToken(memory.getDock("out").getInstructionDestination());
111 fp.sendToken(counter2.getDock("in2").getInstructionDestination());
113 lf = ctx.loopFactory(counter2.getDock("in1"), 1);
119 lf = ctx.loopFactory(counter2.getDock("out"), 1);
121 lf.send(debugIn.getDataDestination());
123 pool.releaseShip(counter1);
124 pool.releaseShip(counter2);
125 //pool.releaseShip(alu);
129 public static void doMem(final boolean read,
130 final FleetProcess fp,
134 final BitVector[] vals) {
137 MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, offset, read);
138 for(int i=0; i<vals.length; i++) {
139 if (read) vals[i] = mos.readWord();
140 else mos.writeWord(vals[i]);
141 int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
142 String status = i + "/" + (vals.length-1) + "= " + pct + "%";
143 if (read) System.out.print("\rread from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+" ");
144 else System.out.print("\rwrote to address: " + status +" ");
147 } catch (IOException e) {
148 throw new RuntimeException(e);
153 public static class MemoryOutputStream {
154 private FleetProcess fp;
155 private ShipPool pool;
159 private int inflight;
160 private boolean read;
162 public MemoryOutputStream(FleetProcess fp,
166 boolean read) throws IOException {
167 this(fp, pool, memory, offset, read, 1);
170 public MemoryOutputStream(FleetProcess fp,
175 int inflight) throws IOException {
177 if (fp.getFleet() != memory.getFleet())
178 throw new RuntimeException("Fleet mismatch");
181 this.memory = memory;
182 this.offset = offset;
183 this.inflight = inflight;
185 this.alu = pool.allocateShip("Alu");
186 pool.allocateShip(fp.getDebugInputDock().getShip());
188 CodeBag ctx = new CodeBag(fp.getFleet());
191 // alu.in1: receive and deliver
192 lf = ctx.loopFactory(alu.getDock("in1"), 0);
193 lf.abortLoopIfTorpedoPresent();
197 // alu.in2: receive tokens, deliver 1's
198 lf = ctx.loopFactory(alu.getDock("in2"), 1);
201 lf.abortLoopIfTorpedoPresent();
205 // alu.inOp: receive tokens, deliver ADD's
206 lf = ctx.loopFactory(alu.getDock("inOp"), 1);
209 lf.abortLoopIfTorpedoPresent();
213 // alu.out: for each token, provide a word of count-data
214 lf = ctx.loopFactory(alu.getDock("out"), 1);
217 lf.abortLoopIfTorpedoPresent();
219 lf.sendWord(read ? memory.getDock("inAddrRead") : memory.getDock("inAddrWrite"));
220 lf.sendWord(alu.getDock("in1"));
221 lf.sendToken(alu.getDock("in2"));
222 lf.sendToken(alu.getDock("inOp"));
226 // memory.inAddrRead: just recv and deliver
227 lf = ctx.loopFactory(memory.getDock("inAddrRead"), 0);
228 lf.abortLoopIfTorpedoPresent();
233 // memory.inDataWrite: recv a word, send a token to alu.out, deliver the word
234 lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
235 lf.abortLoopIfTorpedoPresent();
237 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
238 lf.setPredicate(Predicate.FlagA);
239 lf.sendToken(fp.getDebugInputDock());
240 lf.setPredicate(Predicate.NotFlagA);
241 lf.sendToken(alu.getDock("out"));
243 lf.setPredicate(null);
245 // memory.inAddrWrite: just recv and deliver
246 lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 0);
247 lf.abortLoopIfTorpedoPresent();
252 // memory.out: send a token to debug.in, recv a word, deliver it
253 lf = ctx.loopFactory(memory.getDock("out"), 0);
254 lf.abortLoopIfTorpedoPresent();
256 if (read) lf.sendWord(fp.getDebugInputDock());
257 // FIXME: perhaps feed-through here if we get fancy
260 lf = ctx.loopFactory(fp.getDebugInputDock(), inflight);
261 lf.sendToken(alu.getDock("out"));
263 lf.abortLoopIfTorpedoPresent();
266 lf.sendToken(alu.getDock("out"));
272 public BitVector readWord() {
273 return fp.recvWord();
276 public void writeWord(BitVector bv) {
277 fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(0));
280 public void close() {
281 CodeBag ctx = new CodeBag(fp.getFleet());
283 lf = ctx.loopFactory(fp.getDebugInputDock(), 1);
285 fp.sendTorpedo(fp.getDebugInputDock());
287 for(int i=0; i<inflight; i++) // FIXME: use a loop counter here
293 BitVector bv = new BitVector(fp.getFleet().getWordWidth());
294 fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(1));
301 fp.sendTorpedo(memory.getDock("inAddrRead"));
303 fp.sendTorpedo(memory.getDock("inAddrWrite"));
304 fp.sendTorpedo(memory.getDock("inDataWrite"));
307 fp.sendTorpedo(memory.getDock("out"));
308 fp.sendTorpedo(alu.getDock("in1"));
309 fp.sendTorpedo(alu.getDock("in2"));
310 fp.sendTorpedo(alu.getDock("inOp"));
311 fp.sendTorpedo(alu.getDock("out"));
314 pool.releaseShip(alu);
315 pool.releaseShip(fp.getDebugInputDock().getShip());
320 private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
321 BitVector[] bv = new BitVector[initialValues.length];
322 for(int i=0; i<initialValues.length; i++)
323 bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);
327 public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
328 CodeBag ctx = new CodeBag(fp.getFleet());
331 lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
332 lf.abortLoopIfTorpedoPresent();
336 lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
337 lf.abortLoopIfTorpedoPresent();
344 public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
345 fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
346 fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
349 public static void main(String[] s) throws Exception {
350 Random random = new Random(System.currentTimeMillis());
351 Fleet fleet = new Fpga();
352 FleetProcess fp = fleet.run(new Instruction[0]);
353 Ship memory = fleet.getShip("DDR2",0);
354 //Ship memory = fleet.getShip("Dvi",0);
355 //Ship memory = fleet.getShip("Memory",0);
357 //int size = (548 * 478) / 2;
360 BitVector[] vals = new BitVector[size];
361 BitVector[] vals2 = new BitVector[size];
363 for(int i=0; i<vals.length; i++) {
364 vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
365 for(int j=36; j<vals[i].length(); j++)
366 vals[i].set(j, false);
367 vals[i].set(1, false);
370 ShipPool pool = new ShipPool(fleet);
371 writeMem(fp, pool, memory, 0, vals);
372 readMem(fp, pool, memory, 0, vals2);
373 System.out.println();
375 for(int i=0; i<vals.length; i++)
376 if (!vals[i].equals(vals2[i])) {
377 System.out.println("disagreement! on index " + i + "\n expected="+vals[i]+"\n got="+vals2[i]);
380 System.out.println("done! ("+fails+" failures)");
381 if (fails>0) System.exit(-1);