yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
realmath/generate.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 from __future__ import division
4 from __future__ import print_function
5 
6 import sys
7 import random
8 from contextlib import contextmanager
9 
10 @contextmanager
11 def redirect_stdout(new_target):
12  old_target, sys.stdout = sys.stdout, new_target
13  try:
14  yield new_target
15  finally:
16  sys.stdout = old_target
17 
18 def random_expression(depth = 3, maxparam = 0):
19  def recursion():
20  return random_expression(depth = depth-1, maxparam = maxparam)
21  if depth == 0:
22  if maxparam != 0 and random.randint(0, 1) != 0:
23  return 'p%02d' % random.randint(0, maxparam-1)
24  return random.choice([ '%e', '%f', '%g' ]) % random.uniform(-2, +2)
25  if random.randint(0, 4) == 0:
26  return recursion() + random.choice([ ' < ', ' <= ', ' == ', ' != ', ' >= ', ' > ' ]) + recursion() + ' ? ' + recursion() + ' : ' + recursion()
27  op_prefix = [ '+(', '-(' ]
28  op_infix = [ ' + ', ' - ', ' * ', ' / ' ]
29  op_func1 = [ '$ln', '$log10', '$exp', '$sqrt', '$floor', '$ceil', '$sin', '$cos', '$tan', '$asin', '$acos', '$atan', '$sinh', '$cosh', '$tanh', '$asinh', '$acosh', '$atanh' ]
30  op_func2 = [ '$pow', '$atan2', '$hypot' ]
31  op = random.choice(op_prefix + op_infix + op_func1 + op_func2)
32  if op in op_prefix:
33  return op + recursion() + ')'
34  if op in op_infix:
35  return '(' + recursion() + op + recursion() + ')'
36  if op in op_func1:
37  return op + '(' + recursion() + ')'
38  if op in op_func2:
39  return op + '(' + recursion() + ', ' + recursion() + ')'
40  raise
41 
42 for idx in range(100):
43  with file('temp/uut_%05d.v' % idx, 'w') as f, redirect_stdout(f):
44  print('module uut_%05d(output [63:0] %s);\n' % (idx, ', '.join(['y%02d' % i for i in range(100)])))
45  for i in range(30):
46  if idx < 10:
47  print('localparam p%02d = %s;' % (i, random_expression()))
48  else:
49  print('localparam%s p%02d = %s;' % (random.choice(['', ' real', ' integer']), i, random_expression()))
50  for i in range(30, 60):
51  if idx < 10:
52  print('localparam p%02d = %s;' % (i, random_expression(maxparam = 30)))
53  else:
54  print('localparam%s p%02d = %s;' % (random.choice(['', ' real', ' integer']), i, random_expression(maxparam = 30)))
55  for i in range(100):
56  print('assign y%02d = 65536 * (%s);' % (i, random_expression(maxparam = 60)))
57  print('endmodule')
58  with file('temp/uut_%05d.ys' % idx, 'w') as f, redirect_stdout(f):
59  print('read_verilog uut_%05d.v' % idx)
60  print('rename uut_%05d uut_%05d_syn' % (idx, idx))
61  print('write_verilog uut_%05d_syn.v' % idx)
62  with file('temp/uut_%05d_tb.v' % idx, 'w') as f, redirect_stdout(f):
63  print('module uut_%05d_tb;\n' % idx)
64  print('wire [63:0] %s;' % (', '.join(['r%02d' % i for i in range(100)])))
65  print('wire [63:0] %s;' % (', '.join(['s%02d' % i for i in range(100)])))
66  print('uut_%05d ref(%s);' % (idx, ', '.join(['r%02d' % i for i in range(100)])))
67  print('uut_%05d_syn syn(%s);' % (idx, ', '.join(['s%02d' % i for i in range(100)])))
68  print('task compare_ref_syn;')
69  print(' input [7:0] i;')
70  print(' input [63:0] r, s;')
71  print(' reg [64*8-1:0] buffer;')
72  print(' integer j;')
73  print(' begin')
74  print(' if (-1 <= $signed(r-s) && $signed(r-s) <= +1) begin')
75  print(' // $display("%d: %b %b", i, r, s);')
76  print(' end else if (r === s) begin ')
77  print(' // $display("%d: %b %b", i, r, s);')
78  print(' end else begin ')
79  print(' for (j = 0; j < 64; j = j+1)')
80  print(' buffer[j*8 +: 8] = r[j] !== s[j] ? "^" : " ";')
81  print(' $display("\\n%3d: %b %b", i, r, s);')
82  print(' $display(" %s %s", buffer, buffer);')
83  print(' end')
84  print(' end')
85  print('endtask')
86  print('initial begin #1;')
87  for i in range(100):
88  print(' compare_ref_syn(%2d, r%02d, s%02d);' % (i, i, i))
89  print('end')
90  print('endmodule')
91 
def redirect_stdout
Definition: fsm/generate.py:14
def random_expression