1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package ao486;
|
28 |
|
|
|
29 |
|
|
import java.io.File;
|
30 |
|
|
import java.nio.file.Files;
|
31 |
|
|
import java.util.HashMap;
|
32 |
|
|
import java.util.HashSet;
|
33 |
|
|
|
34 |
|
|
public class Commands {
|
35 |
|
|
|
36 |
|
|
static void parse(String name, Parse parse) throws Exception {
|
37 |
|
|
for(String key : commands.keySet()) {
|
38 |
|
|
String contents = commands.get(key);
|
39 |
|
|
|
40 |
|
|
String start = "<" + name + ">";
|
41 |
|
|
String end = "</" + name + ">";
|
42 |
|
|
|
43 |
|
|
while(true) {
|
44 |
|
|
int start_index = contents.indexOf(start);
|
45 |
|
|
int end_index = contents.indexOf(end);
|
46 |
|
|
|
47 |
|
|
if((start_index != -1 && end_index == -1) || (start_index == -1 && end_index != -1)) throw new Exception("Invalid <" + name + "> section in file: " + key);
|
48 |
|
|
|
49 |
|
|
if(start_index != -1 && end_index != -1) {
|
50 |
|
|
String section = contents.substring(start_index+start.length(), end_index);
|
51 |
|
|
|
52 |
|
|
parse.parse(section.trim());
|
53 |
|
|
|
54 |
|
|
contents = contents.replaceFirst("(?s)<" + name + ">.*?</" + name + ">", "");
|
55 |
|
|
commands.put(key, contents);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
if(start_index == -1 && end_index == -1) break;
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
static void read_command_files(File command_directory) throws Exception {
|
64 |
|
|
if(command_directory.exists() == false || command_directory.isDirectory() == false) throw new Exception("Can not find commands directory: " + command_directory.getCanonicalPath());
|
65 |
|
|
|
66 |
|
|
commands = new HashMap<>();
|
67 |
|
|
|
68 |
|
|
//read all files
|
69 |
|
|
for(File file : command_directory.listFiles()) {
|
70 |
|
|
commands.put(file.getName(), new String(Files.readAllBytes(file.toPath())));
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
//remove all comments and empty lines
|
74 |
|
|
for(String key : commands.keySet()) {
|
75 |
|
|
String contents = commands.get(key);
|
76 |
|
|
|
77 |
|
|
contents = contents.replaceAll("//[^\\n]*", "");
|
78 |
|
|
contents = contents.replaceAll("\\n\\s*\\n", "\n");
|
79 |
|
|
contents = contents.trim();
|
80 |
|
|
contents = contents.replaceAll("\\n\\s+", " ");
|
81 |
|
|
|
82 |
|
|
//System.out.println(contents);
|
83 |
|
|
commands.put(key, contents);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
//parse defines
|
87 |
|
|
HashSet<Integer> command_values = new HashSet<>();
|
88 |
|
|
command_values.add(0); //can not use zero
|
89 |
|
|
|
90 |
|
|
defines = new HashMap<>();
|
91 |
|
|
parse("defines", new ParseDefine(defines, command_values));
|
92 |
|
|
|
93 |
|
|
//parse decode
|
94 |
|
|
decode = new StringBuilder();
|
95 |
|
|
parse("decode", new ParseDecode(decode));
|
96 |
|
|
|
97 |
|
|
//parse microcode
|
98 |
|
|
microcode = new StringBuilder();
|
99 |
|
|
StringBuilder microcode_loop = new StringBuilder();
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
parse("microcode", new ParseMicrocode(defines, microcode, microcode_loop));
|
103 |
|
|
|
104 |
|
|
microcode_loop.append("\n");
|
105 |
|
|
|
106 |
|
|
microcode.append("IF(\n");
|
107 |
|
|
microcode.append(microcode_loop);
|
108 |
|
|
microcode.append(");\n");
|
109 |
|
|
microcode.append("\tSET(mc_cmd_current, mc_cmd);\n");
|
110 |
|
|
microcode.append("\tSET(mc_cmdex_current, mc_cmdex_last);\n");
|
111 |
|
|
microcode.append("\tSET(mc_cmd_next, mc_cmd);\n");
|
112 |
|
|
microcode.append("ENDIF();\n\n");
|
113 |
|
|
|
114 |
|
|
//parse read_local
|
115 |
|
|
read_local = new StringBuilder();
|
116 |
|
|
parse("read_local", new ParseToString(read_local));
|
117 |
|
|
|
118 |
|
|
//parse read
|
119 |
|
|
read = new StringBuilder();
|
120 |
|
|
parse("read", new ParseToString(read));
|
121 |
|
|
|
122 |
|
|
//parse execute_local
|
123 |
|
|
execute_local = new StringBuilder();
|
124 |
|
|
parse("execute_local", new ParseToString(execute_local));
|
125 |
|
|
|
126 |
|
|
//parse execute
|
127 |
|
|
execute = new StringBuilder();
|
128 |
|
|
parse("execute", new ParseToString(execute));
|
129 |
|
|
|
130 |
|
|
//parse write_local
|
131 |
|
|
write_local = new StringBuilder();
|
132 |
|
|
parse("write_local", new ParseToString(write_local));
|
133 |
|
|
|
134 |
|
|
//parse write
|
135 |
|
|
write = new StringBuilder();
|
136 |
|
|
parse("write", new ParseToString(write));
|
137 |
|
|
|
138 |
|
|
//check if all commands are empty
|
139 |
|
|
for(String name : commands.keySet()) {
|
140 |
|
|
String left = commands.get(name);
|
141 |
|
|
left = left.trim();
|
142 |
|
|
|
143 |
|
|
if(left.equals("") == false) {
|
144 |
|
|
throw new Exception("Unrecognized file part: " + name + "\n" + left);
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
public static void main(String args[]) throws Exception {
|
150 |
|
|
read_command_files(new File("./../rtl/commands"));
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
static HashMap<String, String> defines;
|
154 |
|
|
|
155 |
|
|
static StringBuilder decode;
|
156 |
|
|
|
157 |
|
|
static StringBuilder microcode;
|
158 |
|
|
|
159 |
|
|
static StringBuilder read_local;
|
160 |
|
|
static StringBuilder read;
|
161 |
|
|
|
162 |
|
|
static StringBuilder execute_local;
|
163 |
|
|
static StringBuilder execute;
|
164 |
|
|
|
165 |
|
|
static StringBuilder write_local;
|
166 |
|
|
static StringBuilder write;
|
167 |
|
|
|
168 |
|
|
static HashMap<String, String> commands;
|
169 |
|
|
}
|