remove last vestiges of old literal system
[fleet.git] / src / edu / berkeley / fleet / ies44 / InstructionEncoder.java
1 package edu.berkeley.fleet.ies44;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.*;
4 import java.io.*;
5 import static edu.berkeley.fleet.ies44.BitManipulations.*;
6
7 public abstract class InstructionEncoder {
8
9     public static final int WIDTH_WORD          = 37;   /* word width */
10     public static final int WIDTH_CODEBAG_SIZE  = 6;
11     public static final int WIDTH_PUMP_ADDR     = 11;
12     public static final int WIDTH_DEST_ADDR     = 11;
13     public static final int WIDTH_COUNT         = 7;
14
15     public static final int OFFSET_COUNT        = 0;
16     public static final int OFFSET_DEST         = OFFSET_COUNT+WIDTH_COUNT;
17     public static final int OFFSET_CONTROL      = OFFSET_DEST+WIDTH_DEST_ADDR;
18     public static final int OFFSET_RQ           = OFFSET_CONTROL+0;
19     public static final int OFFSET_TO           = OFFSET_CONTROL+1;
20     public static final int OFFSET_DO           = OFFSET_CONTROL+2;
21     public static final int OFFSET_DL           = OFFSET_CONTROL+3;
22     public static final int OFFSET_DI           = OFFSET_CONTROL+4;
23     public static final int OFFSET_TI           = OFFSET_CONTROL+5;
24     public static final int OFFSET_PUMP_ADDR    = OFFSET_TI+1;
25
26     public static final int OFFSET_LITERAL      = OFFSET_COUNT;
27     public static final int WIDTH_LITERAL       = WIDTH_COUNT+WIDTH_DEST_ADDR;
28
29     /** get the bits describing this box's location on the DESTINATION HORN */
30     protected abstract long getDestAddr(Destination box);
31
32     /** get the bits describing this box's location on the INSTRUCTION HORN */
33     protected abstract long getBoxInstAddr(Pump box);
34
35     /** given an INSTRUCTION HORN address, retrieve the corresponding Pump object */
36     protected abstract Pump getBoxByInstAddr(long dest);
37     
38     /** given a DESTINATION HORN address, retrieve the corresponding Pump object */
39     protected abstract Destination getDestByAddr(long dest);
40
41     /** read a machine-formatted instruction from a file (into a Java object) */
42     public Instruction readInstruction(DataInputStream is) throws IOException {
43         long inst = 0;
44         try {
45             inst = (inst << 8) | (is.readByte() & 0xff);
46             inst = (inst << 8) | (is.readByte() & 0xff);
47             inst = (inst << 8) | (is.readByte() & 0xff);
48             inst = (inst << 8) | (is.readByte() & 0xff);
49             inst = (inst << 8) | (is.readByte() & 0xff);
50             inst = (inst << 8) | (is.readByte() & 0xff);
51             return readInstruction(inst);
52         } catch (EOFException eof) {
53             return null;
54         }
55     }
56
57     public Instruction readInstruction(long instr) {
58         long inst = instr;
59         switch((int)getField(WIDTH_WORD-1, WIDTH_WORD-2, inst)) {
60             case 0: {
61                 Pump name           = getBoxByInstAddr(getIntField(OFFSET_PUMP_ADDR+WIDTH_PUMP_ADDR-1, OFFSET_PUMP_ADDR, inst));
62                 Destination dest    = getDestByAddr   (getIntField(OFFSET_DEST+WIDTH_DEST_ADDR-1,      OFFSET_DEST,      inst));
63                 int count           = getIntField(                 OFFSET_COUNT+WIDTH_COUNT-1,         OFFSET_COUNT,     inst);
64                 boolean tokenIn     = getBit(OFFSET_TI, instr);
65                 boolean dataIn      = getBit(OFFSET_DI, instr);
66                 boolean latch       = getBit(OFFSET_DL, instr);
67                 boolean dataOut     = getBit(OFFSET_DO, instr);
68                 boolean tokenOut    = getBit(OFFSET_TO, instr);
69                 boolean requeue     = getBit(OFFSET_RQ, instr);
70                 boolean dataOutDest = dataOut && tokenOut;
71                 boolean isLiteral   = tokenIn && tokenOut && !dataOutDest;
72                 if (isLiteral)
73                     return new Instruction.LocalLiteral(name, getIntField(OFFSET_LITERAL+WIDTH_LITERAL-1, OFFSET_LITERAL, inst));
74                 if (latch & !dataIn) return new Instruction.Kill(name, count, tokenIn);
75                 return new Instruction.Executable(name, dest, count, tokenIn, dataIn, latch, dataOutDest,
76                                                   dataOut, tokenOut, requeue);
77             }
78                 /*
79             case 1: {
80                 Destination name = getDestByAddr(getField(WIDTH_WORD-3, WIDTH_WORD-3-WIDTH_DEST_ADDR+1, inst));
81                 long offset      = getSignedField(WIDTH_WORD-3-WIDTH_DEST_ADDR, WIDTH_CODEBAG_SIZE, instr);
82                 long size        = getSignedField(WIDTH_CODEBAG_SIZE-1,        0,                 instr);
83                 return new Instruction.Literal.CodeBagDescriptor(name, offset, size);
84             }
85                 */
86             case 2: {
87                 Destination name = getDestByAddr(getField(WIDTH_WORD-3, WIDTH_WORD-3-WIDTH_PUMP_ADDR+1, inst));
88                 return new Instruction.Literal.Absolute(name, getSignedField(WIDTH_WORD-3-WIDTH_PUMP_ADDR, 0, instr));
89             }
90
91             case 3: {
92                 Destination name = getDestByAddr(getField(WIDTH_WORD-3, WIDTH_WORD-3-WIDTH_PUMP_ADDR+1, inst));
93                 return new Instruction.Literal.Relative(name, getSignedField(WIDTH_WORD-3-WIDTH_PUMP_ADDR, 0, instr));
94             }
95
96         }
97         return null;
98
99     }
100
101     public long writeInstruction(Instruction d) {
102         long instr = 0;
103
104         // Kill is encoded as Execute with the illegal combination (Latch & ~DataIn)
105         if (d instanceof Instruction.Kill) {
106             Instruction.Kill k = (Instruction.Kill)d;
107             d = new Instruction.Executable(k.pump, null, k.count, k.killOnlyStandingInstructions,
108                                            false, true, false, false, false, false);
109         }
110
111         if (d instanceof Instruction.Literal.CodeBagDescriptor) {
112             Instruction.Literal.CodeBagDescriptor lc = (Instruction.Literal.CodeBagDescriptor)d;
113             d = new Instruction.LocalLiteral(lc.pump, ((lc.offset << WIDTH_CODEBAG_SIZE)) | lc.size);
114         }
115
116         if (d instanceof Instruction.LocalLiteral) {
117             Instruction.LocalLiteral inst = (Instruction.LocalLiteral)d;
118             if (inst.pump != null) {
119                 instr |= putField(OFFSET_PUMP_ADDR+WIDTH_PUMP_ADDR-1, OFFSET_PUMP_ADDR,   getBoxInstAddr(inst.pump));
120                 instr |= putField(OFFSET_TI,                          OFFSET_TI,          1);
121                 instr |= putField(OFFSET_TO,                          OFFSET_TO,          1);
122                 instr |= putField(OFFSET_LITERAL+WIDTH_LITERAL-1, OFFSET_LITERAL,         inst.literal);
123             } else {
124                 instr = inst.literal;
125             }
126
127         } else if (d instanceof Instruction.Executable) {
128             Instruction.Executable inst = (Instruction.Executable)d;
129             instr |= putField(OFFSET_PUMP_ADDR+WIDTH_PUMP_ADDR-1, OFFSET_PUMP_ADDR,   getBoxInstAddr(inst.pump));
130             instr |= putField(OFFSET_DEST+WIDTH_DEST_ADDR-1,      OFFSET_DEST,        inst.dest==null?0:getDestAddr(inst.dest));
131             instr |= putField(OFFSET_COUNT+WIDTH_COUNT-1,         OFFSET_COUNT,       inst.count);
132             instr |= putField(OFFSET_TI,                          OFFSET_TI,          inst.tokenIn?1:0);
133             instr |= putField(OFFSET_DI,                          OFFSET_DI,          inst.dataIn?1:0);
134             instr |= putField(OFFSET_DL,                          OFFSET_DL,          inst.latch?1:0);
135             instr |= putField(OFFSET_DO,                          OFFSET_DO,          (inst.dataOutDest||inst.dataOut)?1:0);
136             instr |= putField(OFFSET_TO,                          OFFSET_TO,          (inst.dataOutDest||inst.tokenOut)?1:0);
137             instr |= putField(OFFSET_RQ,                          OFFSET_RQ,          inst.requeue?1:0);
138
139         }
140         return instr;
141     }
142
143     public void writeInstruction(DataOutputStream os, Instruction d) throws IOException {
144         long instr = writeInstruction(d);
145         for(int i=5; i>=0; i--)
146             os.write(getIntField(i*8+7, i*8, instr));
147    }
148
149 }