yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fsm/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 # set to 'True' to compare verific with yosys
11 test_verific = False
12 
13 @contextmanager
14 def redirect_stdout(new_target):
15  old_target, sys.stdout = sys.stdout, new_target
16  try:
17  yield new_target
18  finally:
19  sys.stdout = old_target
20 
21 def random_expr(variables):
22  c = random.choice(['bin', 'uni', 'var', 'const'])
23  if c == 'bin':
24  op = random.choice(['+', '-', '*', '<', '<=', '==', '!=', '>=', '>', '<<', '>>', '<<<', '>>>', '|', '&', '^', '~^', '||', '&&'])
25  return "(%s %s %s)" % (random_expr(variables), op, random_expr(variables))
26  if c == 'uni':
27  op = random.choice(['+', '-', '~', '|', '&', '^', '~^', '!', '$signed', '$unsigned'])
28  return "%s(%s)" % (op, random_expr(variables))
29  if c == 'var':
30  return random.choice(variables)
31  if c == 'const':
32  bits = random.randint(1, 32)
33  return "%d'd%s" % (bits, random.randint(0, 2**bits-1))
34  raise AssertionError
35 
36 for idx in range(50):
37  with file('temp/uut_%05d.v' % idx, 'w') as f, redirect_stdout(f):
38  rst2 = random.choice([False, True])
39  if rst2:
40  print('module uut_%05d(clk, rst1, rst2, rst, a, b, c, x, y, z);' % (idx))
41  print(' input clk, rst1, rst2;')
42  print(' output rst;')
43  print(' assign rst = rst1 || rst2;')
44  else:
45  print('module uut_%05d(clk, rst, a, b, c, x, y, z);' % (idx))
46  print(' input clk, rst;')
47  variables=['a', 'b', 'c', 'x', 'y', 'z']
48  print(' input%s [%d:0] a;' % (random.choice(['', ' signed']), random.randint(0, 31)))
49  print(' input%s [%d:0] b;' % (random.choice(['', ' signed']), random.randint(0, 31)))
50  print(' input%s [%d:0] c;' % (random.choice(['', ' signed']), random.randint(0, 31)))
51  print(' output reg%s [%d:0] x;' % (random.choice(['', ' signed']), random.randint(0, 31)))
52  print(' output reg%s [%d:0] y;' % (random.choice(['', ' signed']), random.randint(0, 31)))
53  print(' output reg%s [%d:0] z;' % (random.choice(['', ' signed']), random.randint(0, 31)))
54  state_bits = random.randint(5, 16);
55  print(' %sreg [%d:0] state;' % (random.choice(['', '(* fsm_encoding = "one-hot" *)',
56  '(* fsm_encoding = "binary" *)']), state_bits-1))
57  states=[]
58  for i in range(random.randint(2, 10)):
59  n = random.randint(0, 2**state_bits-1)
60  if n not in states:
61  states.append(n)
62  print(' always @(posedge clk) begin')
63  print(' if (%s) begin' % ('rst1' if rst2 else 'rst'))
64  print(' x <= %d;' % random.randint(0, 2**31-1))
65  print(' y <= %d;' % random.randint(0, 2**31-1))
66  print(' z <= %d;' % random.randint(0, 2**31-1))
67  print(' state <= %d;' % random.choice(states))
68  print(' end else begin')
69  print(' case (state)')
70  for state in states:
71  print(' %d: begin' % state)
72  for var in ('x', 'y', 'z'):
73  print(' %s <= %s;' % (var, random_expr(variables)))
74  next_states = states[:]
75  for i in range(random.randint(0, len(states))):
76  next_state = random.choice(next_states)
77  next_states.remove(next_state)
78  print(' if ((%s) %s (%s)) state <= %s;' % (random_expr(variables),
79  random.choice(['<', '<=', '>=', '>']), random_expr(variables), next_state))
80  print(' end')
81  print(' endcase')
82  if rst2:
83  print(' if (rst2) begin')
84  print(' x <= a;')
85  print(' y <= b;')
86  print(' z <= c;')
87  print(' state <= %d;' % random.choice(states))
88  print(' end')
89  print(' end')
90  print(' end')
91  print('endmodule')
92  with file('temp/uut_%05d.ys' % idx, 'w') as f, redirect_stdout(f):
93  if test_verific:
94  print('read_verilog temp/uut_%05d.v' % idx)
95  print('proc;; rename uut_%05d gold' % idx)
96  print('verific -vlog2k temp/uut_%05d.v' % idx)
97  print('verific -import uut_%05d' % idx)
98  print('rename uut_%05d gate' % idx)
99  else:
100  print('read_verilog temp/uut_%05d.v' % idx)
101  print('proc;;')
102  print('copy uut_%05d gold' % idx)
103  print('rename uut_%05d gate' % idx)
104  print('cd gate')
105  print('opt; wreduce; share%s; opt; fsm;;' % random.choice(['', ' -aggressive']))
106  print('cd ..')
107  print('miter -equiv -flatten -ignore_gold_x -make_outputs -make_outcmp gold gate miter')
108  print('sat -verify-no-timeout -timeout 20 -seq 5 -set-at 1 %s_rst 1 -prove trigger 0 -prove-skip 1 -show-inputs -show-outputs miter' % ('gold' if rst2 else 'in'))
109 
def redirect_stdout
Definition: fsm/generate.py:14
def random_expr
Definition: fsm/generate.py:21