move Gadgets to MemoryUtils, discard a ton of obsolete junk
[fleet.git] / src / edu / berkeley / fleet / ir / Junk.java
1 package edu.berkeley.fleet.ir;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.*;
4 import java.net.*;
5 import edu.berkeley.fleet.two.*;
6 import edu.berkeley.fleet.api.*;
7 import edu.berkeley.fleet.fpga.*;
8 import edu.berkeley.fleet.api.Instruction.*;
9 import edu.berkeley.fleet.api.Instruction.Set;
10 import edu.berkeley.fleet.api.Instruction.Set.*;
11 import static edu.berkeley.fleet.api.Predicate.*;
12 import static edu.berkeley.fleet.util.BitManipulations.*;
13
14 public class Junk {
15
16     /*
17     public class RepeatModule extends Module {
18         private final Ship   alu;
19         public final InPort  count = new InPort("count") {
20                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
21                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
22                 public Context.LoopFactory build() {
23                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
24                     lf.abortLoopIfTorpedoPresent();
25                     lf.recvWord();
26                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
27                     lf.setPredicate(Predicate.FlagA);
28                     peer.sendToken(lf);
29                     lf.abortLoopIfTorpedoPresent();
30                     peer.recvWord(lf);
31                     lf.setPredicate(null);
32                     lf.abortLoopIfTorpedoPresent();
33                     lf.deliver();
34                     // RECV_AFTER_TORPEDO == 1
35                     return lf;
36                 }
37             };
38         public final InPort  val = new InPort("val") {
39                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
40                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
41                 public Context.LoopFactory build() {
42                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 1);
43
44                     peer.sendToken(lf);
45                     lf.literal(1);
46                     lf.deliver();
47                     lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO);
48
49                     lf = lf.makeNext(0);
50                     lf.setPredicate(Predicate.FlagA);
51                     lf.abortLoopIfTorpedoPresent();
52                     peer.recvWord(lf);
53                     lf.setPredicate(null);
54                     lf.abortLoopIfTorpedoPresent();
55                     lf.deliver();
56                     lf.literal(1);
57                     lf.abortLoopIfTorpedoPresent();
58                     lf.deliver();
59                     lf.recvWord();
60                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
61                     lf.setPredicate(Predicate.FlagA);
62                     peer.sendToken(lf);
63                     return lf;
64                 }
65             };
66         public final OutPort out = new OutPort("out") {
67                 public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
68                 public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
69                 public Context.LoopFactory build() {
70                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
71                     lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
72
73                     lf = lf.makeNext(0);
74                     lf.abortLoopIfTorpedoPresent();
75                     lf.collectWord();
76                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
77                     lf.setPredicate(Predicate.FlagA);
78                     lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
79                     lf.setPredicate(null);
80                     lf.setPredicate(Predicate.NotFlagA);
81                     lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
82                     lf.setPredicate(null);
83                     lf.abortLoopIfTorpedoPresent();
84                     lf.collectWord();
85                     lf.setPredicate(Predicate.FlagA);
86                     lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
87                     lf.setPredicate(null);
88                     lf.setPredicate(Predicate.NotFlagA);
89                     lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
90                     peer.recvToken(lf);
91                     peer.sendWord(lf);
92                     lf.setPredicate(null);
93                     return lf;
94                 }
95             };
96         public RepeatModule() {
97             this.alu = ctx.allocateShip("Alu");
98         }
99         public void build() {
100             super.build();
101             Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 0);
102             lf.literal(3); // SUB
103             lf.abortLoopIfTorpedoPresent();
104             lf.deliver();
105             lf.literal(1); // IN2
106             lf.abortLoopIfTorpedoPresent();
107             lf.deliver();
108         }
109     }
110     */
111
112     /*
113     public class PunctuatorModule extends Module {
114         private final Ship   alu;
115         private final long punctuator;
116         private final boolean unpunctuate;
117         public final InPort  count = new InPort("count") {
118                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
119                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
120                 public Context.LoopFactory build() {
121                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
122                     lf.recvWord();
123                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
124                     lf.setPredicate(Predicate.FlagA);
125                     peer.sendToken(lf);
126                     //lf.abortLoopIfTorpedoPresent(); FIXME
127                     peer.recvWord(lf);
128                     lf.setPredicate(null);
129                     lf.abortLoopIfTorpedoPresent();
130                     lf.deliver();
131                     lf.abortLoopIfTorpedoPresent();
132                     lf.deliver();
133                     return lf;
134                 }
135             };
136         public final InPort  val = new InPort("val") {
137                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
138                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
139                 public Context.LoopFactory build() {
140                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 0);
141                     lf.literal(0);
142                     lf.deliver();
143                     lf.literal(1);
144                     lf.deliver();
145                     lf.abortLoopIfTorpedoPresent();
146                     lf.recvWord();
147                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
148                     lf.setPredicate(Predicate.FlagA);
149                     peer.sendToken(lf);
150                     lf.abortLoopIfTorpedoPresent();
151                     peer.recvWord(lf);
152                     lf.setPredicate(null);
153                     lf.deliver();
154                     return lf;
155                 }
156             };
157         public final OutPort out = new OutPort("out") {
158                 public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
159                 public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
160                 public Context.LoopFactory build() {
161                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
162                     lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
163
164                     lf = lf.makeNext(0);
165
166                     lf.abortLoopIfTorpedoPresent();
167                     lf.collectWord();
168                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
169                     lf.abortLoopIfTorpedoPresent();
170                     lf.collectWord();
171
172                     if (unpunctuate) {
173                         lf.setPredicate(Predicate.FlagA);
174                         lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
175                         lf.setPredicate(Predicate.NotFlagA);
176                         lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
177                         lf.setPredicate(null);
178                         lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
179                     } else {
180                         // if count==0, issue punctuation and tell in1 to get a new count
181                         lf.setPredicate(Predicate.FlagA);
182                         lf.literal(punctuator);
183                         lf.abortLoopIfTorpedoPresent();
184                         peer.recvToken(lf);
185                         peer.sendWord(lf);
186                         lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
187                         lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
188                         lf.setPredicate(null);
189
190                         // if count!=0, cycle around the decremented count and pass a value through
191                         lf.setPredicate(Predicate.NotFlagA);
192                         lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
193                         lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
194                         lf.setPredicate(null);
195                     }
196
197                     lf.abortLoopIfTorpedoPresent();
198                     lf.collectWord();
199
200                     if (unpunctuate) {
201                         lf.setPredicate(Predicate.NotFlagA);
202                         lf.abortLoopIfTorpedoPresent();
203                         peer.recvToken(lf);
204                         peer.sendWord(lf);
205                         lf.setPredicate(null);
206                     } else {
207                         lf.setPredicate(Predicate.NotFlagA);
208                         lf.abortLoopIfTorpedoPresent();
209                         peer.recvToken(lf);
210                         peer.sendWord(lf);
211                         lf.setPredicate(null);
212                     }
213
214                     return lf;
215                 }
216             };
217         public PunctuatorModule(long punctuator, boolean unpunctuate) {
218             this.alu = ctx.allocateShip("Alu");
219             this.punctuator = punctuator;
220             this.unpunctuate = unpunctuate;
221         }
222         public void build() {
223             super.build();
224             Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 0);
225             lf.literal(6); // CMP
226             lf.abortLoopIfTorpedoPresent();
227             lf.deliver();
228             lf.literal(3); // SUB
229             lf.abortLoopIfTorpedoPresent();
230             lf.deliver();
231             lf.literal(1); // IN2
232             lf.abortLoopIfTorpedoPresent();
233             lf.deliver();
234         }
235     }
236 */
237     /*
238     public class DownCounterModule extends Module {
239         private final int internal_inflight;
240         private final boolean incr_is_already_negative;
241         private final Ship   alu;
242         public final InPort  start = new InPort("start") {
243                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
244                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0)); }
245                 public Context.LoopFactory build() {
246                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 0);
247                     lf.recvWord();
248                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
249                     lf.setPredicate(Predicate.FlagA);
250                     peer.sendToken(lf);
251                     lf.abortLoopIfTorpedoPresent();
252                     peer.recvWord(lf);
253                     lf.setPredicate(null);
254                     lf.deliver();
255                     return lf;
256                 }
257             };
258         public final InPort  incr = new InPort("incr") {
259                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
260                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0)); }
261                 public Context.LoopFactory build() {
262                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 0);
263                     lf.recvToken();
264                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
265                     lf.setPredicate(Predicate.FlagA);
266                     peer.sendToken(lf);
267                     lf.abortLoopIfTorpedoPresent();
268                     peer.recvWord(lf);
269                     lf.deliver();
270                     lf.setPredicate(Predicate.NotFlagA);
271                     lf.abortLoopIfTorpedoPresent();
272                     lf.deliver();
273                     return lf;
274                 }
275             };
276         public final OutPort out = new OutPort("out") {
277                 public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
278                 public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
279                 public Context.LoopFactory build() {
280                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
281                     lf.setFlags(FlagFunction.ONE, FlagFunction.ZERO);
282
283                     lf = lf.makeNext(0);
284                     lf.setPredicate(Predicate.FlagA);                    
285                     lf.sendToken(alu.getDock("in1").getDataDestination(), new BitVector(1).set(1));
286                     lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(1));
287                     lf.setPredicate(Predicate.NotFlagA);
288                     lf.sendWord(alu.getDock("in1").getDataDestination(), new BitVector(1).set(0));
289                     lf.sendToken(alu.getDock("in2").getDataDestination(), new BitVector(1).set(0));
290                     lf.setPredicate(null);
291                     lf.abortLoopIfTorpedoPresent();
292                     lf.collectWord();
293                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
294                     lf.setPredicate(Predicate.NotFlagA);
295                     lf.abortLoopIfTorpedoPresent();
296                     peer.recvToken(lf);
297                     peer.sendWord(lf);
298                     lf.setPredicate(null);
299                     return lf;
300                 }
301             };
302         public DownCounterModule(boolean incr_is_already_negative, int internal_inflight) {
303             this.alu = ctx.allocateShip("Alu");
304             this.incr_is_already_negative = incr_is_already_negative;
305             this.internal_inflight = internal_inflight;
306             if (internal_inflight != 0) throw new RuntimeException("not yet supported");
307         }
308         public void build() {
309             super.build();
310             Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 1);
311             // Phase 2 (FIXME: tokens for flow control here)
312             lf.literal(incr_is_already_negative
313                        ? 2 // ADD
314                        : 3 // SUB
315                        );
316             lf = lf.makeNext(0);
317             lf.abortLoopIfTorpedoPresent();
318             lf.deliver();
319         }
320     }
321     */
322     /**
323      *  A "vector register" which is initialized holding zero or more
324      *  elements and which will expect to receive count-many elements
325      *  before going idle
326      */
327     /*
328 DOES NOT HANDLE TORPEDOES
329     public class VectorRegisterModule extends Module {
330         public final InPort  in;
331         public final OutPort out;
332         public final int count;
333         private final Ship ship;
334         public VectorRegisterModule(final long[] initialValues, final int count) {
335             this(longsToBitVectors(initialValues), count);
336         }
337         public VectorRegisterModule(final BitVector[] initialValues, final int count) {
338             this.count = count;
339             ship = ctx.allocateShip("Fifo");
340             in  = new PlainInPort("in",  ship.getDock("in"), count) {
341                     public Context.LoopFactory build(Context.LoopFactory lf) {
342                         lf.disableInstructionFifoOverflowCheck();
343                         for(int i=0; i<initialValues.length; i++) {
344                             lf.literal(initialValues[i]);
345                             lf.deliver();
346                         }
347                         return super.build(lf.makeNext(1));
348                     }
349                 };
350             out = new PlainOutPort("out", ship.getDock("out"), initialValues.length);
351         }
352     }
353     */
354
355 /*
356     public class DownCounterModule extends Module {
357         private final int internal_inflight;
358         private final boolean incr_is_already_negative;
359         private final Ship   alu;
360         public final InPort  incr = new InPort("incr") {
361                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
362                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in1").getDataDestination()); }
363                 public Context.LoopFactory build() {
364                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in1"), 1);
365
366                     // Phase 1
367                     peer.sendToken(lf);
368                     peer.recvWord(lf);
369                     for(int i=0; i<internal_inflight;   i++) lf.deliver();
370
371                     // Phase 2
372                     for(int i=0; i<internal_inflight*2; i++) {
373                         lf.recvWord();
374                         lf.deliver();
375                     }
376
377                     // Phase 3 (FIXME: tokens for flow control here)
378                     lf = lf.makeNext(0);
379                     lf.recvWord();
380                     lf.deliver();
381
382                     return lf;
383                 }
384             };
385         public final InPort  start = new InPort("start") {
386                 public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
387                 public void sendWord(Context.LoopFactory lf) { lf.sendWord(alu.getDock("in2").getDataDestination()); }
388                 public Context.LoopFactory build() {
389                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("in2"), 1);
390
391                     // Phase 1
392                     peer.sendToken(lf);
393                     peer.recvWord(lf);
394                     for(int i=0; i<internal_inflight; i++) lf.deliver();
395
396                     // Phase 2
397                     lf.literal(0);
398                     for(int i=0; i<internal_inflight; i++) {
399                         lf.deliver();
400                         lf.deliver();
401                         lf.recvWord();
402                     }
403
404                     // Phase 3 (FIXME: tokens for flow control here)
405                     lf = lf.makeNext(0);
406                     lf.deliver();
407
408                     return lf;
409                 }
410             };
411         public final OutPort out = new OutPort("out") {
412                 public void sendToken(Context.LoopFactory lf) { lf.sendToken(alu.getDock("out").getDataDestination()); }
413                 public void recvWord(Context.LoopFactory lf) { lf.recvWord(); }
414                 public Context.LoopFactory build() {
415                     Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("out"), 1);
416
417                     // Phase 1
418                     for(int i=0; i<internal_inflight*2; i++) {
419                         lf.collectWord();
420                         lf.sendWord(alu.getDock("in1").getDataDestination());
421                     }
422
423                     // Phase 2
424                     for(int i=0; i<internal_inflight; i++) {
425                         lf.collectWord();
426                         lf.sendWord(alu.getDock("in2").getDataDestination());
427
428                         lf.collectWord();
429
430                         //lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
431                         //lf.setPredicate(Predicate.FlagA);
432                         //lf.abort();
433                         //lf.setPredicate(null);
434
435                         lf.sendWord(alu.getDock("in1").getDataDestination());
436                         peer.recvToken(lf);
437                         peer.sendWord(lf);
438                     }
439
440                     // Phase 3
441                     lf = lf.makeNext(0);
442
443                     lf.collectWord();
444                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
445                     lf.setPredicate(Predicate.FlagA);
446                     lf.abort();
447                     lf.setPredicate(null);
448                     lf.sendWord(alu.getDock("in1").getDataDestination());
449                     peer.recvToken(lf);
450                     peer.sendWord(lf);
451
452                     return lf;
453                 }
454             };
455         public DownCounterModule(boolean incr_is_already_negative, int internal_inflight) {
456             this.alu = ctx.allocateShip("Alu");
457             this.incr_is_already_negative = incr_is_already_negative;
458             this.internal_inflight = internal_inflight;
459             if (!incr_is_already_negative) throw new RuntimeException("this is broken right now");
460         }
461         public void build() {
462             super.build();
463
464             Context.LoopFactory lf = ctx.new LoopFactory(alu.getDock("inOp"), 1);
465
466             // Phase 1
467             for(int i=0; i<internal_inflight;   i++) {
468                 lf.literal(0);  // IN1
469                 lf.deliver();
470                 lf.literal(1);  // IN2
471                 lf.deliver();
472             }
473
474             // Phase 2 (FIXME: tokens for flow control here)
475             lf.literal(incr_is_already_negative
476                        ? 2 // ADD
477                        : 3 // SUB
478                        );
479             lf = lf.makeNext(0);
480             lf.deliver();
481         }
482     }
483  */
484
485         /*
486         VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 12, 14, 18, 15 }, 0);
487         VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 13, 17, 1,  3  }, 0);
488         VectorRegisterModule cm3 = proc.new VectorRegisterModule(new long[] { 2,  2,  2,  2  }, 0);
489
490         AluModule alu = proc.new AluModule();
491
492         ForeverModule fmm = proc.new ForeverModule(2);
493         fmm.in.connect(cm2.out);
494         fmm.out.connect(alu.in2);
495         fmm.out_const.connect(alu.inOp);
496         alu.in1.connect(cm1.out);
497         alu.out.connect(dm.in);
498         */
499
500         /*
501         VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 43, 10, 5, 5, 5 }, 0);
502         VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 2,   1, 1, 2, 3 }, 0);
503         DownCounterModule dcm = proc.new DownCounterModule(false, 0);
504         dcm.start.connect(cm1.out);
505         dcm.incr.connect(cm2.out);
506         dcm.out.connect(dm.in);
507         */
508
509         /*        
510         VectorRegisterModule cm1 = proc.new VectorRegisterModule(new long[] { 43, 10, 5, 8, 5 }, 0);
511         VectorRegisterModule cm2 = proc.new VectorRegisterModule(new long[] { 7,   1, 1, 2, 3 }, 0);
512         RepeatModule rm = proc.new RepeatModule();
513         rm.val.connect(cm1.out);
514         rm.count.connect(cm2.out);
515         rm.out.connect(dm.in);
516         */
517
518 }
519