| 1 |
import os |
| 2 |
import sys |
| 3 |
import pathlib |
| 4 |
import subprocess |
| 5 |
|
| 6 |
import helpers as hp |
| 7 |
import constants as cn |
| 8 |
|
| 9 |
|
| 10 |
def test_sim_bin(sim_bin): |
| 11 |
try: |
| 12 |
sim_test = subprocess.run( |
| 13 |
[sim_bin, "-h"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 14 |
except: |
| 15 |
parser.print_help() |
| 16 |
sys.exit(1) |
| 17 |
if (sim_test.returncode == 0): |
| 18 |
return sim_bin, True |
| 19 |
return "", False |
| 20 |
|
| 21 |
|
| 22 |
def load_filenames(dir_path, rebuild): |
| 23 |
test_files = list(filter( |
| 24 |
lambda x: not str(x).startswith("."), os.listdir(dir_path+"/elf"))) |
| 25 |
if (len(test_files) != 0 and not rebuild): |
| 26 |
return test_files, True |
| 27 |
else: |
| 28 |
try: |
| 29 |
if (rebuild): |
| 30 |
print("Building {0}".format(dir_path)) |
| 31 |
else: |
| 32 |
print("No tests found. Trying to build tests.") |
| 33 |
tests_built = subprocess.run( |
| 34 |
["make", "-C", dir_path], |
| 35 |
stdout=subprocess.DEVNULL, |
| 36 |
stderr=subprocess.DEVNULL) |
| 37 |
return load_filenames(dir_path, False) |
| 38 |
except: |
| 39 |
print("Failed to build tests. {0} ; {1}".format( |
| 40 |
dir_path, test_files)) |
| 41 |
sys.exit(1) |
| 42 |
|
| 43 |
|
| 44 |
# If output of the qtrvsim-cli is changed this will probably break !!! |
| 45 |
def check_reg_dump(reg_dump): |
| 46 |
res = 2 |
| 47 |
stdout = str(reg_dump.stdout) |
| 48 |
if (stdout.__contains__("R11:")): |
| 49 |
index = stdout.find("R11:") |
| 50 |
if (stdout[index:index+18].__contains__("600d")): |
| 51 |
res = 1 |
| 52 |
elif (stdout[index:index+18].__contains__("bad")): |
| 53 |
res = 0 |
| 54 |
return res, reg_dump |
| 55 |
|
| 56 |
|
| 57 |
def run_test(sim_bin, params, dir_path, filename): |
| 58 |
test_path = dir_path + filename |
| 59 |
try: |
| 60 |
param_bin = [sim_bin, test_path, "--d-regs"] |
| 61 |
if (params.pipeline): |
| 62 |
param_bin.append("--pipelined") |
| 63 |
if (params.cache): |
| 64 |
param_bin.extend(cn.CACHE_SETTINGS) |
| 65 |
return subprocess.run(param_bin, capture_output=True) |
| 66 |
except subprocess.CalledProcessError as err: |
| 67 |
print(err) |
| 68 |
sys.exit(1) |
| 69 |
|
| 70 |
|
| 71 |
def run_tests(sim_bin, params, dir_path, tests): |
| 72 |
succ = 0 |
| 73 |
max_width = 0 |
| 74 |
if (not params.fileprt): |
| 75 |
max_width = hp.max_str_list(tests) |
| 76 |
for test in tests: |
| 77 |
reg_dump = run_test(sim_bin, params, dir_path, test) |
| 78 |
test = test.ljust(max_width) |
| 79 |
test_res, test_reg_dump = check_reg_dump(reg_dump) |
| 80 |
succ += hp.res_print(test, test_res, test_reg_dump, params) |
| 81 |
if (not params.fileprt): |
| 82 |
print(str(succ) + "/" + str(len(tests)) + " tests succesfull.\n") |
| 83 |
|
| 84 |
|
| 85 |
def run_official_tests(sim_bin, params, src_path, tests): |
| 86 |
if (not params.no32): |
| 87 |
if (not params.fileprt): |
| 88 |
print("--- 32 bit register tests ---") |
| 89 |
run_tests(sim_bin, params, src_path + cn.ELF_PATH, tests[0]) |
| 90 |
if (not params.no64): |
| 91 |
if (not params.fileprt): |
| 92 |
print("--- 64 bit register tests ---") |
| 93 |
run_tests(sim_bin, params, src_path + cn.ELF_PATH, tests[1]) |
| 94 |
|
| 95 |
|
| 96 |
def test_selector(sim_bin, params, src_path, tests): |
| 97 |
if (params.pipeline): |
| 98 |
print("Simulator runs in pipelined mode.") |
| 99 |
if (params.cache): |
| 100 |
print("Simulator runs in cache mode.") |
| 101 |
if (params.fileprt): |
| 102 |
line = "" |
| 103 |
else: |
| 104 |
line = "-+-+-+-+-+-+-+-+-" |
| 105 |
print(line+"RVxxUI"+line) |
| 106 |
run_official_tests(sim_bin, params, src_path, hp.get_RVxx(tests, "ui")) |
| 107 |
|
| 108 |
if (params.multiply): |
| 109 |
print(line+"RVxxUM"+line) |
| 110 |
run_official_tests(sim_bin, params, src_path, hp.get_RVxx(tests, "um")) |
| 111 |
|
| 112 |
if (params.atomic): |
| 113 |
print(line+"RVxxUA"+line) |
| 114 |
run_official_tests(sim_bin, params, src_path, hp.get_RVxx(tests, "ua")) |
| 115 |
|
| 116 |
if (params.CSR): |
| 117 |
print(line+"RVxxSI"+line) |
| 118 |
run_official_tests(sim_bin, params, src_path, hp.get_RVxx(tests, "si")) |
| 119 |
print(line+"RVxxMI"+line) |
| 120 |
run_official_tests(sim_bin, params, src_path, hp.get_RVxx(tests, "mi")) |
| 121 |
|
| 122 |
if (len(params.external) > 0): |
| 123 |
print(line+"External Tests"+line) |
| 124 |
dir_path = params.external[0] |
| 125 |
test_files = os.listdir(dir_path) |
| 126 |
test_files = list( |
| 127 |
filter(lambda x: not str(x).startswith("."), test_files)) |
| 128 |
run_tests(sim_bin, params, dir_path, test_files) |
| 129 |
|
| 130 |
|
| 131 |
def delete_elf(src_path): |
| 132 |
try: |
| 133 |
subprocess.run(["make", "-C", src_path + cn.ISA_PATH, "clean"], |
| 134 |
stdout=subprocess.DEVNULL, |
| 135 |
stderr=subprocess.DEVNULL) |
| 136 |
subprocess.run(["make", "-C", src_path + cn.SELF_PATH, "clean"], |
| 137 |
stdout=subprocess.DEVNULL, |
| 138 |
stderr=subprocess.DEVNULL) |
| 139 |
except subprocess.CalledProcessError as err: |
| 140 |
print("Unable to delete elf files.") |
| 141 |
print(err) |