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