change com.sun.async.test -> com.sun.electric.tool.simulation.test
[fleet.git] / src / com / sun / vlsi / chips / marina / test / MarinaPacket.java
1 package com.sun.vlsi.chips.marina.test;
2 /* -*- tab-width: 4 -*- */
3 import com.sun.electric.tool.simulation.test.*;
4 import edu.berkeley.fleet.api.Dock;
5 import edu.berkeley.fleet.api.Instruction;
6 import edu.berkeley.fleet.marina.MarinaFleet;
7
8 /**
9  * This class encapsulates a "packet" -- a data item in flight plus
10  * its tokenhood and path.
11  *
12  * This should be the only class that knows how to turn a packet into
13  * its constituent parts and vice versa.
14  */
15 public class MarinaPacket {
16
17     public static final int PATH_WIDTH   = 14;
18     public static final int WORD_WIDTH   = 37;
19     public static final int PACKET_WIDTH = PATH_WIDTH+WORD_WIDTH+1;
20
21     public final BitVector data;
22     public final boolean tokenhood;
23     public final BitVector path;
24     private final boolean is_instruction;
25
26     public static final BitVector null_path = new BitVector(PATH_WIDTH, "null_path");
27     public static final BitVector null_word = new BitVector(WORD_WIDTH, "null_word");
28     static {
29         null_path.set(0, PATH_WIDTH, false);
30         null_word.set(0, WORD_WIDTH, false);
31     }
32
33     /** "parse" a token from the raw bits in the north proper stopper */
34     public MarinaPacket(BitVector singleBitVector) {
35         MarinaUtils.expectLength(singleBitVector,PACKET_WIDTH);
36         this.data = new BitVector(WORD_WIDTH, "marina packet data");
37         this.path = new BitVector(PATH_WIDTH, "marina packet path");
38         this.tokenhood = !singleBitVector.get(0);
39         this.is_instruction = false;
40         for(int i=0; i<PATH_WIDTH; i++) path.set(i, singleBitVector.get(i+1));
41         for(int i=0; i<WORD_WIDTH; i++) data.set(i, singleBitVector.get(i+PATH_WIDTH+1));
42     }
43
44     /** manually assemble a packet */
45     public MarinaPacket(BitVector data, boolean tokenhood, BitVector path) {
46         MarinaUtils.expectLength(data, WORD_WIDTH);
47         MarinaUtils.expectLength(path, PATH_WIDTH);
48         this.data = data;
49         this.tokenhood = tokenhood;
50         this.path = path;
51         this.is_instruction = false;
52     }
53
54     /** another constructor which uses an all-zeroes path, for convenience */
55     public MarinaPacket(Instruction inst) {
56         BitVector instr =
57             MarinaUtils.berkToSun(MarinaTest.marinaFleet.encodeInstruction(MarinaTest.marinaFleet.getOnlyInputDock(), inst));
58         MarinaUtils.expectLength(instr, MarinaPacket.WORD_WIDTH);
59         
60         //
61         // Due to what Ivan calls "geometric inconvenience", incoming
62         // bit 19 is dropped, and all the higher bits are shifted
63         // downward by one position.  So we have to compensate for
64         // this by inserting a bogus bit at position #19.
65         //
66         BitVector pad = new BitVector(1, "pad");
67         pad.setFromLong(0);
68         this.data = instr.get(0, 18).cat(pad).cat(instr.get(18,18));
69         this.path = null_path;
70         this.tokenhood = false;
71         this.is_instruction = true;
72     }
73
74     /** convert a packet into a single BitVector, suitable for insertion in the north proper stopper */
75     public BitVector toSingleBitVector() {
76         BitVector bv = new BitVector(PACKET_WIDTH, "marina packet");
77         bv.set(0, !tokenhood);
78         for(int i=0; i<PATH_WIDTH; i++) bv.set(i+1,            path.get(i));
79         for(int i=0; i<WORD_WIDTH; i++) bv.set(i+PATH_WIDTH+1, data.get(i));
80         return bv;
81     }
82
83     public String toString() {
84         return
85             "tokenhood="+(tokenhood ? "token" : "data")
86             + ", path["+PATH_WIDTH+":1]=" + path.bitReverse().getState()
87             + ", data["+WORD_WIDTH+":1]=" +
88             (is_instruction
89              ? (data.get(19,18).bitReverse().getState()+"_"+data.get(0,18).bitReverse().getState())
90              : data.bitReverse().getState());
91     }
92 }