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

Subversion Repositories ao486

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

powered by: WebSVN 2.1.0

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