a01369b219e67d88c861339be84dc35a0ffc5e66
[eltron.git] / src / edu / berkeley / cs / obits / Atmel.java
1 package edu.berkeley.cs.obits;
2 import java.util.*;
3 import java.io.*;
4 import org.ibex.util.*;
5
6 public class Atmel {
7
8     public static String pad(int len, String s) { if (s.length() >= len) return s.toUpperCase(); return "0"+pad(len-1,s); }
9     public static String hex32(int i) { return pad(8, Long.toString(i & 0xffffffffffffffffL, 16)); }
10     public static String hex24(int i) { return pad(6, Integer.toString((i & 0xffffff), 16)); }
11     public static String hex8(int i)  { return pad(2, Integer.toString((i & 0xff), 16)); }
12     public static String bin8(int i)  { return pad(8, Integer.toString((i & 0xff), 2)); }
13     public static String dec2(int i)  { return pad(2, Integer.toString(i & 0xff)); }
14
15     public static void main(String[] s) throws Exception {
16         Log.level = Log.DEBUG;
17         new Bits().read(System.in);
18     }
19     private static class Bits {
20         private byte[] bits    = new byte[0];
21         //public int  get(int whichByte, int whichBit, int len) { }
22         //public void set(int whichByte, int whichBit, int len, int val) { }
23
24         private int    control = 0;
25         public int  control()            { return control; }
26         public void control(int control) { this.control = control; }
27
28         public void read2(InputStream bitstream) throws IOException {
29             DataInputStream dis = new DataInputStream(new Encode.Ascii.In(16, bitstream));
30             try {
31                 while(true) {
32                     dis.readUnsignedByte();
33                     dis.readUnsignedByte();
34                     dis.readUnsignedByte();
35                     int i = dis.readUnsignedByte();
36                     System.out.println(bin8(i));
37                 }
38             } catch (EOFException e) {
39             }
40         }
41         public void read(InputStream bitstream) throws IOException {
42             DataInputStream dis = new DataInputStream(new Encode.Ascii.In(2, bitstream));
43             int b = dis.readUnsignedByte();
44             if (b != 0x00) throw new Error("bitstream did not start with zero-byte; got " + b);
45             Log.debug(this, "saw leading zero-byte");
46             b = dis.readUnsignedByte();
47             if (b != 0xB7) throw new Error("bitstream did not start with preamble byte B7; got " + b);
48             Log.debug(this, "saw preamble byte");
49             control(dis.readInt());
50             Log.debug(this, "set control register to " + hex32(control()));
51             int numWindows = dis.readShort();
52             Log.debug(this, "this bitstream has " + numWindows + " window(s)");
53             int count = 0;
54             for(int i=0; i<numWindows; i++) {
55                 int start      = (dis.readUnsignedByte() << 16) | (int)dis.readShort();
56                 int end        = (dis.readUnsignedByte() << 16) | (int)dis.readShort();
57                 count = 0;
58                 Log.debug(this, "  window " + dec2(i) + " spans addresses " + hex24(start) + " - " + hex24(end) + "[count="+count+"]");
59                 int _z = (start & 0x0000ff) >>  0;
60                 int _y = (start & 0x00ff00) >>  8;
61                 int _x = (start & 0xff0000) >> 16;
62                 int z_ = (end & 0x0000ff) >>  0;
63                 int y_ = (end & 0x00ff00) >>  8;
64                 int x_ = (end & 0xff0000) >> 16;
65                 int z =  (start & 0x0000ff) >>  0;
66                 int y =  (start & 0x00ff00) >>  8;
67                 int x =  (start & 0xff0000) >> 16;
68
69                 while(true) {
70                     count++;
71                     mode4(x, y, z, dis.readUnsignedByte());
72                     x++;
73                     if (x > 15) { x = 0; y++; }
74                     if (y > 15) { y = 0; z++; }
75                     do {
76                         z++;
77                         if (z > 77) {
78                             z = _z;
79                             x++;
80                             if (x > 15) {
81                                 x = _x;
82                                 y++;
83                                 if (y > y_) break;
84                             }
85                         }
86                     } while(!valid(x,y,z));
87                 }
88                 //Log.debug(this, "    read " + count + " bytes (" + hex24(count)+")");
89             }
90             /*
91             Log.debug(this, "done " + count);
92             b = dis.readUnsignedByte();
93             if (b != 0xB7) throw new Error("bitstream did not start with preamble byte B7; got " + bin8(b));
94             Log.debug(this, "done2");
95             */
96         }
97
98         public boolean valid(int x, int y, int z) {
99             switch(z) {
100                 case 0x00: case 0x01: break;
101             }
102             if (x > 15) return false;
103             if (y > 15) return false;
104             return true;
105         }
106
107         public void mode4(int x, int y, int z, int d) {
108             //Log.debug(this, "    ("+dec2(x)+","+dec2(y)+"):"+hex8(z)+" = "+bin8(d));
109             //System.out.println(hex8(z)+hex8(y)+hex8(x)+hex8(d));
110         }
111     }
112 }
113         /*
114     public static class At40k extends FPGA {
115
116
117         public At40k(int width, int height) { this.width = width; this.height = height; }
118
119         public static class At40k10 extends At40k { public At40k10() { super(24, 24); } }
120
121         public Sector sector(int col, int row) { return new Sector(col, row); }
122         public final class Sector {
123             public final int col;
124             public final int row;
125             public Sector(int col, int row) {
126                 if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
127                 if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
128                 this.row = row;
129                 this.col = col;
130             }
131         }
132
133         public Cell cell(int col, int row) { return new Cell(col, row); }
134         public final class Cell {
135             public final int col;
136             public final int row;
137             public Sector getSector() { return sector(col - (col % 4), row - (row % 4)); }
138             public Cell(int col, int row) {
139                 this.row = row;
140                 this.col = col;
141             }
142         }
143
144         public class Sector {
145             Cell[][] = new Cell[4][4];
146             Ram ram  = new Ram();
147
148             Buf[] west  = new Buf[4];
149             Buf[] east  = new Buf[4];
150             Buf[] north = new Buf[4];
151             Buf[] south = new Buf[4];
152             Pip   pass  = new Pip[4][4][5][2];
153         }
154
155
156         public class Cell {
157
158             public final Port[] h = new Port[5] { port(), port(), port(), port(), port() };
159             public final Port[] v = new Port[5] { port(), port(), port(), port(), port() };
160             public final Port[] s = new Port[5] { port(), port(), port(), port(), port() };
161             public final Port nw, sw, se, ne;
162             public final Port n,  s,  w,  e;
163             public final Port xout, yout;
164
165             public final Pip zin     = pip(                s[0], s[1], s[2], s[3], s[4]);
166             public final Pip win     = pip(                s[0], s[1], s[2], s[3], s[4]);
167             public final Pip xin     = pip(nw, ne, sw, se, s[0], s[1], s[2], s[3], s[4]);
168             public final Pip yin     = pip(n,  s,  e,  w,  s[0], s[1], s[2], s[3], s[4]);
169
170             public final Pip wpip    = pip(win, zin, and(win, zin), fb);
171             public final Pip zpip    = pip(zin, one, zero);
172
173             public final Lut xlut    = lut(xpip, wmux, ypip);
174             public final Lut ylut    = lut(xpip, wmux, ypip);
175             public final Mux zmux    = mux(xlut, ylut, zpip);
176             public final Reg reg     = reg(zmux);
177             public final Pip center  = pip(reg, zmux);
178
179             public final Pip fb      = pip(zmux, reg);
180
181             public final Pip center  = pip(zmux, reg);
182             public final Pip xout    = pip(xlut, center);
183             public final Pip yout    = pip(ylut, center);
184
185             public final Pip oe      = pip(one, h[4], v[4]);
186             public final Buf out     = buf(fb, oe);
187
188             public final Pip[] o     = pip5(out, s);
189             public final Pip[] h     = pip5(h, s);
190             public final Pip[] v     = pip5(v, s);
191         }
192
193
194     }
195
196
197     public class At94k extends At40k {
198     }
199         */
200