move marina/testCode/com to src/com
[fleet.git] / src / com / sun / vlsi / chips / marina / test / MarinaPacket.java
diff --git a/src/com/sun/vlsi/chips/marina/test/MarinaPacket.java b/src/com/sun/vlsi/chips/marina/test/MarinaPacket.java
new file mode 100644 (file)
index 0000000..b14ef86
--- /dev/null
@@ -0,0 +1,96 @@
+package com.sun.vlsi.chips.marina.test;
+/* -*- tab-width: 4 -*- */
+import com.sun.async.test.BitVector;
+import com.sun.async.test.ChainControl;
+import com.sun.async.test.ChipModel;
+import com.sun.async.test.JtagTester;
+import com.sun.async.test.NanosimModel;
+import edu.berkeley.fleet.api.Dock;
+import edu.berkeley.fleet.api.Instruction;
+import edu.berkeley.fleet.marina.MarinaFleet;
+
+/**
+ * This class encapsulates a "packet" -- a data item in flight plus
+ * its tokenhood and path.
+ *
+ * This should be the only class that knows how to turn a packet into
+ * its constituent parts and vice versa.
+ */
+public class MarinaPacket {
+
+    public static final int PATH_WIDTH   = 14;
+    public static final int WORD_WIDTH   = 37;
+    public static final int PACKET_WIDTH = PATH_WIDTH+WORD_WIDTH+1;
+
+    public final BitVector data;
+    public final boolean tokenhood;
+    public final BitVector path;
+    private final boolean is_instruction;
+
+    public static final BitVector null_path = new BitVector(PATH_WIDTH, "null_path");
+    public static final BitVector null_word = new BitVector(WORD_WIDTH, "null_word");
+    static {
+        null_path.set(0, PATH_WIDTH, false);
+        null_word.set(0, WORD_WIDTH, false);
+    }
+
+    /** "parse" a token from the raw bits in the north proper stopper */
+    public MarinaPacket(BitVector singleBitVector) {
+        MarinaUtils.expectLength(singleBitVector,PACKET_WIDTH);
+        this.data = new BitVector(WORD_WIDTH, "marina packet data");
+        this.path = new BitVector(PATH_WIDTH, "marina packet path");
+        this.tokenhood = !singleBitVector.get(0);
+        this.is_instruction = false;
+        for(int i=0; i<PATH_WIDTH; i++) path.set(i, singleBitVector.get(i+1));
+        for(int i=0; i<WORD_WIDTH; i++) data.set(i, singleBitVector.get(i+PATH_WIDTH+1));
+    }
+
+    /** manually assemble a packet */
+    public MarinaPacket(BitVector data, boolean tokenhood, BitVector path) {
+        MarinaUtils.expectLength(data, WORD_WIDTH);
+        MarinaUtils.expectLength(path, PATH_WIDTH);
+        this.data = data;
+        this.tokenhood = tokenhood;
+        this.path = path;
+        this.is_instruction = false;
+    }
+
+    /** another constructor which uses an all-zeroes path, for convenience */
+    public MarinaPacket(Instruction inst) {
+        BitVector instr =
+            MarinaUtils.berkToSun(MarinaTest.marinaFleet.encodeInstruction(MarinaTest.marinaFleet.getOnlyInputDock(), inst));
+        MarinaUtils.expectLength(instr, MarinaPacket.WORD_WIDTH);
+        
+        //
+        // Due to what Ivan calls "geometric inconvenience", incoming
+        // bit 19 is dropped, and all the higher bits are shifted
+        // downward by one position.  So we have to compensate for
+        // this by inserting a bogus bit at position #19.
+        //
+        BitVector pad = new BitVector(1, "pad");
+        pad.setFromLong(0);
+        this.data = instr.get(0, 18).cat(pad).cat(instr.get(18,18));
+        this.path = null_path;
+        this.tokenhood = false;
+        this.is_instruction = true;
+    }
+
+    /** convert a packet into a single BitVector, suitable for insertion in the north proper stopper */
+    public BitVector toSingleBitVector() {
+        BitVector bv = new BitVector(PACKET_WIDTH, "marina packet");
+        bv.set(0, !tokenhood);
+        for(int i=0; i<PATH_WIDTH; i++) bv.set(i+1,            path.get(i));
+        for(int i=0; i<WORD_WIDTH; i++) bv.set(i+PATH_WIDTH+1, data.get(i));
+        return bv;
+    }
+
+    public String toString() {
+        return
+            "tokenhood="+(tokenhood ? "token" : "data")
+            + ", path["+PATH_WIDTH+":1]=" + path.bitReverse().getState()
+            + ", data["+WORD_WIDTH+":1]=" +
+            (is_instruction
+             ? (data.get(19,18).bitReverse().getState()+"_"+data.get(0,18).bitReverse().getState())
+             : data.bitReverse().getState());
+    }
+}
\ No newline at end of file