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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [test/] [bcd/] [TestDAS.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 TestDAS extends TestUnit implements Serializable {
48
    public static void main(String args[]) throws Exception {
49
        run_test(TestDAS.class);
50
    }
51
 
52
    //--------------------------------------------------------------------------
53
    @Override
54
    public int get_test_count() throws Exception {
55
        return 512;
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 long eax = random.nextInt();
95
 
96
            layers.addFirst(new Layer() {
97
                public long eax() { return eax; }
98
            });
99
 
100
            // instruction
101
            instruction = prepare_instr(cs_d_b, a32, o32, null);
102
 
103
            instruction += "0F0F";
104
 
105
            // add instruction
106
            instr.add_instruction(instruction);
107
 
108
            // end condition
109
            break;
110
        }
111
 
112
        System.out.println("Instruction: [" + instruction + "]");
113
    }
114
 
115
    String prepare_instr(boolean cs_d_b, boolean a32, boolean o32, byte modregrm_bytes[]) throws Exception {
116
 
117
        int opcodes[] = {
118
            0x2F
119
        };
120
 
121
        String prefix = "";
122
        if(cs_d_b != o32) { prefix = "66" + prefix; }
123
        if(cs_d_b != a32) { prefix = "67" + prefix; }
124
 
125
        int opcode = opcodes[random.nextInt(opcodes.length)];
126
        boolean is_modregrm = false;
127
 
128
        byte possible_modregrm = (byte)random.nextInt();
129
        byte possible_sib      = (byte)random.nextInt();
130
 
131
        int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!a32, unsigned(possible_modregrm), unsigned(possible_sib));
132
System.out.println("[len final: " + len + "]");
133
 
134
        byte instr[] = new byte[len];
135
        instr[0] = (byte)opcode;
136
        for(int i=1; i<len; i++) {
137
            if(i==1)        instr[1] = possible_modregrm;
138
            else if(i==2)   instr[2] = possible_sib;
139
            else            instr[i] = (byte)random.nextInt();
140
        }
141
 
142
        return prefix + bytesToHex(instr);
143
   }
144
}
145
 
146
 
147
/*
148
public class TestDAS {
149
    public static void main(String args[]) throws Exception {
150
        TestManager manager = new TestManager();
151
 
152
        TestDASSerializable test = new TestDASSerializable();
153
 
154
        if(false) {
155
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.obj"));
156
            test = (TestDASSerializable)ois.readObject();
157
            ois.close();
158
        }
159
 
160
        for(; test.index<test.get_test_count(); test.index++) {
161
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running test " + (test.index+1) + "/" + test.get_test_count());
162
 
163
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.obj"));
164
            oos.writeObject(test);
165
            oos.close();
166
 
167
            boolean passed = manager.run_test_and_print_result(test);
168
            if(passed == false) break;
169
        }
170
    }
171
}
172
class TestDASSerializable extends TestBase implements Test, Serializable {
173
    TestDASSerializable() {
174
        random = new Random(0);
175
    }
176
 
177
    Random random;
178
    int index;
179
    int local;
180
 
181
    boolean d_b;
182
 
183
    //--------------------------------------------------------------------------
184
    @Override
185
    public int get_test_count() throws Exception {
186
        return 1000;
187
    }
188
 
189
    @Override
190
    public void init() throws Exception {
191
        d_b = random.nextBoolean();
192
    }
193
 
194
    @Override
195
    public boolean fini() throws Exception {
196
        return index < get_test_count();
197
    }
198
 
199
    String prepare_instr() throws Exception {
200
        byte instr[] = new byte[1];
201
 
202
        instr[0] = (byte)0x2F;
203
 
204
        return bytesToHex(instr);
205
    }
206
 
207
    public String get_instructions() throws Exception {
208
        String instr = "";
209
 
210
        while(instr.length() < 2*15) {
211
            instr += prepare_instr();
212
        }
213
        instr = instr.substring(0, 2*15);
214
 
215
System.out.println("[get_instructions: " + instr + "]");
216
        return instr;
217
    }
218
    @Override
219
    public byte get_memory(int address) throws Exception {
220
        return (byte)random.nextInt();
221
    }
222
    @Override
223
    public int eax() throws Exception {
224
        return (random.nextInt() & 0xFFFFFF00) | ((local++) & 0xFF);
225
 
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.