checkpoint
[slipway.git] / src / com / atmel / fpslic / FpslicUtil.java
1 package com.atmel.fpslic;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class FpslicUtil {
7
8     public static int lutSwap(int x) {
9         return
10             (x & 0x80)        |
11             ((x & 0x20) << 1) |
12             ((x & 0x40) >> 1) |
13             (x & 0x10) |
14             (x & 0x08)        |
15             ((x & 0x02) << 1) |
16             ((x & 0x04) >> 1) |
17             (x & 0x01);
18     }
19
20     public static void readMode4(InputStream in, Fpslic fpslic) throws IOException {
21         int count = 0;
22         BufferedReader br = new BufferedReader(new InputStreamReader(in));
23         for(String str = br.readLine(); str != null; str = br.readLine()) {
24             long foo = Long.parseLong(str, 16);
25             fpslic.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
26             count++;
27         }
28         fpslic.flush();
29         in.close();
30     }
31
32     public static void writeMode4(Writer w, Fpslic fpslic) throws IOException {
33         for(int x=0; x<fpslic.getWidth(); x++)
34             for(int y=0; y<fpslic.getWidth(); y++)
35                 for(int z=0; z<255; z++) {
36                     if ((z > 0x09 && z < 0x10) ||
37                         (z > 0x11 && z < 0x20) ||
38                         (z > 0x29 && z < 0x30) ||
39                         (z > 0x39 && z < 0x40) ||
40                         (z > 0x41 && z < 0x60) ||
41                         (z > 0x67 && z < 0x70) ||
42                         (z > 0x77 && z < 0xD0) ||
43                         (z > 0xD3))
44                         continue;
45                     w.write(hex2(z));
46                     w.write(hex2(y));
47                     w.write(hex2(x));
48                     w.write(hex2(fpslic.mode4(z, y, x) & 0xff));
49                     w.write('\n');
50                 }
51         w.flush();
52     }
53
54     private static String hex2(int i) {
55         String ret = Integer.toString(i, 16);
56         while(ret.length() < 2) ret = "0"+ret;
57         return ret.toUpperCase();
58     }
59
60     public static synchronized String printLut(int lut, String xn, String yn, String zn) {
61         try {
62             File f = File.createTempFile("mvsis", ".mvs");
63             f.deleteOnExit();
64
65             FileOutputStream fos = new FileOutputStream(f);
66             PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
67             pw.println(".model clb");
68             pw.println(".inputs "+xn+" "+yn+" "+zn);
69             pw.println(".outputs O");
70             pw.println(".table "+xn+" "+yn+" "+zn+/*("X_xor_Y X_xor_Z Y_xor_Z")+*/ " -> O");
71             for(int i=8; i>=0; i--) {
72                 int x = ((i & 0x01)!=0 ? 1 : 0);
73                 int y = ((i & 0x02)!=0 ? 1 : 0);
74                 int z = ((i & 0x04)!=0 ? 1 : 0);
75                 pw.print(" "+x+" ");
76                 pw.print(" "+y+" ");
77                 pw.print(" "+z+" ");
78                 //pw.print(" "+(x ^ y)+" ");
79                 //pw.print(" "+(y ^ z)+" ");
80                 //pw.print(" "+(z ^ y)+" ");
81                 pw.print((lut & (1<<i))==0 ? 0 : 1);
82                 pw.println();
83             }
84             pw.println(".end");
85             pw.flush();
86             pw.close();
87             Process p = Runtime.getRuntime().exec(new String[] { "mvsis", "-c", "simplify;print_factor", f.getAbsolutePath() });
88             new Gobble("mvsis: ", p.getErrorStream()).start();
89             //new Gobble("mvsis: ", p.getInputStream()).start();
90             BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
91             String ret = br.readLine();
92             //f.delete();
93             return ret.trim();
94         } catch (Exception e) {
95             e.printStackTrace();
96             return "*mvsis_error*";
97         }
98     }
99
100     public static class Gobble extends Thread {
101         private final String header;
102         private final BufferedReader br;
103         public Gobble(String header, BufferedReader br) { this.br = br; this.header = header; }
104         public Gobble(String header, Reader r)          { this(header, new BufferedReader(r)); }
105         public Gobble(String header, InputStream is)    { this(header, new InputStreamReader(is)); }
106         public void run() {
107             try {
108                 for(String s = br.readLine(); s!=null; s=br.readLine())
109                     System.err.println(header + s);
110             } catch (Exception e) {
111                 e.printStackTrace();
112             }
113         }
114     }
115 }