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