checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / AtmelDevice.java
1 package edu.berkeley.obits.device.atmel;
2
3 import edu.berkeley.obits.*;
4 //import static edu.berkeley.cs.obits.device.atmel.Wires.*;
5 import java.util.*;
6 import java.io.*;
7 import org.ibex.util.Log;
8
9 public abstract class AtmelDevice extends Bits implements Device {
10
11     public static class Util {
12         public static int lutSwap(int x) {
13             return
14                 (x & 0x80)        |
15                 ((x & 0x20) << 1) |
16                 ((x & 0x40) >> 1) |
17                 (x & 0x10) |
18                 (x & 0x08)        |
19                 ((x & 0x02) << 1) |
20                 ((x & 0x04) >> 1) |
21                 (x & 0x01);
22         }
23     }
24     
25     public static class Constants {
26         public static final int NONE  = -1;
27         public static final int L0    = 0;
28         public static final int L1    = 1;
29         public static final int L2    = 2;
30         public static final int L3    = 3;
31         public static final int L4    = 4;
32
33         public static final int NORTH = 8;
34         public static final int WEST  = 9;
35         public static final int SOUTH = 10;
36         public static final int EAST  = 11;
37
38         public static final int XLUT  = 12;
39         public static final int YLUT  = 13;
40         public static final int ZMUX  = 14;
41
42         public static final int H4    = 15;
43         public static final int V4    = 16;
44
45         public static final int NW    = 20;
46         public static final int SW    = 21;
47         public static final int NE    = 22;
48         public static final int SE    = 23;
49
50         public static final int SLOW   = 24;
51         public static final int MEDIUM = 25;
52         public static final int FAST   = 26;
53
54         public static final int ALWAYS_ON  = 27;
55         public static final int ALWAYS_OFF = 28;
56
57         public static final int FB    = 29;
58
59         public static final int LUT_SELF  = 0xAA;
60         public static final int LUT_Z     = 0xF0;
61         public static final int LUT_OTHER = 0xCC;
62
63         public static final int TMUX_W_AND_Z   = 0x00001001;
64         public static final int TMUX_W         = 0x00001002;
65         public static final int TMUX_Z         = 0x00001004;
66         public static final int TMUX_W_AND_FB  = 0x00001008;
67         public static final int TMUX_FB        = 0x00001010;
68
69
70     }
71
72     /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */
73     public int getWidth() { return 24; }
74     public int getHeight() { return 24; }
75
76     private static String hex2(int i) {
77         String ret = Integer.toString(i, 16);
78         while(ret.length() < 2) ret = "0"+ret;
79         return ret.toUpperCase();
80     }
81
82     public void readMode4(InputStream in) throws IOException {
83         int count = 0;
84         BufferedReader br = new BufferedReader(new InputStreamReader(in));
85         for(String str = br.readLine(); str != null; str = br.readLine()) {
86             long foo = Long.parseLong(str, 16);
87             mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
88             count++;
89         }
90         flush();
91         in.close();
92     }
93
94     public void writeMode4(Writer w) throws IOException {
95         for(int x=0; x<getWidth(); x++)
96             for(int y=0; y<getWidth(); y++)
97                 for(int z=0; z<255; z++) {
98                     if ((z > 0x09 && z < 0x10) ||
99                         (z > 0x11 && z < 0x20) ||
100                         (z > 0x29 && z < 0x30) ||
101                         (z > 0x39 && z < 0x40) ||
102                         (z > 0x41 && z < 0x60) ||
103                         (z > 0x67 && z < 0x70) ||
104                         (z > 0x77 && z < 0xD0) ||
105                         (z > 0xD3))
106                         continue;
107                     w.write(hex2(z));
108                     w.write(hex2(y));
109                     w.write(hex2(x));
110                     w.write(hex2(mode4(z, y, x) & 0xff));
111                     w.write('\n');
112                 }
113         w.flush();
114     }
115
116
117     public abstract void mode4(int z, int y, int x, int d) throws DeviceException;
118     public abstract byte mode4(int z, int y, int x);
119     public          byte mode4zyx(int zyx) { return mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff); }
120     public          void mode4zyx(int zyx, int d, int invmask) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, d, invmask); }
121     public          void mode4(int z, int y, int x, int d, int invmask) {
122         int old = mode4(z, y, x);
123         old &= ~invmask;
124         old |= d;
125         mode4(z, y, x, old);
126     }
127     public          void mode4zyx(int zyx, int bit, boolean set) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, bit, set); }
128     public          void mode4(int z, int y, int x, int bit, boolean set) {
129         int old = mode4(z, y, x);
130         old &= ~(1 << bit);
131         old |= set ? (1 << bit) : 0;
132         mode4(z, y, x, old);
133     }
134     /*
135     public Sector sector(int col, int row) { return new Sector(col, row); }
136     public final class Sector {
137         public final int col;
138         public final int row;
139         public Sector(int col, int row) {
140             if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
141             if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
142             this.row = row;
143             this.col = col;
144         }
145     }
146
147     public static interface Driver { }
148
149     public static interface HasPlane { public int getPlane(); }
150
151     public static interface XI extends Driver { }
152     public static interface YI extends Driver { }
153     */
154     /*
155     public static void foo(Direction f) { }
156     public static enum Direction implements Drives<Y> { N, S, E, W; }
157     public static enum Bar { A, B, C; }
158     public static Object B = new Object();
159     public static Direction foo = null;
160     static {
161         switch(foo) {
162             case N:
163             case S:
164             case W:
165         }
166     }
167
168     public class Drives<E extends EPR<E>> { }
169     public class EPR<E extends EPR<E>> {
170         public abstract Drives<E> driver();
171         public abstract void      driver(Drives<E>);
172     }
173
174     public class Joins<B extends Bus<B>> { }
175     public abstract class Bus<B extends Bus<B>> {
176         public Set<Joins<E>> drivers();
177     }
178     public interface Drives<E extends EPR> { }
179
180
181     public static interface EPR {
182     }
183     public static interface Drives<E extends EPR> { }
184     public static interface Bus extends EPR { }
185     public static interface Joins<J extends Joins, B extends Bus & Joins<B,J>> extends Bus { }
186     //public static interface Joins<B extends Bus> extends Bus { }
187     public static interface Has<E extends EPR> {
188         public Drives<E> getDriver(E e);
189         public void      setDriver(E e, Drives<E> driver);
190     }
191     //public static interface HasBus<B extends Bus> { public Set<OnBus<B>> on(B b); }
192     public interface Input { }
193     public interface Output { }
194     public interface InOut { }
195     
196     public static abstract class LUT<A extends EPR, B extends EPR, C extends EPR> implements EPR { }
197     public static abstract class And<A extends EPR, B extends EPR> implements EPR { }
198     public static abstract class Reg<A extends EPR> { }
199     public static abstract class Mux<Sel extends EPR, Zero extends EPR, One extends EPR> { }
200     public static abstract class Buf<Sel extends EPR> { }
201
202     public enum DiagonalInputs   implements Input, Drives<X> { NW, SW, NE, SE; }
203     public enum OrthogonalInputs implements Input, Drives<Y> { N,  S,  E,  W;  }
204
205     public <X extends Drives<Y>, Y extends EPR> void connect(X x, Y y) { }
206
207     public static enum Plane { P0, P1, P2, P3, P4; }
208     public static class L<P extends Plane> implements Bus, Drives<X>, Drives<Y>, Drives<Z>, Drives<W> { }
209
210     public final class Cell {
211       public class X                         implements EPR, Drives<XLUT>, Drives<YLUT> { }
212       public class Y                         implements EPR, Drives<XLUT>, Drives<YLUT> { }
213       public class Z                         implements EPR, Drives<A>, Drives<WZ> { }
214       public class F                         implements EPR, Drives<A>, Drives<WF>, Drives<OB> { }
215       public class W                         implements EPR, Drives<A>, Drives<WZ>, Drives<WF> { }
216       public class A                         implements EPR { }
217       public class WZ   extends And<W, Z>    implements EPR, Drives<XLUT>, Drives<YLUT> { }
218       public class WF   extends And<W, F>    implements EPR, Drives<XLUT>, Drives<YLUT> { }
219       public class CM   extends Mux<Z, Y, X> implements EPR, Drives<C> { }
220       public class XLUT extends LUT<X, Y, A> implements EPR, Drives<CM>, Drives<XO> { }
221       public class YLUT extends LUT<A, X, Y> implements EPR, Drives<CM>, Drives<YO> { }
222       public class C                         implements EPR, Drives<R>, Drives<F> { }
223       public class R    extends Reg<C>       implements EPR, Drives<F>, Drives<XO>, Drives<YO>{ }
224       public class XO                        implements EPR, Output { }
225       public class YO                        implements EPR, Output { }
226       public static class OB   extends Buf<F>>       implements EPR, Drives<L0>, Drives<L1>, Drives<L2>, Drives<L3>, Drives<L4> { }
227
228     */
229     //public static L1 L1 = new L1();
230     /*
231     public static class CellImpl implements
232                                  Has<X>,
233                                      Has<Y> {
234         public void           setDriver(X x, Drives<X> d) { }
235         public void           setDriver(Y y, Drives<Y> d) { }
236         public void           setDriver(Z z, Drives<Z> d) { }
237         public void           setDriver(W w, Drives<W> d) { }
238         public Drives<X>      getDriver(X x)            { return null; }
239         public Drives<Y>      getDriver(Y y)            { return null; }
240         public Drives<Z>      getDriver(Z z)            { return null; }
241         public Drives<W>      getDriver(W w)            { return null; }
242
243         public Set<OnBus<L1>> on(L1 l1)                 { return null; }
244         public Drives<Y>      getDriver(Y y)            { return null; }
245         public void           setDriver(Y y, Drives<Y> d) { }
246     }
247     */
248         /*
249     public static abstract class L<D extends C> implements HasPlane, EPR<C> {
250         private final int plane;
251         L(int plane) { this.plane = plane; }
252         public int getPlane() { return plane; }
253         public boolean h();
254         public void    h(boolean connected);
255         public boolean v();
256         public void    v(boolean connected);
257         public boolean f();
258         public void    f(boolean connected);
259     }
260
261     L0 = new L<L0>(0);
262     L1 = new L<L1>(1);
263     L2 = new L<L2>(2);
264     L3 = new L<L3>(3);
265     L4 = new L<L4>(4);
266
267     
268
269     public static enum L implements XI, YI {
270         L0(0), L1(1), L2(2), L3(3) { public int foo() { return 2; } }, L4(4);
271
272         public final int plane;
273         public L(int plane) { this.plane = plane; }
274
275     }
276         */
277
278     //public static interface L0 extends XI, YI { } public static L0 L0 = new L0() { };
279
280     /*    
281
282     public static enum Xi { NONE, L0, L1, L2, L3, NW, SW, NE, SE, L4; }
283     public static enum Yi { NONE, L0, L1, L2, L3, E,  W,  S,  N,  L4; }
284     public static enum Wi { NONE, L0, L1, L4, L3, L2; }
285     public static enum Zi { NONE, L0, L1, L2, L3, L4; }
286     public static enum Ti { W,  Z,  F,  WZ, WF, ONE; }
287     public static enum Ci { X, Y, CMux; }
288     public static enum Fi { R, C; }
289     public static enum Bi { R, C; }
290     public static enum Xo { X, B; }
291     public static enum Yo { Y, B; }
292     public static enum Oe { ONE, H4, V4; }
293
294     public Cell cell(int col, int row) { return new Cell(col, row); }
295     public final class Cell {
296         public final int col;
297         public final int row;
298     */
299         /** assumes LITTLE endian */
300     /*
301         protected int onehot(int val) {
302             int ret = -1;
303             for(int i=0; i<32; i++) {
304                 if ((val & (1 << i)) != 0) {
305                     if (ret != -1) throw new Error("two bits set in a one-hot encoded value");
306                     ret = i;
307                 }
308             }
309             return ret+1;
310         }
311         protected int bits(int octet, int bit, int count) {
312             return AtmelDevice.this.bits.get(offset, count);
313         }
314
315         public void set(Xi xi) { }
316         public void set(Yi xi) { }
317         public void set(Ti xi) { }
318         public void set(Ci xi) { }
319         public void set(Fi xi) { }
320         public void set(Bi xi) { }
321         public void set(Xo xo) { }
322         public void set(Yo yo) { }
323         public void set(Oe oe) { }
324
325         public Xi xi() { return Xi.values()[onehot((bits(3,3,1)<<8)|bits(5,0,8))]; }
326         public Yi yi() { return Yi.values()[onehot((bits(2,1,1)<<8)|bits(4,0,8))]; }
327         public Wi wi() { return Wi.values()[onehot((bits(3,0,3)<<2)|bits(3,4,2))]; }
328         public Zi zi() { return Zi.values()[onehot((bits(2,0,1)<<4)|bits(2,2,4))]; }
329         public Ti ti() { return null; }
330         public Ci ci() { return null; }
331         public Fi fi() { return null; }
332         public Bi bi() { return null; }
333         public Xo xo() { return Xo.values()[onehot(bits(1,6,1))]; }
334         public Yo yo() { return Yo.values()[onehot(bits(1,7,1))]; }
335         public Oe oe() { return Oe.values()[onehot(bits(3,6,2))]; }
336
337         public Sector getSector() { return sector(col - (col % 4), row - (row % 4)); }
338         public Cell(int col, int row) {
339             this.row = row;
340             this.col = col;
341         }
342
343
344         
345         
346         public static enum EPR { L0, L1, L2, L3, L4 }
347         public static enum XInputDriver extends InputDriver {
348         public static enum YInputDriver extends InputDriver { N, S, E, W }
349         public XInputDriver xi() { }
350         public void         xi(XInputDriver) { }
351         public YInputDriver yi() { }
352         public void         yi(YInputDriver) { }
353         public InputDriver  zi() { }
354         public void         zi(InputDriver) { }
355         public InputDriver  wi() { }
356         public void         wi(InputDriver) { }
357
358         public byte         xlut() { }
359         public              xlut(byte) { }
360         public byte         ylut() { }
361         public              ylut(byte) { }
362
363         public static enum CInputDriver { XL, YL, C }
364         public static enum FDriver      { R, C }
365
366         public boolean      cRegistered() { }
367
368     }
369     */
370 }