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