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.*;
8 private final AtmelDevice dev;
9 private final int width;
10 private final int height;
12 public At40k(AtmelDevice dev, int width, int height) {
18 public static class At40k10 extends At40k {
19 public At40k10(AtmelDevice dev) { super(dev, 24, 24); }
22 public Sector sector(int col, int row) { return new Sector(col, row); }
23 public final class Sector {
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");
34 public Cell cell(int col, int row) { return new Cell(col, row); }
35 public final class Cell {
38 public Sector getSector() { return sector(col - (col % 4), row - (row % 4)); }
39 public Cell(int col, int row) {
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); }
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); }
50 public void ff_reset_value(boolean value) {
51 //dev.mode4( /* FIXME WRONG!!! */, row, col, 3, !value); return;
54 public void out(int plane, boolean enable) {
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");
65 public void h(int plane, boolean enable) {
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");
76 public void v(int plane, boolean enable) {
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");
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");
93 if (z) result |= 0x20;
94 if (fb) result |= 0x10;
95 dev.mode4(1, row, col, result, 0x30);
98 public void c(int 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");
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); }
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); }
113 public void oe(int 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");
122 public void xin(int 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");
137 // FIXME: cancel out the others
138 public void yin(int 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");
153 public void win(int 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");
164 public void zin(int 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");
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); }
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);
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));
222 public io(int ionum) {
224 int cell = (94 - pin) / 2;
225 boolean primary = cell * 2 == (94-pin);
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; }
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);
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);