add commented-out version of UnPunctuatorNode2 to UnPunctuatorNode until I have time...
[fleet.git] / src / edu / berkeley / fleet / dataflow / UnPunctuatorNode.java
index 3c72907..627672d 100644 (file)
 package edu.berkeley.fleet.dataflow;
+import java.util.*;
 import edu.berkeley.fleet.loops.*;
 import edu.berkeley.fleet.api.*;
+import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
 
+/**
+ *  Takes an item from the "count" port, then passes/drops through count-1
+ *  items from "val" to "out" and drops/passes the count-th item.
+ */
 public class UnPunctuatorNode extends Node {
     private final Ship    ship  = dfg.pool.allocateShip("Counter");
+    private final InPort  op    = new DockInPort("op",    ship.getDock("inOp"), "PASS_C2_V1");
+    public  final InPort  val   = new DockInPort("val",   ship.getDock("in1"));
+    public  final InPort  count = new DockInPort("count", ship.getDock("in2"));
+    public  final OutPort out   = new DockOutPort("out", ship.getDock("out")) {
+            protected void build(CodeBag ctx, LoopFactory lf) {
+                lf = lf.makeNext(0);
+                lf.abortLoopIfTorpedoPresent();
+                lf.collectWord();
+                lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagC), FlagFunction.ZERO);
+                lf.setPredicate(passPunctuationInsteadOfValues ? Predicate.FlagA : Predicate.NotFlagA);
+                lf.abortLoopIfTorpedoPresent();
+                peer.recvToken(lf);
+                peer.sendWord(lf);
+                lf.setPredicate(null);
+            }
+        };
+
+    private boolean passPunctuationInsteadOfValues;
+    public UnPunctuatorNode(DataFlowGraph dfg) { this(dfg, false); }
+    public UnPunctuatorNode(DataFlowGraph dfg, boolean passPunctuationInsteadOfValues) {
+        super(dfg);
+        this.passPunctuationInsteadOfValues = passPunctuationInsteadOfValues;
+    }
+
+    /*
+    private final Ship    ship  = dfg.pool.allocateShip("Alu");
+    public  final InPort  val   = new DockInPort("val",   ship.getDock("in1"));
     public  final OutPort out   = new DockOutPort("out", ship.getDock("out"));
-    public  final InPort  val   = new DockInPort("in1",  ship.getDock("in1"));
-    public  final InPort  count = new DockInPort("in2",  ship.getDock("in2"), 0, new BitVector[] { null, bv(1) });
-    public  final InPort  op    = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
-            ship.getDock("inOp").getConstant("PASS_C2_V1"),
-            ship.getDock("inOp").getConstant("DROP_C2_V1") } );
-    public UnPunctuatorNode(DataFlowGraph dfg) { super(dfg); }
+    public  final InPort  val2  = new DockInPort("val2",  ship.getDock("in2"));
+    public  final InPort  count = new DockInPort("count", ship.getDock("inOp")) {
+            protected void _build(LoopFactory lf) {
+
+                // FlagA       set means we have a count value ready to use
+                // FlagB+FlagD means a torpedo has struck
+                //    therefore, every recieve     is folowed by if-flag-d-then-set-FlagB
+                //    therefore, every set/dec olc is folowed by if-flag-d-then-copy-NotFlagB-to-FlagA ; if-flag-d-then-clear-FlagD ; if-FlagB-then-set-FlagD
+                lf = lf.makeNext(1);
+                lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO);
+                lf = lf.makeNext(0);
+
+                // new iteration or decrement
+                lf.setPredicate(Predicate.NotFlagA);
+                sendTokenAndRecvWord(lf);
+                lf.setPredicate(Predicate.FlagD);
+                lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagA), FlagFunction.ONE);
+                lf.setPredicate(Predicate.FlagA);
+                lf.decOlc();
+                lf.setPredicate(Predicate.NotFlagA);
+                lf.setOlcFromWord();
+                lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO.add(Predicate.FlagB));
+                lf.setPredicate(Predicate.FlagD);
+                lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO.add(Predicate.FlagB));
+                lf.setOlc(1);
+                lf.setPredicate(Predicate.FlagB);
+                lf.setOlc(0);
+                lf.setPredicate(null);
+
+                lf.setPredicate(Predicate.NotFlagA);
+                lf.literal(passPunctuationInsteadOfValues ? "IN1"   : "DROP1");
+                lf.setPredicate(Predicate.FlagA);
+                lf.literal(passPunctuationInsteadOfValues ? "DROP1" : "IN1");
+                lf.setPredicate(null);
+                lf.abortLoopIfTorpedoPresent();
+                lf.deliver();
+                lf.setPredicate(Predicate.FlagD);
+                lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagA), FlagFunction.ONE);
+                lf.setPredicate(null);
+
+            }
+        };
+    private boolean passPunctuationInsteadOfValues;
+    public UnPunctuatorNode2(DataFlowGraph dfg) { this(dfg, false); }
+    public UnPunctuatorNode2(DataFlowGraph dfg, boolean passPunctuationInsteadOfValues) {
+        super(dfg);
+        this.passPunctuationInsteadOfValues = passPunctuationInsteadOfValues;
+    }
+
+    public static void main(String[] s) throws Exception {
+        Fleet fleet = new Fpga();
+        ShipPool pool = new ShipPool(fleet);
+        DataFlowGraph dfg = new DataFlowGraph(fleet, pool);
+        DebugNode dn = new DebugNode(dfg);
+        DownCounterNode fn = new DownCounterNode(dfg);
+        fn.start.connect(new OnceNode(dfg, 100).out);
+        fn.incr.connect(new OnceNode(dfg, 1).out);
+        UnPunctuatorNode2 up = new UnPunctuatorNode2(dfg);
+        up.val.connect(fn.out);
+        up.count.connect(new ForeverNode(dfg, 12).out);
+        up.out.connect(dn.in);
+        CodeBag cb = new CodeBag(fleet);
+        dfg.build(cb);
+        FleetProcess fp = fleet.run(new Instruction[0]);
+        cb.dispatch(fp);
+        fp.flush();
+        while(true) {
+            System.out.println("word = " + fp.recvWord().toLong());
+        }
+    }
+
+      
+     */
+
 }