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.module.memory;
|
28 |
|
|
|
29 |
|
|
import java.io.File;
|
30 |
|
|
import java.io.InputStream;
|
31 |
|
|
import java.io.InputStreamReader;
|
32 |
|
|
import java.io.LineNumberReader;
|
33 |
|
|
import java.io.OutputStream;
|
34 |
|
|
import java.math.BigInteger;
|
35 |
|
|
import java.util.LinkedList;
|
36 |
|
|
import java.util.regex.Matcher;
|
37 |
|
|
import java.util.regex.Pattern;
|
38 |
|
|
|
39 |
|
|
public class Runner {
|
40 |
|
|
Runner(File directory) {
|
41 |
|
|
this.directory = directory;
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
long make_descriptor(long base, long limit) throws Exception {
|
45 |
|
|
|
46 |
|
|
if(limit >= 0x100000L && (limit & 0xFFFL) != 0xFFFL) throw new Exception(String.format("Illegal limit: %x", limit));
|
47 |
|
|
|
48 |
|
|
long g = 0;
|
49 |
|
|
if(limit >= 0x100000L) {
|
50 |
|
|
g = 1L << 55;
|
51 |
|
|
limit = (limit >> 12) & 0xFFFFFL;
|
52 |
|
|
}
|
53 |
|
|
//System.out.printf("CS BASE: %x, CS LIMIT: %x\n", base, limit);
|
54 |
|
|
return (((base >> 24) & 0xFFL) << 56) | ((base & 0x00FFFFFFL) << 16) | g | (((limit >> 16) & 0xFL) << 48) | (limit & 0xFFFFL);
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
void ser_bool(StringBuilder buf, String name, boolean bool) {
|
58 |
|
|
buf.append(name).append(": ");
|
59 |
|
|
buf.append(bool? "1 " : "0 ");
|
60 |
|
|
buf.append("\n");
|
61 |
|
|
}
|
62 |
|
|
void ser_long(StringBuilder buf, String name, long l) {
|
63 |
|
|
buf.append(name).append(": ");
|
64 |
|
|
buf.append(String.format("%x ", l));
|
65 |
|
|
buf.append("\n");
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
boolean des_bool(String str) {
|
69 |
|
|
return Long.parseLong(str) == 1;
|
70 |
|
|
}
|
71 |
|
|
long des_long(String str) {
|
72 |
|
|
int first = Integer.valueOf(str.substring(0,1), 16);
|
73 |
|
|
if(str.length() == 16 && first >= 8) {
|
74 |
|
|
long val = Long.parseLong(String.format("%x", first-8) + str.substring(1), 16);
|
75 |
|
|
val += Long.MIN_VALUE;
|
76 |
|
|
return val;
|
77 |
|
|
}
|
78 |
|
|
return Long.parseLong(str, 16);
|
79 |
|
|
}
|
80 |
|
|
BigInteger des_BigInteger(String str) {
|
81 |
|
|
return new BigInteger(str, 16);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
void execute() throws Exception {
|
86 |
|
|
|
87 |
|
|
Process p = null;
|
88 |
|
|
|
89 |
|
|
try {
|
90 |
|
|
ProcessBuilder pb = new ProcessBuilder("/opt/iverilog/bin/vvp", "tb_memory.vvp"); //, "-lxt2");
|
91 |
|
|
pb.directory(directory);
|
92 |
|
|
pb.redirectErrorStream(true);
|
93 |
|
|
|
94 |
|
|
p = pb.start();
|
95 |
|
|
|
96 |
|
|
InputStream in = p.getInputStream();
|
97 |
|
|
OutputStream out = p.getOutputStream();
|
98 |
|
|
|
99 |
|
|
Output output = new Output();
|
100 |
|
|
output_invalid = false;
|
101 |
|
|
|
102 |
|
|
boolean started = false;
|
103 |
|
|
int cycle = 0;
|
104 |
|
|
|
105 |
|
|
LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
|
106 |
|
|
|
107 |
|
|
while(true) {
|
108 |
|
|
String line = reader.readLine();
|
109 |
|
|
if(line == null) break;
|
110 |
|
|
|
111 |
|
|
line = line.trim();
|
112 |
|
|
|
113 |
|
|
//System.out.println("line: " + line);
|
114 |
|
|
if(line.startsWith("START")) {
|
115 |
|
|
started = true;
|
116 |
|
|
continue;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
if(started == false) continue;
|
120 |
|
|
|
121 |
|
|
if(line.endsWith("x")) {
|
122 |
|
|
output_invalid = true;
|
123 |
|
|
//System.out.println("line.endsWith: " + line);
|
124 |
|
|
continue;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
if(line.equals("")) {
|
128 |
|
|
if(output_invalid && cycle >= 3) throw new Exception("Invalid output.");
|
129 |
|
|
|
130 |
|
|
if(output_invalid == false) {
|
131 |
|
|
for(Listener listener : listeners) listener.get_output(cycle, output);
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
cycle++;
|
135 |
|
|
|
136 |
|
|
output = new Output();
|
137 |
|
|
output_invalid = false;
|
138 |
|
|
|
139 |
|
|
continue;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
Pattern pat = Pattern.compile("(\\w+?):\\s*([0-9a-fA-F]+)");
|
143 |
|
|
Matcher mat = pat.matcher(line);
|
144 |
|
|
|
145 |
|
|
boolean found = mat.find();
|
146 |
|
|
if(found == false) throw new Exception("Invalid line: " + line);
|
147 |
|
|
|
148 |
|
|
String s1 = mat.group(1);
|
149 |
|
|
String s2 = mat.group(2);
|
150 |
|
|
|
151 |
|
|
s1 = s1.trim();
|
152 |
|
|
s2 = s2.trim();
|
153 |
|
|
|
154 |
|
|
if(s1.equals("request")) {
|
155 |
|
|
Input input = new Input();
|
156 |
|
|
if(cycle == 0) input.rst_n = true;
|
157 |
|
|
if(cycle == 1) input.rst_n = false;
|
158 |
|
|
|
159 |
|
|
if(cycle >= 3) {
|
160 |
|
|
for(Listener listener : listeners) listener.set_input(cycle, input);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
StringBuilder buf = new StringBuilder();
|
164 |
|
|
|
165 |
|
|
ser_bool(buf, "rst_n", input.rst_n);
|
166 |
|
|
|
167 |
|
|
ser_bool(buf, "read_do", input.read_do);
|
168 |
|
|
ser_long(buf, "read_cpl", input.read_cpl);
|
169 |
|
|
ser_long(buf, "read_address", input.read_address);
|
170 |
|
|
ser_long(buf, "read_length", input.read_length);
|
171 |
|
|
ser_bool(buf, "read_lock", input.read_lock);
|
172 |
|
|
ser_bool(buf, "read_rmw", input.read_rmw);
|
173 |
|
|
|
174 |
|
|
ser_bool(buf, "write_do", input.write_do);
|
175 |
|
|
ser_long(buf, "write_cpl", input.write_cpl);
|
176 |
|
|
ser_long(buf, "write_address", input.write_address);
|
177 |
|
|
ser_long(buf, "write_length", input.write_length);
|
178 |
|
|
ser_bool(buf, "write_lock", input.write_lock);
|
179 |
|
|
ser_bool(buf, "write_rmw", input.write_rmw);
|
180 |
|
|
ser_long(buf, "write_data", input.write_data);
|
181 |
|
|
|
182 |
|
|
ser_bool(buf, "tlbcheck_do", input.tlbcheck_do);
|
183 |
|
|
ser_long(buf, "tlbcheck_address", input.tlbcheck_address);
|
184 |
|
|
ser_bool(buf, "tlbcheck_rw", input.tlbcheck_rw);
|
185 |
|
|
|
186 |
|
|
ser_bool(buf, "tlbflushsingle_do", input.tlbflushsingle_do);
|
187 |
|
|
ser_long(buf, "tlbflushsingle_address", input.tlbflushsingle_address);
|
188 |
|
|
|
189 |
|
|
ser_bool(buf, "tlbflushall_do", input.tlbflushall_do);
|
190 |
|
|
|
191 |
|
|
ser_bool(buf, "invdcode_do", input.invdcode_do);
|
192 |
|
|
ser_bool(buf, "invddata_do", input.invddata_do);
|
193 |
|
|
ser_bool(buf, "wbinvddata_do", input.wbinvddata_do);
|
194 |
|
|
|
195 |
|
|
ser_long(buf, "prefetch_cpl", input.cpl);
|
196 |
|
|
ser_long(buf, "prefetch_eip", input.eip);
|
197 |
|
|
ser_long(buf, "cs_cache", make_descriptor(input.cs_base, input.cs_limit));
|
198 |
|
|
|
199 |
|
|
ser_bool(buf, "prefetchfifo_accept_do", input.prefetchfifo_accept_do);
|
200 |
|
|
|
201 |
|
|
ser_bool(buf, "cr0_pg", input.cr0_pg);
|
202 |
|
|
ser_bool(buf, "cr0_wp", input.cr0_wp);
|
203 |
|
|
ser_bool(buf, "cr0_am", input.cr0_am);
|
204 |
|
|
ser_bool(buf, "cr0_cd", input.cr0_cd);
|
205 |
|
|
ser_bool(buf, "cr0_nw", input.cr0_nw);
|
206 |
|
|
|
207 |
|
|
ser_bool(buf, "acflag", input.acflag);
|
208 |
|
|
|
209 |
|
|
ser_long(buf, "cr3", (input.cr3_base & 0xFFFFF000) | (input.cr3_pcd? 1<<4 : 0) | (input.cr3_pwt? 1<<3 : 0));
|
210 |
|
|
|
211 |
|
|
ser_bool(buf, "pipeline_after_read_empty", input.pipeline_after_read_empty);
|
212 |
|
|
ser_bool(buf, "pipeline_after_prefetch_empty", input.pipeline_after_prefetch_empty);
|
213 |
|
|
|
214 |
|
|
ser_bool(buf, "pr_reset", input.pr_reset);
|
215 |
|
|
ser_bool(buf, "rd_reset", input.rd_reset);
|
216 |
|
|
ser_bool(buf, "exe_reset", input.exe_reset);
|
217 |
|
|
ser_bool(buf, "wr_reset", input.wr_reset);
|
218 |
|
|
|
219 |
|
|
ser_bool(buf, "avm_waitrequest", input.avm_waitrequest);
|
220 |
|
|
ser_bool(buf, "avm_readdatavalid", input.avm_readdatavalid);
|
221 |
|
|
ser_long(buf, "avm_readdata", input.avm_readdata);
|
222 |
|
|
|
223 |
|
|
if(input.finished) buf.append("quit: 0\n");
|
224 |
|
|
else buf.append("continue: 0\n");
|
225 |
|
|
|
226 |
|
|
//System.out.println("Send: " + buf.toString());
|
227 |
|
|
out.write(buf.toString().getBytes());
|
228 |
|
|
out.flush();
|
229 |
|
|
|
230 |
|
|
continue;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
switch(s1) {
|
234 |
|
|
case "time": break;
|
235 |
|
|
|
236 |
|
|
case "read_done": output.read_done = des_bool(s2); break;
|
237 |
|
|
case "read_page_fault": output.read_page_fault = des_bool(s2); break;
|
238 |
|
|
case "read_ac_fault": output.read_ac_fault = des_bool(s2); break;
|
239 |
|
|
case "read_data": output.read_data = des_long(s2); break;
|
240 |
|
|
|
241 |
|
|
case "write_done": output.write_done = des_bool(s2); break;
|
242 |
|
|
case "write_page_fault": output.write_page_fault = des_bool(s2); break;
|
243 |
|
|
case "write_ac_fault": output.write_ac_fault = des_bool(s2); break;
|
244 |
|
|
|
245 |
|
|
case "tlbcheck_done": output.tlbcheck_done = des_bool(s2); break;
|
246 |
|
|
case "tlbcheck_page_fault": output.tlbcheck_page_fault = des_bool(s2); break;
|
247 |
|
|
|
248 |
|
|
case "tlbflushsingle_done": output.tlbflushsingle_done = des_bool(s2); break;
|
249 |
|
|
|
250 |
|
|
case "invdcode_done": output.invdcode_done = des_bool(s2); break;
|
251 |
|
|
case "invddata_done": output.invddata_done = des_bool(s2); break;
|
252 |
|
|
case "wbinvddata_done": output.wbinvddata_done = des_bool(s2); break;
|
253 |
|
|
|
254 |
|
|
case "prefetchfifo_accept_data": output.prefetchfifo_accept_data = des_BigInteger(s2); break;
|
255 |
|
|
case "prefetchfifo_accept_empty": output.prefetchfifo_accept_empty= des_bool(s2); break;
|
256 |
|
|
|
257 |
|
|
case "tlb_code_pf_cr2": output.tlb_code_pf_cr2 = des_long(s2); break;
|
258 |
|
|
case "tlb_code_pf_error_code": output.tlb_code_pf_error_code = des_long(s2); break;
|
259 |
|
|
|
260 |
|
|
case "tlb_check_pf_cr2": output.tlb_check_pf_cr2 = des_long(s2); break;
|
261 |
|
|
case "tlb_check_pf_error_code": output.tlb_check_pf_error_code = des_long(s2); break;
|
262 |
|
|
|
263 |
|
|
case "tlb_write_pf_cr2": output.tlb_write_pf_cr2 = des_long(s2); break;
|
264 |
|
|
case "tlb_write_pf_error_code": output.tlb_write_pf_error_code = des_long(s2); break;
|
265 |
|
|
|
266 |
|
|
case "tlb_read_pf_cr2": output.tlb_read_pf_cr2 = des_long(s2); break;
|
267 |
|
|
case "tlb_read_pf_error_code": output.tlb_read_pf_error_code = des_long(s2); break;
|
268 |
|
|
|
269 |
|
|
case "avm_address": output.avm_address = des_long(s2); break;
|
270 |
|
|
case "avm_writedata": output.avm_writedata = des_long(s2); break;
|
271 |
|
|
case "avm_byteenable": output.avm_byteenable = des_long(s2); break;
|
272 |
|
|
case "avm_burstcount": output.avm_burstcount = des_long(s2); break;
|
273 |
|
|
case "avm_write": output.avm_write = des_bool(s2); break;
|
274 |
|
|
case "avm_read": output.avm_read = des_bool(s2); break;
|
275 |
|
|
|
276 |
|
|
default: throw new Exception("Unknown line: " + line);
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
}
|
280 |
|
|
int result = p.waitFor();
|
281 |
|
|
|
282 |
|
|
System.out.println("[Runner] Run result: " + result);
|
283 |
|
|
}
|
284 |
|
|
finally {
|
285 |
|
|
if(p != null) p.destroy();
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
boolean output_invalid;
|
290 |
|
|
LinkedList<Listener> listeners;
|
291 |
|
|
File directory;
|
292 |
|
|
}
|