Refactor wsim to use smaller functions and f-strings

This commit is contained in:
Jordan Carlin 2024-12-01 14:16:24 -08:00
parent cd90e81c76
commit fbe3254857
No known key found for this signature in database

160
bin/wsim
View File

@ -14,11 +14,10 @@
import argparse import argparse
import os import os
######################## # Global variable
# main wsim script WALLY = os.environ.get('WALLY')
########################
# Parse arguments def parseArgs():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("config", help="Configuration file") parser.add_argument("config", help="Configuration file")
parser.add_argument("testsuite", help="Test suite or path to .elf file") parser.add_argument("testsuite", help="Test suite or path to .elf file")
@ -36,138 +35,143 @@ parser.add_argument("--locksteplog", "-b", help="Retired instruction number to b
parser.add_argument("--lockstepverbose", "-lv", help="Run ImperasDV lock, step, and compare with tracing enabled", action="store_true") parser.add_argument("--lockstepverbose", "-lv", help="Run ImperasDV lock, step, and compare with tracing enabled", action="store_true")
parser.add_argument("--covlog", "-d", help="Log coverage after n instructions.", default=0) parser.add_argument("--covlog", "-d", help="Log coverage after n instructions.", default=0)
parser.add_argument("--rvvi", "-r", help="Simulate rvvi hardware interface and ethernet.", action="store_true") parser.add_argument("--rvvi", "-r", help="Simulate rvvi hardware interface and ethernet.", action="store_true")
args = parser.parse_args() return parser.parse_args()
print("Config=" + args.config + " tests=" + args.testsuite + " sim=" + args.sim + " gui=" + str(args.gui) + " args='" + args.args + "'")
def elfFileCheck(args):
ElfFile = "" ElfFile = ""
WALLY = os.environ.get('WALLY') if os.path.isfile(args.elf):
ElfFile = f"+ElfFile={os.path.abspath(args.elf)}"
if(os.path.isfile(args.elf)): elif args.elf != "":
ElfFile = "+ElfFile=" + os.path.abspath(args.elf) print(f"ELF file not found: {args.elf}")
elif (args.elf != ""):
print("ELF file not found: " + args.elf)
exit(1) exit(1)
elif args.testsuite.endswith('.elf'): # No --elf argument; check if testsuite has a .elf extension and use that instead
if(args.testsuite.endswith('.elf') and args.elf == ""): # No --elf argument; check if testsuite has a .elf extension and use that instead if os.path.isfile(args.testsuite):
if (os.path.isfile(args.testsuite)): ElfFile = f"+ElfFile={os.path.abspath(args.testsuite)}"
ElfFile = "+ElfFile=" + os.path.abspath(args.testsuite)
# extract the elf name from the path to be the test suite # extract the elf name from the path to be the test suite
fields = args.testsuite.rsplit('/', 3) fields = args.testsuite.rsplit('/', 3)
# if the name is just ref.elf in a deep path (riscv-arch-test/wally-riscv-arch-test), then use the directory name as the test suite to make it unique; otherwise work directory will have duplicates. # if the name is just ref.elf in a deep path (riscv-arch-test/wally-riscv-arch-test), then use the directory name as the test suite to make it unique; otherwise work directory will have duplicates.
if (len(fields) > 3): if (len(fields) > 3):
if (fields[2] == "ref"): if (fields[2] == "ref"):
args.testsuite = fields[1] + "_" + fields[3] args.testsuite = f"{fields[1]}_{fields[3]}"
else: else:
args.testsuite = fields[2] + "_" + fields[3] args.testsuite = f"{fields[2]}_{fields[3]}"
elif ('/' in args.testsuite): elif ('/' in args.testsuite):
args.testsuite=args.testsuite.rsplit('/', 1)[1] # strip off path if present args.testsuite=args.testsuite.rsplit('/', 1)[1] # strip off path if present
else: else:
print("ELF file not found: " + args.testsuite) print(f"ELF file not found: {args.testsuite}")
exit(1) exit(1)
return ElfFile
if (ElfFile != ""): def validateArgs(args):
args.args += " " + ElfFile
if(args.lockstep and not args.testsuite.endswith('.elf') and not args.testsuite == "buildroot"): if(args.lockstep and not args.testsuite.endswith('.elf') and not args.testsuite == "buildroot"):
print(f"Invalid Options. Cannot run a testsuite, {args.testsuite} with lockstep. Must run a single elf.") print(f"Invalid Options. Cannot run a testsuite, {args.testsuite} with lockstep. Must run a single elf.")
exit(1) exit(1)
elif (args.gui or args.ccov or args.fcov or args.lockstep or args.lockstepverbose) and args.sim not in ["questa", "vcs"]:
# Validate arguments
if (args.gui or args.ccov or args.fcov or args.lockstep or args.lockstepverbose) and args.sim not in ["questa", "vcs"]:
print("Option only supported for Questa and VCS") print("Option only supported for Questa and VCS")
exit(1) exit(1)
elif (args.tb == "testbench_fp" and args.sim != "questa"): elif (args.tb == "testbench_fp" and args.sim != "questa"):
print("Error: testbench_fp presently only supported by Questa, not VCS or Verilator, because of a touchy testbench") print("Error: testbench_fp presently only supported by Questa, not VCS or Verilator, because of a touchy testbench")
exit(1) exit(1)
if (args.vcd): def prepSim(args, ElfFile):
flags = ""
if args.vcd:
args.args += " -DMAKEVCD=1" args.args += " -DMAKEVCD=1"
if args.rvvi:
if (args.rvvi):
args.params += " RVVI_SYNTH_SUPPORTED=1 " args.params += " RVVI_SYNTH_SUPPORTED=1 "
if args.tb == "testbench_fp":
args.params += f" TEST=\" {args.testsuite} \" "
if ElfFile != "":
args.args += f" {ElfFile}"
if args.ccov:
flags += " --ccov"
if args.fcov:
flags += " --fcov"
prefix, suffix = lockstepSetup(args)
flags += suffix
return flags, prefix
if (args.tb == "testbench_fp"): def lockstepSetup(args):
args.params += " TEST=\"" + args.testsuite + "\" " prefix = ""
suffix = ""
ImperasPlusArgs = ""
# if lockstep is enabled, then we need to pass the Imperas lockstep arguments
if(int(args.locksteplog) >= 1): EnableLog = 1 if(int(args.locksteplog) >= 1): EnableLog = 1
else: EnableLog = 0 else: EnableLog = 0
prefix = ""
if (args.lockstep or args.lockstepverbose or args.fcov): if (args.lockstep or args.lockstepverbose or args.fcov):
imperasicPath = os.path.join(WALLY, "config", args.config, "imperas.ic") imperasicPath = os.path.join(WALLY, "config", args.config, "imperas.ic")
if not os.path.isfile(imperasicPath): # If config is a derivative, look for imperas.ic in derivative configs if not os.path.isfile(imperasicPath): # If config is a derivative, look for imperas.ic in derivative configs
imperasicPath = os.path.join(WALLY, "config", "deriv", args.config, "imperas.ic") imperasicPath = os.path.join(WALLY, "config", "deriv", args.config, "imperas.ic")
prefix = "IMPERAS_TOOLS=" + imperasicPath if not os.path.isfile(imperasicPath):
print("Error: imperas.ic not found")
exit(1)
prefix += f"IMPERAS_TOOLS= {imperasicPath}"
if (args.lockstep or args.lockstepverbose): if (args.lockstep or args.lockstepverbose):
if(args.locksteplog != 0): ImperasPlusArgs = " +IDV_TRACE2LOG=" + str(EnableLog) + " +IDV_TRACE2LOG_AFTER=" + str(args.locksteplog) if(args.locksteplog != 0): ImperasPlusArgs = f" +IDV_TRACE2LOG={EnableLog} +IDV_TRACE2LOG_AFTER={args.locksteplog}"
else: ImperasPlusArgs = ""
if(args.fcov): if(args.fcov):
CovEnableStr = "1" if int(args.covlog) > 0 else "0" CovEnableStr = "1" if int(args.covlog) > 0 else "0"
if(args.covlog >= 1): EnableLog = 1 if(args.covlog >= 1): EnableLog = 1
else: EnableLog = 0 else: EnableLog = 0
ImperasPlusArgs = " +IDV_TRACE2COV=" + str(EnableLog) + " +TRACE2LOG_AFTER=" + str(args.covlog) + " +TRACE2COV_ENABLE=" + CovEnableStr ImperasPlusArgs = f" +IDV_TRACE2COV={EnableLog} +TRACE2LOG_AFTER={args.covlog} +TRACE2COV_ENABLE={CovEnableStr}"
suffix = ""
else: else:
CovEnableStr = ""
suffix = "--lockstep" suffix = "--lockstep"
if(args.lockstepverbose): if(args.lockstepverbose):
prefix += ":" + WALLY + "/sim/imperas-verbose.ic" prefix += f":{WALLY}/sim/imperas-verbose.ic"
else:
ImperasPlusArgs = ""
suffix = ""
flags = suffix
args.args += ImperasPlusArgs args.args += ImperasPlusArgs
return prefix, suffix
def createDirs(args):
# other flags
if (args.ccov):
flags += " --ccov"
if (args.fcov):
flags += " --fcov"
# create the output sub-directories.
regressionDir = WALLY + '/sim/'
for d in ["logs", "wkdir", "cov", "ucdb", "fcov", "fcov_ucdb"]: for d in ["logs", "wkdir", "cov", "ucdb", "fcov", "fcov_ucdb"]:
try: os.makedirs(os.path.join(WALLY, "sim", args.sim, d), exist_ok=True)
os.mkdir(regressionDir+args.sim+"/"+d)
except:
pass
cd = "cd $WALLY/sim/" +args.sim def runSim(args, flags, prefix):
# per-simulator launch
if (args.sim == "questa"): if (args.sim == "questa"):
runQuesta(args, flags, prefix)
elif (args.sim == "verilator"):
runVerilator(args, flags, prefix)
elif (args.sim == "vcs"):
runVCS(args, flags, prefix)
def runQuesta(args, flags, prefix):
# Force Questa to use 64-bit mode, sometimes it defaults to 32-bit even on 64-bit machines # Force Questa to use 64-bit mode, sometimes it defaults to 32-bit even on 64-bit machines
prefix = "MTI_VCO_MODE=64 " + prefix prefix = "MTI_VCO_MODE=64 " + prefix
if (args.gui) and (args.tb == "testbench"): if (args.gui) and (args.tb == "testbench"):
args.params += "DEBUG=1" args.params += "DEBUG=1"
if (args.args != ""): if (args.args != ""):
args.args = " --args \\\"" + args.args + "\\\"" args.args = f" --args \\\"{args.args}\\\""
if (args.params != ""): if (args.params != ""):
args.params = " --params \\\"" + args.params + "\\\"" args.params = f" --params \\\"{args.params}\\\""
# Questa cannot accept more than 9 arguments. fcov implies lockstep # Questa cannot accept more than 9 arguments. fcov implies lockstep
cmd = "do wally.do " + args.config + " " + args.testsuite + " " + args.tb + " " + args.args + " " + args.params + " " + flags cmd = f"do wally.do {args.config} {args.testsuite} {args.tb} {args.args} {args.params} {flags}"
if (args.gui): # launch Questa with GUI; add +acc to keep variables accessible if (args.gui): # launch Questa with GUI; add +acc to keep variables accessible
cmd = cd + "; " + prefix + " vsim -do \"" + cmd + " +acc\"" cmd = f"cd $WALLY/sim/questa; {prefix} vsim -do \" {cmd} +acc\""
else: # launch Questa in batch mode else: # launch Questa in batch mode
cmd = cd + "; " + prefix + " vsim -c -do \"" + cmd + "\"" cmd = f"cd $WALLY/sim/questa; {prefix} vsim -c -do \" {cmd} \""
print("Running Questa with command: " + cmd) print(f"Running Questa with command: {cmd}")
os.system(cmd) os.system(cmd)
elif (args.sim == "verilator"):
def runVerilator(args, flags, prefix):
print(f"Running Verilator on {args.config} {args.testsuite}") print(f"Running Verilator on {args.config} {args.testsuite}")
os.system(f"/usr/bin/make -C {regressionDir}/verilator WALLYCONF={args.config} TEST={args.testsuite} TESTBENCH={args.tb} PLUS_ARGS=\"{args.args}\" PARAM_ARGS=\"{args.params}\"") os.system(f"/usr/bin/make -C {WALLY}/sim/verilator WALLYCONF={args.config} TEST={args.testsuite} TESTBENCH={args.tb} PLUS_ARGS=\"{args.args}\" PARAM_ARGS=\"{args.params}\"")
elif (args.sim == "vcs"):
print(f"Running VCS on " + args.config + " " + args.testsuite) def runVCS(args, flags, prefix):
print(f"Running VCS on {args.config} {args.testsuite}")
# if (args.gui): # if (args.gui):
# flags += " --gui" # flags += " --gui"
if (args.args == ""): if (args.args != ""):
vcsargs = "" args.args = f" --args \"{args.args}\" "
else: if (args.params != ""):
vcsargs = " --args \"" + args.args + "\" " args.params = f" --params \"{args.params}\" "
if (args.params == ""): cmd = f"cd $WALLY/sim/vcs; {prefix} ./run_vcs {args.config} {args.testsuite} --tb {args.tb} {args.args} {args.params} {flags}"
vcsparams = ""
else:
vcsparams = " --params \"" + args.params + "\" "
cmd = cd + "; " + prefix + " ./run_vcs " + args.config + " " + args.testsuite + " " + " --tb " + args.tb + " " + vcsargs + vcsparams + " " + flags
print(cmd) print(cmd)
os.system(cmd) os.system(cmd)
if __name__ == "__main__":
args = parseArgs()
print(f"Config={args.config} tests={args.testsuite} sim={args.sim} gui={args.gui} args='{args.args} params='{args.params}'")
ElfFile = elfFileCheck(args)
validateArgs(args)
flags, prefix = prepSim(args, ElfFile)
createDirs(args)
exit(runSim(args, flags, prefix))