move marina/testCode/com to src/com
[fleet.git] / src / com / sun / vlsi / chips / marina / test / MarinaTest.java
1 package com.sun.vlsi.chips.marina.test;
2 /* -*- tab-width: 4 -*- */
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.sun.async.test.BitVector;
7 import com.sun.async.test.ChainControl;
8 import com.sun.async.test.ChainG;
9 import com.sun.async.test.ChainTest;
10 import com.sun.async.test.ChipModel;
11 import com.sun.async.test.HP34401A;
12 import com.sun.async.test.Infrastructure;
13 import com.sun.async.test.JtagSubchainTesterModel;
14 import com.sun.async.test.JtagTester;
15 import com.sun.async.test.ManualPowerChannel;
16 import com.sun.async.test.NanosimModel;
17 import com.sun.async.test.HsimModel;
18 import com.sun.async.test.VerilogModel;
19 import com.sun.async.test.Netscan4;
20 import com.sun.async.test.PowerChannel;
21 import com.sun.async.test.Pst3202Channel;
22 import com.sun.async.test.SiliconChip;
23 import com.sun.async.test.SimulationModel;
24 import com.sun.async.test.VoltageReadable;
25 import com.sun.vlsi.chips.marina.test.Marina.Ilc;
26 import com.sun.vlsi.chips.marina.test.CmdArgs;
27 import com.sun.vlsi.chips.marina.test.CmdArgs.Mode;
28
29 import edu.berkeley.fleet.api.Dock;
30 import edu.berkeley.fleet.api.Instruction;
31 import edu.berkeley.fleet.api.Predicate;
32 import edu.berkeley.fleet.api.Instruction.Set.SetDest;
33 import edu.berkeley.fleet.api.Instruction.Set.SetSource;
34 import edu.berkeley.fleet.marina.MarinaFleet;
35 import edu.berkeley.fleet.marina.MarinaPath;
36
37 /**
38  * Tests for Marina
39  */
40 public class MarinaTest {
41     public static final MarinaFleet marinaFleet = new MarinaFleet();
42     public static final Dock dock = marinaFleet.getOnlyInputDock();
43
44     //--------------------------  constants -----------------------------------
45     private static final String SCAN_CHAIN_XML = "marina.xml";
46     private static final String NET_LIST = "marina.spi";
47
48     public static final int INSTR_SZ = 36;
49
50        
51     public static final Instruction.Set.FlagFunction CLEAR_FLAG 
52         = Instruction.Set.FlagFunction.ZERO;
53     public static final Instruction.Set.FlagFunction SET_FLAG 
54         = Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagA)
55         .add(Predicate.NotFlagA);
56     public static final Instruction.Set.FlagFunction A_FLAG 
57         = Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagA);
58
59     public static final Instruction.Set.FlagFunction B_FLAG 
60         = Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagB);
61     
62     // COLUMN_LATENCY is a delay that is larger than the latency through an Infinity column
63     private static final int COLUMN_LATENCY = 10; // nanoseconds
64
65
66     // Officially, this should be the number of requeueing no-ops that
67     // can be inserted into an idle dock whose OLC is nonzero.
68     //
69     // Less formally, this is roughly the number of stages of
70     // buffering between the instruction insertion point and the
71     // instruction ring, plus the capacity of the instruction ring.
72     private static final int INSTRUCTION_IN_SATURATION_AMOUNT = 19;
73
74     // This is some number which is significantly greater than
75     // INSTRUCTION_IN_SATURATION_AMOUNT.  Increasing it may slow the tests down, but
76     // will never cause them to operate incorrectly.
77     private static final int MORE_THAN_INSTRUCTION_IN_SATURATION_AMOUNT = 25;
78         
79     // This is the number of items which can be in the instruction
80     // fifo ring WITHOUT causing it to stop circulating.
81     private static final int INSTRUCTION_RING_CAPACITY = 13;
82         
83     // Officially, this should be the number of data items which can
84     // be sent from the dock while the "data" proper stopper is in
85     // the "stopped" state
86     //
87     // Less formally, this is roughly the number of stages of
88     // buffering between the dock's data successor and the "data"
89     // proper stopper
90     /*
91       FIXME: what is the correct value here?
92       private static final int DATA_OUT_SATURATION_AMOUNT = XXX;
93     */
94
95     // This is some number which is greater than
96     // DATA_OUT_SATURATION_AMOUNT, but less than the capacity of the
97     // instruction fifo.
98     private static final int MORE_THAN_DATA_OUT_SATURATION_AMOUNT = 10;
99         
100     // Nominal cycle time assuming 4 GHz throughput
101     public static double CYCLE_TIME_NS;
102
103     //--------------------------------  types ---------------------------------
104
105     //--------------------------  private data --------------------------------
106     private static long startTime;
107
108     public static Indenter indenter = new Indenter();
109     private Marina marina;
110     private ChipModel model;
111     //private ChainControl cc;
112     //private JtagTester tester;
113     private CmdArgs cmdArgs;
114     private PowerChannel corePowerSupply, padsPowerSupply;
115     private VoltageReadable coreVoltmeter, voltmeterForCurrent;
116
117     private ChainTest ctD, ctR, ctC, ct;
118     private ChainControl ccD, ccR, ccC, cc;
119         
120     //--------------------------  private methods -----------------------------
121     /** @return true if simulation. Return false if we're testing silicon. */
122     private boolean sim() {return model instanceof SimulationModel;}
123         
124     private void prln(String msg) {indenter.prln(msg);}
125     private void pr(String msg) {indenter.pr(msg);}
126     private void adjustIndent(int n) {indenter.adjustIndent(n);}
127         
128     public static void fatal(boolean pred, String msg) { MarinaUtils.fatal(pred, msg); }
129
130     public static void fatalIfBitVectorsDoNotMatch(BitVector bv1, BitVector bv2) {
131         // FIXME: better error reporting needed here
132
133         fatal(bv1.getNumBits()!=bv2.getNumBits(), "lengths do not match");
134
135         boolean mismatch = false;
136         String err = "";
137         for(int i=0; i<bv1.getNumBits(); i++) {
138             if (bv1.get(i) != bv2.get(i)) {
139                 mismatch = true;
140                 err += ""+i+", ";
141             }
142         }
143         fatal(mismatch, "bit vectors do not match on bits " + err + "\n  "+bv1+"\n  "+bv2);
144     }
145
146     private static void printTestTime() {
147         long endTime = System.currentTimeMillis();
148         System.out.println("Test took: "+(endTime-startTime)/1000.0+"  sec");
149     }
150     
151     // Tell user what we're about to do
152     private static void reportTask(CmdArgs args) {
153         System.out.println("Begin testing Marina");
154         switch (args.mode) {
155             case WHOLE_CHIP_SCHEMATIC_PARASITICS:
156                 System.out.println("  Simulate whole chip, schematic parasitics");
157                 break;
158             case WHOLE_CHIP_LAYOUT_PARASITICS:
159                 System.out.println("  Simulate whole chip, layout parasitics");
160                 break;
161             case TEST_SILICON:
162                 System.out.println("  Test silicon");
163                 break;
164             default:
165                 fatal(true, "unrecognized CmdArgs.Mode");
166                 return;
167         }
168     }
169     private void setUpSuppliesAndMeters(Station station) {
170         // set up power supplies and meters
171         if (!sim()) {
172             prln("Testing station: "+station);
173             Infrastructure.gpibControllers = new int[] {0};
174             switch (cmdArgs.station) {
175                 case ONE:
176                     corePowerSupply = new Pst3202Channel("ch1", "HPST3202", 1);
177                     padsPowerSupply = new Pst3202Channel("ch2", "HPST3202", 2);
178                     break;
179                 case TWO:
180                     corePowerSupply = new Pst3202Channel("ch1", "HPST3202B", 1);
181                     padsPowerSupply = new Pst3202Channel("ch2", "HPST3202B", 2);
182                     break;
183                 default:
184                     fatal(true, "Unrecognized station: "+cmdArgs.station);
185             }
186             corePowerSupply.setCurrent((float)1.7);
187             corePowerSupply.setVoltageWait((float)1.0);
188                   
189             padsPowerSupply.setCurrent((float)0.100);
190             padsPowerSupply.setVoltageWait((float)1.8);
191                   
192             coreVoltmeter = new HP34401A(station.coreVoltmeter);
193             voltmeterForCurrent = new HP34401A(station.currentVoltmenter);
194         }
195     }
196         
197     private MarinaTest(String[] args) {
198         cmdArgs = new CmdArgs(args);
199         reportTask(cmdArgs);
200         if (cmdArgs.mode==Mode.TEST_SILICON) doSilicon(); else doSim();
201     }
202         
203     private void doSim() {
204         String netListName;
205         switch (cmdArgs.mode) {
206             case WHOLE_CHIP_SCHEMATIC_PARASITICS:
207                 netListName = NET_LIST; 
208                 break;
209             case WHOLE_CHIP_LAYOUT_PARASITICS:
210                 netListName = "marina_pads_guts.spi"; 
211                 break;
212             default:
213                 fatal(true, "unrecognized CmdArgs.Mode");
214                 return;
215         }
216         model = cmdArgs.useVerilog
217             ? new VerilogModel()
218             : cmdArgs.useHsim
219             ? new HsimModel()
220             : new NanosimModel();
221             
222         ((SimulationModel)model).setOptimizedDirectReadsWrites(true);
223
224         CYCLE_TIME_NS = cmdArgs.useVerilog ? (100*20) : 0.250;
225         int khz   = model instanceof VerilogModel ? 100000 : cmdArgs.jtagShift ? 20000 : 1000000;
226
227         prln("constructing jtag controller");
228         JtagTester tester = ((SimulationModel)model).createJtagTester("TCK", "TMS", "TRSTb", "TDI", "TDO");
229         tester.printInfo = false;
230
231         ChainControls ccs = new ChainControls();
232         PowerChannel pc = new ManualPowerChannel("pc", false);
233         /*
234         JtagTester testerD, testerR, testerC;
235         testerD = ((SimulationModel)model).createJtagSubchainTester("sid[1:9]", null); 
236         testerR = ((SimulationModel)model).createJtagSubchainTester("sir[1:9]", null); 
237         testerC = ((SimulationModel)model).createJtagSubchainTester("sic[1:9]", null); 
238         testerD.printInfo = testerR.printInfo = testerC.printInfo = false;
239
240         ccD = new ChainControl(SCAN_CHAIN_XML, testerD, 1.8f, khz);
241         ccR = new ChainControl(SCAN_CHAIN_XML, testerR, 1.8f, khz);
242         ccC = new ChainControl(SCAN_CHAIN_XML, testerC, 1.8f, khz);
243         ccD.noTestSeverity = ccR.noTestSeverity = ccC.noTestSeverity = Infrastructure.SEVERITY_NOMESSAGE;
244
245         
246         ctD = new ChainTest(ccD, pc);
247         ctR = new ChainTest(ccR, pc);
248         ctC = new ChainTest(ccC, pc);
249         */        
250         /*
251         ccs.addChain(Marina.DATA_CHAIN, ccD);
252         ccs.addChain(Marina.REPORT_CHAIN, ccR);
253         ccs.addChain(Marina.CONTROL_CHAIN, ccC);
254         */
255
256         cc = new ChainControl(SCAN_CHAIN_XML, tester, 1.8f, khz);
257         cc.noTestSeverity = Infrastructure.SEVERITY_NOMESSAGE;
258         ct = new ChainTest(cc, pc);
259         ccs.addChain(Marina.DATA_CHAIN, cc);
260         ccs.addChain(Marina.REPORT_CHAIN, cc);
261         ccs.addChain(Marina.CONTROL_CHAIN, cc);
262
263         marina = new Marina(ccs, model, !cmdArgs.jtagShift, indenter);
264
265         if (model instanceof VerilogModel)
266             ((SimulationModel)model).start("verilog", "marina.v", VerilogModel.DUMPVARS, !cmdArgs.jtagShift);
267         else if (model instanceof HsimModel)
268             ((SimulationModel)model).start("hsim64", netListName, 0, !cmdArgs.jtagShift);
269         else
270             ((SimulationModel)model).start("nanosim -c cfg", netListName, 0, !cmdArgs.jtagShift);
271
272         /*
273         ccC.resetInBits();
274         ccC.shift(Marina.CONTROL_CHAIN, false, true);
275         */
276
277         cc.resetInBits();
278         cc.shift(Marina.CONTROL_CHAIN, false, true);
279         
280         doOneTest(cmdArgs.testNum);
281         
282         ((SimulationModel)model).finish();
283     }
284     private void doSilicon() {
285         model = new SiliconChip();
286         String ip = cmdArgs.station.ipAddr;
287         JtagTester tester = new Netscan4(ip, cmdArgs.station.jtagChannel);
288         tester.printInfo = false;
289         int khz = 1000;
290         ChainControl cc = new ChainControl("???", tester, 1.8f, khz);
291         cc.noTestSeverity = Infrastructure.SEVERITY_NOMESSAGE;
292         ChainControls ccs = new ChainControls();
293         ccs.addChain(Marina.DATA_CHAIN, cc);
294         ccs.addChain(Marina.REPORT_CHAIN, cc);
295         ccs.addChain(Marina.CONTROL_CHAIN, cc);
296         marina = new Marina(ccs, model, false, indenter);
297         PowerChannel pc = new ManualPowerChannel("pc", false);
298         ChainTest ct = new ChainTest(cc, pc);
299         ct.testAllChains("marina", Infrastructure.SEVERITY_WARNING);
300         doOneTest(cmdArgs.testNum);
301         setUpSuppliesAndMeters(cmdArgs.station);
302     }
303
304     /** In the absence of looping, the longest path through Infinity is 4 column delays */
305     private void waitUntilQuiescent() {
306         model.waitNS(4*COLUMN_LATENCY);
307     }
308     
309     private double readCurrent() {
310         return voltmeterForCurrent.readVoltage() / cmdArgs.station.ammeterShuntResistance;
311     }
312     
313     /** Generate List of BitVectors where Token=true, high 25 data bits  
314      * are alternating ones and zeros, low 12 data bits increment from 
315      * zero, and address is given by addr. */
316     private List<BitVector> makeIncrDataConstAdr(int num, int addr) {
317         List<BitVector> ans = new ArrayList<BitVector>();
318         BitVector dHi = new BitVector(25, "dataHi");
319         BitVector dLo = new BitVector(12, "dataLo");
320         BitVector t = new BitVector("1", "token");
321         BitVector a = new BitVector(14, "addr");
322         dHi.setFromLong(0x00aaaaa);
323         a.setFromLong(addr);
324         for (int i=0; i<num; i++) {
325             dLo.setFromLong(i);
326             ans.add(dHi.cat(dLo).cat(t).cat(a));
327             dHi = dHi.not();
328         }
329         return ans;
330     }
331     private void stopToStop(ProperStopper s1, ProperStopper s2, 
332                             Counter ctr,
333                             List<BitVector> din) {
334         prln("Begin stopToStop");
335         adjustIndent(2);
336         
337         s1.stop();
338         
339         long ctrStart = ctr==null ? 0 : ctr.getCount();
340         
341         s1.fillMany(din);
342         waitUntilQuiescent();
343         
344         List<BitVector> dout = s2.drainMany();
345         
346         MarinaUtils.compareItemsOrdered(din, dout);
347         
348         if (ctr!=null) {
349             long ctrEnd = ctr.getCount();
350             long delta = ctrEnd - ctrStart;
351             long expect = din.size();
352             fatal(delta!=expect, 
353                   "counter delta wrong: expected delta: "+expect+
354                   " counter before:"+ctrStart+" counter after:"+ctrEnd);
355         }
356         
357         adjustIndent(-2);
358         prln("End stopToStop");
359     }
360     /** Burst data from src to dst. gate is stopped while loading src. gate
361      * is then run to allow the burst to flow. */
362     private void stopToStopBurst(ProperStopper src, ProperStopper gate, 
363                                  ProperStopper dst,
364                                  Counter ctr,
365                                  List<BitVector> din) {
366         prln("Begin stopToStopBurst test");
367         adjustIndent(2);
368                 
369         src.stop();
370         gate.stop();
371                 
372         long ctrStart = ctr==null ? 0 : ctr.getCount();
373                 
374         src.fillMany(din);
375         waitUntilQuiescent();
376
377         // open the gate to start the burst
378         gate.run();
379         waitUntilQuiescent();
380                 
381         List<BitVector> dout = dst.drainMany();
382                 
383         MarinaUtils.compareItemsOrdered(din, dout);
384                 
385         if (ctr!=null) {
386             long ctrEnd = ctr.getCount();
387             long delta = ctrEnd - ctrStart;
388                 
389             long expectA = din.size();
390             fatal(delta!=expectA, 
391                   "counter delta wrong: expected delta: "+expectA+
392                   " counter before:"+ctrStart+" counter after:"+ctrEnd);
393         }
394                 
395         adjustIndent(-2);
396         prln("End stopToStopBurst test");
397     }
398
399     private void stopToStopOne(ProperStopper s1, ProperStopper s2, 
400                                Counter ctr, int adr) {
401         prln("Begin stopToStopOne");
402         adjustIndent(2);
403         
404         List<BitVector> din = makeIncrDataConstAdr(1, adr);
405         stopToStop(s1, s2, ctr, din);
406
407         adjustIndent(-2);
408         prln("End stopToStopOne");
409     }
410     
411     private void stopToStopThree(ProperStopper s1, ProperStopper s2, 
412                                  Counter ctr, int adr) {
413         prln("Begin stopToStopOne");
414         adjustIndent(2);
415         
416         List<BitVector> din = makeIncrDataConstAdr(3, adr);
417         stopToStop(s1, s2, ctr, din);
418
419         adjustIndent(-2);
420         prln("End stopToStopOne");
421     }
422
423     private int indexOf(BitVector o, List<BitVector> dIn) {
424         for (int i=0; i<dIn.size(); i++) {
425             if (o.equals(dIn.get(i)))  return i;
426         }
427         return -1;
428     }
429     private String ringDump(List<BitVector> dIn, List<BitVector> dOut) {
430         StringBuffer sb = new StringBuffer();
431         sb.append("  ring dump: ");
432         for (BitVector o : dOut) {
433             sb.append(indexOf(o, dIn)+" ");
434         }
435         return sb.toString();
436     }
437
438     private int[][] makeIntArray2D(int a, int b) {
439         int[][] ans = new int[a][];
440         for (int i=0; i<a; i++) ans[i] = new int[b];
441         return ans;
442     }
443
444     //=========================================================================
445     // Put top level tests here
446
447     private void testChains(Marina marina) {
448         if (ctC!=null) {
449             prln("Testing control chain...");
450             ctC.testOneChain(Marina.CONTROL_CHAIN, Infrastructure.SEVERITY_WARNING);
451             ccC.resetInBits();
452             ccC.shift(Marina.CONTROL_CHAIN, false, true);
453         }
454         
455         if (ctD!=null) {
456             prln("Testing data chain...");
457             ctD.testOneChain(Marina.DATA_CHAIN, Infrastructure.SEVERITY_WARNING);
458             //ccD.resetInBits();
459             //ccD.shift(Marina.DATA_CHAIN, false, true);
460         }
461         
462         if (ctR!=null) {
463             prln("Testing report chain...");            
464             ctR.testOneChain(Marina.REPORT_CHAIN, Infrastructure.SEVERITY_WARNING);
465             //ccR.resetInBits();
466             //ccR.shift(Marina.REPORT_CHAIN, false, true);
467         }
468
469         if (ct!=null) {
470             prln("Testing control chain...");
471             ct.testOneChain(Marina.CONTROL_CHAIN, Infrastructure.SEVERITY_WARNING);
472             cc.resetInBits();
473             cc.shift(Marina.CONTROL_CHAIN, false, true);
474             prln("Testing data chain...");
475             ct.testOneChain(Marina.DATA_CHAIN, Infrastructure.SEVERITY_WARNING);
476             prln("Testing report chain...");            
477             ct.testOneChain(Marina.REPORT_CHAIN, Infrastructure.SEVERITY_WARNING);
478         }
479     }
480
481     private void testProperStoppers(Marina marina) {
482         prln("Begin testProperStoppers");
483         adjustIndent(2);
484
485         for(ProperStopper ps : new ProperStopper[] { marina.data, marina.instrIn }) {
486
487             prln("testing " + (ps == marina.data ? "data" : "instruction") + " stopper");
488             adjustIndent(2);
489
490             prln("un-stopping stopper");
491             ps.run();
492             fatal( ps.getStopped(), "stopper should not have been stopped, but was");
493
494             prln("stopping stopper");
495             ps.stop();
496             fatal( !ps.getStopped(), "stopper should have been stopped, but was not");
497
498             adjustIndent(-2);
499         }
500
501         adjustIndent(-2);
502     }
503
504     private void sendInstructions(Marina marina) {
505         prln("Begin sendInstructions");
506         adjustIndent(2);
507         
508         List<BitVector> din = new ArrayList<BitVector>();
509
510         BitVector count = new BitVector(MarinaPacket.WORD_WIDTH,"count");
511         BitVector one = new BitVector(MarinaPacket.WORD_WIDTH, "one");
512         count.setFromLong(0);
513         one.setFromLong(1);
514         for (int i=0; i<3; i++) {
515             din.add(count);
516             count = count.add(one);
517         }
518
519         for(BitVector d : din)
520             marina.instrIn.fill(new MarinaPacket(d, false, MarinaPacket.null_path));
521
522         adjustIndent(-2);
523         prln("End sendInstructions");
524     }
525
526     private void sendToken(Marina marina) {
527         prln("Begin sendToken");
528         adjustIndent(2);
529         
530         //getCtrsFlags(marina);
531         
532         int nbToks = marina.getNumTokens();
533         fatal(nbToks!=0, "Expected no tokens on initialization but got: "+nbToks+" tokens");
534
535         marina.instrIn.fill(setIlc(1));
536         marina.instrIn.fill(SEND_TOKEN);
537         nbToks = marina.getNumTokens();
538         fatal(nbToks!=1, "Expected one token to emerge but got: "+nbToks+" tokens");
539         
540         adjustIndent(-2);
541         prln("End sendToken");
542     }
543
544     private void sendData(Marina marina) {
545         prln("Begin sendData");
546         adjustIndent(2);
547         
548         edu.berkeley.fleet.api.BitVector bv = new edu.berkeley.fleet.api.BitVector(13);
549         for(int i=0; i<bv.length(); i+=2) bv.set(i, false);
550         MarinaPath path = new MarinaPath((MarinaFleet)dock.getShip().getFleet(), bv);
551
552         marina.instrIn.fill(setIlc(1));
553         marina.instrIn.fill(SEND_DATA);
554         
555         List<BitVector> dataItems = marina.data.drainMany();
556         fatal(dataItems.size()!=1, "Expected one data item to emerge but got: "+dataItems.size()+" data items");
557
558         MarinaPacket mp = new MarinaPacket(dataItems.get(0));
559         fatal(mp.tokenhood, "Expected tokenhood=data, but got tokenhood=token");
560
561         adjustIndent(-2);
562         prln("End sendData");
563     }
564
565     private void sendDataIlcInfinite(Marina marina) {
566         prln("Begin sendDataIlcInfinite");
567         adjustIndent(2);
568         
569         marina.fillSouthProperStopper(new Instruction[] {
570                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,SetSource.Infinity),
571                 SEND_DATA,
572             });
573         
574         // more than MAX_ILC
575         int howmany = 70;
576         List<BitVector> dataItems = marina.data.drainMany(howmany);
577         fatal(dataItems.size()!=howmany,
578               "Expected an unending supply of data items to emerge but only got got: "+dataItems.size());
579
580         adjustIndent(-2);
581         prln("End sendDataIlcInfinite");
582     }
583
584     private Instruction setOlc(int olc) {
585         return new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, olc);
586     }
587     private Instruction setOlcIfZero(int olc) {
588         return new Instruction.Set(dock,Predicate.Default,SetDest.OuterLoopCounter, olc);
589     }
590     private Instruction setIlc(int ilc) {
591         return new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, ilc);
592     }
593
594     private void testFlagD(Marina marina) {
595         prln("Begin testFlagD");
596         adjustIndent(2);
597         
598         List<BitVector> toks;
599
600         Predicate only_if_olc_zero    = Predicate.FlagD;
601         Predicate only_if_olc_nonzero = Predicate.Default;
602
603         marina.instrIn.fill(setIlc(1));
604
605         for(int olc : new int[] { 1, 0 }) {
606             for(boolean predicate_olc_nonzero : new boolean[] { true, false }) {
607                 prln("Attempting send data item with "+
608                      "olc=="+olc+" and "+
609                      "predicate olc"+(predicate_olc_nonzero?"!=0":"==0"));
610                 adjustIndent(2);
611
612                 marina.fillSouthProperStopper(new Instruction[] {
613                         setOlc(olc),
614                         new Instruction.Move(dock,
615                                              predicate_olc_nonzero  // predicate   
616                                              ? only_if_olc_nonzero
617                                              : only_if_olc_zero
618                                              ,
619                                              false,                 // torpedoable 
620                                              null,                  // path        
621                                              false,                 // tokenIn     
622                                              false,                 // dataIn      
623                                              false,                 // latchData   
624                                              false,                 // latchPath   
625                                              true,                  // dataOut     
626                                              false                  // tokenOut    
627                                              ),
628                     });
629                 expectNorthFifoExactly((predicate_olc_nonzero == (olc!=0)) ? 1 : 0);
630
631                 for(int i=0; i<olc; i++)
632                     marina.instrIn.fill(DEC);
633
634                 adjustIndent(-2);
635             }
636         }
637         adjustIndent(-2);
638         prln("End testFlagD");
639     }
640
641     private void testPredicationOnAB(Marina marina) {
642         prln("Begin testPredicationOnAB");
643         adjustIndent(2);
644         
645         List<BitVector> dItems;
646
647         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 1));
648         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, 1));
649
650         for(boolean flag_a : new boolean[] { false, true }) {
651             for(boolean flag_b : new boolean[] { false, true }) {
652                 prln("Setting flags, a="+flag_a+" b="+flag_b);
653                 marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,
654                                                         flag_a
655                                                         ? Instruction.Set.FlagFunction.ONE
656                                                         : Instruction.Set.FlagFunction.ZERO,
657                                                         flag_b
658                                                         ? Instruction.Set.FlagFunction.ONE
659                                                         : Instruction.Set.FlagFunction.ZERO
660                                                         ));
661                 getCtrsFlags(marina);
662
663                 adjustIndent(2);
664                 for(Predicate predicate : new Predicate[] {
665                         Predicate.Default,
666                         Predicate.FlagA,
667                         Predicate.FlagB,
668                         Predicate.NotFlagA,
669                         Predicate.NotFlagB,
670                     }) {
671
672                     prln("Attempting send data with a="+flag_a+", b="+flag_b+", predicate="+predicate);
673                     adjustIndent(2);
674                     marina.instrIn.fill(new Instruction.Move(dock,
675                                                              predicate,  // predicate   
676                                                              false,      // torpedoable 
677                                                              null,       // path        
678                                                              false,      // tokenIn     
679                                                              false,      // dataIn      
680                                                              false,      // latchData   
681                                                              false,      // latchPath   
682                                                              true,       // dataOut     
683                                                              false       // tokenOut    
684                                                              ));
685                     adjustIndent(-2);
686                     dItems = marina.data.drainMany();
687                     int expected = predicate.evaluate(flag_a, flag_b, false, false) ? 1 : 0;
688                     fatal(dItems.size()!=expected, "Expected "+expected+" data items to emerge but got: "+
689                           dItems.size()+" items(s)");
690                 }
691                 adjustIndent(-2);
692             }
693         }
694         adjustIndent(-2);
695         prln("End testPredicationOnAB");
696     }
697
698
699     private void showOlc() {
700         prln("OLC=="+marina.getOLC());
701     }
702     private void expectOlc(int x) {
703         int olc = marina.getOLC();
704         fatal(x!=olc, "expected OLC=="+x+", but scanned out OLC=="+olc);
705     }
706
707     private void getCtrsFlags(Marina marina) {
708         prln("begin getCtrsFlags");
709         adjustIndent(2);
710         
711         showOlc();
712         Ilc ilc = marina.getILC();
713         prln("ILC.done=="+ilc.getDone()+
714              " ILC.infinity=="+ilc.getInfinity()+
715              " ILC.count=="+ilc.getCount());
716         prln("flagA=="+marina.getFlagA());
717         prln("flagB=="+marina.getFlagB());
718         adjustIndent(-2);
719         prln("end getCtrsFlags");
720     }
721
722     private void walkOneOLC(Marina marina) {
723         prln("Begin walkOneOLC");
724         adjustIndent(2);
725         for (int i=0; i<6; i++) {
726
727             if (marina.kesselsCounter) {
728                 System.out.println("master-clearing...");
729                 // master clear on each iteration; otherwise we'd need to "run down" the olc
730                 marina.masterClear();
731                 marina.enableInstructionSend(true);
732             }
733
734             int inOlc = i==-1 ? 0 : (1<<i);
735             marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, inOlc));
736
737             model.waitNS(128 * CYCLE_TIME_NS);
738
739             expectOlc(inOlc);
740             prln("walkOneOLC: "+inOlc+" checks out");
741         }
742         adjustIndent(-2);
743         prln("End walkOneOLC");
744     }
745     private void walkOneILC(Marina marina) {
746         prln("Begin walkOneILC");
747         adjustIndent(2);
748         for (int i=0; i<6; i++) {
749             // Mask off the "zero" bit position
750             int inIlc = 1 << i;
751             prln("inIlc="+inIlc);
752             marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, inIlc));
753             Ilc ilc = marina.getILC();
754             int outIlc = ilc.getCount();
755             fatal(outIlc!=inIlc, "bad ILC count: "+outIlc+" expected: "+inIlc);
756             fatal(ilc.getInfinity(), "bad Infinity bit: true");
757         }
758         prln("Now test the infinity bit");
759         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, SetSource.Infinity));
760         Ilc ilc = marina.getILC();
761         fatal(!ilc.getInfinity(), "bad Infinity bit: false");
762         adjustIndent(-2);
763         prln("End walkOneILC");
764     }
765     private void countIlc(Marina marina) {
766         final int maxIlc = 63;
767         prln("Begin countIlc");
768         adjustIndent(2);
769         
770         marina.instrIn.fill(new 
771                             Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, maxIlc));
772
773         int ilc = marina.getILC().getCount();
774         fatal(ilc!=maxIlc, "bad ILC count: "+ilc+" expected: "+maxIlc);
775                 
776         prln("execute a move instruction that does nothing except decrement the ILC to zero");
777         marina.instrIn.fill(
778                             new Instruction.Move(dock,
779                                                  Predicate.IgnoreFlagD, // predicate   
780                                                  false,                 // torpedoable 
781                                                  null,                  // path        
782                                                  false,                 // tokenIn     
783                                                  false,                 // dataIn      
784                                                  false,                 // latchData   
785                                                  false,                 // latchPath   
786                                                  false,                 // dataOut     
787                                                  false                  // tokenOut    
788                                                  ));
789         
790         // wait for ILC to count from 63 to 0
791         model.waitNS(128 * CYCLE_TIME_NS);
792         //model.waitNS(10000);
793
794         prln("Check that ILC==0");
795         ilc = marina.getILC().getCount();
796         fatal(ilc!=0, "bad ILC count: "+ilc+" expected: "+0);
797         
798         adjustIndent(-2);
799         prln("End countIlc");
800     }
801     // Note: countOlc takes 44 minutes to run on nanosim
802     private void countOlc(Marina marina) {
803         int maxOlc = 63;
804         prln("Begin countOlc");
805         adjustIndent(2);
806
807         marina.instrIn.fill(setOlc(maxOlc));
808
809         for (int i=maxOlc; i>=0; i--) {
810             model.waitNS(128 * CYCLE_TIME_NS);
811             prln("OLC should be: "+i);
812             expectOlc(i);
813             marina.instrIn.fill(new 
814                                 Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, SetSource.Decrement));
815         }
816
817         adjustIndent(-2);
818         prln("End countOlc");
819     }
820     private void loadEveryValueOLC(Marina marina) {
821         adjustIndent(2);
822         for (int i=0; i<(1<<6); i++) {
823
824             if (marina.kesselsCounter) {
825                 System.out.println("master-clearing...");
826                 // master clear on each iteration; otherwise we'd need to "run down" the olc
827                 marina.masterClear();
828                 marina.enableInstructionSend(true);
829             }
830
831             int inOlc = i;
832             marina.fillSouthProperStopper(new Instruction[] {
833
834                     // to ensure that instruction is bubble-limited
835                     RECV_DATA,
836
837                     // the Set-OLC instruction
838                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, inOlc),
839
840                     // put a Set-ILC instruction right behind it with inverted bits to be sure we're
841                     // not capturing the instruction-latch value too late in the cycle
842                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, (inOlc ^ (~(-1<<6)))),
843                     
844                 });
845
846             model.waitNS(128 * CYCLE_TIME_NS);
847             marina.fillNorthProperStopper();
848             model.waitNS(128 * CYCLE_TIME_NS);
849
850             expectOlc(inOlc);
851             prln("loadEveryValueOLC: "+inOlc+" checks out");
852         }
853         adjustIndent(-2);
854     }
855
856     private void saturateInstructionFifo(Marina marina, Instruction instruction, int quantity, boolean expect_it_to_jam_up) {
857         prln("Inserting "+quantity+" copies of \"" + instruction + "\"");
858         adjustIndent(2);
859         int i=0;
860         for(i=0; i<quantity; i++) {
861             prln("Inserting instruction " + (i+1) +"/"+ quantity);
862             marina.instrIn.fill(instruction);
863             boolean jammed = (marina.instrIn.getFillStateWire()==MarinaUtils.StateWireState.FULL);
864             if (jammed && expect_it_to_jam_up) {
865                 prln("Stopper remained full after inserting instruction; this was expected; we are happy.");
866                 adjustIndent(-2);
867                 return;
868             }
869             fatal(jammed, "Instruction stopper did not drain after inserting " + (i+1) + " instructions; not good!");
870         }
871         fatal(expect_it_to_jam_up, "Expected instruction stopper to jam up, but it did not");
872         adjustIndent(-2);
873         prln("Successfully inserted " + i + " instructions");
874     }
875
876     private static MarinaPath null_path = new MarinaPath((MarinaFleet)dock.getShip().getFleet(),
877                                                          MarinaUtils.sunToBerk(MarinaPacket.null_path));
878
879     private static final Instruction DEC = 
880         new Instruction.Set(dock,Predicate.Default,SetDest.OuterLoopCounter, SetSource.Decrement);
881
882     private static final Instruction FLAG_NOP =
883         new Instruction.Set(dock, Predicate.IgnoreFlagD,
884                             CLEAR_FLAG.add(Predicate.FlagA),
885                             CLEAR_FLAG.add(Predicate.FlagB));
886
887     private static final Instruction NOP =
888         new Instruction.Move(dock,
889                              Predicate.IgnoreFlagD,   /* predicate   */
890                              false,                 /* torpedoable */
891                              null,                  /* path        */
892                              false,                 /* tokenIn     */
893                              false,                 /* dataIn      */
894                              false,                 /* latchData   */
895                              false,                 /* latchPath   */
896                              false,                 /* dataOut     */
897                              false                  /* tokenOut    */
898                              );
899
900     private static final Instruction SEND_DATA =
901         new Instruction.Move(dock,
902                              Predicate.IgnoreFlagD,   /* predicate   */
903                              false,                 /* torpedoable */
904                              null_path,                  /* path        */
905                              false,                 /* tokenIn     */
906                              false,                 /* dataIn      */
907                              false,                 /* latchData   */
908                              false,                 /* latchPath   */
909                              true,                  /* dataOut     */
910                              false                  /* tokenOut    */
911                              );
912
913     private static final Instruction SEND_DATA_IF_D_NOT_SET =
914         new Instruction.Move(dock,
915                              Predicate.Default,     /* predicate   */
916                              false,                 /* torpedoable */
917                              null_path,             /* path        */
918                              false,                 /* tokenIn     */
919                              false,                 /* dataIn      */
920                              false,                 /* latchData   */
921                              false,                 /* latchPath   */
922                              true,                  /* dataOut     */
923                              false                  /* tokenOut    */
924                              );
925
926     private static final Instruction SEND_DATA_IF_D_SET =
927         new Instruction.Move(dock,
928                              Predicate.FlagD,       /* predicate   */
929                              false,                 /* torpedoable */
930                              null_path,             /* path        */
931                              false,                 /* tokenIn     */
932                              false,                 /* dataIn      */
933                              false,                 /* latchData   */
934                              false,                 /* latchPath   */
935                              true,                  /* dataOut     */
936                              false                  /* tokenOut    */
937                              );
938
939     private static final Instruction TORPEDOABLE_RECV_DATA =
940         new Instruction.Move(dock,
941                              Predicate.IgnoreFlagD, /* predicate   */
942                              true,                  /* torpedoable */
943                              null,                  /* path        */
944                              false,                 /* tokenIn     */
945                              true,                  /* dataIn      */
946                              true,                  /* latchData   */
947                              false,                 /* latchPath   */
948                              false,                 /* dataOut     */
949                              false                  /* tokenOut    */
950                              );
951
952     private static final Instruction RECV_DATA =
953         new Instruction.Move(dock,
954                              Predicate.IgnoreFlagD,   /* predicate   */
955                              false,                 /* torpedoable */
956                              null,                  /* path        */
957                              false,                 /* tokenIn     */
958                              true,                  /* dataIn      */
959                              true,                  /* latchData   */
960                              false,                 /* latchPath   */
961                              false,                 /* dataOut     */
962                              false                  /* tokenOut    */
963                              );
964
965     private static final Instruction SEND_TOKEN =
966         new Instruction.Move(dock,
967                              Predicate.IgnoreFlagD,   /* predicate   */
968                              false,                 /* torpedoable */
969                              null_path,                  /* path        */
970                              false,                 /* tokenIn     */
971                              false,                 /* dataIn      */
972                              false,                 /* latchData   */
973                              false,                 /* latchPath   */
974                              false,                 /* dataOut     */
975                              true                   /* tokenOut    */
976                              );
977
978     private static final Instruction RECV_TOKEN =
979         new Instruction.Move(dock,
980                              Predicate.IgnoreFlagD,   /* predicate   */
981                              false,                 /* torpedoable */
982                              null,                  /* path        */
983                              true,                  /* tokenIn     */
984                              false,                 /* dataIn      */
985                              false,                 /* latchData   */
986                              false,                 /* latchPath   */
987                              false,                 /* dataOut     */
988                              false                  /* tokenOut    */
989                              );
990
991
992     private void expectNorthFifoNoMoreThan(int num) {
993         model.waitNS(128 * CYCLE_TIME_NS);
994         List<BitVector> dataItems = marina.data.drainMany(num+1);
995         fatal(dataItems.size()>num,
996               "Expected no more than "+num+
997               " data items to emerge but got at least: "+dataItems.size());
998     }
999     private void expectNorthFifoExactly(int num) {
1000         model.waitNS(128 * CYCLE_TIME_NS);
1001         List<BitVector> dataItems = marina.data.drainMany(num+1);
1002         fatal(dataItems.size()!=num,
1003               "Expected exactly "+num+
1004               " data items to emerge but got at least: "+dataItems.size());
1005     }
1006     private void expectNorthFifoAtLeast(int num) {
1007         model.waitNS(128 * CYCLE_TIME_NS);
1008         List<BitVector> dataItems = marina.data.drainMany(num);
1009         fatal(dataItems.size()<num,
1010               "Expected at least "+num+
1011               " data items to emerge but got only: "+dataItems.size());
1012     }
1013     private void expectTokensNoMoreThan(int num) {
1014         int x = marina.getNumTokens();
1015         List<BitVector> dataItems = marina.data.drainMany(num+1);
1016         fatal(x>num,
1017               "Expected no more than "+num+
1018               " tokens to emerge but got at least: "+x);
1019     }
1020     private void expectTokensExactly(int num) {
1021         int x = marina.getNumTokens();
1022         fatal(x!=num,
1023               "Expected exactly "+num+
1024               " tokens but got at least: "+x);
1025     }
1026
1027     private void testFlagDRecomputationTime(Marina marina) {
1028         marina.instrIn.fill(setIlc(1));
1029         marina.fillSouthProperStopper(new Instruction[] {
1030                 RECV_DATA,
1031                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,0),
1032                 SEND_DATA_IF_D_NOT_SET
1033             });
1034         marina.fillNorthProperStopper();
1035         expectNorthFifoNoMoreThan(0);
1036
1037         marina.fillSouthProperStopper(new Instruction[] {
1038                 RECV_DATA,
1039                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,1),
1040                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,SetSource.Decrement),
1041                 SEND_DATA_IF_D_NOT_SET
1042             });
1043         marina.fillNorthProperStopper();
1044         expectNorthFifoNoMoreThan(0);
1045
1046         marina.fillSouthProperStopper(new Instruction[] {
1047                 RECV_DATA,
1048                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,2),
1049                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,SetSource.Decrement),
1050                 SEND_DATA_IF_D_NOT_SET
1051             });
1052         marina.fillNorthProperStopper();
1053         expectNorthFifoExactly(1);
1054         marina.instrIn.fill(DEC);
1055
1056         marina.fillSouthProperStopper(new Instruction[] {
1057                 RECV_DATA,
1058                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,0),
1059                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,1),
1060                 SEND_DATA_IF_D_NOT_SET
1061             });
1062         marina.fillNorthProperStopper();
1063         expectNorthFifoExactly(1);
1064     }
1065
1066     private void testTailWaitsForHead(Marina marina) {
1067         marina.instrIn.fill(setIlc(1));
1068         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 63));
1069         
1070         marina.enableInstructionSend(false);
1071         marina.enableInstructionRecirculate(true);
1072         
1073         marina.instrIn.fill(TORPEDOABLE_RECV_DATA);
1074         marina.instrIn.fill(new Instruction.Head(dock));
1075         marina.instrIn.fill(SEND_DATA);
1076         marina.instrIn.fill(TORPEDOABLE_RECV_DATA);
1077         marina.instrIn.fill(SEND_TOKEN);
1078         marina.instrIn.fill(TORPEDOABLE_RECV_DATA);
1079         marina.instrIn.fill(new Instruction.Tail(dock));
1080         marina.instrIn.fillTorpedo();
1081         
1082         marina.enableInstructionRecirculate(false);
1083         marina.enableInstructionSend(true);
1084         marina.instrIn.run();
1085         
1086         expectNorthFifoNoMoreThan(0);
1087         prln("inserting into north proper stopper");
1088         marina.fillNorthProperStopper();
1089         expectNorthFifoExactly(1);
1090         int nbToks = marina.getNumTokens();
1091         fatal(nbToks!=1, "Expected one token to emerge but got: "+nbToks+" tokens");
1092     }
1093
1094         /*
1095         marina.instrIn.fill(setIlc(1));
1096         marina.instrIn.fill(setOlc(1));
1097
1098         // this makes the head wait for the torpedo
1099         marina.instrIn.fill(TORPEDOABLE_RECV_DATA);
1100
1101         // the head should wait for the tail
1102         marina.instrIn.fill(new Instruction.Head(dock));
1103         marina.instrIn.fill(NOP);
1104         marina.instrIn.fill(SEND_DATA);
1105         marina.instrIn.fill(RECV_DATA);
1106
1107         expectNorthFifoNoMoreThan(0);
1108
1109         marina.instrIn.fillTorpedo();
1110         expectNorthFifoNoMoreThan(0);
1111
1112         marina.instrIn.fill(new Instruction.Tail(dock));
1113         expectNorthFifoExactly(1);
1114         */
1115
1116     private void testTailWithoutHead(Marina marina) {
1117         marina.instrIn.fill(setIlc(1));
1118         marina.fillSouthProperStopper(new Instruction[] {
1119                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 63),
1120                 new Instruction.Tail(dock),
1121                 SEND_DATA,
1122             });
1123         List<BitVector> dataItems = marina.data.drainMany(1);
1124         fatal(dataItems.size()!=0, "Expected exactly no data items to emerge but got at least: "+dataItems.size());
1125     }
1126
1127     private void testHeadWaitsForTail(Marina marina) {
1128         List<BitVector> dataItems;
1129
1130         prln("Begin testHeadWaitsForTail");
1131         adjustIndent(2);
1132
1133         marina.instrIn.fill(setIlc(1));
1134         marina.fillSouthProperStopper(new Instruction[] {
1135                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 63),
1136                 new Instruction.Head(dock),
1137                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1),
1138                 SEND_DATA,
1139             });
1140
1141         expectNorthFifoExactly(0);
1142         marina.instrIn.fill(new Instruction.Tail(dock));
1143         expectNorthFifoAtLeast(1);
1144
1145         adjustIndent(-2);
1146         prln("End testHeadWaitsForTail");
1147     }
1148
1149     private void testNonTorpedoableMoveDoesNotResetDFlag(Marina marina) {
1150         marina.instrIn.fill(setIlc(1));
1151         marina.fillSouthProperStopper(new Instruction[] {
1152                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,63),
1153                 new Instruction.Move(dock,
1154                                      Predicate.IgnoreFlagD, // predicate   
1155                                      true,                  // torpedoable 
1156                                      null,                  // path        
1157                                      true,                  // tokenIn     
1158                                      false,                 // dataIn      
1159                                      false,                 // latchData   
1160                                      false,                 // latchPath   
1161                                      false,                 // dataOut     
1162                                      false                  // tokenOut    
1163                                      ),
1164                 new Instruction.Move(dock,
1165                                      Predicate.FlagD,       // predicate   
1166                                      false,                 // torpedoable 
1167                                      null,                  // path        
1168                                      false,                 // tokenIn     
1169                                      false,                 // dataIn      
1170                                      false,                 // latchData   
1171                                      false,                 // latchPath   
1172                                      true,                  // dataOut     
1173                                      false                  // tokenOut    
1174                                      ),
1175             });
1176         marina.instrIn.fillTorpedo();
1177         expectNorthFifoExactly(1);
1178         marina.fillSouthProperStopper(new Instruction[] {
1179                 new Instruction.Move(dock,
1180                                      Predicate.Default,     // predicate   
1181                                      false,                 // torpedoable 
1182                                      null,                  // path        
1183                                      false,                 // tokenIn     
1184                                      false,                 // dataIn      
1185                                      false,                 // latchData   
1186                                      false,                 // latchPath   
1187                                      true,                  // dataOut     
1188                                      false                  // tokenOut    
1189                                      ),
1190             });
1191         expectNorthFifoNoMoreThan(0);
1192     }
1193
1194     private void testAbort(Marina marina) {
1195         
1196         marina.instrIn.fill(setIlc(1));
1197         marina.fillSouthProperStopper(new Instruction[] {
1198                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.DataLatch,1),
1199                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,2),
1200                 SEND_DATA_IF_D_NOT_SET,
1201                 new Instruction.Head(dock),
1202                 SEND_DATA_IF_D_NOT_SET,
1203                 new Instruction.Set(dock,Predicate.Default,SetDest.DataLatch,2),
1204                 new Instruction.Abort(dock, Predicate.FlagD),
1205                 new Instruction.Set(dock,Predicate.Default,SetDest.OuterLoopCounter,SetSource.Decrement),
1206                 new Instruction.Tail(dock),
1207             }, true);
1208         
1209         for(int i=0; i<4; i++) {
1210             BitVector bv;
1211             
1212             model.waitNS(128 * CYCLE_TIME_NS);
1213             bv = new MarinaPacket(marina.data.drain()).data.bitReverse();
1214             fatal(bv==null, "no data item found");
1215             prln("got " + bv.toLong());
1216             fatal(bv.toLong()!=1, "expected 1, got " + bv.toLong());
1217             
1218             model.waitNS(128 * CYCLE_TIME_NS);
1219             bv = new MarinaPacket(marina.data.drain()).data.bitReverse();
1220             fatal(bv==null, "no data item found");
1221             prln("got " + bv.toLong());
1222             fatal(bv.toLong()!=1, "expected 1, got " + bv.toLong());
1223             
1224             model.waitNS(128 * CYCLE_TIME_NS);
1225             bv = new MarinaPacket(marina.data.drain()).data.bitReverse();
1226             fatal(bv==null, "no data item found");
1227             prln("got " + bv.toLong());
1228             fatal(bv.toLong()!=2, "expected 2, got " + bv.toLong());
1229             
1230         }
1231     }
1232
1233     private void testAbortOutsideOfLoop(Marina marina) {
1234         marina.instrIn.fill(setIlc(1));
1235         marina.fillSouthProperStopper(new Instruction[] {
1236                 // ensure that an abort doesn't cause problems if no loop is in progress
1237                 new Instruction.Abort(dock, Predicate.IgnoreFlagD),
1238                 SEND_DATA,
1239             });
1240         expectNorthFifoExactly(1);
1241     }
1242
1243     private void testFlagAB(Marina marina) {
1244         prln("Begin testFlagAB");
1245         adjustIndent(2);
1246
1247         Instruction.Set.FlagFunction zero = Instruction.Set.FlagFunction.ZERO;
1248         Instruction.Set.FlagFunction one  = zero;
1249
1250         
1251         // we should be able to use any pair of FlagX+NotFlagX,
1252         // but we toss them all in to maximize the chances of the
1253         // test passing (later we will try the individual
1254         // combinations to maximize the chances of failure).
1255         one = one.add(Predicate.FlagA);
1256         one = one.add(Predicate.NotFlagA);
1257         one = one.add(Predicate.FlagB);
1258         one = one.add(Predicate.NotFlagB);
1259         one = one.add(Predicate.FlagC);
1260         one = one.add(Predicate.NotFlagC);
1261
1262         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1));
1263         for(boolean fast : new boolean[] { true, false }) {
1264             // clear the flags to a known state, then check both 0->1 and 1->0 transitions
1265             for(boolean b : new boolean[] { false, true, false }) {
1266                 prln("state: a="+marina.getFlagA()+", b="+marina.getFlagB());
1267                 prln((b?"Setting":"Clearing")+" flags");
1268                 
1269                 Instruction inst = new Instruction.Set(dock,Predicate.IgnoreFlagD,
1270                                                        b ? one : zero,
1271                                                        b ? one : zero
1272                                                        );
1273                 if (fast) {
1274                     marina.fillSouthProperStopper(new Instruction[] {
1275                             RECV_DATA,
1276                             inst,
1277                             NOP,
1278                         });
1279                     model.waitNS(64 * CYCLE_TIME_NS);
1280                     marina.fillNorthProperStopper();
1281                 } else {
1282                     marina.instrIn.fill(inst);
1283                 }
1284
1285                 fatal(marina.getFlagA()!=b,
1286                       "after "+(b?"setting":"clearing")+" FlagA, it was still "+(b?"clear":"set"));
1287                 fatal(marina.getFlagB()!=b,
1288                       "after "+(b?"setting":"clearing")+" FlagB, it was still "+(b?"clear":"set"));
1289             }
1290         }
1291
1292         adjustIndent(-2);
1293         prln("End testFlagAB");         
1294     }
1295
1296     /**
1297      *  WARNING: this is a very, very, very long test case -- it goes
1298      *  through 216 iterations.
1299      */
1300     private void testFlagTruthTable(Marina marina) {
1301         prln("Begin testFlagTruthTable");
1302         adjustIndent(2);
1303
1304         marina.instrIn.fill(setIlc(1));
1305         Instruction.Set.FlagFunction zero = Instruction.Set.FlagFunction.ZERO;
1306         Instruction.Set.FlagFunction one  = zero.add(Predicate.FlagA).add(Predicate.NotFlagA);
1307
1308         for(Predicate a_input : new Predicate[] { null, Predicate.FlagA, Predicate.NotFlagA })
1309             for(Predicate b_input : new Predicate[] { null, Predicate.FlagB, Predicate.NotFlagB })
1310                 for(Predicate c_input : new Predicate[] { null, Predicate.FlagC, Predicate.NotFlagC })
1311                     for(boolean a_state : new boolean[] { false, true })
1312                         for(boolean b_state : new boolean[] { false, true })
1313                             for(boolean c_state : new boolean[] { false, true }) {
1314                                 for(boolean which : new boolean[] { false, true }) {
1315
1316                                     prln("before instruction: a="+a_state+", b="+b_state+", c="+c_state);
1317                                     // set A,B flags to a_state and b_state
1318                                     marina.instrIn.fill(new 
1319                                                         Instruction.Set(dock,Predicate.IgnoreFlagD,
1320                                                                         a_state ? one : zero,
1321                                                                         b_state ? one : zero
1322                                                                         ));
1323                                     
1324                                     // set C flag to c_state
1325                                     BitVector data = new BitVector(37, "empty");
1326                                     BitVector addr = new BitVector(14, "empty");
1327                                     for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1328                                     for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1329                                     addr.set(Marina.INDEX_OF_ADDRESS_BIT_COPIED_TO_C_FLAG_WHEN_DC_EQUALS_ONE, c_state);
1330                                     marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1331                                     marina.instrIn.fill(RECV_DATA);
1332                                     
1333                                     Instruction.Set.FlagFunction func = zero;
1334                                     if (a_input!=null) func = func.add(a_input);
1335                                     if (b_input!=null) func = func.add(b_input);
1336                                     if (c_input!=null) func = func.add(c_input);
1337
1338                                     Instruction inst = new 
1339                                         Instruction.Set(dock,Predicate.IgnoreFlagD,
1340                                                         !which ? func : zero.add(Predicate.FlagA),
1341                                                         which  ? func : zero.add(Predicate.FlagB)
1342                                                         );
1343
1344                                     marina.instrIn.fill(inst);
1345
1346                                     boolean expected_a = !which ? func.evaluate(a_state, b_state, c_state, false) : a_state;
1347                                     boolean expected_b =  which ? func.evaluate(a_state, b_state, c_state, false) : b_state;
1348                                     fatal(expected_a != marina.getFlagA(),
1349                                           "expected A="+expected_a+", but got "+marina.getFlagA());
1350                                     fatal(expected_b != marina.getFlagB(),
1351                                           "expected B="+expected_b+", but got "+marina.getFlagB());
1352                                 }
1353                             }
1354         adjustIndent(-2);
1355         prln("End testFlagTruthTable");         
1356     }
1357
1358     private void recvData(Marina marina) {
1359         prln("Begin recvData");
1360         adjustIndent(2);
1361
1362         marina.instrIn.fill(setIlc(1));
1363         marina.fillSouthProperStopper(new Instruction[] {
1364                 new Instruction.Set(dock,Predicate.IgnoreFlagD, CLEAR_FLAG, CLEAR_FLAG),
1365                 new Instruction.Move(dock,
1366                                      Predicate.IgnoreFlagD, // predicate   
1367                                      false,                 // torpedoable 
1368                                      null,                  // path        
1369                                      false,                 // tokenIn     
1370                                      true,                  // dataIn      
1371                                      false,                 // latchData   
1372                                      false,                 // latchPath   
1373                                      false,                 // dataOut     
1374                                      false                  // tokenOut    
1375                                      ),
1376                 new Instruction.Set(dock,Predicate.IgnoreFlagD, SET_FLAG, SET_FLAG),
1377             });
1378         model.waitNS(64 * CYCLE_TIME_NS);
1379
1380         prln("checking to confirm that A flag is cleared");
1381         fatal(marina.getFlagA(), "bad A flag: "+marina.getFlagA());
1382         
1383         prln("inserting data item in north fifo ring");
1384         BitVector data = new BitVector(37, "empty");
1385         BitVector addr = new BitVector(14, "empty");
1386         for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1387         for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1388         marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1389         model.waitNS(64 * CYCLE_TIME_NS);
1390
1391         prln("checking to see if A flag got set");
1392         fatal(!marina.getFlagA(), "bad A flag: "+marina.getFlagA());
1393
1394         adjustIndent(-2);
1395         prln("End recvData");           
1396     }
1397
1398
1399     private void testRecvAndSendWalkingOne(Marina marina) {
1400         prln("Begin testRecvAndSendWalkingOne");
1401         adjustIndent(2);
1402
1403         marina.instrIn.fill(setIlc(1));
1404
1405         List<BitVector> dataItems;
1406         for(int bit=0; bit<37; bit++) {
1407
1408             BitVector data = new BitVector(37, "empty");
1409             BitVector addr = new BitVector(14, "empty");
1410             for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1411             for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1412             data.set(bit, true);
1413             prln("testing with bit pattern " + data);
1414
1415             prln("inserting data item into north fifo ring");
1416             marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1417
1418             prln("stopping the north proper stopper");
1419             marina.data.stop();
1420
1421             dataItems = marina.data.drainMany(1);
1422             fatal(dataItems.size()!=0,
1423                   "found a data item waiting in the north proper stopper, but should not have");
1424
1425             marina.instrIn.fill(new Instruction.Move(dock,
1426                                                      Predicate.IgnoreFlagD,  // predicate   
1427                                                      false,                  // torpedoable 
1428                                                      null_path,              // path        
1429                                                      false,                  // tokenIn     
1430                                                      true,                   // dataIn      
1431                                                      true,                   // latchData   
1432                                                      false,                  // latchPath   
1433                                                      true,                   // dataOut     
1434                                                      false                   // tokenOut    
1435                                                      ));
1436
1437             dataItems = marina.data.drainMany(2);
1438             fatal(dataItems.size()!=1,
1439                   "found "+dataItems.size()+" data items in north fifo; expected one");
1440             MarinaPacket mp = new MarinaPacket(dataItems.get(0));
1441             fatalIfBitVectorsDoNotMatch(mp.data, data);
1442         }
1443
1444         adjustIndent(-2);
1445         prln("End testRecvAndSendWalkingOne");          
1446     }
1447
1448
1449
1450     private void setOlcFromDataLatch(Marina marina) {
1451         prln("Begin setOlcFromDataLatch");
1452         adjustIndent(2);
1453
1454         marina.instrIn.fill(setIlc(1));
1455
1456         // walk a bit from 0 to 5
1457         for(int bit=0; bit<6; bit++) {
1458             prln("inserting data item in north fifo ring");
1459             BitVector data = new BitVector(37, "empty");
1460             BitVector addr = new BitVector(14, "empty");
1461             for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1462             for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1463             data.set(bit, true);
1464             marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1465
1466             marina.fillSouthProperStopper(new Instruction[] {
1467                     RECV_DATA,
1468                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,SetSource.DataLatch),
1469                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.DataLatch,-1),
1470                 });
1471
1472             model.waitNS(CYCLE_TIME_NS * 64);
1473
1474             expectOlc(1<<bit);
1475
1476             if (marina.kesselsCounter) {
1477                 // master clear on each iteration; otherwise we'd need to "run down" the olc
1478                 marina.masterClear();
1479                 marina.enableInstructionSend(true);
1480             }
1481         }
1482
1483         adjustIndent(-2);
1484         prln("End setOlcFromDataLatch");        
1485     }
1486
1487     private void setIlcFromDataLatch(Marina marina) {
1488         prln("Begin setIlcFromDataLatch");
1489         adjustIndent(2);
1490
1491         marina.instrIn.fill(setIlc(1));
1492
1493         // walk a bit from 0 to 5
1494         for(int bit=5; bit>=0; bit--) {
1495             prln("inserting data item in north fifo ring");
1496             BitVector data = new BitVector(37, "empty");
1497             BitVector addr = new BitVector(14, "empty");
1498             for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1499             for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1500             data.set(bit, true);
1501             marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1502
1503             marina.fillSouthProperStopper(new Instruction[] {
1504                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1),
1505                     RECV_DATA,
1506                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,SetSource.DataLatch),
1507                 });
1508             model.waitNS(CYCLE_TIME_NS * 64);
1509
1510             int ilc = marina.getILC().getCount();
1511             fatal(ilc != (1<<bit), "expected ilc to be " + (1<<bit) + ", but got " + ilc); 
1512         }
1513
1514         adjustIndent(-2);
1515         prln("End setIlcFromDataLatch");        
1516     }
1517
1518     private void testSendAndRecvToken(Marina marina) {
1519         prln("Begin testSendAndRecvToken");
1520         adjustIndent(2);
1521
1522         marina.instrIn.fill(setIlc(1));
1523         marina.fillSouthProperStopper(new Instruction[] {
1524                 SEND_TOKEN,
1525                 RECV_TOKEN,
1526                 SEND_DATA,
1527             });
1528         expectNorthFifoExactly(1);
1529
1530         adjustIndent(-2);
1531         prln("End testSendAndRecvToken");       
1532     }
1533
1534     private void testSignExtendedLiteral(Marina marina) {
1535         prln("Begin testSignExtendedLiteral");
1536         adjustIndent(2);
1537
1538         marina.instrIn.fill(setIlc(1));
1539         for(long val : new long[] { (-1L << 14), -1, 0, 1 }) {
1540
1541             marina.fillSouthProperStopper(new Instruction[] {
1542                     new Instruction.Set(dock,Predicate.IgnoreFlagD,
1543                                         Instruction.Set.SetDest.DataLatch,
1544                                         val),
1545                     SEND_DATA,
1546                 });
1547             model.waitNS(CYCLE_TIME_NS * 64);
1548
1549             List<BitVector> dataItems = marina.data.drainMany(3);
1550             fatal(dataItems.size()!=1, "expected exactly one data item, got " + dataItems.size());
1551
1552             MarinaPacket mp = new MarinaPacket(dataItems.get(0));
1553             BitVector bv = mp.data;
1554             prln("got back " + mp);
1555
1556             boolean mismatch = false;
1557             String err = "";
1558             for(int i=0; i<37; i++) {
1559                 if (bv.get(i) != ( (val & (1L << i)) != 0 )) {
1560                     mismatch = true;
1561                     err += ""+i+", ";
1562                 }
1563             }
1564             fatal(mismatch, "data read back did not match inserted literal; mismatch on bits " + err);
1565         }
1566                 
1567         adjustIndent(-2);
1568         prln("End testSignExtendedLiteral");            
1569     }
1570
1571     private void testShiftedLiteral(Marina marina) {
1572         prln("Begin testShiftedLiteral");
1573         adjustIndent(2);
1574
1575         marina.instrIn.fill(setIlc(1));
1576         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,
1577                                                 Instruction.Set.SetDest.DataLatch,
1578                                                 0));
1579
1580         BitVector dreg = new BitVector(37, "what we think is in the d-register");
1581         for(int i=0; i<37; i++) dreg.set(i, false);
1582
1583         for(long val : new long[] { -1, 0, 1, (-1L << 18) }) {
1584
1585             edu.berkeley.fleet.api.BitVector immediate =
1586                 new edu.berkeley.fleet.api.BitVector(19);
1587             for(int i=0; i<immediate.length(); i++)
1588                 immediate.set(i, (val & (1L << i)) != 0);
1589
1590             // shift over 19 LSB's towards MSB
1591             for(int i=0; i<19; i++)
1592                 if (i+19 <= 36)  dreg.set(i+19, dreg.get(i));
1593             for(int i=0; i<19; i++)
1594                 dreg.set(i, immediate.get(i));
1595
1596             marina.fillSouthProperStopper(new Instruction[] {
1597                     new Instruction.Shift(dock,Predicate.IgnoreFlagD,immediate),
1598                     SEND_DATA,
1599                 });
1600
1601             model.waitNS(CYCLE_TIME_NS * 64);
1602             List<BitVector> dataItems = marina.data.drainMany(3);
1603             fatal(dataItems.size()!=1, "expected exactly one data item, got " + dataItems.size());
1604
1605             BitVector bv = new MarinaPacket(dataItems.get(0)).data;
1606             fatal(!bv.equals(dreg), "data read back did not match inserted literal.\n" +
1607                   "got:     "+bv.bitReverse().getState()+"\n"+
1608                   "expected:"+dreg.bitReverse().getState());
1609         }
1610                 
1611         adjustIndent(-2);
1612         prln("End testShiftedLiteral");         
1613     }
1614
1615     private void testFlagC(Marina marina) {
1616         prln("Begin testFlagC");
1617         adjustIndent(2);
1618
1619         // Russell says:
1620         // addr[14] == sigS
1621         // addr[1]  == sigA
1622         //
1623         // Adam says:
1624         // Dc=1 => sigS is copied into C-flag
1625         // Dc=0 => sigA is copied into C-flag
1626        
1627         marina.instrIn.fill(setIlc(1));
1628         for(boolean dc : new boolean[] { false, true }) {
1629             for(boolean c_flag : new boolean[] { true, false, true }) {
1630
1631                 prln("");
1632                 prln("****** checking case where dc="+dc+", cflag="+c_flag);
1633                 BitVector data = new BitVector(37, "empty");
1634                 BitVector addr = new BitVector(14, "empty");
1635                 for(int i=0; i<data.getNumBits(); i++) data.set(i, false);
1636                 for(int i=0; i<addr.getNumBits(); i++) addr.set(i, false);
1637
1638                 int whichbit = dc
1639                     ? Marina.INDEX_OF_ADDRESS_BIT_COPIED_TO_C_FLAG_WHEN_DC_EQUALS_ONE
1640                     : Marina.INDEX_OF_ADDRESS_BIT_COPIED_TO_C_FLAG_WHEN_DC_EQUALS_ZERO;
1641                 prln("setting addr["+whichbit+"] to "+(c_flag?"1":"0"));
1642                 addr.set(whichbit, c_flag);
1643
1644                 prln("... and filling north fifo proper stopper");
1645                 marina.fillNorthProperStopper(new MarinaPacket(data, false, addr));
1646                 
1647                 prln("clearing flags");
1648                 prln("executing recv data with Dc="+dc);
1649                 prln("copying c-flag to a-flag");
1650                 marina.fillSouthProperStopper(new Instruction[] {
1651                         new Instruction.Set(dock,Predicate.IgnoreFlagD, CLEAR_FLAG, CLEAR_FLAG),
1652                         new Instruction.Move(dock,
1653                                              Predicate.IgnoreFlagD,   /* predicate   */
1654                                              true,                  /* torpedoable */
1655                                              null,                  /* path        */
1656                                              false,                 /* tokenIn     */
1657                                              true,                  /* dataIn      */
1658                                              dc,                    /* latchData   */
1659                                              false,                 /* latchPath   */
1660                                              false,                 /* dataOut     */
1661                                              false                  /* tokenOut    */
1662                                              ),
1663                         FLAG_NOP,
1664                         new Instruction.Set(dock,Predicate.IgnoreFlagD,
1665                                             Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagC),
1666                                             CLEAR_FLAG
1667                                             ),
1668                     });
1669
1670                 model.waitNS(CYCLE_TIME_NS * 64);
1671                 
1672                 prln("checking to confirm that A flag is " + c_flag);
1673                 fatal(marina.getFlagA()!=c_flag, "bad A flag: "+marina.getFlagA());
1674             }
1675         }
1676         adjustIndent(-2);
1677         prln("End testFlagC");          
1678     }
1679
1680     private void sendDataWithPath(Marina marina) {
1681         prln("Begin sendDataWithPath");
1682         adjustIndent(2);
1683
1684         edu.berkeley.fleet.api.BitVector bv = new edu.berkeley.fleet.api.BitVector(13);
1685         marina.instrIn.fill(setIlc(1));
1686
1687         // alternating ones and zeroes
1688         for(int i=0; i<bv.length(); i+=2)
1689             bv.set(i, true);
1690         // and then ones in the lower four bits so it's not symmetric
1691         for(int i=0; i<4; i++)
1692             bv.set(i, true);
1693
1694         MarinaPath path = new MarinaPath((MarinaFleet)dock.getShip().getFleet(), bv);
1695
1696         marina.fillSouthProperStopper(new Instruction[] {
1697                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1),
1698                 new Instruction.Move(dock,
1699                                      Predicate.IgnoreFlagD, /* predicate   */
1700                                      false,                 /* torpedoable */
1701                                      path,                  /* path        */
1702                                      false,                 /* tokenIn     */
1703                                      false,                 /* dataIn      */
1704                                      false,                 /* latchData   */
1705                                      false,                 /* latchPath   */
1706                                      true,                  /* dataOut     */
1707                                      false                  /* tokenOut    */
1708                                      ),
1709             });
1710
1711         List<BitVector> dataItems;
1712         MarinaPacket mp;
1713
1714         dataItems = marina.data.drainMany();
1715         fatal(dataItems.size()!=1, "Expected one data item to emerge but got: "+dataItems.size()+" data items");
1716         mp = new MarinaPacket(dataItems.get(0));
1717
1718         // the 14th bit of the outbound address cannot be set by the
1719         // ship, so we don't care about it
1720         fatalIfBitVectorsDoNotMatch(MarinaUtils.berkToSun(bv), mp.path.get(0,13));
1721
1722         prln("send data with no change to path");
1723         marina.instrIn.fill(new Instruction.Move(dock,
1724                                                  Predicate.IgnoreFlagD, /* predicate   */
1725                                                  false,                 /* torpedoable */
1726                                                  null,                  /* path        */
1727                                                  false,                 /* tokenIn     */
1728                                                  false,                 /* dataIn      */
1729                                                  false,                 /* latchData   */
1730                                                  false,                 /* latchPath   */
1731                                                  true,                  /* dataOut     */
1732                                                  false                  /* tokenOut    */
1733                                                  ));
1734
1735         dataItems = marina.data.drainMany();
1736         fatal(dataItems.size()!=1, "Expected one data item to emerge but got: "+dataItems.size()+" data items");
1737         mp = new MarinaPacket(dataItems.get(0));
1738
1739         // the 14th bit of the outbound address cannot be set by the
1740         // ship, so we don't care about it
1741         fatalIfBitVectorsDoNotMatch(MarinaUtils.berkToSun(bv), mp.path.get(0,13));
1742
1743         adjustIndent(-2);
1744         prln("End sendDataWithPath");
1745     }
1746
1747     private void recvPath(Marina marina) {
1748         prln("Begin recvPath");
1749         adjustIndent(2);
1750
1751         marina.instrIn.fill(setIlc(1));
1752         for(int bit=0; bit<11; bit++) {
1753             BitVector packet_data = new BitVector(37, "inbound data item");
1754             for(int i=0; i<37; i++) packet_data.set(i, false);
1755             packet_data.set(27+bit, true);
1756             BitVector packet_path = new BitVector(14, "inbound data item");
1757             for(int i=0; i<14; i++) packet_path.set(i, false);
1758
1759             marina.fillNorthProperStopper(new MarinaPacket(packet_data, false, packet_path));
1760                                            
1761             prln("recv path, send data (using recv'd path)");
1762             marina.instrIn.fill(new Instruction.Move(dock,
1763                                                      Predicate.IgnoreFlagD,   /* predicate   */
1764                                                      false,                 /* torpedoable */
1765                                                      null,                  /* path        */
1766                                                      false,                 /* tokenIn     */
1767                                                      true,                  /* dataIn      */
1768                                                      true,                  /* latchData   */
1769                                                      true,                  /* latchPath   */
1770                                                      true,                  /* dataOut     */
1771                                                      false                  /* tokenOut    */
1772                                                      ));
1773
1774             List<BitVector> dataItems = marina.data.drainMany();
1775             fatal(dataItems.size()!=1, "Expected one data item to emerge but got: "+dataItems.size()+" data items");
1776             MarinaPacket mp = new MarinaPacket(dataItems.get(0));
1777             
1778             fatalIfBitVectorsDoNotMatch(packet_data.get(25,11), mp.path.get(0,11));
1779             fatalIfBitVectorsDoNotMatch(packet_data, mp.data);
1780         }
1781
1782         adjustIndent(-2);
1783         prln("End recvPath");
1784     }
1785
1786     private void testILC(Marina marina) {
1787         prln("Begin testILC");
1788         adjustIndent(2);
1789
1790         for(int bit=0; bit<6; bit++) {
1791             int ilc = bit<0 ? 0 : (1<<bit);
1792             marina.fillSouthProperStopper(new Instruction[] {
1793                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,ilc),
1794                     SEND_DATA,
1795                 });
1796             List<BitVector> dataItems = marina.data.drainMany();
1797             fatal(dataItems.size()!=ilc, "Expected "+ilc+" data item(s) to emerge but got: "+dataItems.size()+" data items");
1798         }
1799
1800         adjustIndent(-2);
1801         prln("End testILC");
1802     }
1803
1804     private void testILCZero(Marina marina) {
1805         adjustIndent(2);
1806         marina.fillSouthProperStopper(new Instruction[] {
1807                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,0),
1808                 SEND_DATA,
1809                 SEND_TOKEN,
1810             });
1811         expectNorthFifoNoMoreThan(0);
1812         expectTokensExactly(1);
1813         adjustIndent(-2);
1814     }
1815
1816     private void sendTorpedo(Marina marina) {
1817         prln("Begin sendTorpedo");
1818         adjustIndent(2);
1819         marina.instrIn.fill(setIlc(1));
1820         marina.instrIn.fill(setOlc(63));
1821
1822         model.waitNS(128 * CYCLE_TIME_NS);
1823         expectOlc(63);
1824
1825         marina.instrIn.fill(new 
1826                             Instruction.Set(dock,Predicate.IgnoreFlagD, CLEAR_FLAG, CLEAR_FLAG));
1827         fatal(marina.getFlagA(), "bad A flag: true");
1828         fatal(marina.getFlagB(), "bad B flag: true");
1829
1830         prln("execute a move instruction that does nothing but loops until torpedo arrives"); 
1831         prln("A=1, B=B This instruction should not execute because D-flag is set");
1832         prln("Set A=A, B=1 This instruction should execute because D-flag is set");
1833
1834         model.waitNS(128 * CYCLE_TIME_NS);
1835
1836         marina.fillSouthProperStopper(new Instruction[] {
1837                 new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, SetSource.Infinity),
1838                 new Instruction.Move(dock,
1839                                      Predicate.IgnoreFlagD, // predicate   
1840                                      true,                  // torpedoable 
1841                                      null,                  // path        
1842                                      false,                 // tokenIn     
1843                                      true,                  // dataIn      
1844                                      false,                 // latchData   
1845                                      false,                 // latchPath   
1846                                      false,                 // dataOut     
1847                                      false                  // tokenOut    
1848                                      ),
1849                 new Instruction.Set(dock,Predicate.Default,
1850                                     SET_FLAG,
1851                                     Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagB)
1852                                     ),
1853                 new Instruction.Set(dock, Predicate.FlagD,
1854                                     Instruction.Set.FlagFunction.ZERO.add(Predicate.FlagA),
1855                                     SET_FLAG
1856                                     ),
1857             });
1858
1859         model.waitNS(128 * CYCLE_TIME_NS);
1860
1861         prln("send torpedo. This should clear the OLC");
1862         marina.instrIn.fillTorpedo();
1863         model.waitNS(128 * CYCLE_TIME_NS);
1864
1865         model.waitNS(128 * CYCLE_TIME_NS);
1866                 
1867         prln("A should remain false, B should be true");
1868         fatal(marina.getFlagA(), "bad A flag: true");
1869         fatal(!marina.getFlagB(), "bad B flag: false");
1870         
1871         model.waitNS(128 * CYCLE_TIME_NS);
1872
1873         prln("Reload OLC after torpedo, clears D-flag");
1874         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 63));
1875
1876         // FIXME: find another way to test this
1877         model.waitNS(128 * CYCLE_TIME_NS);
1878         expectOlc(63);
1879                 
1880         prln("Set A=1, B=1 This instruction should execute because OLC!=0");
1881         marina.instrIn.fill(new 
1882                             Instruction.Set(dock,Predicate.Default, SET_FLAG, SET_FLAG));
1883
1884         prln("A and B should be true");
1885         fatal(!marina.getFlagA(), "bad A flag: false");
1886         fatal(!marina.getFlagB(), "bad B flag: false");
1887                 
1888         adjustIndent(-2);
1889         prln("End sendTorpedo");        
1890     }    
1891
1892     private void testTorpedoOnAnInfinite(Marina marina) {
1893         prln("Begin testTorpedoOnAnInfinite");
1894         adjustIndent(2);
1895
1896         List<BitVector> dataItems;
1897
1898         marina.instrIn.fill(setIlc(1));
1899         for(boolean torpedoable : new boolean[] { true, false }) {
1900             
1901             marina.fillSouthProperStopper(new Instruction[] {
1902                     new Instruction.Move(dock,
1903                                          Predicate.IgnoreFlagD, // predicate   
1904                                          false,                 // torpedoable 
1905                                          null,                  // path        
1906                                          false,                 // tokenIn     
1907                                          false,                 // dataIn      
1908                                          false,                 // latchData   
1909                                          false,                 // latchPath   
1910                                          false,                 // dataOut     
1911                                          true                   // tokenOut    
1912                                          ),
1913                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,SetSource.Infinity),
1914                     new Instruction.Move(dock,
1915                                          Predicate.IgnoreFlagD, // predicate   
1916                                          torpedoable,           // torpedoable 
1917                                          null,                  // path        
1918                                          true,                  // tokenIn     
1919                                          false,                 // dataIn      
1920                                          false,                 // latchData   
1921                                          false,                 // latchPath   
1922                                          false,                 // dataOut     
1923                                          true                   // tokenOut    
1924                                          ),
1925                     // FIXME: this probably should be removed, unless Ivan doesn't want to change the behavior
1926                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1),
1927
1928                     SEND_DATA,
1929                 });
1930             
1931             // expect nothing to come out, because the NOP is executing
1932             dataItems = marina.data.drainMany(2);
1933             fatal(dataItems.size()!=0, "Expected no data item(s) to emerge but got at least: "+dataItems.size()+" data items");
1934             
1935             marina.instrIn.fillTorpedo();
1936             
1937             int expected = torpedoable?1:0;
1938             dataItems = marina.data.drainMany(2);
1939             fatal(dataItems.size()!=expected, "Expected "+expected+" item to emerge but got: "+dataItems.size()+" data items");
1940
1941             fatal(!marina.getILC().getDone(), "Expected ilc=done, but got "+marina.getILC());
1942         }
1943
1944         adjustIndent(-2);
1945         prln("End testTorpedoOnAnInfinite");
1946     }
1947
1948     private void testDFlagWhenTorpedoLyingInWait(Marina marina) {
1949         marina.fillSouthProperStopper(new Instruction[] {
1950                 RECV_DATA,
1951                 TORPEDOABLE_RECV_DATA,
1952                 marina.kesselsCounter ? null : FLAG_NOP,
1953                 SEND_DATA_IF_D_SET,
1954             });
1955         marina.instrIn.fillTorpedo();
1956         model.waitNS(64 * CYCLE_TIME_NS);
1957         marina.fillNorthProperStopper();
1958         model.waitNS(64 * CYCLE_TIME_NS);
1959         expectNorthFifoExactly(1);
1960     }
1961
1962     private void testSetOlcFollowedByDPredicated(Marina marina) {
1963         for(boolean d_set : new boolean[] { false, true }) {
1964             prln("");
1965             marina.fillSouthProperStopper(new Instruction[] {
1966                     setOlc(0),
1967                     marina.kesselsCounter ? null : FLAG_NOP,
1968                     d_set ? SEND_DATA_IF_D_SET : SEND_DATA_IF_D_NOT_SET,
1969                 });
1970             expectNorthFifoExactly(d_set ? 1 : 0);
1971
1972             prln("");
1973             marina.fillSouthProperStopper(new Instruction[] {
1974                     setOlc(32),
1975                     marina.kesselsCounter ? null : FLAG_NOP,
1976                     d_set ? SEND_DATA_IF_D_SET : SEND_DATA_IF_D_NOT_SET,
1977                 });
1978             expectNorthFifoExactly(d_set ? 0 : 1);
1979
1980             if (marina.kesselsCounter) {
1981                 marina.masterClear();
1982                 marina.enableInstructionSend(true);
1983             }
1984         }
1985     }
1986
1987     private void testOlcDecrementAtHighSpeed(Marina marina) {
1988         prln("Begin testOlcDecrementAtHighSpeed");
1989         adjustIndent(2);
1990
1991         List<BitVector> dataItems;
1992         
1993         // Each element of the following pair of arrays is one "test".
1994         // The OLC will be loaded with olcs[i] and then decremented
1995         // decr_amounts[i] times; after that has happened the zeroness
1996         // of the OLC will be checked by executing a MOVE with
1997         // [olc!=0] as the predicate.
1998
1999         int[] olcs         = new int[] { 3, 3, 3, 10, 41 };
2000         int[] decr_amounts = new int[] { 2, 3, 4, 9,  9  };
2001
2002         for(int which=0; which<olcs.length; which++) {
2003             int olc = olcs[which];
2004             int decr_amount = decr_amounts[which];
2005
2006             prln("inserting set olc="+olc);
2007             prln("inserting set ilc=1");
2008             marina.fillSouthProperStopper(new Instruction[] {
2009                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter,1),
2010                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter,olc),
2011                 });
2012
2013             // commenting the following four lines causes this test to pass
2014
2015             prln("inserting: "+decr_amount+" olc-- instructions");
2016             prln("inserting: [!d] send data");
2017             Instruction[] instructions = new Instruction[decr_amount+1];
2018             for(int i=0; i<decr_amount; i++)
2019                 instructions[i] =
2020                     new Instruction.Set(dock,
2021                                         Predicate.Default,
2022                                         SetDest.OuterLoopCounter,
2023                                         SetSource.Decrement);
2024             instructions[instructions.length-1] =
2025                 new Instruction.Move(dock,
2026                                      Predicate.Default,     // predicate   
2027                                      false,                 // torpedoable 
2028                                      null,                  // path        
2029                                      false,                 // tokenIn     
2030                                      false,                 // dataIn      
2031                                      false,                 // latchData   
2032                                      false,                 // latchPath   
2033                                      true,                  // dataOut     
2034                                      false                  // tokenOut    
2035                                      );
2036
2037             marina.fillSouthProperStopper(instructions);
2038             model.waitNS(64 * CYCLE_TIME_NS);
2039
2040             int expected = decr_amount>=olc ? 0 : 1;
2041             dataItems = marina.data.drainMany(2);
2042             fatal(dataItems.size()!=expected, "Expected "+expected+" item to emerge but got: "+dataItems.size()+" data items");
2043
2044             if (marina.kesselsCounter) {
2045                 // master clear on each iteration; otherwise we'd need to "run down" the olc
2046                 marina.masterClear();
2047                 marina.enableInstructionSend(true);
2048             }
2049         }
2050
2051         adjustIndent(-2);
2052         prln("End testOlcDecrementAtHighSpeed");
2053     }
2054
2055     private void flipIlcBit(Marina marina) {
2056         prln("Begin flipIlcBit");
2057         adjustIndent(2);
2058         prln("Using the set ILC instruction, toggle a single bit between zero and one. \n" +
2059              "Check correct setting of the ILC zero bit");
2060
2061         for (int i=0; i<6; i++) {
2062             int notZero = 1<<i;
2063
2064             prln("Then immediately set ILC="+notZero);
2065             marina.fillSouthProperStopper(new Instruction[] {
2066                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, 0),
2067                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, notZero),
2068                 });
2069                 
2070             model.waitNS(64 * CYCLE_TIME_NS);
2071
2072             prln("Verify ILC using scan chain");
2073             Ilc ilc = marina.getILC();
2074             int ilcCount = ilc.getCount();
2075             fatal(ilcCount!=notZero, "bad ILC count: "+ilcCount+" expected: "+notZero);
2076             fatal(ilc.getInfinity(), "bad ILC Infinity bit: true");
2077                    
2078             marina.fillSouthProperStopper(new Instruction[] {     
2079                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, notZero),
2080                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.InnerLoopCounter, 0),
2081                 });
2082
2083             model.waitNS(64 * CYCLE_TIME_NS);
2084                         
2085             prln("Verify ILC using scan chain");
2086             ilc = marina.getILC();
2087             ilcCount = ilc.getCount();
2088             fatal(ilcCount!=0, "bad ILC count: "+ilcCount+" expected: 0");
2089             fatal(ilc.getInfinity(), "bad ILC Infinity bit: true");
2090         }
2091
2092         adjustIndent(-2);
2093         prln("End flipIlcBit");
2094     }
2095     private void flipOlcBit(Marina marina) {
2096         prln("Begin flipOlcBit");
2097         adjustIndent(2);
2098         prln("Using the set OLC instruction, toggle a single bit between zero and one. \n" +
2099              "Check correct setting of the OLC zero bit");
2100
2101         marina.instrIn.fill(new Instruction.Set(dock,Predicate.IgnoreFlagD, CLEAR_FLAG, CLEAR_FLAG));
2102
2103         for (int i=0; i<6; i++) {
2104             int notZero = 32 >> i;
2105
2106             if (marina.kesselsCounter) {
2107                 // master clear on each iteration; otherwise we'd need to "run down" the olc
2108                 marina.masterClear();
2109                 marina.enableInstructionSend(true);
2110             }
2111
2112             int outOlc;
2113             prln("Set OLC=0");
2114             prln("Then immediately set OLC="+notZero);
2115             marina.fillSouthProperStopper(new Instruction[] {
2116                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 0),
2117                     new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, notZero),
2118                 });
2119
2120             model.waitNS(64 * CYCLE_TIME_NS);
2121             prln("Verify OLC count using scan chain");
2122             expectOlc(notZero);
2123
2124             if (!marina.kesselsCounter) {
2125                 prln("Set OLC="+notZero);
2126                 prln("Then immediately set OLC=0");
2127                 marina.fillSouthProperStopper(new Instruction[] {
2128                         new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, notZero),
2129                         new Instruction.Set(dock,Predicate.IgnoreFlagD,SetDest.OuterLoopCounter, 0),
2130                     });
2131                 
2132                 model.waitNS(64 * CYCLE_TIME_NS);
2133                 prln("Verify OLC count using scan chain");
2134                 expectOlc(0);
2135             }
2136         }
2137         
2138         adjustIndent(-2);
2139         prln("End flipOlcBit");
2140     }
2141     private void testSouthRecirculate(Marina marina, int AMOUNT) {
2142         prln("Begin testSouthRecirculate("+AMOUNT+")");
2143         adjustIndent(2);
2144
2145         marina.enableInstructionSend(false);
2146         marina.enableInstructionRecirculate(true);
2147         
2148         prln("Completely fill south ring");
2149         adjustIndent(2);
2150         for (int i=0; i<AMOUNT; i++) {
2151             prln("inserting item " + (i+1) + " / " + AMOUNT);
2152             BitVector path = new BitVector(MarinaPacket.PATH_WIDTH, "path");
2153             BitVector data = new BitVector(MarinaPacket.WORD_WIDTH, "path");
2154             path.set(0, MarinaPacket.PATH_WIDTH, false);
2155             data.setFromLong(i+9);
2156             marina.instrIn.fill(new MarinaPacket(data, false, path));
2157         }
2158         adjustIndent(-2);
2159
2160         prln("Drain south ring and check contents");
2161         adjustIndent(2);
2162         List<BitVector> out = marina.instrIn.drainMany();
2163         boolean bad = false;
2164         for (int i=0; i<AMOUNT; i++) {
2165             prln("extracting item " + (i+1) + " / " + AMOUNT);
2166             //int expect = (i+Marina.SOUTH_RING_CAPACITY-1) % Marina.SOUTH_RING_CAPACITY;
2167             int expect = i+9;
2168             long got = new MarinaPacket(out.get(i)).data.toLong();
2169             if (got!=expect) {
2170                 bad = true;
2171                 prln("  bad instruction: "+got+" expected: "+expect);
2172             } else {
2173                 prln("  good instruction.");
2174             }
2175         }
2176         fatal(bad, "data inserted does not match data retrieved");
2177         adjustIndent(-2);
2178         
2179         for (int i=0; i<5; i++) {}
2180         
2181         adjustIndent(-2);
2182         prln("End testSouthRecirculate("+AMOUNT+")");
2183     }
2184
2185
2186     private void testOverfillTokens(Marina marina) {
2187         prln("Begin testOverfillTokens");
2188         adjustIndent(2);
2189
2190         for(int i=0; i<marina.TOKEN_FIFO_CAPACITY + 3; i++)
2191             marina.instrIn.fill(SEND_TOKEN);
2192         marina.instrIn.fill(SEND_DATA);
2193         expectNorthFifoExactly(0);
2194         
2195         adjustIndent(-2);
2196         prln("End testSouthRecirculate");
2197     }
2198
2199
2200
2201     private void doOneTest(int testNum) {
2202         prln("");
2203         prln("============================================================");
2204         prln("MarinaTest: performing test: "+testNum);
2205
2206         if (testNum!=0) {
2207             marina.masterClear();
2208             marina.enableInstructionSend(true);
2209         }
2210         
2211         try {
2212             switch (testNum) {
2213                 case 0: {
2214
2215                     doOneTest(1);       // passes extracted parasitics
2216                     doOneTest(2);       // passes extracted parasitics
2217                     doOneTest(3);       // passes extracted parasitics
2218                     doOneTest(4);       // passes extracted parasitics
2219                     doOneTest(5);       // passes extracted parasitics
2220                     doOneTest(6);
2221
2222                     doOneTest(1000);    // passes extracted parasitics
2223                     doOneTest(1001);    // passes extracted parasitics
2224                     doOneTest(1003);    // passes extracted parasitics
2225                     doOneTest(1005);
2226                     doOneTest(3000);    // passes extracted parasitics
2227                     doOneTest(3001);    // passes extracted parasitics
2228                     doOneTest(3003);    // passes extracted parasitics
2229                     doOneTest(3004);    // passes extracted parasitics
2230                     doOneTest(3005);    // passes extracted parasitics
2231                     doOneTest(3006);    // passes extracted parasitics
2232                     doOneTest(3007);    // passes extracted parasitics
2233                     doOneTest(3008);    // passes extracted parasitics
2234                     doOneTest(3009);    // passes extracted parasitics
2235                     doOneTest(3010);    // passes extracted parasitics
2236                     doOneTest(3011);    // passes extracted parasitics
2237                     doOneTest(3012);    // passes extracted parasitics
2238                     doOneTest(3013);    // passes extracted parasitics
2239                     doOneTest(3014);    // passes extracted parasitics
2240                     doOneTest(3015);    // passes extracted parasitics
2241                     doOneTest(3019);    // passes extracted parasitics
2242                     doOneTest(3020);    // passes extracted parasitics
2243                     doOneTest(3022);    // passes extracted parasitics
2244                     doOneTest(3023);    // passes extracted parasitics
2245                     doOneTest(3025);    // passes extracted parasitics
2246                     doOneTest(3026);    // passes extracted parasitics
2247                     doOneTest(3027);    // passes extracted parasitics
2248                     doOneTest(3028);    // passes extracted parasitics
2249                     doOneTest(3029);    // passes extracted parasitics
2250                     doOneTest(3030);    // passes extracted parasitics
2251                     doOneTest(3031);    // passes extracted parasitics
2252                     
2253                     // these tests take a while and usually pass
2254                     doOneTest(1002);
2255                     doOneTest(1004);
2256                     doOneTest(1005);
2257                     doOneTest(1006);
2258                     doOneTest(3002);
2259                     doOneTest(3016);
2260                     doOneTest(3021);
2261                     doOneTest(3024);
2262                     doOneTest(3025);
2263                     doOneTest(3040);
2264                     
2265                     // this takes an insanely long time
2266                     doOneTest(3017);
2267                     break;
2268                 }
2269                 case 1:    testChains(marina);                     break; // passes, 24-Mar (+verilog)
2270                 case 2:    testProperStoppers(marina);             break; // passes, 24-Mar (+verilog)
2271                 case 3:    testSouthRecirculate(marina, 1);        break; // passes, 24-Mar (+verilog)
2272                 case 4:    getCtrsFlags(marina);                   break; //         20-Apr (+verilog)
2273                 case 5:    sendInstructions(marina);               break; // passes, 24-Mar (+verilog)
2274                 case 6:    walkOneOLC(marina);                     break; //         21-Apr (+verilog)
2275                 
2276                     // Russell's tests begin with 1000
2277                 case 1000: walkOneILC(marina);                     break; //         20-Apr (+verilog)
2278                 case 1001: countIlc(marina);                       break; //         20-Apr (+verilog)
2279                 case 1002: countOlc(marina);                       break; //         23-Apr (+verilog)
2280
2281                 case 1003: sendTorpedo(marina);                    break; //         23-Apr (+verilog)  [with wor-hack]
2282
2283                 case 1004: flipIlcBit(marina);                     break; //         20-Apr (+verilog)
2284                 case 1005: flipOlcBit(marina);                     break; //         21-Apr (+verilog)
2285
2286                 case 1006: testSouthRecirculate(marina, Marina.SOUTH_RING_CAPACITY-1);        break; // passes, 24-Mar (+verilog)
2287
2288                     // Adam's tests begin with 3000
2289                 case 3000: sendToken(marina);                      break; // passes, 24-Mar (+verilog)
2290                 case 3001: testFlagAB(marina);                     break; // passes, 08-Apr (+verilog)
2291                 case 3002: testPredicationOnAB(marina);            break; //         22-Apr (+verilog)
2292                 case 3003: testFlagC(marina);                      break; //         23-Apr (+verilog)
2293                 case 3004: testFlagD(marina);                      break; //         23-Apr (+verilog)
2294                 case 3005: testFlagDRecomputationTime(marina);     break;
2295
2296                 case 3006: testTailWaitsForHead(marina);           break;
2297                 case 3007: testTailWithoutHead(marina);            break;
2298                 case 3008: testHeadWaitsForTail(marina);           break; //         22-Apr (+verilog)
2299                 case 3009: testAbort(marina);                      break; //         22-Apr (+verilog)
2300
2301                 case 3010: sendData(marina);                       break; // passes, 24-Mar (+verilog)
2302                 case 3011: recvData(marina);                       break; //         21-Apr (+verilog)
2303                 case 3012: sendDataWithPath(marina);               break; // passes, 13-Apr (+verilog)
2304
2305                 case 3013: testSignExtendedLiteral(marina);        break; //         20-Apr (+verilog)
2306                 case 3014: testShiftedLiteral(marina);             break; //         20-Apr (+verilog)
2307                 case 3015: testSendAndRecvToken(marina);           break; //         21-Apr (+verilog)
2308
2309                 case 3016: sendDataIlcInfinite(marina);            break; //         22-Apr (+verilog)
2310                 case 3017: testFlagTruthTable(marina);             break; //         23-Apr (+verilog)
2311
2312                 case 3019: setOlcFromDataLatch(marina);            break; //         23-Apr (+verilog)
2313                 case 3020: setIlcFromDataLatch(marina);            break; //         23-Apr (+verilog)
2314                 case 3021: recvPath(marina);                       break; //         22-Apr (+verilog)
2315                 case 3022: testILC(marina);                        break; //         23-Apr (+verilog)
2316                 case 3023: testTorpedoOnAnInfinite(marina);        break; //         23-Apr (+verilog)
2317                 case 3024: testRecvAndSendWalkingOne(marina);      break; //         21-Apr (+verilog)
2318                 case 3025: testOlcDecrementAtHighSpeed(marina);    break; //         23-Apr (+verilog)
2319
2320                 case 3026: testNonTorpedoableMoveDoesNotResetDFlag(marina);        break; //         23-Apr (+verilog)
2321                 case 3027: testILCZero(marina);                    break;
2322                 case 3028: testAbortOutsideOfLoop(marina); break;
2323                 case 3029: testDFlagWhenTorpedoLyingInWait(marina); break;
2324                 case 3030: testSetOlcFollowedByDPredicated(marina); break;
2325                 case 3031: testOverfillTokens(marina); break;
2326
2327                 case 3040: loadEveryValueOLC(marina); break;
2328
2329                 default:
2330                     fatal(true, "Test number: "+testNum+" doesn't exist.");
2331                     break;
2332             }
2333             // If we get here then test passed
2334             prln("Test Result: Passed");
2335             printTestTime();
2336             //Infrastructure.exit(0);
2337         } catch (MarinaUtils.FailureException fe) {
2338             System.out.println("******************************************************************************");
2339             System.out.println("******************************************************************************");
2340             System.out.println("******************************************************************************");
2341             System.out.println("******************************************************************************");
2342             fe.printStackTrace();
2343         }
2344     }
2345
2346
2347     //============================ for public use =============================
2348
2349     /** Exit codes:
2350      * 0: test detected success
2351      * 2: test detected failure
2352      * 1: test crashed
2353      */ 
2354     public static void main(String[] args) {
2355         startTime = System.currentTimeMillis();
2356         new MarinaTest(args);
2357     }
2358
2359 }