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);
43 public static void doMem(final boolean read,
44 final FleetProcess fp,
48 final BitVector[] vals) {
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 +" ");
61 } catch (IOException e) {
62 throw new RuntimeException(e);
67 public static class MemoryOutputStream {
68 private FleetProcess fp;
69 private ShipPool pool;
76 public MemoryOutputStream(FleetProcess fp,
80 boolean read) throws IOException {
81 this(fp, pool, memory, offset, read, 1);
84 public MemoryOutputStream(FleetProcess fp,
89 int inflight) throws IOException {
91 if (fp.getFleet() != memory.getFleet())
92 throw new RuntimeException("Fleet mismatch");
97 this.inflight = inflight;
99 this.alu = pool.allocateShip("Alu");
100 pool.allocateShip(fp.getDebugInputDock().getShip());
102 CodeBag ctx = new CodeBag(fp.getFleet());
105 // alu.in1: receive and deliver
106 lf = ctx.loopFactory(alu.getDock("in1"), 0);
107 lf.abortLoopIfTorpedoPresent();
111 // alu.in2: receive tokens, deliver 1's
112 lf = ctx.loopFactory(alu.getDock("in2"), 1);
115 lf.abortLoopIfTorpedoPresent();
119 // alu.inOp: receive tokens, deliver ADD's
120 lf = ctx.loopFactory(alu.getDock("inOp"), 1);
123 lf.abortLoopIfTorpedoPresent();
127 // alu.out: for each token, provide a word of count-data
128 lf = ctx.loopFactory(alu.getDock("out"), 1);
131 lf.abortLoopIfTorpedoPresent();
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"));
140 // memory.inAddrRead: just recv and deliver
141 lf = ctx.loopFactory(memory.getDock("inAddrRead"), 0);
142 lf.abortLoopIfTorpedoPresent();
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();
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"));
157 lf.setPredicate(null);
159 // memory.inAddrWrite: just recv and deliver
160 lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 0);
161 lf.abortLoopIfTorpedoPresent();
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();
170 if (read) lf.sendWord(fp.getDebugInputDock());
171 // FIXME: perhaps feed-through here if we get fancy
174 lf = ctx.loopFactory(fp.getDebugInputDock(), inflight);
175 lf.sendToken(alu.getDock("out"));
177 lf.abortLoopIfTorpedoPresent();
180 lf.sendToken(alu.getDock("out"));
186 public BitVector readWord() {
187 return fp.recvWord();
190 public void writeWord(BitVector bv) {
191 fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(0));
194 public void close() {
195 CodeBag ctx = new CodeBag(fp.getFleet());
197 lf = ctx.loopFactory(fp.getDebugInputDock(), 1);
199 fp.sendTorpedo(fp.getDebugInputDock());
201 for(int i=0; i<inflight; i++) // FIXME: use a loop counter here
207 BitVector bv = new BitVector(fp.getFleet().getWordWidth());
208 fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(1));
215 fp.sendTorpedo(memory.getDock("inAddrRead"));
217 fp.sendTorpedo(memory.getDock("inAddrWrite"));
218 fp.sendTorpedo(memory.getDock("inDataWrite"));
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"));
228 pool.releaseShip(alu);
229 pool.releaseShip(fp.getDebugInputDock().getShip());
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]);
241 public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
242 CodeBag ctx = new CodeBag(fp.getFleet());
245 lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
246 lf.abortLoopIfTorpedoPresent();
250 lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
251 lf.abortLoopIfTorpedoPresent();
258 public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
259 fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
260 fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
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);
271 //int size = (548 * 478) / 2;
274 BitVector[] vals = new BitVector[size];
275 BitVector[] vals2 = new BitVector[size];
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);
284 ShipPool pool = new ShipPool(fleet);
285 writeMem(fp, pool, memory, 0, vals);
286 readMem(fp, pool, memory, 0, vals2);
287 System.out.println();
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]);
294 System.out.println("done! ("+fails+" failures)");
295 if (fails>0) System.exit(-1);