Add opt_rmdff tests
[yosys.git] / tests / share / generate.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import sys
5 import random
6 from contextlib import contextmanager
7
8 @contextmanager
9 def redirect_stdout(new_target):
10 old_target, sys.stdout = sys.stdout, new_target
11 try:
12 yield new_target
13 finally:
14 sys.stdout = old_target
15
16 def random_plus_x():
17 return "%s x" % random.choice(['+', '+', '+', '-', '-', '|', '&', '^'])
18
19 def maybe_plus_x(expr):
20 if random.randint(0, 4) == 0:
21 return "(%s %s)" % (expr, random_plus_x())
22 else:
23 return expr
24
25 parser = argparse.ArgumentParser(formatter_class = argparse.ArgumentDefaultsHelpFormatter)
26 parser.add_argument('-S', '--seed', type = int, help = 'seed for PRNG')
27 parser.add_argument('-c', '--count', type = int, default = 100, help = 'number of test cases to generate')
28 args = parser.parse_args()
29
30 if args.seed is not None:
31 print("PRNG seed: %d" % args.seed)
32 random.seed(args.seed)
33
34 for idx in range(args.count):
35 with open('temp/uut_%05d.v' % idx, 'w') as f:
36 with redirect_stdout(f):
37 if random.choice(['bin', 'uni']) == 'bin':
38 print('module uut_%05d(a, b, c, d, x, s, y);' % (idx))
39 op = random.choice([
40 random.choice(['+', '-', '*', '/', '%']),
41 random.choice(['<', '<=', '==', '!=', '===', '!==', '>=', '>' ]),
42 random.choice(['<<', '>>', '<<<', '>>>']),
43 random.choice(['|', '&', '^', '~^', '||', '&&']),
44 ])
45 print(' input%s [%d:0] a;' % (random.choice(['', ' signed']), random.randint(0, 8)))
46 print(' input%s [%d:0] b;' % (random.choice(['', ' signed']), random.randint(0, 8)))
47 print(' input%s [%d:0] c;' % (random.choice(['', ' signed']), random.randint(0, 8)))
48 print(' input%s [%d:0] d;' % (random.choice(['', ' signed']), random.randint(0, 8)))
49 print(' input%s [%d:0] x;' % (random.choice(['', ' signed']), random.randint(0, 8)))
50 print(' input s;')
51 print(' output [%d:0] y;' % random.randint(0, 8))
52 print(' assign y = (s ? %s(%s %s %s) : %s(%s %s %s))%s;' %
53 (random.choice(['', '$signed', '$unsigned']), maybe_plus_x('a'), op, maybe_plus_x('b'),
54 random.choice(['', '$signed', '$unsigned']), maybe_plus_x('c'), op, maybe_plus_x('d'),
55 random_plus_x() if random.randint(0, 4) == 0 else ''))
56 print('endmodule')
57 else:
58 print('module uut_%05d(a, b, x, s, y);' % (idx))
59 op = random.choice(['~', '-', '!'])
60 print(' input%s [%d:0] a;' % (random.choice(['', ' signed']), random.randint(0, 8)))
61 print(' input%s [%d:0] b;' % (random.choice(['', ' signed']), random.randint(0, 8)))
62 print(' input%s [%d:0] x;' % (random.choice(['', ' signed']), random.randint(0, 8)))
63 print(' input s;')
64 print(' output [%d:0] y;' % random.randint(0, 8))
65 print(' assign y = (s ? %s(%s%s) : %s(%s%s))%s;' %
66 (random.choice(['', '$signed', '$unsigned']), op, maybe_plus_x('a'),
67 random.choice(['', '$signed', '$unsigned']), op, maybe_plus_x('b'),
68 random_plus_x() if random.randint(0, 4) == 0 else ''))
69 print('endmodule')
70 with open('temp/uut_%05d.ys' % idx, 'w') as f:
71 with redirect_stdout(f):
72 print('read_verilog temp/uut_%05d.v' % idx)
73 print('proc;;')
74 print('copy uut_%05d gold' % idx)
75 print('rename uut_%05d gate' % idx)
76 print('tee -a temp/all_share_log.txt log')
77 print('tee -a temp/all_share_log.txt log #job# uut_%05d' % idx)
78 print('tee -a temp/all_share_log.txt wreduce')
79 print('tee -a temp/all_share_log.txt share -aggressive gate')
80 print('miter -equiv -flatten -ignore_gold_x -make_outputs -make_outcmp gold gate miter')
81 print('sat -set-def-inputs -verify -prove trigger 0 -show-inputs -show-outputs miter')
82