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.test;
|
28 |
|
|
|
29 |
|
|
import ao486.module.memory.AvalonListener;
|
30 |
|
|
import ao486.module.memory.GlobalListener;
|
31 |
|
|
import ao486.module.memory.Input;
|
32 |
|
|
import ao486.module.memory.Listener;
|
33 |
|
|
import ao486.module.memory.Output;
|
34 |
|
|
import ao486.module.memory.ReadListener;
|
35 |
|
|
import ao486.module.memory.Test;
|
36 |
|
|
import ao486.module.memory.WriteListener;
|
37 |
|
|
import java.util.HashMap;
|
38 |
|
|
import java.util.LinkedList;
|
39 |
|
|
import java.util.Random;
|
40 |
|
|
|
41 |
|
|
public class TestWriteReadCache extends Test implements Listener, WriteListener.WriteInterface, ReadListener.ReadInterface {
|
42 |
|
|
void go(long seed) throws Exception {
|
43 |
|
|
random = new Random(seed);
|
44 |
|
|
|
45 |
|
|
avalon = new AvalonListener(random, this);
|
46 |
|
|
write = new WriteListener();
|
47 |
|
|
read = new ReadListener();
|
48 |
|
|
global = new GlobalListener();
|
49 |
|
|
|
50 |
|
|
global.set(false, false, false, false, random.nextBoolean(), false, 0xABCDE000, false, false, false, false);
|
51 |
|
|
|
52 |
|
|
written_memory = new HashMap<>();
|
53 |
|
|
|
54 |
|
|
finish_cycle = 0;
|
55 |
|
|
counter = 1;
|
56 |
|
|
|
57 |
|
|
long address = random.nextLong();
|
58 |
|
|
address &= 0xFFFFFFFFL;
|
59 |
|
|
|
60 |
|
|
int length = 1 + random.nextInt(4);
|
61 |
|
|
|
62 |
|
|
long value = random.nextLong();
|
63 |
|
|
value &= 0xFFFFFFFFL;
|
64 |
|
|
|
65 |
|
|
for(int i=0; i<length; i++) {
|
66 |
|
|
written_memory.put(address+i, (byte)((value >> (i*8)) & 0xFF));
|
67 |
|
|
//System.out.printf("write[%d]: %x <- %x\n", i, (address+i), (byte)((value >> (i*8)) & 0xFF));
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
write.write(257, 0, address, length, false, false, value, this);
|
71 |
|
|
|
72 |
|
|
//----------
|
73 |
|
|
|
74 |
|
|
LinkedList<Listener> listeners = new LinkedList<>();
|
75 |
|
|
listeners.add(avalon);
|
76 |
|
|
listeners.add(write);
|
77 |
|
|
listeners.add(read);
|
78 |
|
|
listeners.add(global);
|
79 |
|
|
listeners.add(this);
|
80 |
|
|
|
81 |
|
|
run_simulation(listeners);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
@Override
|
85 |
|
|
public void written(int cycle) throws Exception {
|
86 |
|
|
|
87 |
|
|
counter++;
|
88 |
|
|
|
89 |
|
|
if(counter >= 1000) {
|
90 |
|
|
finish_cycle = cycle+20;
|
91 |
|
|
return;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
boolean read_new = random.nextInt(4) == 0;
|
95 |
|
|
|
96 |
|
|
if(read_new) {
|
97 |
|
|
boolean read_next = random.nextInt(2) == 0;
|
98 |
|
|
|
99 |
|
|
if(read_next) {
|
100 |
|
|
// find some written address
|
101 |
|
|
Long addresses[] = new Long[written_memory.keySet().size()];
|
102 |
|
|
written_memory.keySet().toArray(addresses);
|
103 |
|
|
|
104 |
|
|
read_address = addresses[random.nextInt(addresses.length)];
|
105 |
|
|
|
106 |
|
|
// find fist not written
|
107 |
|
|
while(true) {
|
108 |
|
|
read_address++;
|
109 |
|
|
if(written_memory.containsKey(read_address) == false) break;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
// length
|
113 |
|
|
read_length = 1 + random.nextInt(8);
|
114 |
|
|
|
115 |
|
|
read.read(cycle+1, 0, read_address, read_length, false, false, this);
|
116 |
|
|
}
|
117 |
|
|
else {
|
118 |
|
|
// read from random address
|
119 |
|
|
do {
|
120 |
|
|
read_address = random.nextLong();
|
121 |
|
|
read_address &= 0xFFFFFFFFL;
|
122 |
|
|
} while(written_memory.containsKey(read_address));
|
123 |
|
|
|
124 |
|
|
// length
|
125 |
|
|
read_length = 1 + random.nextInt(8);
|
126 |
|
|
|
127 |
|
|
read.read(cycle+1, 0, read_address, read_length, false, false, this);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
}
|
131 |
|
|
else {
|
132 |
|
|
// read written values
|
133 |
|
|
|
134 |
|
|
// find some written address
|
135 |
|
|
Long addresses[] = new Long[written_memory.keySet().size()];
|
136 |
|
|
written_memory.keySet().toArray(addresses);
|
137 |
|
|
|
138 |
|
|
read_address = addresses[random.nextInt(addresses.length)];
|
139 |
|
|
|
140 |
|
|
// length
|
141 |
|
|
read_length = 1 + random.nextInt(8);
|
142 |
|
|
|
143 |
|
|
read.read(cycle+1, 0, read_address, read_length, false, false, this);
|
144 |
|
|
}
|
145 |
|
|
//System.out.printf("read: %x, %d\n", read_address, read_length);
|
146 |
|
|
}
|
147 |
|
|
@Override
|
148 |
|
|
public void write_page_fault(int cycle, long cr2, long error_code) throws Exception {
|
149 |
|
|
throw new Exception("write page fault: " + cycle + ", cr2: " + cr2 + ", error_code: " + error_code);
|
150 |
|
|
}
|
151 |
|
|
@Override
|
152 |
|
|
public void write_ac_fault(int cycle) throws Exception {
|
153 |
|
|
throw new Exception("write ac fault.");
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
@Override
|
157 |
|
|
public void read(int cycle, long data) throws Exception {
|
158 |
|
|
|
159 |
|
|
// check read data
|
160 |
|
|
for(int i=0; i<read_length; i++) {
|
161 |
|
|
if(written_memory.containsKey(read_address+i)) {
|
162 |
|
|
if((byte)(written_memory.get(read_address+i) & 0xFF) != (byte)(data & 0xFFL)) throw new Exception("check read data failed with written_memory.");
|
163 |
|
|
}
|
164 |
|
|
else if((byte)(avalon.get_memory(read_address+i) & 0xFF) != (byte)(data & 0xFFL)) {
|
165 |
|
|
throw new Exception(String.format("check read data failed with avalon memory: read_address: %x, i: %d", (read_address + i), i));
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
data >>= 8;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
// write next value
|
172 |
|
|
long write_address;
|
173 |
|
|
int write_length;
|
174 |
|
|
|
175 |
|
|
long value = random.nextLong();
|
176 |
|
|
value &= 0xFFFFFFFFL;
|
177 |
|
|
|
178 |
|
|
boolean write_new = random.nextInt(4) == 0;
|
179 |
|
|
|
180 |
|
|
if(write_new) {
|
181 |
|
|
boolean write_next = random.nextInt(2) == 0;
|
182 |
|
|
|
183 |
|
|
if(write_next) {
|
184 |
|
|
// find some written address
|
185 |
|
|
Long addresses[] = new Long[written_memory.keySet().size()];
|
186 |
|
|
written_memory.keySet().toArray(addresses);
|
187 |
|
|
|
188 |
|
|
write_address = addresses[random.nextInt(addresses.length)];
|
189 |
|
|
|
190 |
|
|
// find fist not written
|
191 |
|
|
while(true) {
|
192 |
|
|
write_address++;
|
193 |
|
|
if(written_memory.containsKey(write_address) == false) break;
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
// length
|
197 |
|
|
write_length = 1 + random.nextInt(4);
|
198 |
|
|
}
|
199 |
|
|
else {
|
200 |
|
|
// read from random address
|
201 |
|
|
do {
|
202 |
|
|
write_address = random.nextLong();
|
203 |
|
|
write_address &= 0xFFFFFFFFL;
|
204 |
|
|
} while(written_memory.containsKey(write_address));
|
205 |
|
|
|
206 |
|
|
// length
|
207 |
|
|
write_length = 1 + random.nextInt(4);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
}
|
211 |
|
|
else {
|
212 |
|
|
// read written values
|
213 |
|
|
|
214 |
|
|
// find some written address
|
215 |
|
|
Long addresses[] = new Long[written_memory.keySet().size()];
|
216 |
|
|
written_memory.keySet().toArray(addresses);
|
217 |
|
|
|
218 |
|
|
write_address = addresses[random.nextInt(addresses.length)];
|
219 |
|
|
|
220 |
|
|
// length
|
221 |
|
|
write_length = 1 + random.nextInt(4);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
for(int i=0; i<write_length; i++) {
|
225 |
|
|
written_memory.put(write_address+i, (byte)((value >> (i*8)) & 0xFF));
|
226 |
|
|
//System.out.printf("write[%d]: %x <- %x\n", i, (write_address+i), (byte)((value >> (i*8)) & 0xFF));
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
write.write(cycle+1, 0, write_address, write_length, false, false, value, this);
|
230 |
|
|
|
231 |
|
|
}
|
232 |
|
|
@Override
|
233 |
|
|
public void read_page_fault(int cycle, long cr2, long error_code) throws Exception {
|
234 |
|
|
throw new Exception("read_page_fault: " + cycle + ", cr2: " + cr2 + ", error_code: " + error_code);
|
235 |
|
|
}
|
236 |
|
|
@Override
|
237 |
|
|
public void read_ac_fault(int cycle) throws Exception {
|
238 |
|
|
throw new Exception("read_ac_fault.");
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
@Override
|
243 |
|
|
public void set_input(int cycle, Input input) throws Exception {
|
244 |
|
|
if(cycle >= finish_cycle && finish_cycle > 0) {
|
245 |
|
|
input.finished = true;
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
@Override
|
250 |
|
|
public void get_output(int cycle, Output output) throws Exception {
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
@Override
|
254 |
|
|
public void avalon_read(int cycle, long read_address) throws Exception {
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
HashMap<Long, Byte> written_memory;
|
258 |
|
|
|
259 |
|
|
int counter;
|
260 |
|
|
Random random;
|
261 |
|
|
|
262 |
|
|
int read_length;
|
263 |
|
|
long read_address;
|
264 |
|
|
|
265 |
|
|
int finish_cycle;
|
266 |
|
|
|
267 |
|
|
AvalonListener avalon;
|
268 |
|
|
WriteListener write;
|
269 |
|
|
ReadListener read;
|
270 |
|
|
GlobalListener global;
|
271 |
|
|
|
272 |
|
|
//-------------
|
273 |
|
|
|
274 |
|
|
public static void main(String args[]) throws Exception {
|
275 |
|
|
TestWriteReadCache test1 = new TestWriteReadCache();
|
276 |
|
|
|
277 |
|
|
for(int i=0; i<100; i++) {
|
278 |
|
|
test1.go(i*i);
|
279 |
|
|
|
280 |
|
|
System.out.println("Run " + i + " complete.");
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
System.out.println("[Main] thread end.");
|
284 |
|
|
}
|
285 |
|
|
}
|