2015-10-13 10:17:23 -05:00
|
|
|
#!/usr/bin/env python3
|
2015-10-13 18:31:54 -05:00
|
|
|
#
|
|
|
|
# yosys -- Yosys Open SYnthesis Suite
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#
|
2015-10-13 10:17:23 -05:00
|
|
|
|
|
|
|
import os, sys, getopt, re
|
2015-10-13 18:31:54 -05:00
|
|
|
##yosys-sys-path##
|
2016-09-02 05:01:31 -05:00
|
|
|
from smtio import SmtIo, SmtOpts, MkVcd
|
2016-08-24 16:18:29 -05:00
|
|
|
from collections import defaultdict
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2015-10-13 18:27:55 -05:00
|
|
|
skip_steps = 0
|
2015-12-20 02:58:54 -06:00
|
|
|
step_size = 1
|
2015-10-13 18:27:55 -05:00
|
|
|
num_steps = 20
|
2015-10-13 10:17:23 -05:00
|
|
|
vcdfile = None
|
2016-08-20 09:32:50 -05:00
|
|
|
vlogtbfile = None
|
2016-08-22 10:27:43 -05:00
|
|
|
inconstr = list()
|
2016-08-22 09:48:46 -05:00
|
|
|
outconstr = None
|
2016-08-20 09:32:50 -05:00
|
|
|
gentrace = False
|
2015-10-13 10:17:23 -05:00
|
|
|
tempind = False
|
2016-08-29 15:41:45 -05:00
|
|
|
dumpall = False
|
2015-10-13 18:27:55 -05:00
|
|
|
assume_skipped = None
|
2016-08-27 15:04:15 -05:00
|
|
|
final_only = False
|
2015-10-15 08:08:41 -05:00
|
|
|
topmod = None
|
2016-09-19 13:43:28 -05:00
|
|
|
noinfo = False
|
2016-09-02 05:01:31 -05:00
|
|
|
so = SmtOpts()
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2015-10-13 10:17:23 -05:00
|
|
|
def usage():
|
|
|
|
print("""
|
2015-10-13 17:37:41 -05:00
|
|
|
yosys-smtbmc [options] <yosys_smt2_output>
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-27 15:04:15 -05:00
|
|
|
-t <num_steps>
|
|
|
|
-t <skip_steps>:<num_steps>
|
|
|
|
-t <skip_steps>:<step_size>:<num_steps>
|
|
|
|
default: skip_steps=0, step_size=1, num_steps=20
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-20 09:32:50 -05:00
|
|
|
-g
|
|
|
|
generate an arbitrary trace that satisfies
|
|
|
|
all assertions and assumptions.
|
|
|
|
|
2015-10-13 18:27:55 -05:00
|
|
|
-i
|
2015-10-13 10:17:23 -05:00
|
|
|
instead of BMC run temporal induction
|
|
|
|
|
|
|
|
-m <module_name>
|
2015-10-15 08:08:41 -05:00
|
|
|
name of the top module
|
2016-08-20 09:07:59 -05:00
|
|
|
|
2016-08-24 15:09:50 -05:00
|
|
|
--smtc <constr_filename>
|
2016-08-22 10:27:43 -05:00
|
|
|
read constraints file
|
|
|
|
|
2016-09-19 13:43:28 -05:00
|
|
|
--noinfo
|
|
|
|
only run the core proof, do not collect and print any
|
|
|
|
additional information (e.g. which assert failed)
|
|
|
|
|
2016-08-27 15:04:15 -05:00
|
|
|
--final-only
|
|
|
|
only check final constraints, assume base case
|
|
|
|
|
|
|
|
--assume-skipped <start_step>
|
|
|
|
assume asserts in skipped steps in BMC.
|
|
|
|
no assumptions are created for skipped steps
|
|
|
|
before <start_step>.
|
|
|
|
|
2016-08-20 09:07:59 -05:00
|
|
|
--dump-vcd <vcd_filename>
|
2016-08-20 09:32:50 -05:00
|
|
|
write trace to this VCD file
|
2016-08-20 09:07:59 -05:00
|
|
|
(hint: use 'write_smt2 -wires' for maximum
|
|
|
|
coverage of signals in generated VCD file)
|
2016-08-20 11:43:39 -05:00
|
|
|
|
|
|
|
--dump-vlogtb <verilog_filename>
|
|
|
|
write trace as Verilog test bench
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-24 15:09:50 -05:00
|
|
|
--dump-smtc <constr_filename>
|
2016-08-22 09:48:46 -05:00
|
|
|
write trace as constraints file
|
2016-08-29 15:41:45 -05:00
|
|
|
|
|
|
|
--dump-all
|
|
|
|
when using -g or -i, create a dump file for each
|
|
|
|
step. The character '%' is replaces in all dump
|
|
|
|
filenames with the step number.
|
2015-10-13 10:17:23 -05:00
|
|
|
""" + so.helpmsg())
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2015-10-13 10:17:23 -05:00
|
|
|
try:
|
2016-08-27 15:04:15 -05:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], so.shortopts + "t:igm:", so.longopts +
|
2016-09-19 13:43:28 -05:00
|
|
|
["final-only", "assume-skipped=", "smtc=", "dump-vcd=", "dump-vlogtb=", "dump-smtc=", "dump-all", "noinfo"])
|
2015-10-13 10:17:23 -05:00
|
|
|
except:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
for o, a in opts:
|
|
|
|
if o == "-t":
|
2016-08-27 15:04:15 -05:00
|
|
|
a = a.split(":")
|
|
|
|
if len(a) == 1:
|
2016-08-29 06:53:12 -05:00
|
|
|
num_steps = int(a[0])
|
2016-08-27 15:04:15 -05:00
|
|
|
elif len(a) == 2:
|
|
|
|
skip_steps = int(a[0])
|
|
|
|
num_steps = int(a[1])
|
|
|
|
elif len(a) == 3:
|
|
|
|
skip_steps = int(a[0])
|
|
|
|
step_size = int(a[1])
|
|
|
|
num_steps = int(a[2])
|
2015-10-13 18:27:55 -05:00
|
|
|
else:
|
2016-08-27 15:04:15 -05:00
|
|
|
assert 0
|
|
|
|
elif o == "--assume-skipped":
|
2015-10-13 18:27:55 -05:00
|
|
|
assume_skipped = int(a)
|
2016-08-27 15:04:15 -05:00
|
|
|
elif o == "--final-only":
|
|
|
|
final_only = True
|
2016-08-24 15:09:50 -05:00
|
|
|
elif o == "--smtc":
|
2016-08-22 10:27:43 -05:00
|
|
|
inconstr.append(a)
|
2016-08-20 09:07:59 -05:00
|
|
|
elif o == "--dump-vcd":
|
2015-10-13 10:17:23 -05:00
|
|
|
vcdfile = a
|
2016-08-20 11:43:39 -05:00
|
|
|
elif o == "--dump-vlogtb":
|
|
|
|
vlogtbfile = a
|
2016-08-24 15:09:50 -05:00
|
|
|
elif o == "--dump-smtc":
|
2016-08-22 09:48:46 -05:00
|
|
|
outconstr = a
|
2016-08-29 15:41:45 -05:00
|
|
|
elif o == "--dump-all":
|
|
|
|
dumpall = True
|
2016-09-19 13:43:28 -05:00
|
|
|
elif o == "--noinfo":
|
|
|
|
noinfo = True
|
2015-10-13 10:17:23 -05:00
|
|
|
elif o == "-i":
|
|
|
|
tempind = True
|
2016-08-20 09:32:50 -05:00
|
|
|
elif o == "-g":
|
|
|
|
gentrace = True
|
2015-10-13 17:37:41 -05:00
|
|
|
elif o == "-m":
|
|
|
|
topmod = a
|
2015-10-13 10:17:23 -05:00
|
|
|
elif so.handle(o, a):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
usage()
|
|
|
|
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2016-08-27 07:30:36 -05:00
|
|
|
constr_final_start = None
|
2016-08-24 16:18:29 -05:00
|
|
|
constr_asserts = defaultdict(list)
|
|
|
|
constr_assumes = defaultdict(list)
|
2016-09-24 13:40:22 -05:00
|
|
|
constr_write = list()
|
2016-08-22 10:27:43 -05:00
|
|
|
|
|
|
|
for fn in inconstr:
|
2016-08-24 16:18:29 -05:00
|
|
|
current_states = None
|
2016-08-29 06:53:12 -05:00
|
|
|
current_line = 0
|
2016-08-22 10:27:43 -05:00
|
|
|
|
|
|
|
with open(fn, "r") as f:
|
|
|
|
for line in f:
|
2016-08-29 06:53:12 -05:00
|
|
|
current_line += 1
|
|
|
|
|
2016-08-24 16:18:29 -05:00
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
tokens = line.split()
|
|
|
|
|
|
|
|
if len(tokens) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "initial":
|
2016-08-24 16:18:29 -05:00
|
|
|
current_states = set()
|
2016-09-17 17:48:36 -05:00
|
|
|
if not tempind:
|
|
|
|
current_states.add(0)
|
2016-08-22 10:27:43 -05:00
|
|
|
continue
|
|
|
|
|
2016-08-27 07:30:36 -05:00
|
|
|
if tokens[0] == "final":
|
|
|
|
constr_final = True
|
|
|
|
if len(tokens) == 1:
|
|
|
|
current_states = set(["final-%d" % i for i in range(0, num_steps+1)])
|
|
|
|
constr_final_start = 0
|
|
|
|
elif len(tokens) == 2:
|
|
|
|
i = int(tokens[1])
|
|
|
|
assert i < 0
|
|
|
|
current_states = set(["final-%d" % i for i in range(-i, num_steps+1)])
|
|
|
|
constr_final_start = -i if constr_final_start is None else min(constr_final_start, -i)
|
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
continue
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
if tokens[0] == "state":
|
2016-08-24 16:18:29 -05:00
|
|
|
current_states = set()
|
2016-09-17 17:48:36 -05:00
|
|
|
if not tempind:
|
|
|
|
for token in tokens[1:]:
|
|
|
|
tok = token.split(":")
|
|
|
|
if len(tok) == 1:
|
|
|
|
current_states.add(int(token))
|
|
|
|
elif len(tok) == 2:
|
|
|
|
lower = int(tok[0])
|
|
|
|
if tok[1] == "*":
|
|
|
|
upper = num_steps
|
|
|
|
else:
|
|
|
|
upper = int(tok[1])
|
|
|
|
for i in range(lower, upper+1):
|
|
|
|
current_states.add(i)
|
2016-08-24 16:18:29 -05:00
|
|
|
else:
|
2016-09-17 17:48:36 -05:00
|
|
|
assert 0
|
2016-08-24 16:18:29 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "always":
|
|
|
|
if len(tokens) == 1:
|
|
|
|
current_states = set(range(0, num_steps+1))
|
|
|
|
elif len(tokens) == 2:
|
|
|
|
i = int(tokens[1])
|
|
|
|
assert i < 0
|
|
|
|
current_states = set(range(-i, num_steps+1))
|
|
|
|
else:
|
|
|
|
assert 0
|
2016-08-22 10:27:43 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "assert":
|
2016-08-24 16:18:29 -05:00
|
|
|
assert current_states is not None
|
2016-08-22 10:27:43 -05:00
|
|
|
|
2016-08-24 16:18:29 -05:00
|
|
|
for state in current_states:
|
2016-08-29 06:53:12 -05:00
|
|
|
constr_asserts[state].append(("%s:%d" % (fn, current_line), " ".join(tokens[1:])))
|
2016-08-22 10:27:43 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "assume":
|
2016-08-24 16:18:29 -05:00
|
|
|
assert current_states is not None
|
2016-08-22 10:27:43 -05:00
|
|
|
|
2016-08-24 16:18:29 -05:00
|
|
|
for state in current_states:
|
2016-08-29 06:53:12 -05:00
|
|
|
constr_assumes[state].append(("%s:%d" % (fn, current_line), " ".join(tokens[1:])))
|
2016-08-22 10:27:43 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
|
2016-09-24 13:40:22 -05:00
|
|
|
if tokens[0] == "write":
|
|
|
|
constr_write.append(" ".join(tokens[1:]))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if tokens[0] == "logic":
|
|
|
|
so.logic = " ".join(tokens[1:])
|
|
|
|
continue
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
def get_constr_expr(db, state, final=False, getvalues=False):
|
2016-08-27 07:30:36 -05:00
|
|
|
if final:
|
|
|
|
if ("final-%d" % state) not in db:
|
2016-08-29 07:53:32 -05:00
|
|
|
return ([], [], []) if getvalues else "true"
|
2016-08-27 07:30:36 -05:00
|
|
|
else:
|
|
|
|
if state not in db:
|
2016-08-29 07:53:32 -05:00
|
|
|
return ([], [], []) if getvalues else "true"
|
2016-08-22 10:27:43 -05:00
|
|
|
|
2016-08-24 16:18:29 -05:00
|
|
|
netref_regex = re.compile(r'(^|[( ])\[(-?[0-9]+:|)([^\]]+)\](?=[ )]|$)')
|
|
|
|
|
|
|
|
def replace_netref(match):
|
|
|
|
state_sel = match.group(2)
|
|
|
|
|
|
|
|
if state_sel == "":
|
|
|
|
st = state
|
|
|
|
elif state_sel[0] == "-":
|
|
|
|
st = state + int(state_sel[:-1])
|
|
|
|
else:
|
|
|
|
st = int(state_sel[:-1])
|
|
|
|
|
|
|
|
expr = smt.net_expr(topmod, "s%d" % st, smt.get_path(topmod, match.group(3)))
|
|
|
|
|
|
|
|
return match.group(1) + expr
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
expr_list = list()
|
2016-08-29 06:53:12 -05:00
|
|
|
for loc, expr in db[("final-%d" % state) if final else state]:
|
|
|
|
actual_expr = netref_regex.sub(replace_netref, expr)
|
|
|
|
if getvalues:
|
|
|
|
expr_list.append((loc, expr, actual_expr))
|
|
|
|
else:
|
|
|
|
expr_list.append(actual_expr)
|
|
|
|
|
|
|
|
if getvalues:
|
|
|
|
loc_list, expr_list, acual_expr_list = zip(*expr_list)
|
|
|
|
value_list = smt.get_list(acual_expr_list)
|
|
|
|
return loc_list, expr_list, value_list
|
2016-08-22 10:27:43 -05:00
|
|
|
|
2016-08-22 10:45:01 -05:00
|
|
|
if len(expr_list) == 0:
|
|
|
|
return "true"
|
|
|
|
|
|
|
|
if len(expr_list) == 1:
|
|
|
|
return expr_list[0]
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
return "(and %s)" % " ".join(expr_list)
|
|
|
|
|
|
|
|
|
2016-09-02 05:01:31 -05:00
|
|
|
smt = SmtIo(opts=so)
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-09-24 13:40:22 -05:00
|
|
|
if noinfo and vcdfile is None and vlogtbfile is None and outconstr is None:
|
|
|
|
smt.produce_models = False
|
|
|
|
|
2016-09-05 18:40:31 -05:00
|
|
|
def print_msg(msg):
|
|
|
|
print("%s %s" % (smt.timestamp(), msg))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
print_msg("Solver: %s" % (so.solver))
|
2015-10-13 10:17:23 -05:00
|
|
|
|
|
|
|
with open(args[0], "r") as f:
|
|
|
|
for line in f:
|
|
|
|
smt.write(line)
|
|
|
|
|
2016-09-24 13:40:22 -05:00
|
|
|
for line in constr_write:
|
|
|
|
smt.write(line)
|
|
|
|
|
2016-07-11 04:49:05 -05:00
|
|
|
if topmod is None:
|
|
|
|
topmod = smt.topmod
|
2015-10-15 08:08:41 -05:00
|
|
|
|
2016-07-11 04:49:05 -05:00
|
|
|
assert topmod is not None
|
|
|
|
assert topmod in smt.modinfo
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2016-07-11 05:49:33 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
def write_vcd_trace(steps_start, steps_stop, index):
|
|
|
|
filename = vcdfile.replace("%", index)
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Writing trace to VCD file: %s" % (filename))
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
with open(filename, "w") as vcd_file:
|
2016-09-02 05:01:31 -05:00
|
|
|
vcd = MkVcd(vcd_file)
|
2016-08-29 15:41:45 -05:00
|
|
|
path_list = list()
|
2016-08-18 04:17:45 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
for netpath in sorted(smt.hiernets(topmod)):
|
|
|
|
hidden_net = False
|
|
|
|
for n in netpath:
|
|
|
|
if n.startswith("$"):
|
|
|
|
hidden_net = True
|
|
|
|
if not hidden_net:
|
|
|
|
vcd.add_net([topmod] + netpath, smt.net_width(topmod, netpath))
|
|
|
|
path_list.append(netpath)
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
for i in range(steps_start, steps_stop):
|
|
|
|
vcd.set_time(i)
|
|
|
|
value_list = smt.get_net_bin_list(topmod, path_list, "s%d" % i)
|
|
|
|
for path, value in zip(path_list, value_list):
|
|
|
|
vcd.set_net([topmod] + path, value)
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
vcd.set_time(steps_stop)
|
2015-10-13 10:17:23 -05:00
|
|
|
|
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
def write_vlogtb_trace(steps_start, steps_stop, index):
|
|
|
|
filename = vlogtbfile.replace("%", index)
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Writing trace to Verilog testbench: %s" % (filename))
|
2016-08-20 11:43:39 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
with open(filename, "w") as f:
|
2016-08-20 11:43:39 -05:00
|
|
|
print("module testbench;", file=f)
|
|
|
|
print(" reg [4095:0] vcdfile;", file=f)
|
|
|
|
print(" reg clock = 0, genclock = 1;", file=f)
|
|
|
|
|
|
|
|
primary_inputs = list()
|
|
|
|
clock_inputs = set()
|
|
|
|
|
|
|
|
for name in smt.modinfo[topmod].inputs:
|
|
|
|
if name in ["clk", "clock", "CLK", "CLOCK"]:
|
|
|
|
clock_inputs.add(name)
|
|
|
|
width = smt.modinfo[topmod].wsize[name]
|
|
|
|
primary_inputs.append((name, width))
|
|
|
|
|
|
|
|
for name, width in primary_inputs:
|
|
|
|
if name in clock_inputs:
|
|
|
|
print(" wire [%d:0] PI_%s = clock;" % (width-1, name), file=f)
|
|
|
|
else:
|
|
|
|
print(" reg [%d:0] PI_%s;" % (width-1, name), file=f)
|
|
|
|
|
|
|
|
print(" %s UUT (" % topmod, file=f)
|
2016-09-02 03:02:19 -05:00
|
|
|
print(",\n".join(" .{name}(PI_{name})".format(name=name) for name, _ in primary_inputs), file=f)
|
2016-08-20 11:43:39 -05:00
|
|
|
print(" );", file=f)
|
|
|
|
|
|
|
|
print(" initial begin", file=f)
|
|
|
|
print(" if ($value$plusargs(\"vcd=%s\", vcdfile)) begin", file=f)
|
|
|
|
print(" $dumpfile(vcdfile);", file=f)
|
|
|
|
print(" $dumpvars(0, testbench);", file=f)
|
|
|
|
print(" end", file=f)
|
|
|
|
print(" while (genclock) begin", file=f)
|
|
|
|
print(" #5; clock = 0;", file=f)
|
|
|
|
print(" #5; clock = 1;", file=f)
|
|
|
|
print(" end", file=f)
|
|
|
|
print(" end", file=f)
|
|
|
|
|
|
|
|
print(" initial begin", file=f)
|
|
|
|
|
|
|
|
regs = sorted(smt.hiernets(topmod, regs_only=True))
|
2016-08-29 15:41:45 -05:00
|
|
|
regvals = smt.get_net_bin_list(topmod, regs, "s%d" % steps_start)
|
2016-08-20 11:43:39 -05:00
|
|
|
|
2016-09-02 03:02:19 -05:00
|
|
|
print(" #1;", file=f)
|
2016-08-20 11:43:39 -05:00
|
|
|
for reg, val in zip(regs, regvals):
|
|
|
|
hidden_net = False
|
|
|
|
for n in reg:
|
|
|
|
if n.startswith("$"):
|
|
|
|
hidden_net = True
|
|
|
|
print(" %sUUT.%s = %d'b%s;" % ("// " if hidden_net else "", ".".join(reg), len(val), val), file=f)
|
|
|
|
|
|
|
|
mems = sorted(smt.hiermems(topmod))
|
|
|
|
for mempath in mems:
|
2016-08-29 15:41:45 -05:00
|
|
|
abits, width, ports = smt.mem_info(topmod, "s%d" % steps_start, mempath)
|
|
|
|
mem = smt.mem_expr(topmod, "s%d" % steps_start, mempath)
|
2016-08-21 08:56:22 -05:00
|
|
|
|
|
|
|
addr_expr_list = list()
|
2016-08-29 15:41:45 -05:00
|
|
|
for i in range(steps_start, steps_stop):
|
2016-08-21 08:56:22 -05:00
|
|
|
for j in range(ports):
|
|
|
|
addr_expr_list.append(smt.mem_expr(topmod, "s%d" % i, mempath, j))
|
|
|
|
|
|
|
|
addr_list = set()
|
|
|
|
for val in smt.get_list(addr_expr_list):
|
|
|
|
addr_list.add(smt.bv2int(val))
|
2016-08-20 11:43:39 -05:00
|
|
|
|
|
|
|
expr_list = list()
|
2016-08-21 08:56:22 -05:00
|
|
|
for i in addr_list:
|
2016-08-20 11:43:39 -05:00
|
|
|
expr_list.append("(select %s #b%s)" % (mem, format(i, "0%db" % abits)))
|
|
|
|
|
2016-08-21 08:56:22 -05:00
|
|
|
for i, val in zip(addr_list, smt.get_list(expr_list)):
|
2016-08-20 11:43:39 -05:00
|
|
|
val = smt.bv2bin(val)
|
|
|
|
print(" UUT.%s[%d] = %d'b%s;" % (".".join(mempath), i, len(val), val), file=f)
|
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
for i in range(steps_start, steps_stop):
|
2016-08-20 11:43:39 -05:00
|
|
|
pi_names = [[name] for name, _ in primary_inputs if name not in clock_inputs]
|
|
|
|
pi_values = smt.get_net_bin_list(topmod, pi_names, "s%d" % i)
|
|
|
|
|
2016-09-02 03:02:19 -05:00
|
|
|
print(" #1;", file=f)
|
|
|
|
print(" // state %d" % i, file=f)
|
2016-08-20 11:43:39 -05:00
|
|
|
if i > 0:
|
2016-09-02 03:02:19 -05:00
|
|
|
print(" @(posedge clock);", file=f)
|
2016-08-20 11:43:39 -05:00
|
|
|
for name, val in zip(pi_names, pi_values):
|
|
|
|
print(" PI_%s <= %d'b%s;" % (".".join(name), len(val), val), file=f)
|
|
|
|
|
2016-09-02 03:02:19 -05:00
|
|
|
print(" genclock = 0;", file=f)
|
2016-08-20 11:43:39 -05:00
|
|
|
print(" end", file=f)
|
|
|
|
|
|
|
|
print("endmodule", file=f)
|
|
|
|
|
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
def write_constr_trace(steps_start, steps_stop, index):
|
|
|
|
filename = outconstr.replace("%", index)
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Writing trace to constraints file: %s" % (filename))
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
with open(filename, "w") as f:
|
2016-08-22 09:48:46 -05:00
|
|
|
primary_inputs = list()
|
|
|
|
|
|
|
|
for name in smt.modinfo[topmod].inputs:
|
|
|
|
width = smt.modinfo[topmod].wsize[name]
|
|
|
|
primary_inputs.append((name, width))
|
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
if steps_start == 0:
|
|
|
|
print("initial", file=f)
|
|
|
|
else:
|
|
|
|
print("state %d" % steps_start, file=f)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
regnames = sorted(smt.hiernets(topmod, regs_only=True))
|
2016-08-29 15:41:45 -05:00
|
|
|
regvals = smt.get_net_list(topmod, regnames, "s%d" % steps_start)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
for name, val in zip(regnames, regvals):
|
|
|
|
print("assume (= [%s] %s)" % (".".join(name), val), file=f)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
mems = sorted(smt.hiermems(topmod))
|
|
|
|
for mempath in mems:
|
2016-08-29 15:41:45 -05:00
|
|
|
abits, width, ports = smt.mem_info(topmod, "s%d" % steps_start, mempath)
|
|
|
|
mem = smt.mem_expr(topmod, "s%d" % steps_start, mempath)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
addr_expr_list = list()
|
2016-08-29 15:41:45 -05:00
|
|
|
for i in range(steps_start, steps_stop):
|
2016-08-29 06:53:12 -05:00
|
|
|
for j in range(ports):
|
|
|
|
addr_expr_list.append(smt.mem_expr(topmod, "s%d" % i, mempath, j))
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-09-02 03:02:19 -05:00
|
|
|
addr_list = set((smt.bv2int(val) for val in smt.get_list(addr_expr_list)))
|
2016-08-29 06:53:12 -05:00
|
|
|
|
|
|
|
expr_list = list()
|
|
|
|
for i in addr_list:
|
|
|
|
expr_list.append("(select %s #b%s)" % (mem, format(i, "0%db" % abits)))
|
|
|
|
|
|
|
|
for i, val in zip(addr_list, smt.get_list(expr_list)):
|
|
|
|
print("assume (= (select [%s] #b%s) %s)" % (".".join(mempath), format(i, "0%db" % abits), val), file=f)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
for k in range(steps_start, steps_stop):
|
2016-08-29 06:53:12 -05:00
|
|
|
print("", file=f)
|
|
|
|
print("state %d" % k, file=f)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
pi_names = [[name] for name, _ in sorted(primary_inputs)]
|
2016-08-22 09:48:46 -05:00
|
|
|
pi_values = smt.get_net_list(topmod, pi_names, "s%d" % k)
|
|
|
|
|
|
|
|
for name, val in zip(pi_names, pi_values):
|
|
|
|
print("assume (= [%s] %s)" % (".".join(name), val), file=f)
|
|
|
|
|
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
def write_trace(steps_start, steps_stop, index):
|
2016-08-20 11:43:39 -05:00
|
|
|
if vcdfile is not None:
|
2016-08-29 15:41:45 -05:00
|
|
|
write_vcd_trace(steps_start, steps_stop, index)
|
2016-08-20 11:43:39 -05:00
|
|
|
|
|
|
|
if vlogtbfile is not None:
|
2016-08-29 15:41:45 -05:00
|
|
|
write_vlogtb_trace(steps_start, steps_stop, index)
|
2016-08-20 11:43:39 -05:00
|
|
|
|
2016-08-22 09:48:46 -05:00
|
|
|
if outconstr is not None:
|
2016-08-29 15:41:45 -05:00
|
|
|
write_constr_trace(steps_start, steps_stop, index)
|
2016-08-22 09:48:46 -05:00
|
|
|
|
2016-08-20 11:43:39 -05:00
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
def print_failed_asserts_worker(mod, state, path):
|
2016-08-17 13:10:02 -05:00
|
|
|
assert mod in smt.modinfo
|
|
|
|
|
2016-09-13 06:23:06 -05:00
|
|
|
if smt.get("(|%s_a| %s)" % (mod, state)) in ["true", "#b1"]:
|
2016-08-17 13:10:02 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
for cellname, celltype in smt.modinfo[mod].cells.items():
|
2016-08-29 07:53:32 -05:00
|
|
|
print_failed_asserts_worker(celltype, "(|%s_h %s| %s)" % (mod, cellname, state), path + "." + cellname)
|
2016-08-17 13:10:02 -05:00
|
|
|
|
|
|
|
for assertfun, assertinfo in smt.modinfo[mod].asserts.items():
|
2016-09-13 06:23:06 -05:00
|
|
|
if smt.get("(|%s| %s)" % (assertfun, state)) in ["false", "#b0"]:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Assert failed in %s: %s" % (path, assertinfo))
|
2016-08-17 13:10:02 -05:00
|
|
|
|
|
|
|
|
2016-08-29 06:53:12 -05:00
|
|
|
def print_failed_asserts(state, final=False):
|
2016-09-19 13:43:28 -05:00
|
|
|
if noinfo: return
|
2016-08-29 06:53:12 -05:00
|
|
|
loc_list, expr_list, value_list = get_constr_expr(constr_asserts, state, final=final, getvalues=True)
|
|
|
|
|
|
|
|
for loc, expr, value in zip(loc_list, expr_list, value_list):
|
|
|
|
if smt.bv2int(value) == 0:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Assert %s failed: %s" % (loc, expr))
|
2016-08-29 06:53:12 -05:00
|
|
|
|
|
|
|
if not final:
|
2016-08-29 07:53:32 -05:00
|
|
|
print_failed_asserts_worker(topmod, "s%d" % state, topmod)
|
2016-08-29 06:53:12 -05:00
|
|
|
|
|
|
|
|
2016-08-30 12:27:42 -05:00
|
|
|
def print_anyconsts_worker(mod, state, path):
|
|
|
|
assert mod in smt.modinfo
|
|
|
|
|
|
|
|
for cellname, celltype in smt.modinfo[mod].cells.items():
|
|
|
|
print_anyconsts_worker(celltype, "(|%s_h %s| %s)" % (mod, cellname, state), path + "." + cellname)
|
|
|
|
|
|
|
|
for fun, info in smt.modinfo[mod].anyconsts.items():
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Value for anyconst in %s (%s): %d" % (path, info, smt.bv2int(smt.get("(|%s| %s)" % (fun, state)))))
|
2016-08-30 12:27:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
def print_anyconsts(state):
|
2016-09-19 13:43:28 -05:00
|
|
|
if noinfo: return
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts_worker(topmod, "s%d" % state, topmod)
|
|
|
|
|
|
|
|
|
2015-10-13 17:18:38 -05:00
|
|
|
if tempind:
|
2015-10-15 08:54:59 -05:00
|
|
|
retstatus = False
|
2015-12-20 02:58:54 -06:00
|
|
|
skip_counter = step_size
|
2015-10-13 18:27:55 -05:00
|
|
|
for step in range(num_steps, -1, -1):
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(declare-fun s%d () |%s_s|)" % (step, topmod))
|
|
|
|
smt.write("(assert (|%s_u| s%d))" % (topmod, step))
|
|
|
|
smt.write("(assert (|%s_h| s%d))" % (topmod, step))
|
|
|
|
smt.write("(assert (not (|%s_is| s%d)))" % (topmod, step))
|
2016-09-17 17:48:36 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_assumes, step))
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2015-10-13 18:27:55 -05:00
|
|
|
if step == num_steps:
|
2016-09-17 17:48:36 -05:00
|
|
|
smt.write("(assert (not (and (|%s_a| s%d) %s)))" % (topmod, step, get_constr_expr(constr_asserts, step)))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
|
|
|
else:
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (|%s_t| s%d s%d))" % (topmod, step, step+1))
|
|
|
|
smt.write("(assert (|%s_a| s%d))" % (topmod, step))
|
2016-09-17 17:48:36 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_asserts, step))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2015-10-13 18:27:55 -05:00
|
|
|
if step > num_steps-skip_steps:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Skipping induction in step %d.." % (step))
|
2015-10-13 17:18:38 -05:00
|
|
|
continue
|
|
|
|
|
2015-12-20 02:58:54 -06:00
|
|
|
skip_counter += 1
|
|
|
|
if skip_counter < step_size:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Skipping induction in step %d.." % (step))
|
2015-12-20 02:58:54 -06:00
|
|
|
continue
|
|
|
|
|
|
|
|
skip_counter = 0
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Trying induction in step %d.." % (step))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
|
|
|
if smt.check_sat() == "sat":
|
|
|
|
if step == 0:
|
2015-10-15 08:54:59 -05:00
|
|
|
print("%s Temporal induction failed!" % smt.timestamp())
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(num_steps)
|
2016-08-29 06:53:12 -05:00
|
|
|
print_failed_asserts(num_steps)
|
2016-08-29 15:41:45 -05:00
|
|
|
write_trace(step, num_steps+1, '%')
|
|
|
|
|
|
|
|
elif dumpall:
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(num_steps)
|
2016-08-29 15:41:45 -05:00
|
|
|
print_failed_asserts(num_steps)
|
|
|
|
write_trace(step, num_steps+1, "%d" % step)
|
2015-10-13 17:18:38 -05:00
|
|
|
|
|
|
|
else:
|
2015-10-15 08:54:59 -05:00
|
|
|
print("%s Temporal induction successful." % smt.timestamp())
|
|
|
|
retstatus = True
|
2015-10-13 17:18:38 -05:00
|
|
|
break
|
|
|
|
|
2016-08-22 10:27:43 -05:00
|
|
|
|
2016-09-02 05:09:09 -05:00
|
|
|
else: # not tempind
|
2015-12-20 02:58:54 -06:00
|
|
|
step = 0
|
2015-10-15 08:54:59 -05:00
|
|
|
retstatus = True
|
2015-12-20 02:58:54 -06:00
|
|
|
while step < num_steps:
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(declare-fun s%d () |%s_s|)" % (step, topmod))
|
|
|
|
smt.write("(assert (|%s_u| s%d))" % (topmod, step))
|
|
|
|
smt.write("(assert (|%s_h| s%d))" % (topmod, step))
|
2016-08-22 10:27:43 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_assumes, step))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
|
|
|
if step == 0:
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (|%s_i| s0))" % (topmod))
|
|
|
|
smt.write("(assert (|%s_is| s0))" % (topmod))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
|
|
|
else:
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (|%s_t| s%d s%d))" % (topmod, step-1, step))
|
|
|
|
smt.write("(assert (not (|%s_is| s%d)))" % (topmod, step))
|
2015-10-13 17:18:38 -05:00
|
|
|
|
2015-10-13 18:27:55 -05:00
|
|
|
if step < skip_steps:
|
|
|
|
if assume_skipped is not None and step >= assume_skipped:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Skipping step %d (and assuming pass).." % (step))
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (|%s_a| s%d))" % (topmod, step))
|
2016-08-22 10:27:43 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_asserts, step))
|
2015-10-13 18:27:55 -05:00
|
|
|
else:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Skipping step %d.." % (step))
|
2015-12-20 02:58:54 -06:00
|
|
|
step += 1
|
2015-10-13 18:27:55 -05:00
|
|
|
continue
|
|
|
|
|
2015-12-20 02:58:54 -06:00
|
|
|
last_check_step = step
|
|
|
|
for i in range(1, step_size):
|
|
|
|
if step+i < num_steps:
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(declare-fun s%d () |%s_s|)" % (step+i, topmod))
|
|
|
|
smt.write("(assert (|%s_u| s%d))" % (topmod, step+i))
|
|
|
|
smt.write("(assert (|%s_h| s%d))" % (topmod, step+i))
|
|
|
|
smt.write("(assert (|%s_t| s%d s%d))" % (topmod, step+i-1, step+i))
|
2016-08-22 10:27:43 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_assumes, step+i))
|
2015-12-20 02:58:54 -06:00
|
|
|
last_check_step = step+i
|
|
|
|
|
2016-08-24 15:09:50 -05:00
|
|
|
if not gentrace:
|
2016-08-27 15:04:15 -05:00
|
|
|
if not final_only:
|
|
|
|
if last_check_step == step:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Checking asserts in step %d.." % (step))
|
2016-08-27 15:04:15 -05:00
|
|
|
else:
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Checking asserts in steps %d to %d.." % (step, last_check_step))
|
2016-08-27 15:04:15 -05:00
|
|
|
smt.write("(push 1)")
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (not (and %s)))" % " ".join(["(|%s_a| s%d)" % (topmod, i) for i in range(step, last_check_step+1)] +
|
2016-08-27 15:04:15 -05:00
|
|
|
[get_constr_expr(constr_asserts, i) for i in range(step, last_check_step+1)]))
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-27 15:04:15 -05:00
|
|
|
if smt.check_sat() == "sat":
|
|
|
|
print("%s BMC failed!" % smt.timestamp())
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(step)
|
2016-08-29 06:53:12 -05:00
|
|
|
for i in range(step, last_check_step+1):
|
|
|
|
print_failed_asserts(i)
|
2016-08-29 15:41:45 -05:00
|
|
|
write_trace(0, last_check_step+1, '%')
|
2016-08-27 15:04:15 -05:00
|
|
|
retstatus = False
|
|
|
|
break
|
|
|
|
|
|
|
|
smt.write("(pop 1)")
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-09-24 13:40:22 -05:00
|
|
|
if (constr_final_start is not None) or (last_check_step+1 != num_steps):
|
|
|
|
for i in range(step, last_check_step+1):
|
|
|
|
smt.write("(assert (|%s_a| s%d))" % (topmod, i))
|
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_asserts, i))
|
2016-08-24 15:09:50 -05:00
|
|
|
|
2016-08-27 07:30:36 -05:00
|
|
|
if constr_final_start is not None:
|
|
|
|
for i in range(step, last_check_step+1):
|
|
|
|
if i < constr_final_start:
|
|
|
|
continue
|
|
|
|
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Checking final constraints in step %d.." % (i))
|
2016-08-27 07:30:36 -05:00
|
|
|
smt.write("(push 1)")
|
|
|
|
|
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_assumes, i, final=True))
|
|
|
|
smt.write("(assert (not %s))" % get_constr_expr(constr_asserts, i, final=True))
|
|
|
|
|
|
|
|
if smt.check_sat() == "sat":
|
|
|
|
print("%s BMC failed!" % smt.timestamp())
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(i)
|
2016-08-29 06:53:12 -05:00
|
|
|
print_failed_asserts(i, final=True)
|
2016-08-29 15:41:45 -05:00
|
|
|
write_trace(0, i+1, '%')
|
2016-08-27 07:30:36 -05:00
|
|
|
retstatus = False
|
|
|
|
break
|
|
|
|
|
|
|
|
smt.write("(pop 1)")
|
|
|
|
if not retstatus:
|
|
|
|
break
|
|
|
|
|
2016-09-02 05:09:09 -05:00
|
|
|
else: # gentrace
|
2016-08-27 07:30:36 -05:00
|
|
|
for i in range(step, last_check_step+1):
|
2016-09-03 07:26:00 -05:00
|
|
|
smt.write("(assert (|%s_a| s%d))" % (topmod, i))
|
2016-08-27 07:30:36 -05:00
|
|
|
smt.write("(assert %s)" % get_constr_expr(constr_asserts, i))
|
2016-08-24 15:09:50 -05:00
|
|
|
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Solving for step %d.." % (last_check_step))
|
2016-08-24 15:09:50 -05:00
|
|
|
if smt.check_sat() != "sat":
|
|
|
|
print("%s No solution found!" % smt.timestamp())
|
|
|
|
retstatus = False
|
|
|
|
break
|
2015-12-20 02:58:54 -06:00
|
|
|
|
2016-08-29 15:41:45 -05:00
|
|
|
elif dumpall:
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(0)
|
2016-08-29 15:41:45 -05:00
|
|
|
write_trace(0, last_check_step+1, "%d" % step)
|
|
|
|
|
2015-12-20 02:58:54 -06:00
|
|
|
step += step_size
|
2015-10-13 10:17:23 -05:00
|
|
|
|
2016-08-24 15:09:50 -05:00
|
|
|
if gentrace:
|
2016-08-30 12:27:42 -05:00
|
|
|
print_anyconsts(0)
|
2016-08-29 15:41:45 -05:00
|
|
|
write_trace(0, num_steps, '%')
|
2016-08-24 15:09:50 -05:00
|
|
|
|
2015-10-13 10:17:23 -05:00
|
|
|
|
|
|
|
smt.write("(exit)")
|
|
|
|
smt.wait()
|
|
|
|
|
2016-09-05 18:40:31 -05:00
|
|
|
print_msg("Status: %s" % ("PASSED" if retstatus else "FAILED (!)"))
|
2015-10-15 08:54:59 -05:00
|
|
|
sys.exit(0 if retstatus else 1)
|