f0668586b6a92d3b4347d85d10c347925eb4a42c
[slipway.git] / src / com / atmel / fpslic / Fpslic.java
1 package com.atmel.fpslic;
2
3 import java.util.*;
4 import java.io.*;
5 import org.ibex.util.Log;
6
7 public abstract class Fpslic {
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     /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */
24     public int getWidth() { return 24; }
25     public int getHeight() { return 24; }
26
27     private static String hex2(int i) {
28         String ret = Integer.toString(i, 16);
29         while(ret.length() < 2) ret = "0"+ret;
30         return ret.toUpperCase();
31     }
32
33     public void readMode4(InputStream in) throws IOException {
34         int count = 0;
35         BufferedReader br = new BufferedReader(new InputStreamReader(in));
36         for(String str = br.readLine(); str != null; str = br.readLine()) {
37             long foo = Long.parseLong(str, 16);
38             mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
39             count++;
40         }
41         flush();
42         in.close();
43     }
44
45     public abstract void flush();
46
47     public void writeMode4(Writer w) throws IOException {
48         for(int x=0; x<getWidth(); x++)
49             for(int y=0; y<getWidth(); y++)
50                 for(int z=0; z<255; z++) {
51                     if ((z > 0x09 && z < 0x10) ||
52                         (z > 0x11 && z < 0x20) ||
53                         (z > 0x29 && z < 0x30) ||
54                         (z > 0x39 && z < 0x40) ||
55                         (z > 0x41 && z < 0x60) ||
56                         (z > 0x67 && z < 0x70) ||
57                         (z > 0x77 && z < 0xD0) ||
58                         (z > 0xD3))
59                         continue;
60                     w.write(hex2(z));
61                     w.write(hex2(y));
62                     w.write(hex2(x));
63                     w.write(hex2(mode4(z, y, x) & 0xff));
64                     w.write('\n');
65                 }
66         w.flush();
67     }
68
69
70     public abstract void mode4(int z, int y, int x, int d);
71     public abstract byte mode4(int z, int y, int x);
72     public          byte mode4zyx(int zyx) { return mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff); }
73     public          void mode4zyx(int zyx, int d, int invmask) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, d, invmask); }
74     public          void mode4(int z, int y, int x, int d, int invmask) {
75         int old = mode4(z, y, x);
76         old &= ~invmask;
77         old |= d;
78         mode4(z, y, x, old);
79     }
80     public          void mode4zyx(int zyx, int bit, boolean set) { mode4(zyx>>24, (zyx>>16)&0xff, (zyx>>8)&0xff, bit, set); }
81     public          void mode4(int z, int y, int x, int bit, boolean set) {
82         int old = mode4(z, y, x);
83         old &= ~(1 << bit);
84         old |= set ? (1 << bit) : 0;
85         mode4(z, y, x, old);
86     }
87 }