added fleet api classes
[fleet.git] / src / edu / berkeley / fleet / CodeBag.java
1 package edu.berkeley.fleet;
2
3 import java.util.*;
4 import java.io.*;
5
6 /** a codebag */
7 public class CodeBag {
8
9     private static int max_allocated_descriptor = 1;
10     private static HashMap<Integer,CodeBag> codeBagsByDescriptor = new HashMap<Integer,CodeBag>();
11
12     public static CodeBag getCodeBagByDescriptor(int descriptor) {
13         return codeBagsByDescriptor.get(descriptor);
14     }
15
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>();
21
22     public CodeBag(CodeBag parent, String name) {
23         this.parent = parent;
24         this.name = name;
25         this.descriptor = max_allocated_descriptor++;
26         codeBagsByDescriptor.put(descriptor, this);
27         if (parent != null && name != null)
28             parent.add(name, this);
29     }
30
31     public void dispatch(Fleet fleet) {
32         for(Dispatchable d : dispatchables) {
33             Log.dispatch(d);
34             d.dispatch(fleet);
35         }
36     }
37
38     public void add(Dispatchable instr) {
39         dispatchables.add(instr);
40     }
41
42     public void add(String name, CodeBag instr) {
43         codeBags.put(name, instr);
44     }
45
46     public int getDescriptor() {
47         return descriptor;
48     }
49
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);
55     }
56
57     public void dump(OutputStream os, long data_) throws Exception {
58         int data = (int)data_;
59         os.write((byte)data);
60         //System.out.println("0x"+Integer.toString(data,16)+" ");
61         System.out.println(data);
62     }
63     public void dump(Fleet fleet) throws Exception {
64         OutputStream os = new FileOutputStream("fleet.bin");
65
66         for(Dispatchable d : dispatchables) {
67             if (d instanceof Instruction) {
68                 Instruction inst = (Instruction)d;
69
70                 long instr = inst.destination.resolve(fleet).addr << 1;
71
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
82                 long out = 0;
83                 out |= new PortReference("command", "data").resolve(fleet).addr;
84                 out |= instr << 11;
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;
93                 long out = 0;
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);
102             }
103
104         }
105         os.flush();
106         os.close();
107     }
108
109     public String toString() {
110         if (name != null) return name;
111         StringBuffer ret = new StringBuffer();
112         for(Dispatchable d : dispatchables) {
113             ret.append(d);
114             ret.append("\n");
115         }
116         for(String n : codeBags.keySet()) {
117             ret.append(n + ": ");
118             ret.append(codeBags.get(n));
119             ret.append("\n");
120         }
121         return "{\n  "+Log.indent(ret.toString(), "  ")+"\n}";
122     }
123
124
125 }