1 package edu.berkeley.fleet.ir;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.concurrent.Semaphore;
6 import edu.berkeley.fleet.two.*;
7 import edu.berkeley.fleet.api.*;
8 import edu.berkeley.fleet.fpga.*;
9 import edu.berkeley.fleet.api.Instruction.*;
10 import edu.berkeley.fleet.api.Instruction.Set;
11 import edu.berkeley.fleet.api.Instruction.Set.*;
12 import static edu.berkeley.fleet.api.Predicate.*;
13 import static edu.berkeley.fleet.util.BitManipulations.*;
17 - refactor the cleanup into the subclasses of Port (phase1, phase2, etc)
20 // does peer.recvWord() have to honor the currently-set predicate?
22 // public class ReplaceModule extends Module { }
23 // public class CountMergeModule extends Module { }
24 // public class SortMergeModule extends Module { }
25 // public class FanOutModule extends Module { }
26 // public class MemoryModule extends Module { }
27 // public class DoneModule extends Module { }
29 public class Process {
32 public static int reset_count = 0;
33 public static HashSet<Dock> torpedoes = new HashSet<Dock>();
35 public final Fleet fleet;
36 public final ShipPool pool;
38 public Process(Fleet fleet) {
40 this.pool = new ShipPool(fleet);
43 private HashSet<Module> modules = new HashSet<Module>();
45 public void build(Context ctx) {
46 for(Module mod : modules)
49 public void reset(Context ctx, int phase, Destination ackDestination) {
52 for(Module mod : modules)
53 mod.reset(ctx, phase, ackDestination);
58 void doReset(Context ctx, int phase, Dock dock, Port peer, Destination ackDestination, boolean peerUsed) {
59 if (dock.getShip().getType().equals("Debug")) return;
63 // Phase 0: torpedo every output dock, put it in
64 // collecting mode. Cannot combine with phase 1,
65 // because until output docks are in vacuum mode we
66 // cannot be sure that the tokens to the input docks
67 // will eventually succeed. This may cause the
68 // instructions sent after the tokens to back up into
71 if (!dock.isInputDock()) {
73 LoopFactory lf = new LoopFactory(ctx, dock, 1);
74 lf.sendToken(ackDestination);
76 lf.abortLoopIfTorpedoPresent();
83 // Phase 1: torpedo every input dock, put it in loopback mode
85 if (dock.isInputDock()) {
87 LoopFactory lf = new LoopFactory(ctx, dock, 1);
88 lf.sendToken(ackDestination);
90 // FIXME: this won't work right for ports that
91 // get "shared" by two senders (for example,
94 if (peerUsed && peer!=null) {
96 lf.abortLoopIfTorpedoPresent();
97 ((OutPort)peer).recvWord(lf);
98 ((OutPort)peer).sendToken(lf);
105 // Phase 2: torpedo every output dock, have it absorb tokens
107 if (!dock.isInputDock()) {
109 LoopFactory lf = new LoopFactory(ctx, dock, 1);
111 for(int i=0; i<((InPort)peer).getTokensToAbsorb(); i++)
113 lf.sendToken(ackDestination);
119 // Phase 3: torpedo every input dock, and we're done
121 if (dock.isInputDock()) {
122 if (peerUsed && peer!=null) {
125 LoopFactory lf = new LoopFactory(ctx, dock, 1);
126 lf.sendToken(ackDestination);
137 Process.this.modules.add(this);
140 private HashMap<String,Port> ports = new HashMap<String,Port>();
142 public InPort getInPort(String name) { return (InPort)ports.get(name); }
143 public OutPort getOutPort(String name) { return (OutPort)ports.get(name); }
145 public void build(Context ctx) { for(Port p : ports.values()) p.build(ctx); }
146 public void reset(Context ctx, int phase, Destination ackDestination) {
147 for(Port p : ports.values()) p.reset(ctx, phase, ackDestination);
150 public abstract class Port {
151 public final String name;
152 public Port(String name) {
154 if (Module.this.ports.get(name)!=null) throw new RuntimeException();
155 Module.this.ports.put(name,this);
157 public abstract void build(Context ctx);
158 public abstract void reset(Context ctx, int phase, Destination ackDestination);
161 public abstract class InPort extends Port {
163 public InPort(String name) { super(name); }
164 public void connect(OutPort peer) {
168 public void setPeer(OutPort peer) {
169 if (this.peer!=null) throw new RuntimeException("cannot call setPeer() twice");
173 /** this port's peer (an OutPort) invokes this to have "recvToken" or equivalent inserted */
174 public abstract void recvToken(LoopFactory loopfactory_at_output_dock);
175 /** this port's peer (an OutPort) invokes this to have "sendWord" or equivalent inserted */
176 public abstract void sendWord(LoopFactory loopfactory_at_output_dock);
178 public int getTokensToAbsorb() { return 0; }
181 public abstract class OutPort extends Port {
183 public OutPort(String name) { super(name); }
184 public void connect(InPort peer) {
188 public void setPeer(InPort peer) {
189 if (this.peer!=null) throw new RuntimeException("cannot call setPeer() twice");
193 /** this port's peer (an InPort) invokes this to have "sendToken" or equivalent inserted */
194 public abstract void sendToken(LoopFactory loopfactory_at_input_dock);
195 /** this port's peer (an InPort) invokes this to have "recvWord" or equivalent inserted */
196 public abstract void recvWord(LoopFactory loopfactory_at_input_dock);
199 public final class DockInPort extends InPort {
203 public DockInPort(String name, Dock dock) { this(name, dock, 0); }
204 public DockInPort(String name, Dock dock, int count) { this(name, dock, count, new BitVector[] { null }); }
205 public DockInPort(String name, Dock dock, int count, BitVector[] pattern) {
209 this.pattern = pattern;
211 public void recvToken(LoopFactory lf) { lf.recvToken(); }
212 public void sendWord(LoopFactory lf) { lf.sendWord(dock.getDataDestination()); }
213 public void build(Context ctx) { build(ctx, new LoopFactory(ctx, dock, 1)); }
214 // number-in-flight is considered a property of the input dock in a pair
215 public int getInflight() { return 4; }
216 //public int getInflight() { return 1; }
217 public int getTokensToAbsorb() { return getInflight(); }
218 private boolean peerUsed() {
219 if (peer==null) return false;
220 for(int i=0; i<pattern.length; i++) if (pattern[i]==null) return true;
223 public void reset(Context ctx, int phase, Destination ackDestination) {
224 doReset(ctx, phase, dock, peer, ackDestination, peerUsed());
226 protected void build(Context ctx, LoopFactory lf) {
227 int inflight = (count != 0 && count < getInflight()) ? count : getInflight();
230 for(int i=0; i<inflight; i++) peer.sendToken(lf);
232 lf = lf.makeNext(count);
233 for(int i=0; i<pattern.length; i++) {
234 if (pattern[i]==null) {
236 lf.abortLoopIfTorpedoPresent();
241 lf.interruptibleNop();
244 lf.literal(pattern[i]);
245 lf.abortLoopIfTorpedoPresent();
251 // "torpedoable nop" to keep the dock in a receptive state
252 lf.abortLoopIfTorpedoPresent();
258 public /*final*/ class DockOutPort extends OutPort {
259 public final Dock dock;
260 public final int count;
261 public DockOutPort(String name, Dock dock) { this(name, dock, 0); }
262 public DockOutPort(String name, Dock dock, int count) { super(name); this.dock = dock; this.count = count; }
263 public void sendToken(LoopFactory lf) { lf.sendToken(dock.getDataDestination()); }
264 public void recvWord(LoopFactory lf) { lf.recvWord(); }
265 public void build(Context ctx) { build(ctx, new LoopFactory(ctx, dock, 1)); }
266 protected void build(Context ctx, LoopFactory lf) {
267 if (peer==null) return;
268 lf = lf.makeNext(count);
269 lf.abortLoopIfTorpedoPresent();
274 public void reset(Context ctx, int phase, Destination ackDestination) {
275 doReset(ctx, phase, dock, peer, ackDestination, true);
280 private static BitVector bv(long l) { return new BitVector(/*FIXME fleet.getWordWidth()*/37).set(l); }
281 private static BitVector[] bv(long[] l) {
282 BitVector[] ret = new BitVector[l.length];
283 for(int i=0; i<ret.length; i++) ret[i] = bv(l[i]);
288 * Deliver the constant at out forever. Flow control in<->out is
289 * maintained, but out is not flow-controlled, so be sure
290 * that every datum sent there is consumed synchronously wiht
291 * data items sent to out.
293 public class ForeverModule extends Module {
294 private BitVector bv;
295 public final OutPort out = new OutPort("out") {
296 public void sendToken(LoopFactory lf) { }
297 public void recvWord(LoopFactory lf) { }
298 public void build(Context ctx) { }
299 public void reset(Context ctx, int phase, Destination ackDestination) { }
300 public void setPeer(InPort peer) {
302 DockInPort pip = ((DockInPort)peer);
303 for(int i=0; i<pip.pattern.length; i++) {
304 if (pip.pattern[i]==null)
309 public ForeverModule(long l) { this(new BitVector(fleet.getWordWidth()).set(l)); }
310 public ForeverModule(final BitVector bv) { this.bv = bv; }
313 public class OnceModule extends Module {
314 private BitVector bv;
315 public final OutPort out = new OutPort("out") {
316 public void sendToken(LoopFactory lf) { }
317 public void recvWord(LoopFactory lf) { }
318 public void build(Context ctx) { }
319 public void reset(Context ctx, int phase, Destination ackDestination) { }
320 public void setPeer(InPort peer) {
322 DockInPort pip = ((DockInPort)peer);
323 BitVector[] pip_pattern = pip.pattern;
324 BitVector[] temp = new BitVector[pip_pattern.length * 2];
327 boolean done = false;
328 // FIXME: if peer.count is already 1, this gets simpler and different
329 for(i=0; i<temp.length; i++) {
330 if (pip_pattern[j] != null) {
331 temp[i] = pip_pattern[j];
338 if (j >= pip_pattern.length) j = 0;
340 pip.pattern = new BitVector[i];
341 System.arraycopy(temp, 0, pip.pattern, 0, i);
345 public OnceModule(long l) { this(new BitVector(fleet.getWordWidth()).set(l)); }
346 public OnceModule(final BitVector bv) { this.bv = bv; }
349 public class DebugModule extends Module {
350 public final Ship ship = pool.allocateShip("Debug");
351 public final InPort in = new DockInPort("in", ship.getDock("in"));
352 // NOTE: shutdown needs to treat this specially
353 public DebugModule() { }
356 public class UnPunctuatorModule extends Module {
357 private final Ship ship = pool.allocateShip("Counter");
358 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
359 public final InPort val = new DockInPort("in1", ship.getDock("in1"));
360 public final InPort count = new DockInPort("in2", ship.getDock("in2"), 0, new BitVector[] { null, bv(1) });
361 public final InPort op = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] { bv(6 /*PASS_C2_V1*/), bv(10 /*DROP_C2_V1*/) } );
362 public UnPunctuatorModule() { }
365 public class PunctuatorModule extends Module {
366 private final long punc;
367 private final Ship ship = pool.allocateShip("Counter");
368 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
369 public final InPort val = new DockInPort("in1", ship.getDock("in1"));
370 public final InPort op = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] { bv(6 /*PASS_C2_V1*/), bv(7 /*PASS_C2_V2*/) } );
371 public final InPort count;
372 public PunctuatorModule(long punc) {
374 this.count = new DockInPort("in2", ship.getDock("in2"), 0, new BitVector[] { null, bv(1), bv(punc) });
378 public class AluModule extends Module {
379 public final Ship ship = pool.allocateShip("Alu");
380 public final InPort in1 = new DockInPort("in1", ship.getDock("in1"));
381 public final InPort in2 = new DockInPort("in2", ship.getDock("in2"));
382 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"));
383 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
384 public AluModule() { }
387 public class DownCounterModule extends Module {
388 public final Ship ship = pool.allocateShip("Counter");
389 public final InPort start = new DockInPort("in1", ship.getDock("in1"));
390 public final InPort incr = new DockInPort("in2", ship.getDock("in2"));
391 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] { bv(12 /*COUNTDOWN*/) });
392 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
393 public DownCounterModule() { }
396 public class RepeatModule extends Module {
397 public final Ship ship = pool.allocateShip("Counter");
398 public final InPort count = new DockInPort("in1", ship.getDock("in1"));
399 public final InPort val = new DockInPort("in2", ship.getDock("in2"));
400 public final InPort inOP = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] { bv(1 /*REPEAT_C1_V2*/) });
401 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
402 public RepeatModule() { }
405 public class SortedMergeModule extends Module {
406 public final Ship ship = pool.allocateShip("Alu");
407 public final InPort in1 = new DockInPort("in1", ship.getDock("in1"));
408 public final InPort in2 = new DockInPort("in2", ship.getDock("in2"));
409 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] { bv(9 /*MAXMERGE*/) });
410 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
411 public SortedMergeModule() { }
414 public class MemoryModule extends Module {
415 public final Ship ship;
416 public final InPort inCBD;
417 public final InPort inAddrRead1;
418 public final InPort inAddrRead2;
419 public final InPort inAddrWrite;
420 public final InPort inDataWrite;
421 public final OutPort outRead1;
422 public final OutPort outRead2;
423 public final OutPort outWrite;
424 public MemoryModule(Ship memoryShip) {
425 this.ship = memoryShip;
426 this.inCBD = ship.getType().equals("Memory") ? new DockInPort("inCBD", ship.getDock("inCBD")) : null;
427 this.inAddrWrite = new DockInPort("inAddrWrite", ship.getDock("inAddrWrite"));
428 this.inDataWrite = new DockInPort("inDataWrite", ship.getDock("inDataWrite"));
429 this.inAddrRead1 = new InPort("inAddrRead1") {
430 public void recvToken(LoopFactory lf) { lf.recvToken(); }
431 public void sendWord(LoopFactory lf) { lf.sendWord(ship.getDock("inAddrRead").getDataDestination(), new BitVector(1).set(0)); }
432 public void build(Context ctx) { }
433 public int getTokensToAbsorb() { return outRead1.peer.getTokensToAbsorb(); }
434 public void reset(Context ctx, int phase, Destination ackDestination) {
435 doReset(ctx, phase, ship.getDock("inAddrRead"), null, ackDestination, false);
438 this.inAddrRead2 = new InPort("inAddrRead2") {
439 public void recvToken(LoopFactory lf) { lf.recvToken(); }
440 public void sendWord(LoopFactory lf) { lf.sendWord(ship.getDock("inAddrRead").getDataDestination(), new BitVector(1).set(1)); }
441 public void build(Context ctx) { }
442 public int getTokensToAbsorb() { return outRead2.peer.getTokensToAbsorb(); }
443 public void reset(Context ctx, int phase, Destination ackDestination) { }
445 this.outRead1 = new OutPort("outRead1") {
446 public void sendToken(LoopFactory lf) { inAddrRead1.peer.sendToken(lf); }
447 public void recvWord(LoopFactory lf) { lf.recvWord(); }
448 public void build(Context ctx) { }
449 public void reset(Context ctx, int phase, Destination ackDestination) { }
451 this.outRead2 = new OutPort("outRead2") {
452 public void sendToken(LoopFactory lf) { inAddrRead2.peer.sendToken(lf); }
453 public void recvWord(LoopFactory lf) { lf.recvWord(); }
454 public void build(Context ctx) { }
455 public void reset(Context ctx, int phase, Destination ackDestination) { }
457 this.outWrite = new DockOutPort("out", ship.getDock("out")) {
458 protected void build(Context ctx, LoopFactory lf) {
460 lf.abortLoopIfTorpedoPresent();
463 lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO.add(FlagC));
464 if (this.peer != null) {
465 lf.setPredicate(Predicate.FlagB);
467 lf.abortLoopIfTorpedoPresent();
468 this.peer.recvToken(lf);
469 this.peer.sendWord(lf);
472 lf.setPredicate(Predicate.NotFlagB);
473 lf.abortLoopIfTorpedoPresent();
475 lf.setFlags(FlagFunction.ZERO.add(NotFlagC).add(FlagB), FlagFunction.ZERO.add(FlagC).add(FlagB));
476 if (outRead1.peer != null) {
477 lf.setPredicate(Predicate.NotFlagB);
478 outRead1.peer.sendWord(lf);
480 if (outRead2.peer != null) {
481 lf.setPredicate(Predicate.NotFlagA);
482 outRead2.peer.sendWord(lf);
484 lf.setPredicate(null);
488 public void build(Context ctx) {
492 lf = new LoopFactory(ctx, ship.getDock("inAddrRead"), 0);
493 lf.abortLoopIfTorpedoPresent();
495 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
496 lf.setPredicate(Predicate.NotFlagA);
497 lf.sendToken(ship.getDock("out").getDataDestination(), new BitVector(1).set(0));
498 lf.setPredicate(Predicate.FlagA);
499 lf.sendToken(ship.getDock("out").getDataDestination(), new BitVector(1).set(1));
500 lf.setPredicate(null);
505 public static void main(String[] s) throws Exception {
506 Fleet fleet = new Fpga();
507 //Fleet fleet = new Interpreter(false);
509 Random random = new Random(System.currentTimeMillis());
510 long[] vals = new long[256];
511 for(int i=0; i<vals.length; i++) {
512 vals[i] = Math.abs(random.nextInt());
515 Ship mem1 = fleet.getShip("Memory", 0);
516 Ship mem2 = fleet.getShip("Memory", 1);
517 //Ship mem2 = fleet.getShip("DDR2", 0);
523 fp = fleet.run(new Instruction[0]);
524 Gadgets.writeMem(fp, mem1, 0, bv(vals));
525 int vals_length = vals.length;
527 // Disable readback/writeback inside the loop
530 while(stride < vals_length) {
532 // reset the FleetProcess
533 //fp.terminate(); fp = null;
535 System.out.println("stride " + stride);
537 // if we reset the FleetProcess, restart it
538 if (fp==null) fp = fleet.run(new Instruction[0]);
541 vals = mergeSort(fp, fleet, vals, vals_length, stride, mem1, mem2);
543 // verify the cleanup
546 Ship mem = mem1; mem1=mem2; mem2=mem;
549 System.out.println();
552 BitVector[] bvs = new BitVector[vals_length];
553 Gadgets.readMem(fp, mem1, 0, bvs);
554 System.out.println("results:");
555 for(int i=0; i<vals_length; i++)
556 System.out.println(bvs[i].toLong());
559 // FIXME: check for "lingering" torpedoes?
560 public static void verifyClean(FleetProcess fp) {
561 Ship debug = fp.getFleet().getShip("Debug", 0);
562 Dock debugIn = debug.getDock("in");
567 ctx = new Context(fp.getFleet());
568 lf = new LoopFactory(ctx, debugIn, 1);
573 Gadgets.dispatch(fp, ctx);
576 System.out.println("checking debug.in");
577 if (fp.recvWord().toLong() != 12) throw new RuntimeException("debug dock not properly initialized");
578 if (fp.recvWord().toLong() != 5) throw new RuntimeException("debug dock not properly initialized");
581 for(Ship ship : fp.getFleet())
582 if (!"Debug".equals(ship.getType()))
583 for (Dock dock : ship) {
584 System.out.print("checking " + dock + " ");
586 k = (k + 23) % 65535;
587 ctx = new Context(fp.getFleet());
589 boolean reverse = (k%2)==0;
591 lf = new LoopFactory(ctx, debugIn, 4);
593 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
594 lf.setPredicate(Predicate.NotFlagA);
596 lf.setPredicate(Predicate.FlagA);
598 lf.setPredicate(null);
601 lf = new LoopFactory(ctx, dock, 1);
602 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
603 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
604 lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
605 lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
608 // if a torpedo was lying in wait, the problem will be manifest as a "freezup"
609 lf.abortLoopIfTorpedoPresent();
612 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
613 lf.setPredicate(Predicate.NotFlagA);
614 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(0));
615 lf.setPredicate(Predicate.FlagA);
616 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(1));
617 lf.setPredicate(null);
619 Gadgets.dispatch(fp, ctx);
623 for(int i=0; i<4; i++) {
624 kk = fp.recvWord().toLong();
625 System.out.print("\rchecking " + dock + " (got "+(i+1)+")");
626 if (kk != ((reverse ^ (i%2!=0)) ? k+1 : k)) {
627 System.out.println();
628 throw new RuntimeException(dock+" not properly initialized ("+(i+1)+")");
631 System.out.println();
635 public static long[] mergeSort(FleetProcess fp, Fleet fleet,
636 long[] vals, int vals_length, int stride_length,
637 Ship memoryShip1, Ship memoryShip2) throws Exception {
640 BitVector[] mem = new BitVector[vals_length];
641 for(int i=0; i<mem.length; i++) mem[i] = new BitVector(fleet.getWordWidth()).set(vals[i]);
642 Gadgets.writeMem(fp, memoryShip1, 0, mem);
645 //////////////////////////////////////////////////////////////////////////////
647 Process proc = new Process(fleet);
648 DebugModule dm = proc.new DebugModule();
650 int end_of_data = vals_length;
651 int num_strides = end_of_data / (stride_length * 2);
653 MemoryModule mm = proc.new MemoryModule(memoryShip1);
654 SortedMergeModule sm = proc.new SortedMergeModule();
656 // So far: we have four spare Counter ships; one can be used for resetting
657 for(int i=0; i<2; i++) {
659 DownCounterModule c0 = proc.new DownCounterModule();
660 DownCounterModule c1 = proc.new DownCounterModule();
662 c0.start.connect(proc.new ForeverModule(stride_length).out);
663 c0.incr.connect(proc.new ForeverModule(1).out);
665 c1.start.connect(proc.new OnceModule(end_of_data + i*stride_length).out);
666 c1.incr.connect(proc.new OnceModule(stride_length*2).out);
668 RepeatModule r1 = proc.new RepeatModule();
669 r1.val.connect(c1.out);
670 r1.count.connect(proc.new ForeverModule(stride_length).out);
672 AluModule alu = proc.new AluModule();
673 alu.in1.connect(r1.out);
674 alu.in2.connect(c0.out);
675 alu.inOp.connect(proc.new ForeverModule(2).out); // ADD
676 alu.out.connect(i==0 ? mm.inAddrRead1 : mm.inAddrRead2);
678 PunctuatorModule punc = proc.new PunctuatorModule(-1);
679 punc.count.connect(proc.new ForeverModule(stride_length).out);
680 punc.val.connect(i==0 ? mm.outRead1 : mm.outRead2);
681 punc.out.connect(i==0 ? sm.in1 : sm.in2);
684 UnPunctuatorModule unpunc = proc.new UnPunctuatorModule();
685 unpunc.val.connect(sm.out);
686 unpunc.count.connect(proc.new ForeverModule(2*stride_length).out);
688 DownCounterModule cw = proc.new DownCounterModule();
689 cw.start.connect(proc.new OnceModule(end_of_data).out);
690 cw.incr.connect(proc.new OnceModule(1).out);
692 MemoryModule mm2 = proc.new MemoryModule(memoryShip2);
693 mm2.inAddrWrite.connect(cw.out);
694 mm2.inDataWrite.connect(unpunc.out);
695 mm2.outWrite.connect(dm.in);
697 //////////////////////////////////////////////////////////////////////////////
699 Context ctx = new Context(fp.getFleet());
700 ctx.setAutoflush(true);
702 ArrayList<Instruction> ai = new ArrayList<Instruction>();
705 for(Instruction ins : ai) {
706 //System.out.println(ins);
707 fp.sendInstruction(ins);
711 for(int i=0; i<vals_length; i++) {
712 System.out.print("\rreading back... " + i+"/"+vals_length+" ");
713 BitVector rec = fp.recvWord();
714 System.out.print(" (prev result: " + rec + " = " + rec.toLong() + ")");
716 System.out.println("\rdone. ");
718 //if (true) return ret;
720 Context ctx2 = new Context(fp.getFleet());
721 Dock debugIn = fleet.getShip("Debug",0).getDock("in");
723 fp.sendToken(debugIn.getInstructionDestination());
726 LoopFactory lf = new LoopFactory(ctx2, debugIn, 0);
728 lf.abortLoopIfTorpedoPresent();
732 Gadgets.dispatch(fp, ctx2);
737 Ship counter = proc.pool.allocateShip("Counter");
739 for(int phase=0; phase<=3; phase++) {
740 System.out.println("== phase "+phase+" ==================================================================");
741 ctx2 = new Context(fp.getFleet());
743 Destination ackDestination = counter.getDock("in2").getDataDestination();
744 proc.reset(ctx2, phase, ackDestination);
746 Context ctx3 = new Context(fp.getFleet());
747 lf = new LoopFactory(ctx3, counter.getDock("inOp"), 1);
752 lf = new LoopFactory(ctx3, counter.getDock("in1"), 1);
753 lf.literal(reset_count-1);
757 lf = new LoopFactory(ctx3, counter.getDock("in2"), 0);
758 lf.abortLoopIfTorpedoPresent();
761 lf = new LoopFactory(ctx3, counter.getDock("out"), 1);
763 lf.sendToken(counter.getDock("in2").getInstructionDestination()); // HACK: we don't check to make sure this hits
764 lf.sendToken(debugIn.getDataDestination());
765 Gadgets.dispatch(fp, ctx3); // HACK: we don't check to make sure that this is "firmly in place"
767 for(Dock dock : torpedoes) fp.sendToken(dock.getInstructionDestination());
768 Gadgets.dispatch(fp, ctx2);
770 System.out.println("flushed");
773 System.out.println("phase done");
775 System.out.println();
778 fp.sendToken(debugIn.getInstructionDestination());
781 //System.out.println("verifying cleanup:");
784 System.out.println("reading back:");
787 ret = new long[vals_length];
788 BitVector[] mem = new BitVector[vals_length];
789 Gadgets.readMem(fp, memoryShip2, 0, mem);
790 for(int i=0; i<ret.length; i++) ret[i] = mem[i].toLong();
795 private BitVector[] longsToBitVectors(long[] initialValues) {
796 BitVector[] bv = new BitVector[initialValues.length];
797 for(int i=0; i<initialValues.length; i++)
798 bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);