picorv32/scripts/smt2-bmc/async.py

334 lines
15 KiB
Python
Raw Normal View History

2015-08-22 15:54:21 +08:00
#!/usr/bin/env python3
2015-08-09 20:23:02 +08:00
2015-08-10 18:11:33 +08:00
import os, sys, getopt
2015-08-28 06:59:12 +08:00
from smtio import smtio, smtopts
2015-08-09 20:23:02 +08:00
steps = 12
words = 0
allmem = False
fastmem = False
initzero = False
2015-08-15 06:20:06 +08:00
check_mem = True
check_regs = True
2015-08-28 06:59:12 +08:00
so = smtopts()
2015-08-16 00:07:01 +08:00
def usage():
print("""
python3 async.py [options]
-t <steps>
default: 12
2015-08-28 06:59:12 +08:00
""" + so.helpmsg())
2015-08-16 00:07:01 +08:00
sys.exit(1)
try:
2015-09-19 02:19:14 +08:00
opts, args = getopt.getopt(sys.argv[1:], so.optstr + "t:")
2015-08-16 00:07:01 +08:00
except:
usage()
for o, a in opts:
2015-08-28 06:59:12 +08:00
if o == "-t":
2015-08-16 00:07:01 +08:00
steps = int(a)
2015-08-28 06:59:12 +08:00
elif so.handle(o, a):
pass
2015-08-16 00:07:01 +08:00
else:
usage()
if len(args) > 0:
usage()
2015-08-10 18:11:33 +08:00
2015-08-28 06:59:12 +08:00
smt = smtio(opts=so)
2015-08-15 05:57:09 +08:00
2015-08-28 06:59:12 +08:00
print("Solver: %s" % so.solver)
smt.setup("QF_AUFBV", "PicoRV32 \"async.py\" BMC script, powered by Yosys")
2015-08-09 20:23:02 +08:00
regs_a = list()
regs_b = list()
with open("async_a.smt2", "r") as f:
for line in f:
if line.startswith("; yosys-smt2-register "):
line = line.split()
regs_a.append((line[2], int(line[3])))
else:
2015-08-15 06:20:06 +08:00
smt.write(line)
2015-08-09 20:23:02 +08:00
with open("async_b.smt2", "r") as f:
for line in f:
if line.startswith("; yosys-smt2-register "):
line = line.split()
regs_b.append((line[2], int(line[3])))
else:
2015-08-15 06:20:06 +08:00
smt.write(line)
2015-08-09 20:23:02 +08:00
2015-08-13 17:52:53 +08:00
for step in range(steps):
2015-08-15 06:20:06 +08:00
smt.write("(declare-fun a%d () main_a_s)" % step)
smt.write("(declare-fun b%d () main_b_s)" % step)
2015-08-13 17:52:53 +08:00
if fastmem:
2015-08-15 06:20:06 +08:00
smt.write("(assert (|main_a_n domem| a%d))" % step)
smt.write("(assert (|main_b_n domem| b%d))" % step)
if words > 0:
if allmem:
2015-08-15 06:20:06 +08:00
smt.write("(assert (bvult (|main_a_n mem_addr| a%d) #x%08x))" % (step, words))
smt.write("(assert (bvult (|main_b_n mem_addr| b%d) #x%08x))" % (step, words))
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (or (not (|main_a_n mem_valid| a%d)) (bvult (|main_a_n mem_addr| a%d) #x%08x)))" % (step, step, words))
smt.write("(assert (or (not (|main_b_n mem_valid| b%d)) (bvult (|main_b_n mem_addr| b%d) #x%08x)))" % (step, step, words))
2015-08-13 17:52:53 +08:00
if step == 0:
# start with synced memory and register file
2015-08-15 06:20:06 +08:00
smt.write("(assert (= (|main_a_m cpu.cpuregs| a0) (|main_b_m cpu.cpuregs| b0)))")
smt.write("(assert (= (|main_a_m memory| a0) (|main_b_m memory| b0)))")
# reset in first cycle
smt.write("(assert (not (|main_a_n resetn| a%d)))" % step)
smt.write("(assert (not (|main_b_n resetn| b%d)))" % step)
2015-08-13 17:52:53 +08:00
2015-08-09 20:23:02 +08:00
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (main_a_t a%d a%d))" % (step-1, step))
smt.write("(assert (main_b_t b%d b%d))" % (step-1, step))
smt.write("(assert (|main_a_n resetn| a%d))" % step)
smt.write("(assert (|main_b_n resetn| b%d))" % step)
2015-08-13 17:52:53 +08:00
2015-08-28 06:59:12 +08:00
print("%s Checking sequence of length %d.." % (smt.timestamp(), step))
2015-08-15 06:20:06 +08:00
smt.write("(push 1)")
2015-08-13 17:52:53 +08:00
# stop with a trap and no pending memory xfer
2015-08-15 06:20:06 +08:00
smt.write("(assert (not (|main_a_n mem_valid| a%d)))" % step)
smt.write("(assert (not (|main_b_n mem_valid| b%d)))" % step)
smt.write("(assert (|main_a_n trap| a%d))" % step)
smt.write("(assert (|main_b_n trap| b%d))" % step)
# look for differences in memory and/or register file
if check_mem and check_regs:
smt.write(("(assert (or (distinct (|main_a_m cpu.cpuregs| a%d) (|main_b_m cpu.cpuregs| b%d)) " +
"(distinct (|main_a_m memory| a%d) (|main_b_m memory| b%d))))") % (step, step, step, step))
elif check_mem:
smt.write(("(assert (distinct (|main_a_m memory| a%d) (|main_b_m memory| b%d)))") % (step, step))
elif check_regs:
smt.write(("(assert (distinct (|main_a_m cpu.cpuregs| a%d) (|main_b_m cpu.cpuregs| b%d)))") % (step, step))
else:
assert False
2015-08-13 17:52:53 +08:00
if smt.check_sat() == "sat":
2015-08-13 17:52:53 +08:00
2015-08-28 06:59:12 +08:00
print("%s Creating model.." % smt.timestamp())
2015-08-13 17:52:53 +08:00
def make_cpu_regs(step):
for i in range(1, 32):
2015-08-15 06:20:06 +08:00
smt.write("(define-fun a%d_r%d () (_ BitVec 32) (select (|main_a_m cpu.cpuregs| a%d) #b%s))" % (step, i, step, bin(32+i)[3:]))
smt.write("(define-fun b%d_r%d () (_ BitVec 32) (select (|main_b_m cpu.cpuregs| b%d) #b%s))" % (step, i, step, bin(32+i)[3:]))
2015-08-13 17:52:53 +08:00
make_cpu_regs(0)
make_cpu_regs(step)
def print_status(mod, step):
2015-08-15 06:20:06 +08:00
resetn = smt.get_net_bool("main_" + mod, "resetn", "%s%d" % (mod, step))
memvld = smt.get_net_bool("main_" + mod, "mem_valid", "%s%d" % (mod, step))
domem = smt.get_net_bool("main_" + mod, "domem", "%s%d" % (mod, step))
memrdy = smt.get_net_bool("main_" + mod, "mem_ready", "%s%d" % (mod, step))
trap = smt.get_net_bool("main_" + mod, "trap", "%s%d" % (mod, step))
2015-08-13 17:52:53 +08:00
print("status %5s: resetn=%s, memvld=%s, domem=%s, memrdy=%s, trap=%s" % ("%s[%d]" % (mod, step), resetn, memvld, domem, memrdy, trap))
def print_mem_xfer(mod, step):
2015-08-15 06:20:06 +08:00
if allmem or smt.get("(and (|main_%s_n mem_valid| %s%d) (|main_%s_n mem_ready| %s%d))" % (mod, mod, step, mod, mod, step)) == 'true':
mem_addr = smt.get_net_hex("main_" + mod, "mem_addr", "%s%d" % (mod, step))
mem_wdata = smt.get_net_hex("main_" + mod, "mem_wdata", "%s%d" % (mod, step))
mem_wstrb = smt.get_net_bin("main_" + mod, "mem_wstrb", "%s%d" % (mod, step))
mem_rdata = smt.get_net_hex("main_" + mod, "mem_rdata", "%s%d" % (mod, step))
if allmem and smt.get("(and (|main_%s_n mem_valid| %s%d) (|main_%s_n mem_ready| %s%d))" % (mod, mod, step, mod, mod, step)) == 'true':
print("mem %5s: addr=%s, wdata=%s, wstrb=%s, rdata=%s <-" % ("%s[%d]" % (mod, step), mem_addr, mem_wdata, mem_wstrb, mem_rdata))
else:
print("mem %5s: addr=%s, wdata=%s, wstrb=%s, rdata=%s" % ("%s[%d]" % (mod, step), mem_addr, mem_wdata, mem_wstrb, mem_rdata))
2015-08-13 17:52:53 +08:00
def print_cpu_regs(step):
for i in range(1, 32):
2015-08-15 06:20:06 +08:00
ra = smt.bv2hex(smt.get("a%d_r%d" % (step, i)))
rb = smt.bv2hex(smt.get("b%d_r%d" % (step, i)))
2015-08-13 17:52:53 +08:00
print("%3s[%d]: A=%s B=%s%s" % ("x%d" % i, step, ra, rb, " !" if ra != rb else ""))
assert smt.check_sat() == "sat"
2015-08-13 17:52:53 +08:00
if initzero:
for rn, rs in regs_a:
force_to_zero = True
2015-08-15 06:20:06 +08:00
if smt.get_net_bin("main_a", rn, "a0").count("1") != 0:
print("Looking for a solution with |main_a_n %s| initialized to all zeros.." % rn)
2015-08-15 06:20:06 +08:00
smt.write("(push 1)")
if rs == 1:
2015-08-15 06:20:06 +08:00
smt.write("(assert (not (|main_a_n %s| a0)))" % rn)
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (= (|main_a_n %s| a0) #b%s))" % (rn, "0" * rs))
if smt.check_sat() != "sat":
force_to_zero = False
2015-08-15 06:20:06 +08:00
smt.write("(pop 1)")
if force_to_zero:
if rs == 1:
2015-08-15 06:20:06 +08:00
smt.write("(assert (not (|main_a_n %s| a0)))" % rn)
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (= (|main_a_n %s| a0) #b%s))" % (rn, "0" * rs))
assert smt.check_sat() == "sat"
for rn, rs in regs_b:
force_to_zero = True
2015-08-15 06:20:06 +08:00
if smt.get_net_bin("main_b", rn, "b0").count("1") != 0:
print("Looking for a solution with |main_b_n %s| initialized to all zeros.." % rn)
2015-08-15 06:20:06 +08:00
smt.write("(push 1)")
if rs == 1:
2015-08-15 06:20:06 +08:00
smt.write("(assert (not (|main_b_n %s| b0)))" % rn)
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (= (|main_b_n %s| b0) #b%s))" % (rn, "0" * rs))
if smt.check_sat() != "sat":
force_to_zero = False
2015-08-15 06:20:06 +08:00
smt.write("(pop 1)")
if force_to_zero:
if rs == 1:
2015-08-15 06:20:06 +08:00
smt.write("(assert (not (|main_b_n %s| b0)))" % rn)
else:
2015-08-15 06:20:06 +08:00
smt.write("(assert (= (|main_b_n %s| b0) #b%s))" % (rn, "0" * rs))
assert smt.check_sat() == "sat"
2015-08-13 17:52:53 +08:00
print()
print_cpu_regs(0)
print()
print_cpu_regs(step)
print()
for i in range(step+1):
print_status("a", i)
print()
for i in range(step+1):
print_status("b", i)
print()
for i in range(1, step+1):
print_mem_xfer("a", i)
print()
for i in range(1, step+1):
print_mem_xfer("b", i)
with open("async_tb.v", "w") as f:
2015-08-13 17:52:53 +08:00
print()
print("writing verilog test bench...")
memory_words = 1
memory_datas = { "a": dict(), "b": dict() }
for i in range(step, 0, -1):
for mod in ["a", "b"]:
2015-08-15 06:20:06 +08:00
if allmem or smt.get("(and (|main_%s_n mem_valid| %s%d) (|main_%s_n mem_ready| %s%d))" % (mod, mod, i, mod, mod, i)) == 'true':
mem_addr = smt.get_net_hex("main_" + mod, "mem_addr", "%s%d" % (mod, i))
mem_rdata = smt.get_net_hex("main_" + mod, "mem_rdata", "%s%d" % (mod, i))
2015-08-13 17:52:53 +08:00
memory_datas[mod][mem_addr] = mem_rdata
memory_words = max((int(mem_addr, 16) >> 2)+1, memory_words)
2015-08-13 17:52:53 +08:00
memory_data = dict()
for k, v in memory_datas["a"].items(): memory_data[k] = v
for k, v in memory_datas["b"].items(): memory_data[k] = v
print("`timescale 1 ns / 1 ps", file=f)
print("", file=f)
print("module testbench;", file=f)
print(" reg clk = 1, resetn, domem_a, domem_b;", file=f)
print(" always #5 clk = ~clk;", file=f)
print("", file=f)
print(" main #(", file=f)
print(" .MEMORY_WORDS(%d)," % memory_words, file=f)
print(" .ENABLE_REGS_DUALPORT(0),", file=f)
print(" .TWO_STAGE_SHIFT(0),", file=f)
print(" .TWO_CYCLE_COMPARE(0),", file=f)
print(" .TWO_CYCLE_ALU(0)", file=f)
print(" ) main_a (", file=f)
print(" .clk(clk),", file=f)
print(" .resetn(resetn),", file=f)
print(" .domem(domem_a)", file=f)
print(" );", file=f)
print("", file=f)
print(" main #(", file=f)
print(" .MEMORY_WORDS(%d)," % memory_words, file=f)
print(" .ENABLE_REGS_DUALPORT(1),", file=f)
print(" .TWO_STAGE_SHIFT(1),", file=f)
print(" .TWO_CYCLE_COMPARE(1),", file=f)
print(" .TWO_CYCLE_ALU(1)", file=f)
print(" ) main_b (", file=f)
print(" .clk(clk),", file=f)
print(" .resetn(resetn),", file=f)
print(" .domem(domem_b)", file=f)
print(" );", file=f)
print("", file=f)
print(" task check_reg;", file=f)
print(" input [4:0] n;", file=f)
print(" begin", file=f)
print(" if (main_a.cpu.cpuregs[n] != main_b.cpu.cpuregs[n])", file=f)
print(" $display(\"Divergent values for reg %1d: A=%08x B=%08x\", n, main_a.cpu.cpuregs[n], main_b.cpu.cpuregs[n]);", file=f)
print(" end", file=f)
print(" endtask", file=f)
print("", file=f)
print(" task check_mem;", file=f)
print(" input [31:0] n;", file=f)
print(" begin", file=f)
print(" if (main_a.memory[n] != main_b.memory[n])", file=f)
print(" $display(\"Divergent values for memory addr %08x: A=%08x B=%08x\", n, main_a.cpu.cpuregs[n], main_b.cpu.cpuregs[n]);", file=f)
print(" end", file=f)
print(" endtask", file=f)
print("", file=f)
print(" initial begin", file=f)
print(" $dumpfile(\"async_tb.vcd\");", file=f)
2015-08-13 17:52:53 +08:00
print(" $dumpvars(0, testbench);", file=f)
print("", file=f)
for rn, rs in regs_a:
2015-08-15 06:20:06 +08:00
print(" main_a.%s = %d'b %s;" % (rn, rs, smt.get_net_bin("main_a", rn, "a0")), file=f)
print("", file=f)
for rn, rs in regs_b:
2015-08-15 06:20:06 +08:00
print(" main_b.%s = %d'b %s;" % (rn, rs, smt.get_net_bin("main_b", rn, "b0")), file=f)
print("", file=f)
2015-08-13 17:52:53 +08:00
for i in range(1, 32):
2015-08-15 06:20:06 +08:00
ra = smt.bv2hex(smt.get("a%d_r%d" % (0, i)))
rb = smt.bv2hex(smt.get("b%d_r%d" % (0, i)))
2015-08-13 17:52:53 +08:00
print(" main_a.cpu.cpuregs[%2d] = 'h %s; main_b.cpu.cpuregs[%2d] = 'h %s;" % (i, ra, i, rb), file=f)
print("", file=f)
for addr, data in memory_data.items():
print(" main_a.memory['h %08x] = 'h %s;" % (int(addr, 16) >> 2, data), file=f)
print(" main_b.memory['h %08x] = 'h %s;" % (int(addr, 16) >> 2, data), file=f)
2015-08-13 17:52:53 +08:00
print("", file=f)
for i in range(step+1):
2015-08-15 06:20:06 +08:00
print(" resetn = %d;" % smt.get_net_bool("main_a", "resetn", "a%d" % i), file=f)
print(" domem_a = %d;" % smt.get_net_bool("main_a", "domem", "a%d" % i), file=f)
print(" domem_b = %d;" % smt.get_net_bool("main_b", "domem", "b%d" % i), file=f)
2015-08-13 17:52:53 +08:00
print(" @(posedge clk);", file=f)
print("", file=f)
for i in range(1, 32):
print(" check_reg(%d);" % i, file=f)
for addr, data in memory_data.items():
print(" check_mem('h %s);" % addr, file=f)
print("", file=f)
print(" @(posedge clk);", file=f)
print(" @(posedge clk);", file=f)
2015-08-13 17:52:53 +08:00
print(" $finish;", file=f)
print(" end", file=f)
print("endmodule", file=f)
if words > 0:
print("running verilog test bench...")
2015-08-15 17:28:35 +08:00
os.system("iverilog -o async_tb -s testbench async_tb.v main.v ../../picorv32.v && ./async_tb")
break
2015-08-13 17:52:53 +08:00
else: # unsat
2015-08-15 06:20:06 +08:00
smt.write("(pop 1)")
2015-08-09 20:23:02 +08:00
2015-08-28 06:59:12 +08:00
print("%s Done." % smt.timestamp())
2015-08-15 06:20:06 +08:00
smt.write("(exit)")
smt.wait()
2015-08-10 18:11:33 +08:00