remove dead code from MemoryUtils
[fleet.git] / src / edu / berkeley / fleet / loops / MemoryUtils.java
index f8c5163..8d5ad97 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.*;
@@ -15,98 +16,221 @@ import static edu.berkeley.fleet.api.Predicate.*;
 
 public class MemoryUtils {
 
-    public static void readMem(FleetProcess fp, Ship memory, long offset, BitVector[] vals) throws RuntimeException {
-        doMem(true, fp, memory, offset, vals);
+    public static void readMem(FleetProcess fp,
+                               ShipPool pool,
+                               Ship memory,
+                               long offset,
+                               BitVector[] vals) throws RuntimeException {
+        doMem(true, fp, pool, memory, offset, vals);
     }
-    public static void writeMem(FleetProcess fp, Ship memory, long offset, long[] vals) throws RuntimeException {
-        doMem(false, fp, memory, offset, long2bv(fp.getFleet(), 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));
     }
-    public static void writeMem(FleetProcess fp, Ship memory, long offset, BitVector[] vals) throws RuntimeException {
-        doMem(false, fp, memory, offset, vals);
+
+    public static void writeMem(FleetProcess fp,
+                               ShipPool pool,
+                                Ship memory,
+                                long offset,
+                                BitVector[] vals) throws RuntimeException {
+        doMem(false, fp, pool, memory, offset, vals);
     }
-    public static void doMem(final boolean read, final FleetProcess fp, 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     = fp.getDebugInputDock();
+    public static void doMem(final boolean read,
+                             final FleetProcess fp,
+                             final ShipPool pool,
+                             final Ship memory,
+                             final long offset,
+                             final BitVector[] vals) {
 
-        final Semaphore sem = new Semaphore(12 /* FIXME */);
+        try {
+            MemoryOutputStream mos = new MemoryOutputStream(fp, pool, memory, offset, read);
+            for(int i=0; i<vals.length; i++) {
+                if (read) vals[i] = mos.readWord();
+                else      mos.writeWord(vals[i]);
+                int pct = (int)Math.ceil(100.0*((double)(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 +"           ");
+            }
+            mos.close();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
 
-        Context ctx = new Context(fp.getFleet());
-        LoopFactory lf;
-        if (read) {
-            lf = new LoopFactory(ctx, inAddrRead, 0);
+
+    public static class MemoryOutputStream {
+        private FleetProcess fp;
+        private ShipPool pool;
+        private Ship memory;
+        private Ship alu;
+        private long offset;
+        private int inflight;
+        private boolean read;
+
+        public MemoryOutputStream(FleetProcess fp,
+                                  ShipPool pool,
+                                  Ship memory,
+                                  long offset,
+                                  boolean read) throws IOException {
+            this(fp, pool, memory, offset, read, 1);
+        }
+
+        public MemoryOutputStream(FleetProcess fp,
+                                  ShipPool pool,
+                                  Ship memory,
+                                  long offset,
+                                  boolean read,
+                                  int inflight) throws IOException {
+
+            if (fp.getFleet() != memory.getFleet())
+                throw new RuntimeException("Fleet mismatch");
+            this.fp = fp;
+            this.pool = pool;
+            this.memory = memory;
+            this.offset = offset;
+            this.inflight = inflight;
+            this.read = read;
+            this.alu = pool.allocateShip("Alu");
+            pool.allocateShip(fp.getDebugInputDock().getShip());
+
+            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();
-        } else {
-            lf = new LoopFactory(ctx, inAddrWrite, 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.recvWord();
+            lf.recvToken();
             lf.deliver();
-            lf = new LoopFactory(ctx, inDataWrite, 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.recvWord();
+            lf.recvToken();
             lf.deliver();
-        }
+            
+            // 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(read ? memory.getDock("inAddrRead") : memory.getDock("inAddrWrite"));
+            lf.sendWord(alu.getDock("in1"));
+            lf.sendToken(alu.getDock("in2"));
+            lf.sendToken(alu.getDock("inOp"));
+            lf.collectWord();
 
-        lf = new LoopFactory(ctx, out, 0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.collectWord();
-        lf.sendWord(debugIn.getDataDestination());
+            if (read) {
+                // memory.inAddrRead: just recv and deliver
+                lf = ctx.loopFactory(memory.getDock("inAddrRead"), 0);
+                lf.abortLoopIfTorpedoPresent();
+                lf.recvWord();
+                lf.deliver();
 
-        lf = new LoopFactory(ctx, debugIn, 0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.recvWord();
-        lf.deliver();
+            } else {
+                // memory.inDataWrite: recv a word, send a token to alu.out, deliver the word
+                lf = ctx.loopFactory(memory.getDock("inDataWrite"), 0);
+                lf.abortLoopIfTorpedoPresent();
+                lf.recvWord();
+                lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                lf.setPredicate(Predicate.FlagA);
+                lf.sendToken(fp.getDebugInputDock());
+                lf.setPredicate(Predicate.NotFlagA);
+                lf.sendToken(alu.getDock("out"));
+                lf.deliver();
+                lf.setPredicate(null);
+                
+                // memory.inAddrWrite: just recv and deliver
+                lf = ctx.loopFactory(memory.getDock("inAddrWrite"), 0);
+                lf.abortLoopIfTorpedoPresent();
+                lf.recvWord();
+                lf.deliver();
+            }
 
-        ArrayList<Instruction> ai = new ArrayList<Instruction>();
-        ctx.emit(ai);
-        for(Instruction ins : ai)
-            fp.sendInstruction(ins);
-
-        new Thread() {
-            public void run() { try {
-                    for(int i=0; i<vals.length; i++) {
-                        if (!sem.tryAcquire()) {
-                            fp.flush();
-                            sem.acquire();
-                        }
-                        if (read) {
-                            fp.sendWord(inAddrRead.getDataDestination(), new BitVector(fp.getFleet().getWordWidth()).set(i+offset));
-                        } else {
-                            fp.sendWord(inAddrWrite.getDataDestination(), new BitVector(fp.getFleet().getWordWidth()).set(i+offset));
-                            fp.sendWord(inDataWrite.getDataDestination(), vals[i]);
-                        }
-                    }
-                    fp.flush();
-                } catch (Exception e) { throw new RuntimeException(e); }
-                }
-        }.start();
+            // memory.out: send a token to debug.in, recv a word, deliver it
+            lf = ctx.loopFactory(memory.getDock("out"), 0);
+            lf.abortLoopIfTorpedoPresent();
+            lf.collectWord();
+            if (read) lf.sendWord(fp.getDebugInputDock());
+            // FIXME: perhaps feed-through here if we get fancy
 
-        for(int i=0; i<vals.length; i++) {
-            BitVector outv = fp.recvWord();
-            if (read) vals[i] = outv;
-            if (read) System.out.print("\rread from address: " + i + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
-            else      System.out.print("\rwrote to address: " + i+"           ");
-            sem.release();
+            if (read) {
+                lf = ctx.loopFactory(fp.getDebugInputDock(), inflight);
+                lf.sendToken(alu.getDock("out"));
+                lf = lf.makeNext(0);
+                lf.abortLoopIfTorpedoPresent();
+                lf.recvWord();
+                lf.deliver();
+                lf.sendToken(alu.getDock("out"));
+            }
+            ctx.dispatch(fp);
+            fp.flush();
         }
 
-        if (read) {
-            fp.sendToken(inAddrRead.getInstructionDestination());
-        } else {
-            fp.sendToken(inAddrWrite.getInstructionDestination());
-            fp.sendToken(inDataWrite.getInstructionDestination());
+        public BitVector readWord() {
+            return fp.recvWord();
+        }
+
+        public void writeWord(BitVector bv) {
+            fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(0));
+        }
+
+        public void close() {
+            CodeBag ctx = new CodeBag(fp.getFleet());
+            LoopFactory lf;
+            lf = ctx.loopFactory(fp.getDebugInputDock(), 1);
+            if (read) {
+                fp.sendTorpedo(fp.getDebugInputDock());
+                fp.flush();
+                for(int i=0; i<inflight; i++) // FIXME: use a loop counter here
+                    lf.recvWord();
+                lf.deliver();
+            } else {
+                lf.recvWord();
+                lf.deliver();
+                BitVector bv = new BitVector(fp.getFleet().getWordWidth());
+                fp.sendWord(memory.getDock("inDataWrite").getDataDestination(), bv, new BitVector(1).set(1));
+            }
+            ctx.dispatch(fp);
+            fp.flush();
+            fp.recvWord();
+
+            if (read) {
+                fp.sendTorpedo(memory.getDock("inAddrRead"));
+            } else {
+                fp.sendTorpedo(memory.getDock("inAddrWrite"));
+                fp.sendTorpedo(memory.getDock("inDataWrite"));
+            }
+
+            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"));
+            fp.flush();
+            
+            pool.releaseShip(alu);
+            pool.releaseShip(fp.getDebugInputDock().getShip());
         }
-        fp.sendToken(out.getInstructionDestination());
-        fp.sendToken(debugIn.getInstructionDestination());
-        System.out.println();
     }
 
+
     private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
         BitVector[] bv = new BitVector[initialValues.length];
         for(int i=0; i<initialValues.length; i++)
@@ -115,15 +239,15 @@ public class MemoryUtils {
     }
 
     public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
-        Context ctx = new Context(fp.getFleet());
+        CodeBag ctx = new CodeBag(fp.getFleet());
         LoopFactory lf;
 
-        lf = new LoopFactory(ctx, memoryShip.getDock("out"), 0);
+        lf = ctx.loopFactory(memoryShip.getDock("out"), 0);
         lf.abortLoopIfTorpedoPresent();
         lf.collectPacket();
-        lf.sendWord(null);
+        lf.sendPacket();
 
-        lf = new LoopFactory(ctx, memoryShip.getDock("inCBD"), 0);
+        lf = ctx.loopFactory(memoryShip.getDock("inCBD"), 0);
         lf.abortLoopIfTorpedoPresent();
         lf.recvWord();
         lf.deliver();
@@ -141,16 +265,33 @@ public class MemoryUtils {
         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("Memory",0);
-        BitVector[] vals  = new BitVector[2 * 1024];
-        BitVector[] vals2 = new BitVector[2 * 1024];
-        for(int i=0; i<vals.length; i++)
+
+        //int size = (548 * 478) / 2;
+        int size = 2048;
+
+        BitVector[] vals  = new BitVector[size];
+        BitVector[] vals2 = new BitVector[size];
+
+        for(int i=0; i<vals.length; i++) {
             vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
-        writeMem(fp, memory, 0, vals);
-        readMem(fp, memory, 0, vals2);
+            for(int j=36; j<vals[i].length(); j++)
+                vals[i].set(j, false);
+            vals[i].set(1, false);
+        }
+
+        ShipPool pool = new ShipPool(fleet);
+        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("done!");
+            if (!vals[i].equals(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);
     }
 }