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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [bcd/] [TestAAM.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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.test.bcd;
28
 
29
import ao486.test.TestUnit;
30
import static ao486.test.TestUnit.run_test;
31
import ao486.test.layers.FlagsLayer;
32
import ao486.test.layers.GeneralRegisterLayer;
33
import ao486.test.layers.HandleModeChangeLayer;
34
import ao486.test.layers.IOLayer;
35
import ao486.test.layers.InstructionLayer;
36
import ao486.test.layers.Layer;
37
import ao486.test.layers.MemoryLayer;
38
import ao486.test.layers.OtherLayer;
39
import ao486.test.layers.Pair;
40
import ao486.test.layers.SegmentLayer;
41
import ao486.test.layers.StackLayer;
42
import java.io.Serializable;
43
import java.util.LinkedList;
44
import java.util.Random;
45
 
46
 
47
public class TestAAM extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestAAM.class);
50
    }
51
 
52
    //--------------------------------------------------------------------------
53
    @Override
54
    public int get_test_count() throws Exception {
55
        return 100;
56
    }
57
 
58
    @Override
59
    public void init() throws Exception {
60
 
61
        random = new Random(3+index);
62
 
63
        String instruction;
64
        while(true) {
65
            layers.clear();
66
 
67
            LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
68
 
69
            InstructionLayer instr = new InstructionLayer(random, prohibited_list);
70
            layers.add(instr);
71
            layers.add(new StackLayer(random, prohibited_list));
72
            layers.add(new OtherLayer(OtherLayer.Type.RANDOM, random));
73
            layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
74
            layers.add(new GeneralRegisterLayer(random));
75
            layers.add(new SegmentLayer(random));
76
            layers.add(new MemoryLayer(random));
77
            layers.add(new IOLayer(random));
78
            layers.addFirst(new HandleModeChangeLayer(
79
                    getInput("cr0_pe"),
80
                    getInput("vmflag"),
81
                    getInput("cs_rpl"),
82
                    getInput("cs_p"),
83
                    getInput("cs_s"),
84
                    getInput("cs_type")
85
            ));
86
 
87
            // instruction size
88
            boolean cs_d_b = getInput("cs_d_b") == 1;
89
 
90
            boolean a32 = random.nextBoolean();
91
            boolean o32 = random.nextBoolean();
92
 
93
 
94
            final int val = random.nextInt() % 10;
95
            final long eax =
96
                (val == 0)? 0 :
97
                (val == 1)? 1 :
98
                (val == 2)? 254 :
99
                (val == 3)? 255 :
100
                random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
101
 
102
            layers.addFirst(new Layer() {
103
                public long eax() { return eax; }
104
            });
105
 
106
            // instruction
107
            instruction = prepare_instr(cs_d_b, a32, o32, null);
108
 
109
            instruction += "0F0F";
110
 
111
            // add instruction
112
            instr.add_instruction(instruction);
113
 
114
            // end condition
115
            break;
116
        }
117
 
118
        System.out.println("Instruction: [" + instruction + "]");
119
    }
120
 
121
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
122
 
123
        int opcodes[] = {
124
            0xD4
125
        };
126
 
127
        String prefix = "";
128
        if(cs_d_b != o32) { prefix = "66" + prefix; }
129
        if(cs_d_b != a32) { prefix = "67" + prefix; }
130
 
131
        int opcode = opcodes[random.nextInt(opcodes.length)];
132
 
133
        byte instr[] = new byte[2];
134
        instr[0] = (byte)opcode;
135
        instr[1] = (random.nextInt(10) == 0)? 0 : (byte)(random.nextInt() & 0xFF);
136
 
137
        return prefix + bytesToHex(instr);
138
   }
139
}
140
 
141
 
142
/*
143
public class TestAAM {
144
    public static void main(String args[]) throws Exception {
145
        TestManager manager = new TestManager();
146
 
147
        TestAAMSerializable test = new TestAAMSerializable();
148
 
149
        if(false) {
150
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.obj"));
151
            test = (TestAAMSerializable)ois.readObject();
152
            ois.close();
153
        }
154
 
155
        for(; test.index<test.get_test_count(); test.index++) {
156
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running test " + (test.index+1) + "/" + test.get_test_count());
157
 
158
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.obj"));
159
            oos.writeObject(test);
160
            oos.close();
161
 
162
            boolean passed = manager.run_test_and_print_result(test);
163
            if(passed == false) break;
164
        }
165
    }
166
}
167
class TestAAMSerializable extends TestBase implements Test, Serializable {
168
    TestAAMSerializable() {
169
        random = new Random(1);
170
    }
171
 
172
    Random random;
173
    int index;
174
    int local;
175
 
176
    boolean d_b;
177
 
178
    //--------------------------------------------------------------------------
179
    @Override
180
    public int get_test_count() throws Exception {
181
        return 1000;
182
    }
183
 
184
    @Override
185
    public void init() throws Exception {
186
        d_b = random.nextBoolean();
187
    }
188
 
189
    @Override
190
    public boolean fini() throws Exception {
191
        return index < get_test_count();
192
    }
193
 
194
    String prepare_instr() throws Exception {
195
        byte instr[] = new byte[2];
196
 
197
        instr[0] = (byte)0xD4;
198
        instr[1] = (byte)local++;
199
 
200
        return bytesToHex(instr);
201
    }
202
 
203
    public String get_instructions() throws Exception {
204
        String instr = "";
205
 
206
        while(instr.length() < 2*15) {
207
            instr += prepare_instr();
208
        }
209
        instr = instr.substring(0, 2*15);
210
 
211
System.out.println("[get_instructions: " + instr + "]");
212
        return instr;
213
    }
214
    @Override
215
    public byte get_memory(int address) throws Exception {
216
        return (byte)random.nextInt();
217
    }
218
    @Override
219
    public int eax() throws Exception {
220
        int val = random.nextInt() % 10;
221
        return  (val == 0)? 0 :
222
                (val == 1)? 1 :
223
                (val == 2)? 254 :
224
                (val == 3)? 255 :
225
                random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
226
    }
227
    @Override
228
    public int get_ebx() throws Exception {
229
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
230
    }
231
    @Override
232
    public int get_ecx() throws Exception {
233
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
234
    }
235
    @Override
236
    public int get_edx() throws Exception {
237
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
238
    }
239
    @Override
240
    public int get_esi() throws Exception {
241
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
242
    }
243
    @Override
244
    public int get_edi() throws Exception {
245
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
246
    }
247
    @Override
248
    public int get_ebp() throws Exception {
249
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
250
    }
251
    @Override
252
    public int get_esp() throws Exception {
253
        return random.nextInt() & ((random.nextInt(3) == 0)? 0xFFFFFFFF : 0x00000FFF);
254
    }
255
    @Override
256
    public boolean get_cf() throws Exception {
257
        return random.nextBoolean();
258
    }
259
    @Override
260
    public boolean get_pf() throws Exception {
261
        return random.nextBoolean();
262
    }
263
    @Override
264
    public boolean get_af() throws Exception {
265
        return random.nextBoolean();
266
    }
267
    @Override
268
    public boolean get_zf() throws Exception {
269
        return random.nextBoolean();
270
    }
271
    @Override
272
    public boolean get_sf() throws Exception {
273
        return random.nextBoolean();
274
    }
275
    @Override
276
    public boolean get_tf() throws Exception {
277
        return random.nextBoolean();
278
    }
279
    @Override
280
    public boolean get_if() throws Exception {
281
        return random.nextBoolean();
282
    }
283
    @Override
284
    public boolean get_df() throws Exception {
285
        return random.nextBoolean();
286
    }
287
    @Override
288
    public boolean get_of() throws Exception {
289
        return random.nextBoolean();
290
    }
291
    @Override
292
    public int get_iopl() throws Exception {
293
        return random.nextInt(4);
294
    }
295
    @Override
296
    public boolean get_nt() throws Exception {
297
        return random.nextBoolean();
298
    }
299
    @Override
300
    public boolean get_rf() throws Exception {
301
        return random.nextBoolean();
302
    }
303
    @Override
304
    public boolean get_vm() throws Exception {
305
        return random.nextBoolean();
306
    }
307
    @Override
308
    public boolean get_ac() throws Exception {
309
        return random.nextBoolean();
310
    }
311
    @Override
312
    public boolean get_id() throws Exception {
313
        return random.nextBoolean();
314
    }
315
    @Override
316
    public int get_cs_base() throws Exception {
317
        return 0;
318
    }
319
    @Override
320
    public int get_cs_limit() throws Exception {
321
        return 0x000FFFFF;
322
    }
323
    @Override
324
    public boolean get_cs_d_b() throws Exception {
325
        return d_b;
326
    }
327
    @Override
328
    public int get_ds_base() throws Exception {
329
        return 0;
330
    }
331
    @Override
332
    public int get_ds_limit() throws Exception {
333
        return 0x000FFFFF;
334
    }
335
    @Override
336
    public int get_es_base() throws Exception {
337
        return 0;
338
    }
339
    @Override
340
    public int get_es_limit() throws Exception {
341
        return 0x000FFFFF;
342
    }
343
    @Override
344
    public int get_fs_base() throws Exception {
345
        return 0;
346
    }
347
    @Override
348
    public int get_fs_limit() throws Exception {
349
        return 0x000FFFFF;
350
    }
351
    @Override
352
    public int get_gs_base() throws Exception {
353
        return 0;
354
    }
355
    @Override
356
    public int get_gs_limit() throws Exception {
357
        return 0x000FFFFF;
358
    }
359
    @Override
360
    public int get_ss_base() throws Exception {
361
        return 0;
362
    }
363
    @Override
364
    public int get_ss_limit() throws Exception {
365
        return 0x000FFFFF;
366
    }
367
}
368
*/

powered by: WebSVN 2.1.0

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