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