1 package edu.berkeley.fleet;
9 private static int max_allocated_descriptor = 1;
10 private static HashMap<Integer,CodeBag> codeBagsByDescriptor = new HashMap<Integer,CodeBag>();
12 public static CodeBag getCodeBagByDescriptor(int descriptor) {
13 return codeBagsByDescriptor.get(descriptor);
16 private final int descriptor;
17 private final CodeBag parent;
18 private final String name;
19 private ArrayList<Dispatchable> dispatchables = new ArrayList<Dispatchable>();
20 private HashMap<String,CodeBag> codeBags = new HashMap<String,CodeBag>();
22 public CodeBag(CodeBag parent, String name) {
25 this.descriptor = max_allocated_descriptor++;
26 codeBagsByDescriptor.put(descriptor, this);
27 if (parent != null && name != null)
28 parent.add(name, this);
31 public void dispatch(Fleet fleet) {
32 for(Dispatchable d : dispatchables) {
38 public void add(Dispatchable instr) {
39 dispatchables.add(instr);
42 public void add(String name, CodeBag instr) {
43 codeBags.put(name, instr);
46 public int getDescriptor() {
50 public CodeBag getCodeBag(String name) {
51 CodeBag ret = codeBags.get(name);
52 if (ret != null) return ret;
53 if (parent==null) return null;
54 return parent.getCodeBag(name);
57 public void dump(OutputStream os, long data_) throws Exception {
58 int data = (int)data_;
60 //System.out.println("0x"+Integer.toString(data,16)+" ");
61 System.out.println(data);
63 public void dump(Fleet fleet) throws Exception {
64 OutputStream os = new FileOutputStream("fleet.bin");
66 for(Dispatchable d : dispatchables) {
67 if (d instanceof Instruction) {
68 Instruction inst = (Instruction)d;
70 long instr = inst.destination.resolve(fleet).addr << 1;
72 instr |= (((long)inst.count) << (11+1));
73 if (inst.ack) instr |= (1L << (11+1+7+0));
74 if (inst.dataOut) instr |= (1L << (11+1+7+1));
75 if (inst.latch) instr |= (1L << (11+1+7+2));
76 if (inst.dataIn) instr |= (1L << (11+1+7+3));
77 if (inst.trigger) instr |= (1L << (11+1+7+4));
78 instr |= ((long)inst.source.resolve(fleet).instr_addr) << (11+5+7+1);
79 // 1111 00001 0000001 00000001110 0
80 // 1111 00001 0000001 00000001110 0
81 // 110000010000001000000011100
83 out |= new PortReference("command", "data").resolve(fleet).addr;
85 dump(os, (out >> (5*8)) & 0xff);
86 dump(os, (out >> (4*8)) & 0xff);
87 dump(os, (out >> (3*8)) & 0xff);
88 dump(os, (out >> (2*8)) & 0xff);
89 dump(os, (out >> (1*8)) & 0xff);
90 dump(os, (out >> (0*8)) & 0xff);
91 } else if (d instanceof Literal.LiteralDatum) {
92 Literal.LiteralDatum ld = (Literal.LiteralDatum)d;
94 out |= ld.destination.resolve(fleet).addr;
95 out |= ((long)ld.data) << 11;
96 dump(os, (out >> (5*8)) & 0xff);
97 dump(os, (out >> (4*8)) & 0xff);
98 dump(os, (out >> (3*8)) & 0xff);
99 dump(os, (out >> (2*8)) & 0xff);
100 dump(os, (out >> (1*8)) & 0xff);
101 dump(os, (out >> (0*8)) & 0xff);
109 public String toString() {
110 if (name != null) return name;
111 StringBuffer ret = new StringBuffer();
112 for(Dispatchable d : dispatchables) {
116 for(String n : codeBags.keySet()) {
117 ret.append(n + ": ");
118 ret.append(codeBags.get(n));
121 return "{\n "+Log.indent(ret.toString(), " ")+"\n}";