add Junk.java
authoradam <adam@megacz.com>
Mon, 3 Nov 2008 09:35:30 +0000 (10:35 +0100)
committeradam <adam@megacz.com>
Mon, 3 Nov 2008 09:35:30 +0000 (10:35 +0100)
src/edu/berkeley/fleet/ir/Junk.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/fleet/ir/Junk.java b/src/edu/berkeley/fleet/ir/Junk.java
new file mode 100644 (file)
index 0000000..9fc1ce2
--- /dev/null
@@ -0,0 +1,518 @@
+package edu.berkeley.fleet.ir;
+import java.util.*;
+import java.net.*;
+import edu.berkeley.fleet.two.*;
+import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.fpga.*;
+import edu.berkeley.fleet.api.Instruction.*;
+import edu.berkeley.fleet.api.Instruction.Set;
+import edu.berkeley.fleet.api.Instruction.Set.*;
+import static edu.berkeley.fleet.api.Predicate.*;
+import static edu.berkeley.fleet.util.BitManipulations.*;
+
+public class Junk {
+
+    /*
+    public class RepeatModule extends Module {
+        private final Ship   alu;
+        public final InPort  count = new InPort("count") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.recvWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvWord(lf);
+                    lf.setPredicate(null);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    // RECV_AFTER_TORPEDO == 1
+                    return lf;
+                }
+            };
+        public final InPort  val = new InPort("val") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 1);
+
+                    peer.sendToken(lf);
+                    lf.literal(1);
+                    lf.deliver();
+                    lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO);
+
+                    lf = lf.makeNext(0);
+                    lf.setPredicate(Predicate.FlagA);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvWord(lf);
+                    lf.setPredicate(null);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    lf.literal(1);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    lf.recvWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    return lf;
+                }
+            };
+        public final OutPort out = new OutPort("out") {
+                public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
+                public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
+                    lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+
+                    lf = lf.makeNext(0);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+                    lf.setPredicate(null);
+                    lf.setPredicate(Predicate.NotFlagA);
+                    lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
+                    lf.setPredicate(null);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+                    lf.setPredicate(Predicate.FlagA);
+                    lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
+                    lf.setPredicate(null);
+                    lf.setPredicate(Predicate.NotFlagA);
+                    lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
+                    peer.recvToken(lf);
+                    peer.sendWord(lf);
+                    lf.setPredicate(null);
+                    return lf;
+                }
+            };
+        public RepeatModule() {
+            this.alu = ctx.allocateShip("Alu");
+        }
+        public void build() {
+            super.build();
+            Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 0);
+            lf.literal(3); // SUB
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+            lf.literal(1); // IN2
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+        }
+    }
+    */
+
+    /*
+    public class PunctuatorModule extends Module {
+        private final Ship   alu;
+        private final long punctuator;
+        private final boolean unpunctuate;
+        public final InPort  count = new InPort("count") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
+                    lf.recvWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    //lf.abortLoopIfTorpedoPresent(); FIXME
+                    peer.recvWord(lf);
+                    lf.setPredicate(null);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    return lf;
+                }
+            };
+        public final InPort  val = new InPort("val") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 0);
+                    lf.literal(0);
+                    lf.deliver();
+                    lf.literal(1);
+                    lf.deliver();
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.recvWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvWord(lf);
+                    lf.setPredicate(null);
+                    lf.deliver();
+                    return lf;
+                }
+            };
+        public final OutPort out = new OutPort("out") {
+                public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
+                public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
+                    lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+
+                    lf = lf.makeNext(0);
+
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+
+                    if (unpunctuate) {
+                        lf.setPredicate(Predicate.FlagA);
+                        lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+                        lf.setPredicate(Predicate.NotFlagA);
+                        lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
+                        lf.setPredicate(null);
+                        lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
+                    } else {
+                        // if count==0, issue punctuation and tell in1 to get a new count
+                        lf.setPredicate(Predicate.FlagA);
+                        lf.literal(punctuator);
+                        lf.abortLoopIfTorpedoPresent();
+                        peer.recvToken(lf);
+                        peer.sendWord(lf);
+                        lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+                        lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
+                        lf.setPredicate(null);
+
+                        // if count!=0, cycle around the decremented count and pass a value through
+                        lf.setPredicate(Predicate.NotFlagA);
+                        lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
+                        lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
+                        lf.setPredicate(null);
+                    }
+
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+
+                    if (unpunctuate) {
+                        lf.setPredicate(Predicate.NotFlagA);
+                        lf.abortLoopIfTorpedoPresent();
+                        peer.recvToken(lf);
+                        peer.sendWord(lf);
+                        lf.setPredicate(null);
+                    } else {
+                        lf.setPredicate(Predicate.NotFlagA);
+                        lf.abortLoopIfTorpedoPresent();
+                        peer.recvToken(lf);
+                        peer.sendWord(lf);
+                        lf.setPredicate(null);
+                    }
+
+                    return lf;
+                }
+            };
+        public PunctuatorModule(long punctuator, boolean unpunctuate) {
+            this.alu = ctx.allocateShip("Alu");
+            this.punctuator = punctuator;
+            this.unpunctuate = unpunctuate;
+        }
+        public void build() {
+            super.build();
+            Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 0);
+            lf.literal(6); // CMP
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+            lf.literal(3); // SUB
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+            lf.literal(1); // IN2
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+        }
+    }
+*/
+    /*
+    public class DownCounterModule extends Module {
+        private final int internal_inflight;
+        private final boolean incr_is_already_negative;
+        private final Ship   alu;
+        public final InPort  start = new InPort("start") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
+                    lf.recvWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvWord(lf);
+                    lf.setPredicate(null);
+                    lf.deliver();
+                    return lf;
+                }
+            };
+        public final InPort  incr = new InPort("incr") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 0);
+                    lf.recvToken();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    peer.sendToken(lf);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvWord(lf);
+                    lf.deliver();
+                    lf.setPredicate(Predicate.NotFlagA);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.deliver();
+                    return lf;
+                }
+            };
+        public final OutPort out = new OutPort("out") {
+                public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
+                public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
+                    lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO);
+
+                    lf = lf.makeNext(0);
+                    lf.setPredicate(Predicate.FlagA);                    
+                    lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
+                    lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
+                    lf.setPredicate(Predicate.NotFlagA);
+                    lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
+                    lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
+                    lf.setPredicate(null);
+                    lf.abortLoopIfTorpedoPresent();
+                    lf.collectWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.NotFlagA);
+                    lf.abortLoopIfTorpedoPresent();
+                    peer.recvToken(lf);
+                    peer.sendWord(lf);
+                    lf.setPredicate(null);
+                    return lf;
+                }
+            };
+        public DownCounterModule(boolean incr_is_already_negative, int internal_inflight) {
+            this.alu = ctx.allocateShip("Alu");
+            this.incr_is_already_negative = incr_is_already_negative;
+            this.internal_inflight = internal_inflight;
+            if (internal_inflight != 0) throw new RuntimeException("not yet supported");
+        }
+        public void build() {
+            super.build();
+            Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 1);
+            // Phase 2 (FIXME: tokens for flow control here)
+            lf.literal(incr_is_already_negative
+                       ? 2 // ADD
+                       : 3 // SUB
+                       );
+            lf = lf.makeNext(0);
+            lf.abortLoopIfTorpedoPresent();
+            lf.deliver();
+        }
+    }
+    */
+    /**
+     *  A "vector register" which is initialized holding zero or more
+     *  elements and which will expect to receive count-many elements
+     *  before going idle
+     */
+    /*
+DOES NOT HANDLE TORPEDOES
+    public class VectorRegisterModule extends Module {
+        public final InPort  in;
+        public final OutPort out;
+        public final int count;
+        private final Ship ship;
+        public VectorRegisterModule(final long[] initialValues, final int count) {
+            this(longsToBitVectors(initialValues), count);
+        }
+        public VectorRegisterModule(final BitVector[] initialValues, final int count) {
+            this.count = count;
+            ship = ctx.allocateShip("Fifo");
+            in  = new PlainInPort("in",  ship.getDock("in"), count) {
+                    public Context.LoopFactory build(Context.LoopFactory lf) {
+                        lf.disableInstructionFifoOverflowCheck();
+                        for(int i=0; i<initialValues.length; i++) {
+                            lf.literal(initialValues[i]);
+                            lf.deliver();
+                        }
+                        return super.build(lf.makeNext(1));
+                    }
+                };
+            out = new PlainOutPort("out", ship.getDock("out"), initialValues.length);
+        }
+    }
+    */
+
+/*
+    public class DownCounterModule extends Module {
+        private final int internal_inflight;
+        private final boolean incr_is_already_negative;
+        private final Ship   alu;
+        public final InPort  incr = new InPort("incr") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination()); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 1);
+
+                    // Phase 1
+                    peer.sendToken(lf);
+                    peer.recvWord(lf);
+                    for(int i=0; i<internal_inflight;   i++) lf.deliver();
+
+                    // Phase 2
+                    for(int i=0; i<internal_inflight*2; i++) {
+                        lf.recvWord();
+                        lf.deliver();
+                    }
+
+                    // Phase 3 (FIXME: tokens for flow control here)
+                    lf = lf.makeNext(0);
+                    lf.recvWord();
+                    lf.deliver();
+
+                    return lf;
+                }
+            };
+        public final InPort  start = new InPort("start") {
+                public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
+                public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination()); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 1);
+
+                    // Phase 1
+                    peer.sendToken(lf);
+                    peer.recvWord(lf);
+                    for(int i=0; i<internal_inflight; i++) lf.deliver();
+
+                    // Phase 2
+                    lf.literal(0);
+                    for(int i=0; i<internal_inflight; i++) {
+                        lf.deliver();
+                        lf.deliver();
+                        lf.recvWord();
+                    }
+
+                    // Phase 3 (FIXME: tokens for flow control here)
+                    lf = lf.makeNext(0);
+                    lf.deliver();
+
+                    return lf;
+                }
+            };
+        public final OutPort out = new OutPort("out") {
+                public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
+                public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
+                public Context.LoopFactory build() {
+                    Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
+
+                    // Phase 1
+                    for(int i=0; i<internal_inflight*2; i++) {
+                        lf.collectWord();
+                        lf.sendWord(alu.getDock("in1").getDataDestination());
+                    }
+
+                    // Phase 2
+                    for(int i=0; i<internal_inflight; i++) {
+                        lf.collectWord();
+                        lf.sendWord(alu.getDock("in2").getDataDestination());
+
+                        lf.collectWord();
+
+                        //lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                        //lf.setPredicate(Predicate.FlagA);
+                        //lf.abort();
+                        //lf.setPredicate(null);
+
+                        lf.sendWord(alu.getDock("in1").getDataDestination());
+                        peer.recvToken(lf);
+                        peer.sendWord(lf);
+                    }
+
+                    // Phase 3
+                    lf = lf.makeNext(0);
+
+                    lf.collectWord();
+                    lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
+                    lf.setPredicate(Predicate.FlagA);
+                    lf.abort();
+                    lf.setPredicate(null);
+                    lf.sendWord(alu.getDock("in1").getDataDestination());
+                    peer.recvToken(lf);
+                    peer.sendWord(lf);
+
+                    return lf;
+                }
+            };
+        public DownCounterModule(boolean incr_is_already_negative, int internal_inflight) {
+            this.alu = ctx.allocateShip("Alu");
+            this.incr_is_already_negative = incr_is_already_negative;
+            this.internal_inflight = internal_inflight;
+            if (!incr_is_already_negative) throw new RuntimeException("this is broken right now");
+        }
+        public void build() {
+            super.build();
+
+            Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 1);
+
+            // Phase 1
+            for(int i=0; i<internal_inflight;   i++) {
+                lf.literal(0);  // IN1
+                lf.deliver();
+                lf.literal(1);  // IN2
+                lf.deliver();
+            }
+
+            // Phase 2 (FIXME: tokens for flow control here)
+            lf.literal(incr_is_already_negative
+                       ? 2 // ADD
+                       : 3 // SUB
+                       );
+            lf = lf.makeNext(0);
+            lf.deliver();
+        }
+    }
+ */
+
+        /*
+        VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 12, 14, 18, 15 }, 0);
+        VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 13, 17, 1,  3  }, 0);
+        VectorRegisterModule cm3 = proc.new VectorRegisterModule(new long[] { 2,  2,  2,  2  }, 0);
+
+        AluModule alu = proc.new AluModule();
+
+        ForeverModule fmm = proc.new ForeverModule(2);
+        fmm.in.connect(cm2.out);
+        fmm.out.connect(alu.in2);
+        fmm.out_const.connect(alu.inOp);
+        alu.in1.connect(cm1.out);
+        alu.out.connect(dm.in);
+        */
+
+        /*
+        VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 43, 10, 5, 5, 5 }, 0);
+        VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 2,   1, 1, 2, 3 }, 0);
+        DownCounterModule dcm = proc.new DownCounterModule(false, 0);
+        dcm.start.connect(cm1.out);
+        dcm.incr.connect(cm2.out);
+        dcm.out.connect(dm.in);
+        */
+
+        /*        
+        VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 43, 10, 5, 8, 5 }, 0);
+        VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 7,   1, 1, 2, 3 }, 0);
+        RepeatModule rm = proc.new RepeatModule();
+        rm.val.connect(cm1.out);
+        rm.count.connect(cm2.out);
+        rm.out.connect(dm.in);
+        */
+
+}
+