maskToFunction

6485 ワード

public class Util {

    public static String maskToFunction(final String mask, int fanin) {

        Assert.isLegal(fanin <= 4, "virtex/virtex2 prim lut <= lut4");

        int mask_int = 0;

        mask_int = Integer.parseInt(mask, 16);

        int bit_count = 1 << fanin;

        Assert.isLegal(mask_int < (1 << bit_count), "mask is larger than fanin limit");

        String result = "";

        if (mask.length() == 1) {
            char maskc = Character.toUpperCase(mask.charAt(0));
            if (fanin == 1) {
                if      (maskc == '2') result = "(A1)";
                else if (maskc == '1') result = "(~A1)";
            } else {
                if      (maskc == 'A') result = "(A1)";
                else if (maskc == '5') result = "(~A1)";
                else if (maskc == 'C') result = "(A2)";
                else if (maskc == '3') result = "(~A2)";
                else if (maskc == '8') result = "(A1*A2)";
                else if (maskc == '7') result = "(~(A1*A2))";
                else if (maskc == 'E') result = "(A1+A2)";
                else if (maskc == '1') result = "(~(A1+A2))";
                else if (maskc == '6') result = "(A1@A2)";
                else if (maskc == '9') result = "(~(A1@A2))";
            }
        } else if (mask.length() == 2) {
            if      (mask.equals("AA")) result = "(A1)";
            else if (mask.equals("55")) result = "(~A1)";
            else if (mask.equals( "CC")) result = "(A2)";
            else if (mask.equals( "33")) result = "(~A2)";
            else if (mask.equals( "F0")) result = "(A3)";
            else if (mask.equals( "0F")) result = "(~A3)";
            else if (mask.equals( "80")) result = "(A1*A2*A3)";
            else if (mask.equals( "7F")) result = "(~(A1*A2*A3))";
            else if (mask.equals( "FE")) result = "(A1+A2+A3)";
            else if (mask.equals( "01")) result = "(~(A1+A2+A3))";
            else if (mask.equals( "96")) result = "(A1@A2@A3)";
            else if (mask.equals( "69")) result = "(~(A1@A2@A3))";
            else if (mask.equals( "E4")) result = "((~A1*A2)+(A1*A3))";
            else if (mask.equals( "D8")) result = "((~A1*A3)+(A1*A2))";
            else if (mask.equals( "CA")) result = "((A1*~A3)+(A2*A3))";
            else if (mask.equals( "AC")) result = "((A2*~A3)+(A1*A3))";
        } else if (mask.length() == 4) {
            if      (mask.equals( "AAAA")) result = "(A1)";
            else if (mask.equals( "5555")) result = "(~A1)";
            else if (mask.equals( "CCCC")) result = "(A2)";
            else if (mask.equals( "3333")) result = "(~A2)";
            else if (mask.equals( "F0F0")) result = "(A3)";
            else if (mask.equals( "0F0F")) result = "(~A3)";
            else if (mask.equals( "FF00")) result = "(A4)";
            else if (mask.equals( "00FF")) result = "(~A4)";
            else if (mask.equals( "8000")) result = "(A1*A2*A3*A4)";
            else if (mask.equals( "7FFF")) result = "(~(A1*A2*A3*A4))";
            else if (mask.equals( "3FFF")) result = "(~(A2*A3*A4))";
            else if (mask.equals( "FFFE")) result = "(A1+A2+A3+A4)";
            else if (mask.equals( "0001")) result = "(~(A1+A2+A3+A4))";
            else if (mask.equals( "6996")) result = "(A1@A2@A3@A4)";
            else if (mask.equals( "9669")) result = "(~(A1@A2@A3@A4))";
            else if (mask.equals( "F888")) result = "((A1*A2)+(A3*A4))";
        }

        if (result.isEmpty())
            for(int i = 0; i < bit_count; i++ ) {
                String prod = "";
                if( (mask_int & ( 1 << i )) == 0)
                    continue;
                for (int j = 0; j < fanin; ++j) {
                    prod += ( ( (i & (1<<j)) == 0) ? "A":"~A") + (j+1) + "*";
                }
                if (prod.isEmpty()) {
                    continue;
                } else if (prod.endsWith("*")) {
                    prod = prod.substring(0, prod.length()-1);
                }
                result += ("(" + prod + ")+");
            }

        if (result.isEmpty()) {
            result = "0";
        } else if (result.endsWith("+")) {
            result = result.substring(0, result.length()-1);
        }
        return ("D=" + result);
    }
    
    public static void main(String[] args) {
        System.out.println(maskToFunction("96", 3));
        System.out.println(maskToFunction("e8", 3));
        System.out.println(maskToFunction("6", 2));
        System.out.println(maskToFunction("95", 3));
        System.out.println(maskToFunction("3666", 4));
        System.out.println(maskToFunction("08ce", 4));
    }
}

 
 
結果は
D=(A1@A2@A3)
D=(~A1*~A2*A3)+(~A1*A2*~A3)+(A1*~A2*~A3)+(~A1*~A2*~A3)
D=(A1@A2)
D=(A1*A2*A3)+(A1*~A2*A3)+(A1*A2*~A3)+(~A1*~A2*~A3)
D=(~A1*A2*A3*A4)+(A1*~A2*A3*A4)+(~A1*A2*~A3*A4)+(A1*~A2*~A3*A4)+(~A1*A2*A3*~A4)+(A1*~A2*A3*~A4)+(A1*A2*~A3*~A4)+(~A1*A2*~A3*~A4)
D=(~A1*A2*A3*A4)+(A1*~A2*A3*A4)+(~A1*~A2*A3*A4)+(A1*~A2*~A3*A4)+(~A1*~A2*~A3*A4)+(~A1*~A2*A3*~A4)