Skip to content
This repository was archived by the owner on Apr 29, 2026. It is now read-only.

Commit aa146d4

Browse files
authored
Sync Riscof plugin (#142)
* add * synchronized plugins
1 parent cd4aceb commit aa146d4

4 files changed

Lines changed: 133 additions & 81 deletions

File tree

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#ifndef _COMPLIANCE_MODEL_H
22
#define _COMPLIANCE_MODEL_H
3+
#if XLEN == 64
4+
#define ALIGNMENT 3
5+
#else
6+
#define ALIGNMENT 2
7+
#endif
8+
39
#define RVMODEL_DATA_SECTION \
410
.pushsection .tohost,"aw",@progbits; \
511
.align 8; .global tohost; tohost: .dword 0; \
@@ -11,23 +17,23 @@
1117
.word 4;
1218

1319
//RV_COMPLIANCE_HALT
14-
#define RVMODEL_HALT \
15-
li x1, 1; \
16-
write_tohost: \
17-
sw x1, tohost, t5; \
18-
j write_tohost;
20+
#define RVMODEL_HALT ;\
21+
li x1, 1 ;\
22+
1: ;\
23+
sw x1, tohost, t2 ;\
24+
j 1b ;\
1925

2026
#define RVMODEL_BOOT
2127

2228
//RV_COMPLIANCE_DATA_BEGIN
2329
#define RVMODEL_DATA_BEGIN \
2430
RVMODEL_DATA_SECTION \
25-
.align 4;\
31+
.align ALIGNMENT;\
2632
.global begin_signature; begin_signature:
2733

2834
//RV_COMPLIANCE_DATA_END
2935
#define RVMODEL_DATA_END \
30-
.align 4;\
36+
.align ALIGNMENT;\
3137
.global end_signature; end_signature:
3238

3339
//RVTEST_IO_INIT
@@ -43,18 +49,13 @@
4349
//RVTEST_IO_ASSERT_DFPR_EQ
4450
#define RVMODEL_IO_ASSERT_DFPR_EQ(_D, _R, _I)
4551

46-
#define RVMODEL_SET_MSW_INT \
47-
li t1, 1; \
48-
li t2, 0x2000000; \
49-
sw t1, 0(t2);
52+
#define RVMODEL_SET_MSW_INT
5053

51-
#define RVMODEL_CLEAR_MSW_INT \
52-
li t2, 0x2000000; \
53-
sw x0, 0(t2);
54+
#define RVMODEL_CLEAR_MSW_INT
5455

5556
#define RVMODEL_CLEAR_MTIMER_INT
5657

5758
#define RVMODEL_CLEAR_MEXT_INT
5859

5960

60-
#endif // _COMPLIANCE_MODEL_H
61+
#endif // _COMPLIANCE_MODEL_H

riscof/Templates/setup/model/riscof_model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def build(self, isa_yaml, platform_yaml):
9696
self.isa += 'i'
9797
if "M" in ispec["ISA"]:
9898
self.isa += 'm'
99+
if "A" in ispec["ISA"]:
100+
self.isa += 'a'
99101
if "F" in ispec["ISA"]:
100102
self.isa += 'f'
101103
if "D" in ispec["ISA"]:
@@ -140,6 +142,7 @@ def runTests(self, testList):
140142
# be named as DUT-<dut-name>.signature. The below variable creates an absolute path of
141143
# signature file.
142144
sig_file = os.path.join(test_dir, self.name[:-1] + ".signature")
145+
log_file = os.path.join(test_dir, self.name[:-1] + ".log")
143146

144147
# for each test there are specific compile macros that need to be enabled. The macros in
145148
# the testList node only contain the macros/values. For the gcc toolchain we need to
@@ -154,8 +157,9 @@ def runTests(self, testList):
154157
# the "else" clause is executed below assigning the sim command to simple no action
155158
# echo statement.
156159
if self.target_run:
157-
# set up the simulation command. Template is for spike. Please change.
158-
simcmd = self.dut_exe + ' --isa={0} +signature={1} +signature-granularity=4 {2}'.format(self.isa, sig_file, elf)
160+
# set up the simulation command. Template is for dutname. Please change.
161+
simcmd = self.dut_exe + ' --misaligned --isa={0} +signature={1} +signature-granularity=4 {2}'.format(self.isa, sig_file, elf)
162+
simcmd = simcmd + ';' + self.dut_exe + ' --isa={0} --log-commits -l my.elf 2> {1}'.format(self.isa, log_file)
159163
else:
160164
simcmd = 'echo "NO RUN"'
161165

@@ -231,11 +235,11 @@ def runTests(self, testList):
231235
# # comment out the lines below and raise a SystemExit
232236
#
233237
# if self.target_run:
234-
# # build the command for running the elf on the DUT. In this case we use spike and indicate
238+
# # build the command for running the elf on the DUT. In this case we use dutname and indicate
235239
# # the isa arg that we parsed in the build stage, elf filename and signature filename.
236-
# # Template is for spike. Please change for your DUT
240+
# # Template is for dutname. Please change for your DUT
237241
# execute = self.dut_exe + ' --isa={0} +signature={1} +signature-granularity=4 {2}'.format(self.isa, sig_file, elf)
238-
# logger.debug('Executing on Spike ' + execute)
242+
# logger.debug('Executing on dutname ' + execute)
239243
#
240244
# # launch the execute command. Change the test_dir if required.
241245
# utils.shellCommand(execute).run(cwd=test_dir)

riscof/Templates/setup/reference/env/model_test.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef _COMPLIANCE_MODEL_H
22
#define _COMPLIANCE_MODEL_H
3+
34
#define RVMODEL_DATA_SECTION \
45
.pushsection .tohost,"aw",@progbits; \
56
.align 8; .global tohost; tohost: .dword 0; \
@@ -11,22 +12,23 @@
1112
.word 4;
1213

1314
//RV_COMPLIANCE_HALT
14-
#define RVMODEL_HALT \
15-
li x1, 1; \
16-
write_tohost: \
17-
sw x1, tohost, t5; \
18-
j write_tohost;
15+
#define RVMODEL_HALT ;\
16+
li x1, 1 ;\
17+
1: ;\
18+
sw x1, tohost, t2 ;\
19+
j 1b ;\
1920

2021
#define RVMODEL_BOOT
2122

2223
//RV_COMPLIANCE_DATA_BEGIN
2324
#define RVMODEL_DATA_BEGIN \
24-
.align 4; .global begin_signature; begin_signature:
25+
RVMODEL_DATA_SECTION \
26+
.align 4;\
27+
.global begin_signature; begin_signature:
2528

2629
//RV_COMPLIANCE_DATA_END
2730
#define RVMODEL_DATA_END \
28-
.align 4; .global end_signature; end_signature: \
29-
RVMODEL_DATA_SECTION \
31+
.align 4; .global end_signature; end_signature:
3032

3133
//RVTEST_IO_INIT
3234
#define RVMODEL_IO_INIT
@@ -49,5 +51,7 @@
4951

5052
#define RVMODEL_CLEAR_MEXT_INT
5153

54+
#define RVMODEL_PMP_GRAIN 0
55+
#define RVMODEL_NUM_PMPS 16
5256

5357
#endif // _COMPLIANCE_MODEL_H

riscof/Templates/setup/reference/riscof_model.py

Lines changed: 96 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,83 @@
66
import logging
77
import random
88
import string
9+
import json
910
from string import Template
10-
import sys
1111

1212
import riscof.utils as utils
13-
import riscof.constants as constants
1413
from riscof.pluginTemplate import pluginTemplate
14+
import riscof.constants as constants
15+
from riscv_isac.isac import isac
1516

1617
logger = logging.getLogger()
1718

18-
class refname(pluginTemplate):
19-
__model__ = "refname"
20-
21-
#TODO: please update the below to indicate family, version, etc of your DUT.
22-
__version__ = "XXX"
19+
class sail_cSim(pluginTemplate):
20+
__model__ = "sail_c_simulator"
21+
__version__ = "0.5.0"
2322

2423
def __init__(self, *args, **kwargs):
2524
sclass = super().__init__(*args, **kwargs)
2625

2726
config = kwargs.get('config')
28-
29-
self.ref_exe = os.path.join(config['PATH'] if 'PATH' in config else "","refname")
27+
if config is None:
28+
logger.error("Config node for sail_cSim missing.")
29+
raise SystemExit(1)
3030
self.num_jobs = str(config['jobs'] if 'jobs' in config else 1)
31-
self.pluginpath=os.path.abspath(config['pluginpath'])
31+
self.pluginpath = os.path.abspath(config['pluginpath'])
32+
self.sail_exe = { '32' : os.path.join(config['PATH'] if 'PATH' in config else "","riscv_sim_rv32d"),
33+
'64' : os.path.join(config['PATH'] if 'PATH' in config else "","riscv_sim_rv64d")}
3234
self.isa_spec = os.path.abspath(config['ispec']) if 'ispec' in config else ''
3335
self.platform_spec = os.path.abspath(config['pspec']) if 'ispec' in config else ''
3436
self.make = config['make'] if 'make' in config else 'make'
35-
logger.debug("refname plugin initialised using the following configuration.")
37+
logger.debug("SAIL CSim plugin initialised using the following configuration.")
3638
for entry in config:
3739
logger.debug(entry+' : '+config[entry])
3840
return sclass
3941

4042
def initialise(self, suite, work_dir, archtest_env):
4143
self.suite = suite
42-
if shutil.which(self.ref_exe) is None:
43-
logger.error('Please install Executable for DUTNAME to proceed further')
44-
raise SystemExit(1)
4544
self.work_dir = work_dir
46-
47-
#TODO: The following assumes you are using the riscv-gcc toolchain. If
48-
# not please change appropriately
4945
self.objdump_cmd = 'riscv{1}-unknown-elf-objdump -D {0} > {2};'
5046
self.compile_cmd = 'riscv{1}-unknown-elf-gcc -march={0} \
5147
-static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles\
5248
-T '+self.pluginpath+'/env/link.ld\
5349
-I '+self.pluginpath+'/env/\
5450
-I ' + archtest_env
5551

56-
# set all the necessary variables like compile command, elf2hex
57-
# commands, objdump cmds. etc whichever you feel necessary and required
58-
# for your plugin.
59-
6052
def build(self, isa_yaml, platform_yaml):
6153
ispec = utils.load_yaml(isa_yaml)['hart0']
6254
self.xlen = ('64' if 64 in ispec['supported_xlen'] else '32')
55+
self.isa_yaml_path = isa_yaml
6356
self.isa = 'rv' + self.xlen
64-
#TODO: The following assumes you are using the riscv-gcc toolchain. If
65-
# not please change appropriately
6657
self.compile_cmd = self.compile_cmd+' -mabi='+('lp64 ' if 64 in ispec['supported_xlen'] else 'ilp32 ')
6758
if "I" in ispec["ISA"]:
6859
self.isa += 'i'
6960
if "M" in ispec["ISA"]:
7061
self.isa += 'm'
71-
if "C" in ispec["ISA"]:
72-
self.isa += 'c'
62+
if "A" in ispec["ISA"]:
63+
self.isa += 'a'
7364
if "F" in ispec["ISA"]:
7465
self.isa += 'f'
7566
if "D" in ispec["ISA"]:
7667
self.isa += 'd'
68+
if "C" in ispec["ISA"]:
69+
self.isa += 'c'
70+
objdump = "riscv{0}-unknown-elf-objdump".format(self.xlen)
71+
if shutil.which(objdump) is None:
72+
logger.error(objdump+": executable not found. Please check environment setup.")
73+
raise SystemExit(1)
74+
compiler = "riscv{0}-unknown-elf-gcc".format(self.xlen)
75+
if shutil.which(compiler) is None:
76+
logger.error(compiler+": executable not found. Please check environment setup.")
77+
raise SystemExit(1)
78+
if shutil.which(self.sail_exe[self.xlen]) is None:
79+
logger.error(self.sail_exe[self.xlen]+ ": executable not found. Please check environment setup.")
80+
raise SystemExit(1)
81+
if shutil.which(self.make) is None:
82+
logger.error(self.make+": executable not found. Please check environment setup.")
83+
raise SystemExit(1)
7784

78-
# based on the validated isa and platform configure your simulator or
79-
# build your RTL here
80-
81-
def runTests(self, testList, cgf_file=None):
85+
def runTests(self, testList, cgf_file=None, header_file= None):
8286
if os.path.exists(self.work_dir+ "/Makefile." + self.name[:-1]):
8387
os.remove(self.work_dir+ "/Makefile." + self.name[:-1])
8488
make = utils.makeUtil(makefilePath=os.path.join(self.work_dir, "Makefile." + self.name[:-1]))
@@ -94,36 +98,75 @@ def runTests(self, testList, cgf_file=None):
9498
execute = "@cd "+testentry['work_dir']+";"
9599

96100
cmd = self.compile_cmd.format(testentry['isa'].lower(), self.xlen) + ' ' + test + ' -o ' + elf
97-
98-
#TODO: we are using -D to enable compile time macros. If your
99-
# toolchain is not riscv-gcc you may want to change the below code
100101
compile_cmd = cmd + ' -D' + " -D".join(testentry['macros'])
101102
execute+=compile_cmd+";"
102103

103104
execute += self.objdump_cmd.format(elf, self.xlen, 'ref.disass')
104105
sig_file = os.path.join(test_dir, self.name[:-1] + ".signature")
105106

106-
#TODO: You will need to add any other arguments to your DUT
107-
# executable if any in the quotes below
108-
execute += self.ref_exe + ''
109-
110-
#TODO: The following is useful only if your reference model can
111-
# support coverage extraction from riscv-isac. Else leave it
112-
# commented out
113-
114-
#cov_str = ' '
115-
#for label in testentry['coverage_labels']:
116-
# cov_str+=' -l '+label
117-
#if cgf_file is not None:
118-
# coverage_cmd = 'riscv_isac --verbose info coverage -d \
119-
# -t {0}.log --parser-name c_sail -o coverage.rpt \
120-
# --sig-label begin_signature end_signature \
121-
# --test-label rvtest_code_begin rvtest_code_end \
122-
# -e {0}.elf -c {1} -x{2} {3};'.format(\
123-
# test_name, ' -c '.join(cgf_file), self.xlen, cov_str)
124-
#else:
125-
# coverage_cmd = ''
126-
#execute+=coverage_cmd
107+
isa_yaml = utils.load_yaml(self.isa_yaml_path)
108+
# Verify the availability of PMP:
109+
if "PMP" in isa_yaml['hart0']:
110+
pmp_flags = {}
111+
if isa_yaml['hart0']["PMP"]["implemented"] == True:
112+
if "pmp-grain" in isa_yaml['hart0']["PMP"]:
113+
pmp_flags["pmp-grain"] = isa_yaml['hart0']["PMP"]["pmp-grain"]
114+
else:
115+
logger.error("PMP grain not defined")
116+
pmp_flags = ""
117+
if "pmp-count" in isa_yaml['hart0']["PMP"]:
118+
pmp_flags["pmp-count"] = isa_yaml['hart0']["PMP"]["pmp-count"]
119+
else:
120+
logger.error("PMP count not defined")
121+
pmp_flags = ""
122+
else:
123+
pmp_flags = ""
124+
125+
try:
126+
sail_config = subprocess.run(["riscv_sim_rv64d", "--print-default-config"], check= True, text=True, capture_output=True)
127+
sail_config = json.loads(sail_config.stdout)
128+
except subprocess.CalledProcessError as e:
129+
print("riscv_sim_rv64d --print-default-config failed:", e.stderr)
130+
exit(1)
131+
except json.JSONDecodeError:
132+
print("riscv_sim_rv64d --print-default-config output is not valid JSON.")
133+
exit(1)
134+
135+
sail_config["memory"]["pmp"]["grain"] = pmp_flags["pmp-grain"]
136+
sail_config["memory"]["pmp"]["count"] = pmp_flags["pmp-count"]
137+
138+
#For User-configuration: Replace this variable with your configuration. "/home/riscv-arch-test/custom_sail_config.json"
139+
sail_config_path = os.path.join(self.pluginpath, 'env', 'sail_config.json')
140+
141+
# Write the updated configuration back to the file
142+
with open(sail_config_path, 'w', encoding='utf-8') as file:
143+
json.dump(sail_config, file, indent=4)
144+
145+
execute += self.sail_exe[self.xlen] + ' --config={0} -v --trace=step --signature-granularity=8 --test-signature={1} {2} > {3}.log 2>&1;'.format(sail_config_path, sig_file, elf, test_name)
146+
147+
cov_str = ' '
148+
for label in testentry['coverage_labels']:
149+
cov_str+=' -l '+label
150+
151+
cgf_mac = ' '
152+
header_file_flag = ' '
153+
if header_file is not None:
154+
header_file_flag = f' -h {header_file} '
155+
cgf_mac += ' -cm common '
156+
for macro in testentry['mac']:
157+
cgf_mac+=' -cm '+macro
158+
159+
if cgf_file is not None:
160+
coverage_cmd = 'riscv_isac --verbose info coverage -d \
161+
-t {0}.log --parser-name c_sail -o coverage.rpt \
162+
--sig-label begin_signature end_signature \
163+
-e ref.elf -c {1} -x{2} {3} {4} {5};'.format(\
164+
test_name, ' -c '.join(cgf_file), self.xlen, cov_str, header_file_flag, cgf_mac)
165+
else:
166+
coverage_cmd = ''
167+
168+
169+
execute+=coverage_cmd
127170

128171
make.add_target(execute)
129172
make.execute_all(self.work_dir)

0 commit comments

Comments
 (0)