8 #define bswap16(x) (x=(unsigned short)((x<<8)|(x>>8)))
9 #define bswap32(x) (x=((x<<24)|((x<<8)&0xff0000)|((x>>8)&0x00ff00)|((unsigned)x>>24)))
11 static void write_op(unsigned short op, unsigned short word0, unsigned short word1, unsigned short word2)
18 fwrite(&op, 1, sizeof(op), f);
19 fwrite(&word0, 1, sizeof(word0), f);
20 fwrite(&word1, 1, sizeof(word1), f);
21 fwrite(&word2, 1, sizeof(word2), f);
24 static void write32(unsigned int a)
27 fwrite(&a, 1, sizeof(a), f);
30 static int op_check(unsigned short op)
32 if ((op&0xf000) == 0x6000) return 0; // Bxx
33 if ((op&0xf0f8) == 0x50c8) return 0; // DBxx
34 if ((op&0xff80) == 0x4e80) return 0; // Jsr
35 if ((op&0xf000) == 0xa000) return 0; // a-line
36 if ((op&0xf000) == 0xf000) return 0; // f-line
37 if ((op&0xfff8)==0x4e70&&op!=0x4e71&&op!=0x4e76) return 0; // reset, rte, rts
39 if ((op&0x3f) >= 0x28) op = (op&~0x3f) | (rand() % 0x28);
43 static unsigned short safe_rand(void)
47 /* avoid branch opcodes */
52 while (!op_check(op));
63 f = fopen("test_misc2.bin", "wb");
66 write32(0x00ff8000); // stack
69 for (i=0x100/4-2; i; i--)
71 write32(0x200+i*4); // exception vectors
74 for (i=0x100/4; i; i--)
79 for (i=0x100/4; i; i--)
81 write32(0x4e734e73); // fill with rte instructions
84 for (op = 0; op < 0x10000; op++)
86 if ((op&0xf000) == 0x6000) // Bxx
89 write_op(op, 6, 0, 0);
91 else if ((op&0xf0f8)==0x50c8) // DBxx
93 write_op(op, 6, 0, 0);
95 else if ((op&0xff80)==0x4e80) // Jsr
97 int addr = 0x300 + op*8 + 8;
98 if ((op&0x3f) == 0x39)
99 write_op(op, addr >> 16, addr & 0xffff, 0);
101 else if ((op&0xf000)==0xa000 || (op&0xf000)==0xf000) // a-line, f-line
103 if (op != 0xa000 && op != 0xf000) continue;
105 else if ((op&0xfff8)==0x4e70&&op!=0x4e71&&op!=0x4e76); // rte, rts, stop, reset
108 write_op(op, safe_rand(), safe_rand(), safe_rand());
112 // jump to the beginning
113 write_op(0x4ef8, 0x300, 0x4ef8, 0x300);
114 write_op(0x4ef8, 0x300, 0x4ef8, 0x300);