1 package edu.berkeley.fleet.ies44;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.*;
4 import edu.berkeley.fleet.util.*;
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.*;
10 public abstract class InstructionEncoder {
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;
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................");
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..........");
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..........................");
51 /** get the bits describing this box's location on the DESTINATION HORN */
52 protected abstract long getDestAddr(Destination box);
54 /** get the bits describing this box's location on the INSTRUCTION HORN */
55 protected abstract long getBoxInstAddr(Pump box);
57 /** given an INSTRUCTION HORN address, retrieve the corresponding Pump object */
58 protected abstract Pump getBoxByInstAddr(long dest);
60 /** given a DESTINATION HORN address, retrieve the corresponding Pump object */
61 protected abstract Destination getDestByAddr(long dest);
63 /** read a machine-formatted instruction from a file (into a Java object) */
64 public Instruction readInstruction(DataInputStream is) throws IOException {
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) {
79 public Instruction readInstruction(long inst) {
80 Pump name = getBoxByInstAddr(PUMP_NAME.getval(inst));
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);
88 if (DL.get(inst)) return new Instruction.DecrLoop(name);
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));
98 if (!MOVE.get(inst)) throw new RuntimeException("unknown instruction: 0x" + Long.toString(inst, 16));
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 Instruction.PredicatedInstruction.P_ALWAYS, /* FIXME */
122 public long writeInstruction(Instruction d) {
126 instr = PUMP_NAME.setval(instr, getBoxInstAddr(d.pump));
128 if (d instanceof Instruction.CodeBagDescriptor) {
129 Instruction.CodeBagDescriptor lc = (Instruction.CodeBagDescriptor)d;
130 // MAJOR MAJOR FIXME: upper half here...
131 d = new Instruction.HalfLiteral(lc.pump, ((lc.offset << WIDTH_CODEBAG_SIZE)) | lc.size, 1, false);
134 if (d instanceof Instruction.UnClog) {
135 instr = UNCLOG.set(instr);
137 } else if (d instanceof Instruction.Kill) {
138 Instruction.Kill k = (Instruction.Kill)d;
139 instr = KILL.set(instr);
140 instr = KILL.setval(instr, k.count);
142 } else if (d instanceof Instruction.Clog) {
143 instr = CLOG.set(instr);
145 } else if (d instanceof Instruction.Massacre) {
146 instr = MASSACRE.set(instr);
148 } else if (d instanceof Instruction.SetFlags) {
149 Instruction.SetFlags sf = (Instruction.SetFlags)d;
150 instr = FLAGS.set(instr);
151 instr = FLAGS_A.setval(instr, sf.flag_a);
152 instr = FLAGS_B.setval(instr, sf.flag_b);
154 } else if (d instanceof Instruction.HalfLiteral) {
155 Instruction.HalfLiteral inst = (Instruction.HalfLiteral)d;
156 if (inst.pump != null) {
158 instr = LITERAL_HI.set(instr);
159 instr = LITERAL_HI.setval(instr, inst.literal);
161 instr = LITERAL_LO.set(instr);
162 instr = LITERAL_LO.setval(instr, inst.literal);
165 instr = inst.literal;
168 } else if (d instanceof Instruction.DecrLoop) {
169 instr = DL.set(instr);
171 } else if (d instanceof Instruction.Counter) {
172 Instruction.Counter ic = (Instruction.Counter)d;
173 if (ic.dest == DATA_LATCH && ic.source == LOOP_COUNTER) instr = TAKE_LOOP.set(instr);
174 else if (ic.dest == REPEAT_COUNTER && ic.source == DATA_LATCH) instr = REPEAT_FROM_DATA.set(instr);
175 else if (ic.dest == LOOP_COUNTER && ic.source == DATA_LATCH) instr = LOOP_FROM_DATA.set(instr);
176 else if (ic.dest == REPEAT_COUNTER) instr = REPEAT_FROM_LITERAL.setval(REPEAT_FROM_LITERAL.set(instr), ic.source);
177 else if (ic.dest == LOOP_COUNTER) instr = LOOP_FROM_LITERAL.setval(LOOP_FROM_LITERAL.set(instr), ic.source);
178 else throw new RuntimeException();
180 } else if (d instanceof Instruction.Move) {
181 Instruction.Move inst = (Instruction.Move)d;
182 instr = MOVE.set(instr);
183 if (inst.tokenIn) instr = TI.set(instr);
184 if (inst.dataIn) instr = DI.set(instr);
185 if (inst.latch) instr = DC.set(instr);
186 if (inst.dataOut) instr = DO.set(instr);
187 if (inst.tokenOut) instr = TO.set(instr);
188 if (inst.dataOutDest) instr = PATH_DATA.set(instr);
190 instr = PATH_LITERAL.set(instr);
191 instr = PATH_LITERAL.setval(instr, inst.dest==null?0:getDestAddr(inst.dest));
193 if (inst.standing) instr = STAND.set(instr);
196 throw new RuntimeException("unrecognized instruction " + d);
202 public void writeInstruction(DataOutputStream os, Instruction d) throws IOException {
203 long instr = writeInstruction(d);
204 for(int i=5; i>=0; i--)
205 os.write(getIntField(i*8+7, i*8, instr));