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