OpenCores
URL https://opencores.org/ocsvn/ao68000/ao68000/trunk

Subversion Repositories ao68000

[/] [ao68000/] [trunk/] [sw/] [ao68000_tool/] [ParseParams.java] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 alfik
/*
2
 * Copyright 2010, Aleksander Osman, alfik@poczta.fm. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without modification, are
5
 * permitted provided that the following conditions are met:
6
 *
7
 *  1. Redistributions of source code must retain the above copyright notice, this list of
8
 *     conditions and the following disclaimer.
9
 *
10
 *  2. Redistributions in binary form must reproduce the above copyright notice, this list
11
 *     of conditions and the following disclaimer in the documentation and/or other materials
12
 *     provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 */
24
 
25
package ao68000_tool;
26
 
27
import java.io.File;
28
import java.io.FileInputStream;
29
import java.util.Vector;
30
import java.util.regex.Matcher;
31
import java.util.regex.Pattern;
32
import java.util.HashMap;
33
 
34
class ParseParams {
35
    /**
36
     * Parse microcode_params.v to get information about what to control with the microcode,
37
     * that is, about the microcode parameters.
38
     *
39
     * @param file_name         - path to microcode_params.v,
40
     * @return                  - the contents of Parser.java,
41
     * @throws Exception        - in case of file read error.
42
     */
43
    static String parse(String file_name) throws Exception {
44
        // load file
45
        File file = new File(file_name);
46
        byte bytes[] = new byte[(int)file.length()];
47
        FileInputStream in = new FileInputStream(file);
48
        if( in.read(bytes) != bytes.length ) throw new Exception("Can not read from file: " + file.getCanonicalPath());
49
        in.close();
50
 
51
        // find 'OPERATIONS START' and 'OPERATIONS END' substrings
52
        String all_string = new String(bytes);
53
        int start_index = all_string.indexOf("OPERATIONS START");
54
        if(start_index == -1) throw new Exception("Can not find 'OPERATIONS START' substring in " + file.getCanonicalPath());
55
        int end_index = all_string.indexOf("OPERATIONS END");
56
        if(end_index == -1) throw new Exception("Can not find 'OPERATIONS END' substring in " + file.getCanonicalPath());
57
 
58
        // prepare Parser.java header and constructors
59
        String java = "";
60
        java += "package ao68000_tool;" + "\n";
61
        java += "class Parser {" + "\n";
62
        java += "\t" + "boolean newline;" + "\n";
63
        java += "\t" + "Parser() { this(true); }" + "\n";
64
        java += "\t" + "Parser(boolean newline) { this.newline = newline; }" + "\n";
65
 
66
        // prepare data structures to keep information about the microcode parameters
67
        prefixes = new Vector<String>();
68
        prefix_locations = new HashMap<String, Integer>();
69
        name_values = new HashMap<String, Integer>();
70
 
71
        // split read file into lines
72
        String string = all_string.substring(start_index, end_index);
73
        String tokens[] = string.split("\\n");
74
 
75
        // prepare patterns, initialize counters
76
        Pattern pat = Pattern.compile("`define\\s*(\\S+)\\s*(\\d+)'d(\\d+).*");
77
        int last_parameter_size = 0;
78
        control_bit_offset = 0;
79
 
80
        // parse each line
81
        for(String s : tokens) {
82
            Matcher m0 = pat.matcher(s);
83
 
84
            // match parameter names and values
85
            if( m0.matches() ) {
86
                String name = m0.group(1);
87
                int size = Integer.parseInt(m0.group(2));
88
                int value = Integer.parseInt(m0.group(3));
89
 
90
                // check if parameter name ends with _IDLE
91
                if(name.endsWith("_IDLE")) {
92
                    last_parameter_size = size;
93
 
94
                    String prefix = name.substring(0, name.length()-5);
95
 
96
                    prefixes.add(prefix + "_");
97
                    prefix_locations.put(prefix + "_start", control_bit_offset);
98
                    prefix_locations.put(prefix + "_end", control_bit_offset+last_parameter_size-1);
99
                    control_bit_offset += last_parameter_size;
100
                }
101
                else {
102
                    java += "\t" + "Parser " + name + "() throws Exception {" + "\n";
103
                    java += "\t\t" + "GenerateMicrocode.entry(newline, \"" + name + "\");" + "\n";
104
                    java += "\t\t" + "return new Parser(false);" + "\n";
105
                    java += "\t" + "}" + "\n";
106
 
107
                    name_values.put(name, value);
108
                }
109
            }
110
        }
111
 
112
        // prepare Parser.java ending
113
        java += "\t" + "void label(String label) throws Exception { GenerateMicrocode.entry(newline, \"label_\" + label); }" + "\n";
114
        java += "\t" + "Parser offset(String label) throws Exception {" + "\n";
115
        java += "\t\t" + "GenerateMicrocode.entry(newline, \"offset_\" + label);" + "\n";
116
        java += "\t\t" + "return new Parser(false);" + "\n";
117
        java += "\t" + "}" + "\n";
118
 
119
        java += "}" + "\n";
120
 
121
        return java;
122
    }
123
 
124
    static Vector<String> prefixes;
125
    static HashMap<String, Integer> name_values;
126
    static HashMap<String, Integer> prefix_locations;
127
    static int control_bit_offset;
128
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.