3d43d75b707581c1480e78d9842f656a3e5a1681
[slipway.git] / src / edu / berkeley / obits / device / atmel / At40k.java
1 package edu.berkeley.obits.device.atmel;
2 import static edu.berkeley.obits.device.atmel.AtmelDevice.Constants.*;
3 import edu.berkeley.obits.*;
4 import org.ibex.util.*;
5
6 public class At40k {
7
8     private final AtmelDevice dev;
9     private final int width;
10     private final int height;
11
12     public At40k(AtmelDevice dev, int width, int height) {
13         this.width = width;
14         this.height = height;
15         this.dev = dev;
16     }
17
18     public static class At40k10 extends At40k {
19         public At40k10(AtmelDevice dev) { super(dev, 24, 24); }
20     }
21
22     public Sector sector(int col, int row) { return new Sector(col, row); }
23     public final class Sector {
24         public final int col;
25         public final int row;
26         public Sector(int col, int row) {
27             if (row % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 row");
28             if (col % 4 != 0) throw new Error("Sector must be created with a multiple-of-4 col");
29             this.row = row;
30             this.col = col;
31         }
32     }
33
34     public Cell cell(int col, int row) { return new Cell(col, row); }
35     public final class Cell {
36         public final int col;
37         public final int row;
38         public Sector getSector() { return sector(col - (col % 4), row - (row % 4)); }
39         public Cell(int col, int row) {
40             this.row = row;
41             this.col = col;
42         }
43
44         /* bit positions mean:  [MSB] ___ __y _x_ _xy z__ z_y zx_ zxy [LSB] */
45         public void ylut(byte table)   { dev.mode4(6, row, col, ~table); }
46
47         /* bit positions mean:  [MSB] ___ __y _x_ _xy z__ z_y zx_ zxy [LSB] */
48         public void xlut(byte table)   { dev.mode4(7, row, col, ~table); }
49
50         public void ff_reset_value(boolean value) {
51             //dev.mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
52         }
53
54         public void out(int plane, boolean enable) {
55             switch(plane) {
56                 case 0: dev.mode4(0x00, row, col, 2, enable); return;
57                 case 1: dev.mode4(0x00, row, col, 3, enable); return;
58                 case 2: dev.mode4(0x00, row, col, 5, enable); return;
59                 case 3: dev.mode4(0x00, row, col, 4, enable); return;
60                 case 4: dev.mode4(0x00, row, col, 1, enable); return;
61                 default: throw new RuntimeException("invalid argument");
62             }
63         }
64
65         public void h(int plane, boolean enable) {
66             switch(plane) {
67                 case 0: dev.mode4(0x08, row, col, 0, enable); return;
68                 case 1: dev.mode4(0x08, row, col, 2, enable); return;
69                 case 2: dev.mode4(0x08, row, col, 5, enable); return;
70                 case 3: dev.mode4(0x08, row, col, 6, enable); return;
71                 case 4: dev.mode4(0x00, row, col, 6, enable); return;
72                 default: throw new RuntimeException("invalid argument");
73             }
74         }
75         
76         public void v(int plane, boolean enable) {
77             switch(plane) {
78                 case 0: dev.mode4(0x08, row, col, 1, enable); return;
79                 case 1: dev.mode4(0x08, row, col, 3, enable); return;
80                 case 2: dev.mode4(0x08, row, col, 4, enable); return;
81                 case 3: dev.mode4(0x08, row, col, 7, enable); return;
82                 case 4: dev.mode4(0x00, row, col, 7, enable); return;
83                 default: throw new RuntimeException("invalid argument");
84             }
85         }
86         
87         public void t(boolean z, boolean w, boolean fb) {
88             // still not totally satisfied...
89             //     how to we distinguish between z&w vs z or w&fb vs fb?
90             //     what does it mean for both bits (0x30) to be set to 1?
91             if (fb && z) throw new RuntimeException("invalid combination");
92             int result = 0;
93             if (z)  result |= 0x20;
94             if (fb) result |= 0x10;
95             dev.mode4(1, row, col, result, 0x30);
96         }
97
98         public void c(int source) {
99             switch(source) {
100                 case XLUT: dev.mode4(1, row, col, 0x00, 0xc0); break;
101                 case YLUT: dev.mode4(1, row, col, 0x40, 0xc0); break;
102                 case ZMUX: dev.mode4(1, row, col, 0x80, 0xc0); break;
103                 default:   throw new RuntimeException("Invalid Argument");
104             }
105         }
106
107         public void b(boolean registered) { dev.mode4(1, row, col, 3, !registered); }
108         public void f(boolean registered) { dev.mode4(1, row, col, 2, !registered); }
109
110         public void xo(boolean center)    { dev.mode4(1, row, col, 1, center); }
111         public void yo(boolean center)    { dev.mode4(1, row, col, 0, center); }
112
113         public void oe(int source) {
114             switch(source) {
115                 case NONE: dev.mode4(0x02, row, col, 0, 0x3); break;
116                 case H4:   dev.mode4(0x02, row, col, 1, 0x3); break;
117                 case V4:   dev.mode4(0x02, row, col, 2, 0x3); break;
118                 default: throw new RuntimeException("invalid argument");
119             }
120         }
121
122         public void xin(int source) {
123             switch(source) {
124                 case SW: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<7); break;
125                 case NE: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<6); break;
126                 case SE: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<5); break;
127                 case NW: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<4); break;
128                 case L4: dev.mode4(0x03, row, col, 4, true);  dev.mode4(0x05, row, col,    0); break;
129                 case L3: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<3); break;
130                 case L2: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<2); break;
131                 case L1: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<1); break;
132                 case L0: dev.mode4(0x03, row, col, 4, false); dev.mode4(0x05, row, col, 1<<0); break;
133                 default: throw new RuntimeException("invalid argument");
134             }
135         }
136
137         // FIXME: cancel out the others
138         public void yin(int source) {
139             switch(source) {
140                 case NORTH: dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<7); break;
141                 case SOUTH: dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<6); break;
142                 case WEST:  dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<5); break;
143                 case EAST:  dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<4); break;
144                 case L4:    dev.mode4(0x02, row, col, 6, true);  dev.mode4(0x04, row, col,    0); break;
145                 case L3:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<3); break;
146                 case L2:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<2); break;
147                 case L1:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<1); break;
148                 case L0:    dev.mode4(0x02, row, col, 6, false); dev.mode4(0x04, row, col, 1<<0); break;
149                 default: throw new RuntimeException("invalid argument");
150             }
151         }
152
153         public void win(int source) {
154             switch(source) {
155                 case L4:    dev.mode4(0x03, row, col, 1<<5, 0xEC); break;
156                 case L3:    dev.mode4(0x03, row, col, 1<<6, 0xEC); break;
157                 case L2:    dev.mode4(0x03, row, col, 1<<7, 0xEC); break;
158                 case L1:    dev.mode4(0x03, row, col, 1<<3, 0xEC); break;
159                 case L0:    dev.mode4(0x03, row, col, 1<<2, 0xEC); break;
160                 default: throw new RuntimeException("invalid argument");
161             }
162         }
163
164         public void zin(int source) {
165             switch(source) {
166                 case L4:    dev.mode4(0x02, row, col, 1<<7, 0xDB); break;
167                 case L3:    dev.mode4(0x02, row, col, 1<<5, 0xDB); break;
168                 case L2:    dev.mode4(0x02, row, col, 1<<4, 0xDB); break;
169                 case L1:    dev.mode4(0x02, row, col, 1<<3, 0xDB); break;
170                 case L0:    dev.mode4(0x02, row, col, 1<<2, 0xDB); break;
171                 default: throw new RuntimeException("invalid argument");
172             }
173         }
174
175     }
176
177     public IOB iob_top(int col, boolean primary)   { return new IOB(col, 0, primary); }
178     public IOB iob_bot(int col, boolean primary)   { return new IOB(col, 1, primary); }
179     public IOB iob_left(int row, boolean primary)  { return new IOB(1, row, primary); }
180     public IOB iob_right(int row, boolean primary) { return new IOB(2, row, primary); }
181     /*
182     public IOB fromPin(int pin) {
183         if (pin >=  4 && pin <= 11) return io(pin-3);
184         if (pin >= 15 && pin <= 18) return io(pin-2);
185         if (pin >= 19 && pin <= 24) return io(pin);
186         if (pin >= 27 && pin <= 30) return io(pin-2);
187         if (pin >= 33 && pin <= 36) return io(pin);
188         if (pin >= 38 && pin <= 47) return io(pin+1);
189
190
191         if (pin >= 33 && pin <= 36) return io(pin+36);
192         if (pin >= 38 && pin <= 41) return io(pin+43);
193         if (pin >= 42 && pin <= 43) return io(pin+47);
194         if (pin >= 44 && pin <= 47) return io(pin+49);
195         if (pin >= 57 && pin <= 62) return io(pin+40);
196         if (pin >= 63 && pin <= 66) return io(pin+46);
197         if (pin >= 68 && pin <= 71) return io(pin+53);
198         if (pin >= 72 && pin <= 73) return io(pin+53);
199         if (pin >= 74 && pin <= 75) return io(pin+63);
200         if (pin >= 76 && pin <= 77) return io(143+(pin-76));
201         if (pin >= 80 && pin <= 81) return io(145+(pin-80));
202         if (pin >= 82 && pin <= 85) return io(151+(pin-82));
203         if (pin >= 86 && pin <= 89) return io(165+(pin-86));
204         if (pin >= 91 && pin <= 94) return io(177+(pin-91));
205         if (pin >= 95 && pin <= 96) return io(183+(pin-95));
206         if (pin >= 97 && pin <= 100) return io(189+(pin-97));
207         if (pin >= 161 && pin <= 164) return io(289+(pin-161));
208         if (pin >= 165 && pin <= 166) return io(297+(pin-165));
209         if (pin >= 167 && pin <= 168) return io(303+(pin-167));
210         if (pin >= 169 && pin <= 170) return io(309+(pin-169));
211         if (pin >= 172 && pin <= 173) return io(313+(pin-172));
212         if (pin >= 174 && pin <= 175) return io(325+(pin-174));
213         if (pin >= 176 && pin <= 179) return io(327+(pin-176));
214         if (pin >= 180 && pin <= 181) return io(335+(pin-180));
215         if (pin >= 184 && pin <= 185) return io(337+(pin-184));
216         if (pin >= 186 && pin <= 191) return io(343+(pin-186));
217         if (pin >= 192 && pin <= 193) return io(359+(pin-192));
218         if (pin >= 195 && pin <= 196) return io(363+(pin-195));
219         if (pin >= 197 && pin <= 200) return io(369+(pin-197));
220         if (pin >= 201 && pin <= 204) return io(381+(pin-201));
221     }
222     public io(int ionum) {
223         if (ionum <= 94) {
224             int cell = (94 - pin) / 2;
225             boolean primary = cell * 2 == (94-pin);
226         }
227     }
228     */
229     public final class IOB {
230         public final int col;
231         public final int row;
232         public final boolean primary;
233         public IOB(int col, int row, boolean primary) { this.col = col; this.row = row; this.primary = primary; }
234
235         private int z(int code) { return 0x60 | (primary ? 0x08 : 0x10) | code; }
236         public void pullup()   { dev.mode4(z(0), row, col, (dev.mode4(z(0), row, col) & (~0x6)) | 0x00); }
237         public void pulldown() { dev.mode4(z(0), row, col, (dev.mode4(z(0), row, col) & (~0x6)) | 0x03); }
238         public void pullnone() { dev.mode4(z(0), row, col, (dev.mode4(z(0), row, col) & (~0x6)) | 0x01); }
239         public void oe(boolean oe) {
240             int old =  dev.mode4(z(1), row, col) & (~(1<<5));
241             old     |= oe ? 0 : (1<<5);
242             dev.mode4(z(1), row, col, old & 0xff);
243         }
244         public void output(int which) {
245             if (which < 0 || which > 6) throw new RuntimeException("oem(x) only valid for 0<=x<=5");
246             int d = dev.mode4(z(1), row, col) & 0x80;
247             if (which>0) { d |= 1 << (which==6 ? 6 : which==0 ? 5 : (which-1)); }
248             Log.warn("z", Integer.toString(z(1),16));
249             Log.warn("y", Integer.toString(row,16));
250             Log.warn("x", Integer.toString(col,16));
251             Log.warn("d", Integer.toString(d,16));
252             dev.mode4(z(1), row, col, d);
253         }
254         
255     }
256
257 }