remove extraneous torpedo transmission from MemoryUtils
[fleet.git] / src / edu / berkeley / fleet / loops / MemoryUtils.java
index 7a92a32..8290954 100644 (file)
@@ -2,6 +2,7 @@ package edu.berkeley.fleet.loops;
 import edu.berkeley.fleet.loops.*;
 import java.util.concurrent.Semaphore;
 import java.util.*;
+import java.io.*;
 import java.net.*;
 import edu.berkeley.fleet.two.*;
 import edu.berkeley.fleet.fpga.*;
@@ -20,252 +21,165 @@ public class MemoryUtils {
                                Ship memory,
                                long offset,
                                BitVector[] vals) throws RuntimeException {
-        doMem(true, fp, pool, memory, offset, vals);
-    }
-
-    public static void writeMem(FleetProcess fp,
-                               ShipPool pool,
-                                Ship memory,
-                                long offset,
-                                long[] vals) throws RuntimeException {
-        doMem(false, fp, pool, memory, offset, long2bv(fp.getFleet(), vals));
+        try {
+            Ship alu = pool.allocateShip("Alu");
+            pool.assertAllocated(memory);
+            MemoryInputStream mos = new MemoryInputStream(fp, pool, memory, alu, offset, 1);
+            fp.flush();
+            for(int i=0; i<vals.length; i++) {
+                int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
+                String status = i + "/" + (vals.length-1) + "= " + pct + "%";
+                System.out.print("\rreading from address: " + status + "...");
+                vals[i] = mos.readWord();
+                System.out.print("\rread    from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
+            }
+            mos.close();
+            pool.releaseShip(alu);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     public static void writeMem(FleetProcess fp,
-                               ShipPool pool,
+                                ShipPool pool,
                                 Ship memory,
                                 long offset,
                                 BitVector[] vals) throws RuntimeException {
-        doMem(false, fp, pool, memory, offset, vals);
+        try {
+            Ship alu = pool.allocateShip("Alu");
+            pool.assertAllocated(memory);
+            MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, alu, offset);
+            for(int i=0; i<vals.length; i++) {
+                int pct = (int)Math.ceil(100.0*((double)(i)/((double)(vals.length-1))));
+                String status = i + "/" + (vals.length-1) + "= " + pct + "%";
+                System.out.print("\rwrote to address: " + status +"           ");
+                mos.writeWord(vals[i]);
+            }
+            mos.close();
+            pool.releaseShip(alu);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
-    /*
-    public static void clearMem(FleetProcess fp,
-                                Ship memory,
-                                ShipPool pool,
-                                long offset,
-                                long count) throws RuntimeException {
-        if (fp.getFleet() != memory.getFleet())
-            throw new RuntimeException("Fleet mismatch");
-
-        Ship counter1 = pool.allocateShip("Counter");
-        Ship counter2 = pool.allocateShip("Counter");
-        //Ship alu      = pool.allocateShip("Alu");
-
-        final Dock debugIn     = fp.getDebugInputDock();
-
-        CodeBag ctx = new CodeBag(fp.getFleet());
-        LoopFactory lf;
-
-        lf = ctx.loopFactory(counter1.getDock("inOp"), 1);
-        lf.literal(counter1.getDock("inOp").getConstant("COUNT"));
-        lf.deliver();
-
-        lf = ctx.loopFactory(counter1.getDock("in1"), 1);
-        lf.literal(count);
-        lf.deliver();
-
-        lf = ctx.loopFactory(counter1.getDock("in2"), 1);
-        lf.literal(1);
-        lf.deliver();
-
-        lf = ctx.loopFactory(counter1.getDock("out"), 0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.recvToken();
-        lf.collect();
-        lf.send(memory.getDock("inAddrWrite").getDataDestination());
+    public static class MemoryOutputStream extends DockOutputStream {
 
-        lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
-        lf.literal(0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.deliver();
+        private FleetProcess fp;
+        private Ship memory;
+        private Ship alu;
 
-        lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 1);
-        lf.sendToken(counter1.getDock("out").getDataDestination());
-        lf = lf.makeNext(0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.recvWord();
-        lf.deliver();
-        lf.sendToken(counter1.getDock("out").getDataDestination());
+        public MemoryOutputStream(FleetProcess fp,
+                                  ShipPool pool,
+                                  Ship memory,
+                                  Ship alu,
+                                  long offset) throws IOException {
 
-        lf = ctx.loopFactory(memory.getDock("out"), 0);
-        lf.recvToken();
-        lf.recvWord();
-        lf.send(counter2.getDock("in2").getDataDestination());
+            super(fp, pool, memory.getDock("inDataWrite"), alu.getDock("out").getDataDestination());
+            pool.assertAllocated(memory);
+            pool.assertAllocated(alu);
 
-        lf = ctx.loopFactory(counter2.getDock("inOp"), 1);
-        lf.literal(counter2.getDock("inOp").getConstant("DROP_C1_V2"));
-        lf.deliver();
-        lf.literal(counter2.getDock("inOp").getConstant("PASS_C1_V2"));
-        lf.deliver();
+            this.fp = fp;
+            this.memory = memory;
+            this.alu = alu;
 
-        lf = ctx.loopFactory(counter2.getDock("in2"), 0);
-        lf.recvWord();
-        lf.deliver();
+            buildIt(fp, memory, alu, memory.getDock("inAddrWrite"), offset);
+            CodeBag cb = new CodeBag(fp.getFleet());
+            LoopFactory lf;
+            lf = cb.loopFactory(memory.getDock("out"), 0);
+            lf.abortLoopIfTorpedoPresent();
+            lf.collectWord();
+            cb.dispatch(fp);
+        }
 
-        fp.sendToken(counter1.getDock("out").getInstructionDestination());
-        fp.sendToken(memory.getDock("inDataWrite").getInstructionDestination());
-        fp.sendToken(memory.getDock("inAddrWrite").getInstructionDestination());
-        fp.sendToken(memory.getDock("out").getInstructionDestination());
-        fp.sendToken(counter2.getDock("in2").getInstructionDestination());
-        
-        lf = ctx.loopFactory(counter2.getDock("in1"), 1);
-        lf.literal(count-1);
-        lf.deliver();
-        lf.literal(1);
-        lf.deliver();
-        
-        lf = ctx.loopFactory(counter2.getDock("out"), 1);
-        lf.collect();
-        lf.send(debugIn.getDataDestination());
+        public void close() {
+            super.close();
+            fp.sendTorpedo(memory.getDock("inAddrWrite"));
+            fp.sendTorpedo(memory.getDock("out"));
+            fp.sendTorpedo(alu.getDock("in1"));
+            fp.sendTorpedo(alu.getDock("in2"));
+            fp.sendTorpedo(alu.getDock("inOp"));
+            fp.sendTorpedo(alu.getDock("out"));
+        }
 
-        pool.releaseShip(counter1);
-        pool.releaseShip(counter2);
-        //pool.releaseShip(alu);
     }
-    */
-
-    public static void doMem(final boolean read,
-                             final FleetProcess fp,
-                             final ShipPool pool,
-                             final Ship memory,
-                             final long offset,
-                             final BitVector[] vals) throws RuntimeException {
-        if (fp.getFleet() != memory.getFleet())
-            throw new RuntimeException("Fleet mismatch");
 
-        final Dock inAddrWrite = memory.getDock("inAddrWrite");
-        final Dock inDataWrite = memory.getDock("inDataWrite");
-        final Dock inAddrRead  = memory.getDock("inAddrRead");
-        final Dock out         = memory.getDock("out");
-        final Dock debugIn     = read ? fp.getDebugInputDock() : null;
-        if (debugIn!=null) pool.allocateShip(debugIn.getShip());
 
-        Ship counter = pool.allocateShip("Counter");
-        Dock counter_in1  = counter.getDock("in1");
-        Dock counter_in2  = counter.getDock("in2");
-        Dock counter_inOp = counter.getDock("inOp");
-        Dock counter_out  = counter.getDock("out");
-
-        Ship alu = pool.allocateShip("Alu");
-
-        CodeBag ctx = new CodeBag(fp.getFleet());
-        LoopFactory lf;
-        if (read) {
-            lf = ctx.loopFactory(inAddrRead, 0);
-            lf.abortLoopIfTorpedoPresent();
-            lf.sendToken(counter_out);
-            lf.recvWord();
-            lf.deliver();
-        } else {
-            lf = ctx.loopFactory(inAddrWrite, 0);
-            lf.sendToken(counter_out);
-            lf.abortLoopIfTorpedoPresent();
-            lf.recvWord();
-            lf.deliver();
-            lf = ctx.loopFactory(inDataWrite, 0);
-            lf.abortLoopIfTorpedoPresent();
-            lf.recvWord();
-            lf.deliver();
+    public static class MemoryInputStream extends DockInputStream {
+
+        private FleetProcess fp;
+        private Ship memory;
+        private Ship alu;
+
+        public MemoryInputStream(FleetProcess fp,
+                                 ShipPool pool,
+                                 Ship memory,
+                                 Ship alu,
+                                 long offset,
+                                 int inflight) throws IOException {
+            super(fp, pool, memory.getDock("out"), alu.getDock("out").getDataDestination(), inflight);
+            pool.assertAllocated(memory);
+            pool.assertAllocated(alu);
+            this.fp = fp;
+            this.memory = memory;
+            this.alu = alu;
+            buildIt(fp, memory, alu, memory.getDock("inAddrRead"), offset);
         }
 
-        lf = ctx.loopFactory(counter_in1, 1);
-        lf.literal(vals.length);
-        lf.deliver();
-
-        lf = ctx.loopFactory(counter_in2, 1);
-        lf.literal(1);
-        lf.deliver();
+        public void close() {
+            super.close();
+            fp.sendTorpedo(memory.getDock("inAddrRead"));
+            fp.sendTorpedo(alu.getDock("in1"));
+            fp.sendTorpedo(alu.getDock("in2"));
+            fp.sendTorpedo(alu.getDock("inOp"));
+            fp.sendTorpedo(alu.getDock("out"));
+        }
+    }
 
-        lf = ctx.loopFactory(counter_inOp, 1);
-        lf.literal("COUNT");
-        lf.deliver();
+    private static void buildIt(FleetProcess fp, Ship memory, Ship alu, Dock dest, long offset) {
 
-        lf = ctx.loopFactory(counter_out, 0);
-        lf.recvToken();
-        lf.abortLoopIfTorpedoPresent();
-        lf.collectWord();
-        lf.sendWord(alu.getDock("in1"));
-        lf.sendToken(alu.getDock("in2"));
-        lf.sendToken(alu.getDock("inOp"));
+        CodeBag ctx = new CodeBag(fp.getFleet());
 
+        LoopFactory lf;
+        // alu.in1: receive and deliver
         lf = ctx.loopFactory(alu.getDock("in1"), 0);
         lf.abortLoopIfTorpedoPresent();
         lf.recvWord();
         lf.deliver();
-
-        lf = ctx.loopFactory(alu.getDock("in2"), 0);
+            
+        // alu.in2: receive tokens, deliver 1's
+        lf = ctx.loopFactory(alu.getDock("in2"), 1);
+        lf.literal(1);
+        lf = lf.makeNext(0);
         lf.abortLoopIfTorpedoPresent();
         lf.recvToken();
-        lf.literal(offset);
         lf.deliver();
-
-        lf = ctx.loopFactory(alu.getDock("inOp"), 0);
+            
+        // alu.inOp: receive tokens, deliver ADD's
+        lf = ctx.loopFactory(alu.getDock("inOp"), 1);
+        lf.literal("ADD");
+        lf = lf.makeNext(0);
         lf.abortLoopIfTorpedoPresent();
         lf.recvToken();
-        lf.literal("ADD");
         lf.deliver();
-
-        lf = ctx.loopFactory(alu.getDock("out"), 0);
+            
+        // alu.out: for each token, provide a word of count-data
+        lf = ctx.loopFactory(alu.getDock("out"), 1);
+        lf.literal(offset);
+        lf = lf.makeNext(0);
         lf.abortLoopIfTorpedoPresent();
+        lf.recvToken();
+        lf.sendWord(dest);
+        lf.sendWord(alu.getDock("in1"));
+        lf.sendToken(alu.getDock("in2"));
+        lf.sendToken(alu.getDock("inOp"));
         lf.collectWord();
-        lf.sendWord(read ? inAddrRead : inAddrWrite);
 
-        lf = ctx.loopFactory(out, 0);
-        if (read) lf.recvToken();
+        lf = ctx.loopFactory(dest, 0);
         lf.abortLoopIfTorpedoPresent();
-        lf.collectWord();
-        if (read) lf.sendWord(debugIn.getDataDestination());
-
-        if (read) {
-            lf = ctx.loopFactory(debugIn, 0);
-            lf.sendToken(out);
-            lf.abortLoopIfTorpedoPresent();
-            lf.recvWord();
-            lf.deliver();
-        }
+        lf.recvWord();
+        lf.deliver();
 
         ctx.dispatch(fp);
-
-        for(int i=vals.length-1; i>=0; i--) {
-            if (!read)
-                fp.sendWord(inDataWrite.getDataDestination(), vals[i]);
-            if (read) {
-                BitVector outv = fp.recvWord();
-                vals[i] = outv;
-            }
-            int pct = (int)Math.ceil(100.0*((double)(vals.length-1-i)/((double)(vals.length-1))));
-            String status = i + "/" + (vals.length-1) + "= " + pct + "%";
-            if (read) System.out.print("\rread from address: " + status + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
-            else      System.out.print("\rwrote to address: " + status +"           ");
-        }
-        fp.flush();
-
-        if (read) {
-            fp.sendToken(inAddrRead.getInstructionDestination());
-            fp.sendToken(debugIn.getInstructionDestination());
-        } else {
-            fp.sendToken(inAddrWrite.getInstructionDestination());
-            fp.sendToken(inDataWrite.getInstructionDestination());
-        }
-        fp.sendToken(alu.getDock("in1").getInstructionDestination());
-        fp.sendToken(alu.getDock("in2").getInstructionDestination());
-        fp.sendToken(alu.getDock("inOp").getInstructionDestination());
-        fp.sendToken(alu.getDock("out").getInstructionDestination());
-        fp.sendToken(counter_out.getInstructionDestination());
-        fp.sendToken(out.getInstructionDestination());
-        System.out.println();
-
-        pool.releaseShip(alu);
-        pool.releaseShip(counter);
-        if (read) pool.releaseShip(debugIn.getShip());
-    }
-
-    private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
-        BitVector[] bv = new BitVector[initialValues.length];
-        for(int i=0; i<initialValues.length; i++)
-            bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);
-        return bv;
     }
 
     public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
@@ -294,8 +208,8 @@ public class MemoryUtils {
         Random random = new Random(System.currentTimeMillis());
         Fleet fleet = new Fpga();
         FleetProcess fp = fleet.run(new Instruction[0]);
-        //Ship memory = fleet.getShip("DDR2",0);
-        Ship memory = fleet.getShip("Dvi",0);
+        Ship memory = fleet.getShip("DDR2",0);
+        //Ship memory = fleet.getShip("Dvi",0);
         //Ship memory = fleet.getShip("Memory",0);
 
         //int size = (548 * 478) / 2;
@@ -308,24 +222,20 @@ public class MemoryUtils {
             vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
             for(int j=36; j<vals[i].length(); j++)
                 vals[i].set(j, false);
-            vals[i].set(1, false);
         }
 
-        BitVector bv = new BitVector(fleet.getWordWidth());
-        /*
-        for(int i=0; i<bv.length(); i++) bv.set(i, true);
-        for(int i=0; i<vals.length; i++)
-            vals[i] = bv;
-        */
         ShipPool pool = new ShipPool(fleet);
+        pool.allocateShip(memory);
         writeMem(fp, pool, memory, 0, vals);
         readMem(fp, pool, memory, 0, vals2);
+        System.out.println();
         int fails = 0;
         for(int i=0; i<vals.length; i++)
             if (!vals[i].equals(vals2[i])) {
-                System.out.println("disagreement!  on index " + i + "\n  "+vals[i]+"\n  "+vals2[i]);
+                System.out.println("disagreement!  on index " + i + "\n  expected="+vals[i]+"\n       got="+vals2[i]);
                 fails++;
             }
         System.out.println("done! ("+fails+" failures)");
+        if (fails>0) System.exit(-1);
     }
 }