add commented-out version of UnPunctuatorNode2 to UnPunctuatorNode until I have time...
[fleet.git] / src / edu / berkeley / fleet / dataflow / UnPunctuatorNode.java
1 package edu.berkeley.fleet.dataflow;
2 import java.util.*;
3 import edu.berkeley.fleet.loops.*;
4 import edu.berkeley.fleet.api.*;
5 import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
6
7 /**
8  *  Takes an item from the "count" port, then passes/drops through count-1
9  *  items from "val" to "out" and drops/passes the count-th item.
10  */
11 public class UnPunctuatorNode extends Node {
12     private final Ship    ship  = dfg.pool.allocateShip("Counter");
13     private final InPort  op    = new DockInPort("op",    ship.getDock("inOp"), "PASS_C2_V1");
14     public  final InPort  val   = new DockInPort("val",   ship.getDock("in1"));
15     public  final InPort  count = new DockInPort("count", ship.getDock("in2"));
16     public  final OutPort out   = new DockOutPort("out", ship.getDock("out")) {
17             protected void build(CodeBag ctx, LoopFactory lf) {
18                 lf = lf.makeNext(0);
19                 lf.abortLoopIfTorpedoPresent();
20                 lf.collectWord();
21                 lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagC), FlagFunction.ZERO);
22                 lf.setPredicate(passPunctuationInsteadOfValues ? Predicate.FlagA : Predicate.NotFlagA);
23                 lf.abortLoopIfTorpedoPresent();
24                 peer.recvToken(lf);
25                 peer.sendWord(lf);
26                 lf.setPredicate(null);
27             }
28         };
29
30     private boolean passPunctuationInsteadOfValues;
31     public UnPunctuatorNode(DataFlowGraph dfg) { this(dfg, false); }
32     public UnPunctuatorNode(DataFlowGraph dfg, boolean passPunctuationInsteadOfValues) {
33         super(dfg);
34         this.passPunctuationInsteadOfValues = passPunctuationInsteadOfValues;
35     }
36
37     /*
38     private final Ship    ship  = dfg.pool.allocateShip("Alu");
39     public  final InPort  val   = new DockInPort("val",   ship.getDock("in1"));
40     public  final OutPort out   = new DockOutPort("out", ship.getDock("out"));
41     public  final InPort  val2  = new DockInPort("val2",  ship.getDock("in2"));
42     public  final InPort  count = new DockInPort("count", ship.getDock("inOp")) {
43             protected void _build(LoopFactory lf) {
44
45                 // FlagA       set means we have a count value ready to use
46                 // FlagB+FlagD means a torpedo has struck
47                 //    therefore, every recieve     is folowed by if-flag-d-then-set-FlagB
48                 //    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
49                 lf = lf.makeNext(1);
50                 lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO);
51                 lf = lf.makeNext(0);
52
53                 // new iteration or decrement
54                 lf.setPredicate(Predicate.NotFlagA);
55                 sendTokenAndRecvWord(lf);
56                 lf.setPredicate(Predicate.FlagD);
57                 lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagA), FlagFunction.ONE);
58                 lf.setPredicate(Predicate.FlagA);
59                 lf.decOlc();
60                 lf.setPredicate(Predicate.NotFlagA);
61                 lf.setOlcFromWord();
62                 lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO.add(Predicate.FlagB));
63                 lf.setPredicate(Predicate.FlagD);
64                 lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO.add(Predicate.FlagB));
65                 lf.setOlc(1);
66                 lf.setPredicate(Predicate.FlagB);
67                 lf.setOlc(0);
68                 lf.setPredicate(null);
69
70                 lf.setPredicate(Predicate.NotFlagA);
71                 lf.literal(passPunctuationInsteadOfValues ? "IN1"   : "DROP1");
72                 lf.setPredicate(Predicate.FlagA);
73                 lf.literal(passPunctuationInsteadOfValues ? "DROP1" : "IN1");
74                 lf.setPredicate(null);
75                 lf.abortLoopIfTorpedoPresent();
76                 lf.deliver();
77                 lf.setPredicate(Predicate.FlagD);
78                 lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagA), FlagFunction.ONE);
79                 lf.setPredicate(null);
80
81             }
82         };
83     private boolean passPunctuationInsteadOfValues;
84     public UnPunctuatorNode2(DataFlowGraph dfg) { this(dfg, false); }
85     public UnPunctuatorNode2(DataFlowGraph dfg, boolean passPunctuationInsteadOfValues) {
86         super(dfg);
87         this.passPunctuationInsteadOfValues = passPunctuationInsteadOfValues;
88     }
89
90     public static void main(String[] s) throws Exception {
91         Fleet fleet = new Fpga();
92         ShipPool pool = new ShipPool(fleet);
93         DataFlowGraph dfg = new DataFlowGraph(fleet, pool);
94         DebugNode dn = new DebugNode(dfg);
95         DownCounterNode fn = new DownCounterNode(dfg);
96         fn.start.connect(new OnceNode(dfg, 100).out);
97         fn.incr.connect(new OnceNode(dfg, 1).out);
98         UnPunctuatorNode2 up = new UnPunctuatorNode2(dfg);
99         up.val.connect(fn.out);
100         up.count.connect(new ForeverNode(dfg, 12).out);
101         up.out.connect(dn.in);
102         CodeBag cb = new CodeBag(fleet);
103         dfg.build(cb);
104         FleetProcess fp = fleet.run(new Instruction[0]);
105         cb.dispatch(fp);
106         fp.flush();
107         while(true) {
108             System.out.println("word = " + fp.recvWord().toLong());
109         }
110     }
111
112       
113      */
114
115 }