eliminate standing bit
[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 edu.berkeley.fleet.util.*;
5 import java.io.*;
6 import static edu.berkeley.fleet.util.BitManipulations.*;
7 import static edu.berkeley.fleet.api.Instruction.*;
8 import static edu.berkeley.fleet.api.Instruction.Counter.*;
9
10 public abstract class InstructionEncoder {
11
12     public static final int WIDTH_WORD             = 37;
13     public static final int WIDTH_ADDR             = 11;
14     public static final int WIDTH_CODEBAG_SIZE     = 6;
15     public static final int WIDTH_COUNTER_LITERAL  = 6;
16
17     public static final Mask FLAGS                = new Mask("...............000000................");
18     public static final Mask FLAGS_A              = new Mask("...............000000vvvvvvvv........");
19     public static final Mask FLAGS_B              = new Mask("...............000000........vvvvvvvv");
20     public static final Mask REPEAT_FROM_DATA     = new Mask("...............000001........00......");
21     public static final Mask REPEAT_FROM_LITERAL  = new Mask("...............000001........10vvvvvv");
22     public static final Mask REPEAT_FROM_STANDING = new Mask("...............000001........11......");
23     public static final Mask LOOP_FROM_DATA       = new Mask("...............000010.........0......");
24     public static final Mask LOOP_FROM_LITERAL    = new Mask("...............000010.........1vvvvvv");
25     public static final Mask TAKE_LOOP            = new Mask("...............000011................");
26     public static final Mask KILL                 = new Mask("...............000100..........vvvvvv");
27     public static final Mask MASSACRE             = new Mask("...............000101................");
28     public static final Mask CLOG                 = new Mask("...............000110................");
29     public static final Mask UNCLOG               = new Mask("...............000111................");
30
31     public static final Mask SK                  = new Mask("...........1.........................");
32     public static final Mask DL                  = new Mask("............1........................");
33     public static final Mask P                   = new Mask(".............vv......................");
34     public static final Mask P_A                 = new Mask(".............00......................");
35     public static final Mask P_B                 = new Mask(".............10......................");
36     public static final Mask P_Z                 = new Mask(".............01......................");
37     public static final Mask P_ALWAYS            = new Mask(".............11......................");
38     public static final Mask MOVE                = new Mask("...............001...................");
39     public static final Mask TI                  = new Mask("....................1................");
40     public static final Mask DI                  = new Mask(".....................1...............");
41     public static final Mask DC                  = new Mask("......................1..............");
42     public static final Mask DO                  = new Mask(".......................1.............");
43     public static final Mask TO                  = new Mask("........................1............");
44     public static final Mask PATH_LITERAL        = new Mask(".........................1vvvvvvvvvvv");
45     public static final Mask PATH_DATA           = new Mask(".........................01..........");
46     public static final Mask PATH_NOCHANGE       = new Mask(".........................00..........");
47
48     public static final Mask LITERAL_LO          = new Mask("...............010vvvvvvvvvvvvvvvvvvv");
49     public static final Mask LITERAL_HI          = new Mask("...............011vvvvvvvvvvvvvvvvvvv");
50     public static final Mask LITERAL             = new Mask("...............1..vvvvvvvvvvvvvvvvvvv");
51     public static final Mask LITERAL_SEL         = new Mask("................vv...................");
52     public static final Mask PUMP_NAME           = new Mask("vvvvvvvvvvv..........................");
53
54
55     /** get the bits describing this box's location on the DESTINATION HORN */
56     protected abstract long getDestAddr(Destination box);
57
58     /** get the bits describing this box's location on the INSTRUCTION HORN */
59     protected abstract long getBoxInstAddr(Pump box);
60
61     /** given an INSTRUCTION HORN address, retrieve the corresponding Pump object */
62     protected abstract Pump getBoxByInstAddr(long dest);
63     
64     /** given a DESTINATION HORN address, retrieve the corresponding Pump object */
65     protected abstract Destination getDestByAddr(long dest);
66
67     /** read a machine-formatted instruction from a file (into a Java object) */
68     public Instruction readInstruction(DataInputStream is) throws IOException {
69         long inst = 0;
70         try {
71             inst = (inst << 8) | (is.readByte() & 0xff);
72             inst = (inst << 8) | (is.readByte() & 0xff);
73             inst = (inst << 8) | (is.readByte() & 0xff);
74             inst = (inst << 8) | (is.readByte() & 0xff);
75             inst = (inst << 8) | (is.readByte() & 0xff);
76             inst = (inst << 8) | (is.readByte() & 0xff);
77             return readInstruction(inst);
78         } catch (EOFException eof) {
79             return null;
80         }
81     }
82
83     public Instruction readInstruction(long inst) {
84         Pump name           = getBoxByInstAddr(PUMP_NAME.getval(inst));
85
86         if (UNCLOG.get(inst))   return new Instruction.UnClog(name);
87         if (CLOG.get(inst))     return new Instruction.Clog(name);
88         if (MASSACRE.get(inst)) return new Instruction.Massacre(name);
89         if (KILL.get(inst))     return new Instruction.Kill(name, (int)(KILL.getval(inst)+1));
90
91         int predicate = 0;
92         if (P_ALWAYS.get(inst)) predicate = Instruction.PredicatedInstruction.P_ALWAYS;
93         if (P_A.get(inst)) predicate = Instruction.PredicatedInstruction.P_IF_A;
94         if (P_B.get(inst)) predicate = Instruction.PredicatedInstruction.P_IF_B;
95         if (P_Z.get(inst)) predicate = Instruction.PredicatedInstruction.P_IF_Z;
96
97         if (LOOP_FROM_LITERAL.get(inst))    return new Counter(name, predicate, (int)LOOP_FROM_LITERAL.getval(inst),   LOOP_COUNTER);
98         if (REPEAT_FROM_LITERAL.get(inst))  return new Counter(name, predicate, (int)REPEAT_FROM_LITERAL.getval(inst), REPEAT_COUNTER);
99         if (LOOP_FROM_DATA.get(inst))       return new Counter(name, predicate, DATA_LATCH, LOOP_COUNTER);
100         if (REPEAT_FROM_DATA.get(inst))     return new Counter(name, predicate, DATA_LATCH, REPEAT_COUNTER);
101         if (REPEAT_FROM_STANDING.get(inst)) return new Counter(name, predicate, STANDING, REPEAT_COUNTER);
102         if (TAKE_LOOP.get(inst))            return new Counter(name, predicate, LOOP_COUNTER, DATA_LATCH);
103
104         if (DL.get(inst))                  return new Instruction.DecrLoop(name, predicate);
105
106         if (LITERAL_LO.get(inst))  return new Instruction.HalfLiteral(name, predicate, LITERAL_LO.getval(inst), 0, false);
107         if (LITERAL_HI.get(inst))  return new Instruction.HalfLiteral(name, predicate, LITERAL_HI.getval(inst), 0, true);
108         if (FLAGS.get(inst))       return new Instruction.SetFlags(name, predicate, (int)FLAGS_A.getval(inst), (int)FLAGS_B.getval(inst));
109
110         if (!MOVE.get(inst)) throw new RuntimeException("unknown instruction: 0x" + Long.toString(inst, 16));
111
112         Destination dest    = getDestByAddr(PATH_LITERAL.getval(inst));
113         boolean tokenIn     = TI.get(inst);
114         boolean dataIn      = DI.get(inst);
115         boolean latch       = DC.get(inst);
116         boolean dataOut     = DO.get(inst);
117         boolean tokenOut    = TO.get(inst);
118         boolean dataOutDest = PATH_DATA.get(inst);
119         return new Instruction.Move(name,
120                                     predicate,
121                                     dest,
122                                     tokenIn,
123                                     dataIn,
124                                     latch,
125                                     dataOutDest,
126                                     dataOut,
127                                     tokenOut,
128                                     false,
129                                     false);
130     }
131
132     public long writeInstruction(Instruction d) {
133         long instr = 0;
134
135         if (d.pump != null)
136             instr = PUMP_NAME.setval(instr, getBoxInstAddr(d.pump));
137
138         if (d instanceof Instruction.PredicatedInstruction) {
139             Instruction.PredicatedInstruction pi = (Instruction.PredicatedInstruction)d;
140             switch(pi.predicate) {
141                 case Instruction.PredicatedInstruction.P_ALWAYS: instr = P_ALWAYS.set(instr); break;
142                 case Instruction.PredicatedInstruction.P_IF_A:   instr = P_A.set(instr);      break;
143                 case Instruction.PredicatedInstruction.P_IF_B:   instr = P_B.set(instr);      break;
144                 case Instruction.PredicatedInstruction.P_IF_Z:   instr = P_Z.set(instr);      break;
145             }
146         }
147
148         if (d instanceof Instruction.CodeBagDescriptor) {
149             Instruction.CodeBagDescriptor lc = (Instruction.CodeBagDescriptor)d;
150             // MAJOR MAJOR FIXME: upper half here...
151             d = new Instruction.HalfLiteral(lc.pump, Instruction.PredicatedInstruction.P_ALWAYS,
152                                             ((lc.offset << WIDTH_CODEBAG_SIZE)) | lc.size, 1, false);
153         }
154
155         if (d instanceof Instruction.UnClog) {
156             instr = UNCLOG.set(instr);
157
158         } else if (d instanceof Instruction.Kill) {
159             Instruction.Kill k = (Instruction.Kill)d;
160             instr = KILL.set(instr);
161             instr = KILL.setval(instr, k.count);
162
163         } else if (d instanceof Instruction.Clog) {
164             instr = CLOG.set(instr);
165
166         } else if (d instanceof Instruction.Massacre) {
167             instr = MASSACRE.set(instr);
168
169         } else if (d instanceof Instruction.SetFlags) {
170             Instruction.SetFlags sf = (Instruction.SetFlags)d;
171             instr = FLAGS.set(instr);
172             instr = FLAGS_A.setval(instr, sf.flag_a);
173             instr = FLAGS_B.setval(instr, sf.flag_b);
174
175         } else if (d instanceof Instruction.HalfLiteral) {
176             Instruction.HalfLiteral inst = (Instruction.HalfLiteral)d;
177             if (inst.pump != null) {
178                 if (inst.high) {
179                     instr = LITERAL_HI.set(instr);
180                     instr = LITERAL_HI.setval(instr, inst.literal);
181                 } else {
182                     instr = LITERAL_LO.set(instr);
183                     instr = LITERAL_LO.setval(instr, inst.literal);
184                 }
185             } else {
186                 instr = inst.literal;
187             }
188
189         } else if (d instanceof Instruction.DecrLoop) {
190             instr = DL.set(instr);
191
192         } else if (d instanceof Instruction.Counter) {
193             Instruction.Counter ic = (Instruction.Counter)d;
194             if      (ic.dest == DATA_LATCH && ic.source == LOOP_COUNTER)   instr = TAKE_LOOP.set(instr);
195             else if (ic.dest == LOOP_COUNTER && ic.source == DATA_LATCH)   instr = LOOP_FROM_DATA.set(instr);
196             else if (ic.dest == REPEAT_COUNTER && ic.source == DATA_LATCH) instr = REPEAT_FROM_DATA.set(instr);
197             else if (ic.dest == REPEAT_COUNTER && ic.source == STANDING)   instr = REPEAT_FROM_STANDING.set(instr);
198             else if (ic.dest == REPEAT_COUNTER)                            instr = REPEAT_FROM_LITERAL.setval(REPEAT_FROM_LITERAL.set(instr), ic.source);
199             else if (ic.dest == LOOP_COUNTER)                              instr = LOOP_FROM_LITERAL.setval(LOOP_FROM_LITERAL.set(instr), ic.source);
200             else throw new RuntimeException();
201
202         } else if (d instanceof Instruction.Move) {
203             Instruction.Move inst = (Instruction.Move)d;
204             instr  = MOVE.set(instr);
205             if (inst.tokenIn)                    instr = TI.set(instr);
206             if (inst.dataIn)                     instr = DI.set(instr);
207             if (inst.latch)                      instr = DC.set(instr);
208             if (inst.dataOut)                    instr = DO.set(instr);
209             if (inst.tokenOut)                   instr = TO.set(instr);
210             if (inst.dataOutDest)                instr = PATH_DATA.set(instr);
211             else {
212                 instr  = PATH_LITERAL.set(instr);
213                 instr  = PATH_LITERAL.setval(instr, inst.dest==null?0:getDestAddr(inst.dest));
214             }
215
216         } else {
217             throw new RuntimeException("unrecognized instruction " + d);
218
219         }
220         return instr;
221     }
222
223     public void writeInstruction(DataOutputStream os, Instruction d) throws IOException {
224         long instr = writeInstruction(d);
225         for(int i=5; i>=0; i--)
226             os.write(getIntField(i*8+7, i*8, instr));
227    }
228
229 }