7936ad0dcedf2a24c3c6a94e51f49ca812bcd296
[fleet.git] / src / edu / berkeley / fleet / two / FleetTwoFleet.java
1 package edu.berkeley.fleet.two;
2 import java.io.*;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.util.*;
5 import edu.berkeley.fleet.api.Instruction.Set;
6 import static edu.berkeley.fleet.api.Instruction.Set.*;
7 import static edu.berkeley.fleet.api.Instruction.*;
8 import static edu.berkeley.fleet.api.Predicate.*;
9
10 /** common subclass for the "FleetTwo" generation of processors, all 37-bit wide, with AM33 encoding */
11 public abstract class FleetTwoFleet extends Fleet {
12
13     private final boolean hack = "true".equals(System.getProperty("inverted-dc-bit-hack", "false"));
14
15     public final Mask PACKET_TOKEN;
16     public final Mask PACKET_IS_TOKEN;
17     public final Mask PACKET_DATA;
18     public final Mask PACKET_SIGNAL;
19     public final Mask PACKET_DEST;
20
21     public final Mask CBD_SIZE;
22     public final Mask CBD_OFFSET;
23
24     public final Mask WHOLE_WORD;
25     public final Mask DISPATCH_PATH;
26     public final Mask DISPATCH_INSTR;
27     public final Mask NOT_INTERRUPTIBLE;
28     public final Mask IS_INTERRUPTIBLE;
29     public final Mask P;
30     public final Mask P_NOT_A;
31     public final Mask P_A;
32     public final Mask P_NOT_B;
33     public final Mask P_B;
34     public final Mask P_UNUSED;
35     public final Mask P_OLC_ZERO;
36     public final Mask P_OLC_NONZERO;
37     public final Mask P_ALWAYS;
38
39     public final Mask SHIFT;
40
41     public final Mask TAIL;
42     public final Mask HEAD;
43     public final Mask ABORT;
44
45     public final Mask MOVE;
46     public final Mask TI;
47     public final Mask DI;
48     public final Mask FLUSH;
49     public final Mask DC;
50     public final Mask DO;
51     public final Mask TO;
52     public final Mask PATH_IMMEDIATE;
53     public final Mask PATH_DATA;
54     public final Mask PATH_NOCHANGE;
55
56     public final Mask SET_OLC_FROM_IMMEDIATE;
57     public final Mask SET_OLC_FROM_DATA_LATCH;
58     public final Mask SET_OLC_FROM_OLC_MINUS_ONE;
59
60     public final Mask SET_ILC_FROM_IMMEDIATE;
61     public final Mask SET_ILC_FROM_INFINITY;
62     public final Mask SET_ILC_FROM_DATA_LATCH;
63
64     public final Mask SET_IMMEDIATE;
65     public final Mask SET_IMMEDIATE_EXTEND;
66
67     public final Mask SET_FLAGS;
68     public final Mask SET_FLAGS_A;
69     public final Mask SET_FLAGS_B;
70     public final Mask SET_FLAGS_VALUE_A;
71     public final Mask SET_FLAGS_VALUE_NOT_A;
72     public final Mask SET_FLAGS_VALUE_B;
73     public final Mask SET_FLAGS_VALUE_NOT_B;
74     public final Mask SET_FLAGS_VALUE_C;
75     public final Mask SET_FLAGS_VALUE_NOT_C;
76
77     public final int  WIDTH_WORD;
78     public final int  WIDTH_PACKET;
79
80     public final int DataLatch_WIDTH;
81     private final long mask;
82
83     public FleetTwoFleet() { this(false); }
84
85     public FleetTwoFleet(boolean useInternalEncoding) {
86         PACKET_TOKEN               = new Mask("v.................................................");
87         PACKET_IS_TOKEN            = new Mask("1.................................................");
88         PACKET_DATA                = new Mask(".vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv............");
89         PACKET_SIGNAL              = new Mask("......................................v...........");
90         PACKET_DEST                = new Mask(".......................................vvvvvvvvvvv");
91
92         CBD_SIZE                   = new Mask("...............................vvvvvv");
93         CBD_OFFSET                 = new Mask("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv......");
94
95         WHOLE_WORD                 = new Mask("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
96
97         if (useInternalEncoding) {
98
99             DISPATCH_PATH              = new Mask("v....................................");
100             DISPATCH_INSTR             = new Mask(".vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
101             P                          = new Mask(".vvvvvv..............................");
102             P_NOT_A                    = new Mask(".010001..............................");
103             P_A                        = new Mask(".010010..............................");
104             P_NOT_B                    = new Mask(".010100..............................");
105             P_B                        = new Mask(".011000..............................");
106             P_UNUSED                   = new Mask(".111111..............................");
107             P_OLC_ZERO                 = new Mask(".100000..............................");
108             P_OLC_NONZERO              = new Mask(".010000..............................");
109             P_ALWAYS                   = new Mask(".000000..............................");
110
111             HEAD                       = new Mask(".......1.............................");
112             ABORT                      = new Mask("........1......1.........000100000001");
113             TAIL                       = new Mask(".........1...........................");
114
115             // actually "is interruptible"
116             NOT_INTERRUPTIBLE          = new Mask("...........0....0....................");
117             IS_INTERRUPTIBLE           = new Mask("...........1....1....................");
118
119             SHIFT                      = new Mask("..........1......1vvvvvvvvvvvvvvvvvvv");
120             SET_IMMEDIATE              = new Mask("..........1......0.....vvvvvvvvvvvvvv");
121             SET_IMMEDIATE_EXTEND       = new Mask("..........1......0....v..............");
122             MOVE                       = new Mask("............1........................");
123             TI                         = new Mask("............1.....1..................");
124             DI                         = new Mask("............1......1.................");
125             FLUSH                      = new Mask("..........1......0.....011...........");
126             DC                         = new Mask("............1.......1................");
127             DO                         = new Mask("............1........1...............");
128             TO                         = new Mask("............1.........1..............");
129             PATH_IMMEDIATE             = new Mask("............1..........1vvvvvvvvvvvvv");
130             PATH_DATA                  = new Mask("............1..........00............");
131             PATH_NOCHANGE              = new Mask("............1..........01............");
132
133             SET_OLC_FROM_OLC_MINUS_ONE = new Mask(".............1..1....................");
134             SET_OLC_FROM_DATA_LATCH    = new Mask("..............1.11...................");
135             SET_OLC_FROM_IMMEDIATE     = new Mask("..............1.10.............vvvvvv");
136
137             SET_ILC_FROM_DATA_LATCH    = new Mask("..............1.01...................");
138             SET_ILC_FROM_IMMEDIATE     = new Mask("..............1.00...........0.vvvvvv");
139             SET_ILC_FROM_INFINITY      = new Mask("..............1.0............1.......");
140
141             SET_FLAGS                  = new Mask("...............1.....................");
142             SET_FLAGS_A                = new Mask("...............1...............vvvvvv");
143             SET_FLAGS_B                = new Mask("...............1.........vvvvvv......");
144
145             SET_FLAGS_VALUE_A          = new Mask(".....1");
146             SET_FLAGS_VALUE_NOT_A      = new Mask("....1.");
147             SET_FLAGS_VALUE_B          = new Mask("...1..");
148             SET_FLAGS_VALUE_NOT_B      = new Mask("..1...");
149             SET_FLAGS_VALUE_C          = new Mask(".1....");
150             SET_FLAGS_VALUE_NOT_C      = new Mask("1.....");
151
152         } else {
153             DISPATCH_PATH              = new Mask("vvvvvvvvvvv..........................");
154             DISPATCH_INSTR             = new Mask("...........vvvvvvvvvvvvvvvvvvvvvvvvvv");
155             NOT_INTERRUPTIBLE          = new Mask("...........1.........................");
156             IS_INTERRUPTIBLE           = new Mask("...........0.........................");
157             P                          = new Mask(".............vvv.....................");
158             P_NOT_A                    = new Mask(".............000.....................");
159             P_A                        = new Mask(".............001.....................");
160             P_NOT_B                    = new Mask(".............010.....................");
161             P_B                        = new Mask(".............011.....................");
162             P_UNUSED                   = new Mask(".............100.....................");
163             P_OLC_ZERO                 = new Mask(".............101.....................");
164             P_OLC_NONZERO              = new Mask(".............110.....................");
165             P_ALWAYS                   = new Mask(".............111.....................");
166
167             SHIFT                      = new Mask("................00vvvvvvvvvvvvvvvvvvv");
168
169             ABORT                      = new Mask("................1100.................");  // note: has a predicate!
170
171             HEAD                       = new Mask("................1110.................");
172             TAIL                       = new Mask("................1111.................");
173
174             MOVE                       = new Mask("................01...................");
175             TI                         = new Mask("................011..................");
176             DI                         = new Mask("................01.1.................");
177             FLUSH                      = new Mask("................01.01................");
178             DC                         = new Mask("................01.11................");
179             DO                         = new Mask("................01...1...............");
180             TO                         = new Mask("................01....1..............");
181             PATH_IMMEDIATE             = new Mask("................01.....1vvvvvvvvvvvvv");
182             PATH_DATA                  = new Mask("................01.....01............");
183             PATH_NOCHANGE              = new Mask("................01.....00............");
184
185             SET_OLC_FROM_IMMEDIATE     = new Mask("................101000100......vvvvvv");
186             SET_OLC_FROM_DATA_LATCH    = new Mask("................101000010............");
187             SET_OLC_FROM_OLC_MINUS_ONE = new Mask("................101000001............");
188
189             SET_ILC_FROM_IMMEDIATE     = new Mask("................10010010......0vvvvvv");
190             SET_ILC_FROM_INFINITY      = new Mask("................10010010......1......");
191             SET_ILC_FROM_DATA_LATCH    = new Mask("................10010001.............");
192
193             SET_IMMEDIATE              = new Mask("................100010.vvvvvvvvvvvvvv");
194             SET_IMMEDIATE_EXTEND       = new Mask("................100010v..............");
195
196             SET_FLAGS                  = new Mask("................100001...............");
197             SET_FLAGS_A                = new Mask("................100001...vvvvvv......");
198             SET_FLAGS_B                = new Mask("................100001.........vvvvvv");
199
200             SET_FLAGS_VALUE_A          = new Mask("1.....");
201             SET_FLAGS_VALUE_NOT_A      = new Mask(".1....");
202             SET_FLAGS_VALUE_B          = new Mask("..1...");
203             SET_FLAGS_VALUE_NOT_B      = new Mask("...1..");
204             SET_FLAGS_VALUE_C          = new Mask("....1.");
205             SET_FLAGS_VALUE_NOT_C      = new Mask(".....1");
206         }
207
208         WIDTH_WORD                 = PACKET_DATA.valmaskwidth;
209         WIDTH_PACKET               = PACKET_TOKEN.width;
210         DataLatch_WIDTH = SET_IMMEDIATE.valmaskmax-SET_IMMEDIATE.valmaskmin+1;  // FIXME: this is an abstraction breakage
211         mask  = ~(-1L << DataLatch_WIDTH);
212     }
213
214     // Misc //////////////////////////////////////////////////////////////////////////////
215
216     public boolean isSmallEnoughToFit(long immediate) {
217         if ((immediate & ~mask) == 0)   return true;
218         if ((immediate |  mask) == -1L) return true;
219         return false;
220     }
221
222     public boolean isSmallEnoughToFit(BitVector immediate) {
223         boolean b = immediate.get((int)DataLatch_WIDTH);
224         for(int i=DataLatch_WIDTH+1; i<immediate.length(); i++)
225             if (immediate.get(i) != b) return false;
226         return true;
227     }
228
229     public int getWordWidth() { return 37; }
230
231     //////////////////////////////////////////////////////////////////////////////
232
233     /** get the bits describing this box's location on the DESTINATION HORN */
234     protected abstract BitVector getDestAddr(Path box);
235
236     private static final BitVector SIGNAL_ZERO = new BitVector(1);
237     private static final BitVector SIGNAL_ONE  = new BitVector(1).set(0,true);
238
239     /** decode a path, given the starting point and the bits that comprise it */
240     public Path getPathByAddr(Dock source, BitVector dest) {
241         for(Ship ship : this)
242             for(Dock bb : ship) {
243                 for(Destination d : new Destination[] { bb.getInstructionDestination(), bb.getDataDestination() }) {
244                     for(BitVector signal : new BitVector[] { SIGNAL_ZERO, SIGNAL_ONE }) {
245                         Path p = (Path)source.getPath(d, signal);
246                         if (getDestAddr(p).equalsZeroExtended(dest)) return p;
247                     }
248                 }
249             }
250         return null;
251     }
252
253     public Instruction decodeInstruction(BitVector instruction, Dock dispatchFrom) {
254         return readInstruction(instruction.toLong(), dispatchFrom);
255     }
256
257     public BitVector   encodeInstruction(Instruction instr, Dock dispatchFrom) {
258         return new BitVector(getWordWidth()).set(writeInstruction(instr, dispatchFrom));
259     }
260
261     public int getShiftWidth() { return SHIFT.valmaskwidth; }
262     public int getSetWidth() { return SET_IMMEDIATE.valmaskwidth+1; }
263
264     // FIXME: should use BitVector here
265     public Instruction readInstruction(long inst, Dock dispatchFrom) {
266         Dock dock = getPathByAddr(dispatchFrom, DISPATCH_PATH.getvalAsBitVector(inst)).getDestination().getDock();
267
268         if (TAIL.get(inst))   return new Tail(dock);
269         if (HEAD.get(inst))   return new Head(dock);
270
271         Predicate predicate = Default;
272         if (P_ALWAYS.get(inst)) predicate = IgnoreFlagD;
273         if (P_OLC_NONZERO.get(inst))    predicate = Default;
274         if (P_OLC_ZERO.get(inst))    predicate = FlagD;
275         if (P_A.get(inst))      predicate = FlagA;
276         if (P_B.get(inst))      predicate = FlagB;
277         if (P_NOT_A.get(inst))  predicate = NotFlagA;
278         if (P_NOT_B.get(inst))  predicate = NotFlagB;
279
280         if (FLUSH.get(inst))
281             return new Flush(dock, predicate);
282         if (ABORT.get(inst))
283             return new Abort(dock, predicate);
284         if (SHIFT.get(inst))                return new Shift(dock, predicate, new BitVector(dock.getShip().getFleet().getShiftWidth()).set(SHIFT.getval(inst)));
285         if (SET_IMMEDIATE.get(inst)) {
286             boolean extend = SET_IMMEDIATE_EXTEND.getval(inst) != 0;
287             long immediate = SET_IMMEDIATE.getval(inst);
288             if (extend) immediate |= (-1L << DataLatch_WIDTH);
289             return new Set(dock, predicate, SetDest.DataLatch, (immediate));
290         }
291
292         if (SET_OLC_FROM_OLC_MINUS_ONE.get(inst))
293             return new Set(dock, predicate, SetDest.OuterLoopCounter, SetSource.Decrement);
294         if (SET_OLC_FROM_IMMEDIATE.get(inst))
295             return new Set(dock, predicate, SetDest.OuterLoopCounter, (SET_OLC_FROM_IMMEDIATE.getval(inst)));
296         if (SET_ILC_FROM_IMMEDIATE.get(inst))
297             return new Set(dock, predicate, SetDest.InnerLoopCounter, (SET_ILC_FROM_IMMEDIATE.getval(inst)));
298         if (SET_OLC_FROM_DATA_LATCH.get(inst))
299             return new Set(dock, predicate, SetDest.OuterLoopCounter, SetSource.DataLatch);
300         if (SET_ILC_FROM_DATA_LATCH.get(inst))
301             return new Set(dock, predicate, SetDest.InnerLoopCounter, SetSource.DataLatch);
302         if (SET_ILC_FROM_INFINITY.get(inst))
303             return new Set(dock, predicate, SetDest.InnerLoopCounter, SetSource.Infinity);
304         if (SET_FLAGS.get(inst)) {
305             long flag_a = SET_FLAGS_A.getval(inst);
306             long flag_b = SET_FLAGS_B.getval(inst);
307             FlagFunction ap = FlagFunction.ZERO;
308             FlagFunction bp = FlagFunction.ZERO;
309             if (SET_FLAGS_VALUE_A    .get(flag_a)) ap = ap.add(FlagA    );
310             if (SET_FLAGS_VALUE_NOT_A.get(flag_a)) ap = ap.add(NotFlagA );
311             if (SET_FLAGS_VALUE_B    .get(flag_a)) ap = ap.add(FlagB    );
312             if (SET_FLAGS_VALUE_NOT_B.get(flag_a)) ap = ap.add(NotFlagB );
313             if (SET_FLAGS_VALUE_C    .get(flag_a)) ap = ap.add(FlagC    );
314             if (SET_FLAGS_VALUE_NOT_C.get(flag_a)) ap = ap.add(NotFlagC );
315             if (SET_FLAGS_VALUE_A    .get(flag_b)) bp = bp.add(FlagA    );
316             if (SET_FLAGS_VALUE_NOT_A.get(flag_b)) bp = bp.add(NotFlagA );
317             if (SET_FLAGS_VALUE_B    .get(flag_b)) bp = bp.add(FlagB    );
318             if (SET_FLAGS_VALUE_NOT_B.get(flag_b)) bp = bp.add(NotFlagB );
319             if (SET_FLAGS_VALUE_C    .get(flag_b)) bp = bp.add(FlagC    );
320             if (SET_FLAGS_VALUE_NOT_C.get(flag_b)) bp = bp.add(NotFlagC );
321             return new Set(dock,  predicate, ap, bp);
322         }
323         if (MOVE.get(inst))
324             return new Move(dock,
325                             predicate,
326                             !NOT_INTERRUPTIBLE.get(inst),
327                             PATH_DATA.get(inst)?null:getPathByAddr(dock, PATH_IMMEDIATE.getvalAsBitVector(inst)),
328                             TI.get(inst),
329                             DI.get(inst),
330                             hack?(!DC.get(inst)):DC.get(inst),
331                             PATH_DATA.get(inst),
332                             DO.get(inst),
333                             TO.get(inst)
334                             );
335         throw new RuntimeException("unknown instruction: 0x" + Long.toString(inst, 16));
336     }
337
338     // FIXME: should use BitVector here
339     public long writeInstruction(Instruction d, Dock dispatchFrom) {
340         long instr = 0;
341
342         if (d.dock != null) {
343             BitVector bv = getDestAddr(dispatchFrom.getPath(d.dock.getInstructionDestination(),null));
344             BitVector bv2 = new BitVector(DISPATCH_PATH.valmaskwidth);
345             for(int i=0; i<Math.min(bv.length(),bv2.length()); i++)
346                 bv2.set(i, bv.get(i));
347             instr = DISPATCH_PATH.setval(instr, bv2);
348         }
349
350         boolean dl = false;
351         Instruction pi = d;
352         switch(pi.predicate) {
353             case IgnoreFlagD:         instr = P_ALWAYS.set(instr); break;
354             case FlagD:         instr = P_OLC_ZERO.set(instr); break;
355             case Default: instr = P_OLC_NONZERO.set(instr); break;
356             case FlagA:      instr = P_A.set(instr);      break;
357             case FlagB:      instr = P_B.set(instr);      break;
358             case NotFlagA:  instr = P_NOT_A.set(instr);      break;
359             case NotFlagB:  instr = P_NOT_B.set(instr);      break;
360             default: throw new RuntimeException("error!");
361         }
362
363         if (d instanceof Tail) {
364             instr = TAIL.set(instr);
365
366         } else if (d instanceof Head) {
367             instr = HEAD.set(instr);
368
369         } else if (d instanceof Shift) {
370             Shift shift = (Shift)d;
371             instr = SHIFT.set(instr);
372             instr = SHIFT.setval(instr, shift.immediate.getBitVector());
373
374         } else if (d instanceof Flush) {
375             instr = FLUSH.set(instr);
376
377         } else if (d instanceof Abort) {
378             instr = ABORT.set(instr);
379
380         } else if (d instanceof Set) {
381             Set s = (Set)d;
382             switch(s.dest) {
383                 case InnerLoopCounter:
384                     switch(s.source) {
385                         case DataLatch:
386                             instr = SET_ILC_FROM_DATA_LATCH.set(instr);
387                             break;
388                         case Immediate:
389                             instr = SET_ILC_FROM_IMMEDIATE.setval(SET_ILC_FROM_IMMEDIATE.set(instr), s.immediate);
390                             break;
391                         case Infinity:
392                             instr = SET_ILC_FROM_INFINITY.set(instr);
393                             break;
394                     }
395                     break;
396                 case OuterLoopCounter:
397                     switch(s.source) {
398                         case Decrement:
399                             instr = SET_OLC_FROM_OLC_MINUS_ONE.set(instr);
400                             break;
401                         case DataLatch:
402                             instr = SET_OLC_FROM_DATA_LATCH.set(instr);
403                             break;
404                         case Immediate:
405                             instr = SET_OLC_FROM_IMMEDIATE.setval(SET_OLC_FROM_IMMEDIATE.set(instr), s.immediate);
406                             break;
407                     }
408                     break;
409                 case Flags: {
410                     instr = SET_FLAGS.set(instr);
411                     instr = SET_FLAGS_A.setval(instr, flagFunctionToLong(s.newFlagA));
412                     instr = SET_FLAGS_B.setval(instr, flagFunctionToLong(s.newFlagB));
413                     break;
414                 }
415                 case DataLatch: {
416                     instr = SET_IMMEDIATE.set(instr);
417                     instr = SET_IMMEDIATE_EXTEND.setval(instr, s.immediate < 0 ? 1 : 0);
418                     instr = SET_IMMEDIATE.setval(instr, s.immediate & ~(-1L << DataLatch_WIDTH));
419                     break;
420                 }
421             }
422
423         } else if (d instanceof Move) {
424             Move inst = (Move)d;
425             instr  = MOVE.set(instr);
426             if (inst.tokenIn)                    instr = TI.set(instr);
427             if (inst.dataIn)                     instr = DI.set(instr);
428             if (hack) {
429                 if (!inst.latchData)                 instr = DC.set(instr);
430                 System.err.println("WARNING: employing inverted-Dc-bit hack -- DO NOT FORGET TO DISABLE THIS WHEN THE NETLIST GETS UPDATED!");
431             } else {
432                 if (inst.latchData)                  instr = DC.set(instr);
433             }
434             if (inst.dataOut)                    instr = DO.set(instr);
435             if (inst.tokenOut)                   instr = TO.set(instr);
436             if (!inst.interruptible)             instr = NOT_INTERRUPTIBLE.set(instr);
437             else                                 instr = IS_INTERRUPTIBLE.set(instr);
438
439             if (inst.latchPath)                  instr = PATH_DATA.set(instr);
440             else if (inst.path!=null) {
441                 instr  = PATH_IMMEDIATE.set(instr);
442                 BitVector bv  = getDestAddr(inst.path);
443                 BitVector bv2 = new BitVector(PATH_IMMEDIATE.valmaskwidth);
444                 for(int i=0; i<Math.min(bv.length(),bv2.length()); i++)
445                     bv2.set(i, bv.get(i));
446                 instr  = PATH_IMMEDIATE.setval(instr, bv2);
447             } else {
448                 instr  = PATH_NOCHANGE.set(instr);
449             }
450
451         } else {
452             throw new RuntimeException("unrecognized instruction " + d.getClass().getName());
453
454         }
455         return instr;
456     }
457
458     private long flagFunctionToLong(FlagFunction ff) {
459         long ret = 0;
460         for(Predicate p : ff)
461             switch(p) {
462                 case FlagA    : ret = SET_FLAGS_VALUE_A    .set(ret); break;
463                 case NotFlagA : ret = SET_FLAGS_VALUE_NOT_A.set(ret); break;
464                 case FlagB    : ret = SET_FLAGS_VALUE_B    .set(ret); break;
465                 case NotFlagB : ret = SET_FLAGS_VALUE_NOT_B.set(ret); break;
466                 case FlagC    : ret = SET_FLAGS_VALUE_C    .set(ret); break;
467                 case NotFlagC : ret = SET_FLAGS_VALUE_NOT_C.set(ret); break;
468                 default: throw new RuntimeException();
469             }
470         return ret;
471     }
472
473 }