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, true);
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();
256 public BitVector getConstant(String constantName) {
257 return dock.getConstant(constantName);
261 public /*final*/ class DockOutPort extends OutPort {
262 public final Dock dock;
263 public final int count;
264 public DockOutPort(String name, Dock dock) { this(name, dock, 0); }
265 public DockOutPort(String name, Dock dock, int count) { super(name); this.dock = dock; this.count = count; }
266 public void sendToken(LoopFactory lf) { lf.sendToken(dock.getDataDestination()); }
267 public void recvWord(LoopFactory lf) { lf.recvWord(); }
268 public void build(Context ctx) { build(ctx, new LoopFactory(ctx, dock, 1)); }
269 protected void build(Context ctx, LoopFactory lf) {
270 if (peer==null) return;
271 lf = lf.makeNext(count);
272 lf.abortLoopIfTorpedoPresent();
277 public void reset(Context ctx, int phase, Destination ackDestination) {
278 doReset(ctx, phase, dock, peer, ackDestination, true);
283 private static BitVector bv(long l) { return new BitVector(/*FIXME fleet.getWordWidth()*/37).set(l); }
284 private static BitVector[] bv(long[] l) {
285 BitVector[] ret = new BitVector[l.length];
286 for(int i=0; i<ret.length; i++) ret[i] = bv(l[i]);
291 * Deliver the constant at out forever. Flow control in<->out is
292 * maintained, but out is not flow-controlled, so be sure
293 * that every datum sent there is consumed synchronously wiht
294 * data items sent to out.
296 public class ForeverModule extends Module {
297 private BitVector bv;
298 public final OutPort out = new OutPort("out") {
299 public void sendToken(LoopFactory lf) { }
300 public void recvWord(LoopFactory lf) { }
301 public void build(Context ctx) { }
302 public void reset(Context ctx, int phase, Destination ackDestination) { }
303 public void setPeer(InPort peer) {
305 DockInPort pip = ((DockInPort)peer);
306 for(int i=0; i<pip.pattern.length; i++) {
307 if (pip.pattern[i]==null)
312 public ForeverModule(long l) { this(new BitVector(fleet.getWordWidth()).set(l)); }
313 public ForeverModule(final BitVector bv) { this.bv = bv; }
316 public class OnceModule extends Module {
317 private BitVector bv;
318 public final OutPort out = new OutPort("out") {
319 public void sendToken(LoopFactory lf) { }
320 public void recvWord(LoopFactory lf) { }
321 public void build(Context ctx) { }
322 public void reset(Context ctx, int phase, Destination ackDestination) { }
323 public void setPeer(InPort peer) {
325 DockInPort pip = ((DockInPort)peer);
326 BitVector[] pip_pattern = pip.pattern;
327 BitVector[] temp = new BitVector[pip_pattern.length * 2];
330 boolean done = false;
331 // FIXME: if peer.count is already 1, this gets simpler and different
332 for(i=0; i<temp.length; i++) {
333 if (pip_pattern[j] != null) {
334 temp[i] = pip_pattern[j];
341 if (j >= pip_pattern.length) j = 0;
343 pip.pattern = new BitVector[i];
344 System.arraycopy(temp, 0, pip.pattern, 0, i);
348 public OnceModule(long l) { this(new BitVector(fleet.getWordWidth()).set(l)); }
349 public OnceModule(final BitVector bv) { this.bv = bv; }
352 public class DebugModule extends Module {
353 public final Ship ship = pool.allocateShip("Debug");
354 public final InPort in = new DockInPort("in", ship.getDock("in"));
355 // NOTE: shutdown needs to treat this specially
356 public DebugModule() { }
359 public class UnPunctuatorModule extends Module {
360 private final Ship ship = pool.allocateShip("Counter");
361 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
362 public final InPort val = new DockInPort("in1", ship.getDock("in1"));
363 public final InPort count = new DockInPort("in2", ship.getDock("in2"), 0, new BitVector[] { null, bv(1) });
364 public final InPort op = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
365 ship.getDock("inOp").getConstant("PASS_C2_V1"),
366 ship.getDock("inOp").getConstant("DROP_C2_V1") } );
367 public UnPunctuatorModule() { }
370 public class PunctuatorModule extends Module {
371 private final long punc;
372 private final Ship ship = pool.allocateShip("Counter");
373 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
374 public final InPort val = new DockInPort("in1", ship.getDock("in1"));
375 public final InPort op = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
376 ship.getDock("inOp").getConstant("PASS_C2_V1"),
377 ship.getDock("inOp").getConstant("PASS_C2_V2") } );
378 public final InPort count;
379 public PunctuatorModule(long punc) {
381 this.count = new DockInPort("in2", ship.getDock("in2"), 0, new BitVector[] { null, bv(1), bv(punc) });
385 public class AluModule extends Module {
386 public final Ship ship = pool.allocateShip("Alu");
387 public final InPort in1 = new DockInPort("in1", ship.getDock("in1"));
388 public final InPort in2 = new DockInPort("in2", ship.getDock("in2"));
389 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"));
390 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
391 public AluModule() { }
394 public class DownCounterModule extends Module {
395 public final Ship ship = pool.allocateShip("Counter");
396 public final InPort start = new DockInPort("in1", ship.getDock("in1"));
397 public final InPort incr = new DockInPort("in2", ship.getDock("in2"));
398 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
399 ship.getDock("inOp").getConstant("COUNT") });
400 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
401 public DownCounterModule() { }
404 public class RepeatModule extends Module {
405 public final Ship ship = pool.allocateShip("Counter");
406 public final InPort count = new DockInPort("in1", ship.getDock("in1"));
407 public final InPort val = new DockInPort("in2", ship.getDock("in2"));
408 public final InPort inOP = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
409 ship.getDock("inOp").getConstant("REPEAT_C1_V2") });
410 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
411 public RepeatModule() { }
414 public class SortedMergeModule extends Module {
415 public final Ship ship = pool.allocateShip("Alu");
416 public final InPort in1 = new DockInPort("in1", ship.getDock("in1"));
417 public final InPort in2 = new DockInPort("in2", ship.getDock("in2"));
418 public final InPort inOp = new DockInPort("inOp", ship.getDock("inOp"), 0, new BitVector[] {
419 ship.getDock("inOp").getConstant("MAXMERGE") });
420 public final OutPort out = new DockOutPort("out", ship.getDock("out"));
421 public SortedMergeModule() { }
424 public class MemoryModule extends Module {
425 public final Ship ship;
426 public final InPort inCBD;
427 public final InPort inAddrRead1;
428 public final InPort inAddrRead2;
429 public final InPort inAddrWrite;
430 public final InPort inDataWrite;
431 public final OutPort outRead1;
432 public final OutPort outRead2;
433 public final OutPort outWrite;
434 public MemoryModule(Ship memoryShip) {
435 this.ship = memoryShip;
436 this.inCBD = ship.getType().equals("Memory") ? new DockInPort("inCBD", ship.getDock("inCBD")) : null;
437 this.inAddrWrite = new DockInPort("inAddrWrite", ship.getDock("inAddrWrite"));
438 this.inDataWrite = new DockInPort("inDataWrite", ship.getDock("inDataWrite"));
439 this.inAddrRead1 = new InPort("inAddrRead1") {
440 public void recvToken(LoopFactory lf) { lf.recvToken(); }
441 public void sendWord(LoopFactory lf) { lf.sendWord(ship.getDock("inAddrRead").getDataDestination(), new BitVector(1).set(0)); }
442 public void build(Context ctx) { }
443 public int getTokensToAbsorb() { return outRead1.peer.getTokensToAbsorb(); }
444 public void reset(Context ctx, int phase, Destination ackDestination) {
445 doReset(ctx, phase, ship.getDock("inAddrRead"), null, ackDestination, false);
448 this.inAddrRead2 = new InPort("inAddrRead2") {
449 public void recvToken(LoopFactory lf) { lf.recvToken(); }
450 public void sendWord(LoopFactory lf) { lf.sendWord(ship.getDock("inAddrRead").getDataDestination(), new BitVector(1).set(1)); }
451 public void build(Context ctx) { }
452 public int getTokensToAbsorb() { return outRead2.peer.getTokensToAbsorb(); }
453 public void reset(Context ctx, int phase, Destination ackDestination) { }
455 this.outRead1 = new OutPort("outRead1") {
456 public void sendToken(LoopFactory lf) { inAddrRead1.peer.sendToken(lf); }
457 public void recvWord(LoopFactory lf) { lf.recvWord(); }
458 public void build(Context ctx) { }
459 public void reset(Context ctx, int phase, Destination ackDestination) { }
461 this.outRead2 = new OutPort("outRead2") {
462 public void sendToken(LoopFactory lf) { inAddrRead2.peer.sendToken(lf); }
463 public void recvWord(LoopFactory lf) { lf.recvWord(); }
464 public void build(Context ctx) { }
465 public void reset(Context ctx, int phase, Destination ackDestination) { }
467 this.outWrite = new DockOutPort("out", ship.getDock("out")) {
468 protected void build(Context ctx, LoopFactory lf) {
470 lf.abortLoopIfTorpedoPresent();
473 lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO.add(FlagC));
474 if (this.peer != null) {
475 lf.setPredicate(Predicate.FlagB);
477 lf.abortLoopIfTorpedoPresent();
478 this.peer.recvToken(lf);
479 this.peer.sendWord(lf);
482 lf.setPredicate(Predicate.NotFlagB);
483 lf.abortLoopIfTorpedoPresent();
485 lf.setFlags(FlagFunction.ZERO.add(NotFlagC).add(FlagB), FlagFunction.ZERO.add(FlagC).add(FlagB));
486 if (outRead1.peer != null) {
487 lf.setPredicate(Predicate.NotFlagB);
488 outRead1.peer.sendWord(lf);
490 if (outRead2.peer != null) {
491 lf.setPredicate(Predicate.NotFlagA);
492 outRead2.peer.sendWord(lf);
494 lf.setPredicate(null);
498 public void build(Context ctx) {
502 lf = new LoopFactory(ctx, ship.getDock("inAddrRead"), 0);
503 lf.abortLoopIfTorpedoPresent();
505 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
506 lf.setPredicate(Predicate.NotFlagA);
507 lf.sendToken(ship.getDock("out").getDataDestination(), new BitVector(1).set(0));
508 lf.setPredicate(Predicate.FlagA);
509 lf.sendToken(ship.getDock("out").getDataDestination(), new BitVector(1).set(1));
510 lf.setPredicate(null);
515 public static void main(String[] s) throws Exception {
516 Fleet fleet = new Fpga();
517 //Fleet fleet = new Interpreter(false);
519 Random random = new Random(System.currentTimeMillis());
520 long[] vals = new long[256];
521 for(int i=0; i<vals.length; i++) {
522 vals[i] = Math.abs(random.nextInt());
525 Ship mem1 = fleet.getShip("Memory", 0);
526 Ship mem2 = fleet.getShip("Memory", 1);
527 //Ship mem2 = fleet.getShip("DDR2", 0);
533 fp = fleet.run(new Instruction[0]);
534 MemoryUtils.writeMem(fp, mem1, 0, bv(vals));
535 int vals_length = vals.length;
537 // Disable readback/writeback inside the loop
540 while(stride < vals_length) {
542 // reset the FleetProcess
543 //fp.terminate(); fp = null;
545 System.out.println("stride " + stride);
547 // if we reset the FleetProcess, restart it
548 if (fp==null) fp = fleet.run(new Instruction[0]);
551 vals = mergeSort(fp, fleet, vals, vals_length, stride, mem1, mem2);
553 // verify the cleanup
556 Ship mem = mem1; mem1=mem2; mem2=mem;
559 System.out.println();
562 BitVector[] bvs = new BitVector[vals_length];
563 MemoryUtils.readMem(fp, mem1, 0, bvs);
564 System.out.println("results:");
565 for(int i=0; i<vals_length; i++)
566 System.out.println(bvs[i].toLong());
569 // FIXME: check for "lingering" torpedoes?
570 public static void verifyClean(FleetProcess fp) {
571 Ship debug = fp.getFleet().getShip("Debug", 0);
572 Dock debugIn = debug.getDock("in");
577 ctx = new Context(fp.getFleet());
578 lf = new LoopFactory(ctx, debugIn, 1);
586 System.out.println("checking debug.in");
587 if (fp.recvWord().toLong() != 12) throw new RuntimeException("debug dock not properly initialized");
588 if (fp.recvWord().toLong() != 5) throw new RuntimeException("debug dock not properly initialized");
591 for(Ship ship : fp.getFleet())
592 if (!"Debug".equals(ship.getType()))
593 for (Dock dock : ship) {
594 System.out.print("checking " + dock + " ");
596 k = (k + 23) % 65535;
597 ctx = new Context(fp.getFleet());
599 boolean reverse = (k%2)==0;
601 lf = new LoopFactory(ctx, debugIn, 4);
603 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
604 lf.setPredicate(Predicate.NotFlagA);
606 lf.setPredicate(Predicate.FlagA);
608 lf.setPredicate(null);
611 lf = new LoopFactory(ctx, dock, 1);
612 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
613 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
614 lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
615 lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
618 // if a torpedo was lying in wait, the problem will be manifest as a "freezup"
619 lf.abortLoopIfTorpedoPresent();
622 lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
623 lf.setPredicate(Predicate.NotFlagA);
624 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(0));
625 lf.setPredicate(Predicate.FlagA);
626 lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(1));
627 lf.setPredicate(null);
633 for(int i=0; i<4; i++) {
634 kk = fp.recvWord().toLong();
635 System.out.print("\rchecking " + dock + " (got "+(i+1)+")");
636 if (kk != ((reverse ^ (i%2!=0)) ? k+1 : k)) {
637 System.out.println();
638 throw new RuntimeException(dock+" not properly initialized ("+(i+1)+")");
641 System.out.println();
645 public static long[] mergeSort(FleetProcess fp, Fleet fleet,
646 long[] vals, int vals_length, int stride_length,
647 Ship memoryShip1, Ship memoryShip2) throws Exception {
650 BitVector[] mem = new BitVector[vals_length];
651 for(int i=0; i<mem.length; i++) mem[i] = new BitVector(fleet.getWordWidth()).set(vals[i]);
652 MemoryUtils.writeMem(fp, memoryShip1, 0, mem);
655 //////////////////////////////////////////////////////////////////////////////
657 Process proc = new Process(fleet);
658 DebugModule dm = proc.new DebugModule();
660 int end_of_data = vals_length;
661 int num_strides = end_of_data / (stride_length * 2);
663 MemoryModule mm = proc.new MemoryModule(memoryShip1);
664 SortedMergeModule sm = proc.new SortedMergeModule();
666 // So far: we have four spare Counter ships; one can be used for resetting
667 for(int i=0; i<2; i++) {
669 DownCounterModule c0 = proc.new DownCounterModule();
670 DownCounterModule c1 = proc.new DownCounterModule();
672 c0.start.connect(proc.new ForeverModule(stride_length).out);
673 c0.incr.connect(proc.new ForeverModule(1).out);
675 c1.start.connect(proc.new OnceModule(end_of_data + i*stride_length).out);
676 c1.incr.connect(proc.new OnceModule(stride_length*2).out);
678 RepeatModule r1 = proc.new RepeatModule();
679 r1.val.connect(c1.out);
680 r1.count.connect(proc.new ForeverModule(stride_length).out);
682 AluModule alu = proc.new AluModule();
683 alu.in1.connect(r1.out);
684 alu.in2.connect(c0.out);
685 alu.inOp.connect(proc.new ForeverModule(((Module.DockInPort)alu.inOp).getConstant("ADD")).out);
686 alu.out.connect(i==0 ? mm.inAddrRead1 : mm.inAddrRead2);
688 PunctuatorModule punc = proc.new PunctuatorModule(-1);
689 punc.count.connect(proc.new ForeverModule(stride_length).out);
690 punc.val.connect(i==0 ? mm.outRead1 : mm.outRead2);
691 punc.out.connect(i==0 ? sm.in1 : sm.in2);
694 UnPunctuatorModule unpunc = proc.new UnPunctuatorModule();
695 unpunc.val.connect(sm.out);
696 unpunc.count.connect(proc.new ForeverModule(2*stride_length).out);
698 DownCounterModule cw = proc.new DownCounterModule();
699 cw.start.connect(proc.new OnceModule(end_of_data).out);
700 cw.incr.connect(proc.new OnceModule(1).out);
702 MemoryModule mm2 = proc.new MemoryModule(memoryShip2);
703 mm2.inAddrWrite.connect(cw.out);
704 mm2.inDataWrite.connect(unpunc.out);
705 mm2.outWrite.connect(dm.in);
707 //////////////////////////////////////////////////////////////////////////////
709 Context ctx = new Context(fp.getFleet());
710 ctx.setAutoflush(true);
712 ArrayList<Instruction> ai = new ArrayList<Instruction>();
715 for(Instruction ins : ai) {
716 //System.out.println(ins);
717 fp.sendInstruction(ins);
721 for(int i=0; i<vals_length; i++) {
722 System.out.print("\rreading back... " + i+"/"+vals_length+" ");
723 BitVector rec = fp.recvWord();
724 System.out.print(" (prev result: " + rec + " = " + rec.toLong() + ")");
726 System.out.println("\rdone. ");
728 //if (true) return ret;
730 Context ctx2 = new Context(fp.getFleet());
731 Dock debugIn = fleet.getShip("Debug",0).getDock("in");
733 fp.sendToken(debugIn.getInstructionDestination());
736 LoopFactory lf = new LoopFactory(ctx2, debugIn, 0);
738 lf.abortLoopIfTorpedoPresent();
747 Ship counter = proc.pool.allocateShip("Counter");
749 for(int phase=0; phase<=3; phase++) {
750 System.out.println("== phase "+phase+" ==================================================================");
751 ctx2 = new Context(fp.getFleet());
753 Destination ackDestination = counter.getDock("in2").getDataDestination();
754 proc.reset(ctx2, phase, ackDestination);
756 Context ctx3 = new Context(fp.getFleet());
757 lf = new LoopFactory(ctx3, counter.getDock("inOp"), 1);
758 lf.literal("DROP_C1_V2");
762 lf = new LoopFactory(ctx3, counter.getDock("in1"), 1);
763 lf.literal(reset_count-1);
767 lf = new LoopFactory(ctx3, counter.getDock("in2"), 0);
768 lf.abortLoopIfTorpedoPresent();
771 lf = new LoopFactory(ctx3, counter.getDock("out"), 1);
773 lf.sendToken(counter.getDock("in2").getInstructionDestination()); // HACK: we don't check to make sure this hits
774 lf.sendToken(debugIn.getDataDestination());
775 ctx3.dispatch(fp); // HACK: we don't check to make sure that this is "firmly in place"
777 for(Dock dock : torpedoes) fp.sendToken(dock.getInstructionDestination());
780 System.out.println("flushed");
783 System.out.println("phase done");
785 System.out.println();
788 fp.sendToken(debugIn.getInstructionDestination());
791 //System.out.println("verifying cleanup:");
794 System.out.println("reading back:");
797 ret = new long[vals_length];
798 BitVector[] mem = new BitVector[vals_length];
799 MemoryUtils.readMem(fp, memoryShip2, 0, mem);
800 for(int i=0; i<ret.length; i++) ret[i] = mem[i].toLong();
805 private BitVector[] longsToBitVectors(long[] initialValues) {
806 BitVector[] bv = new BitVector[initialValues.length];
807 for(int i=0; i<initialValues.length; i++)
808 bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);