Skip to content

Commit 551e8f8

Browse files
committed
Change XLEN and FLEN to be configure-time options
This means you only need to compile the emulator once in order to support RV32F, RV32D, RV64F and RV64D.
1 parent 8ff2a70 commit 551e8f8

22 files changed

+1097
-777
lines changed

CMakeLists.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ execute_process(
125125
)
126126
message(STATUS "Sail library directory: ${sail_dir}")
127127

128-
set(DEFAULT_ARCHITECTURES "rv32d;rv64d" CACHE STRING "Architectures to build by default (rv32f|rv64f|rv32d|rv64d)(_rvfi)? " )
129-
130128
option(COVERAGE "Compile with Sail coverage collection enabled.")
131129

132130
# Softfloat support.
@@ -167,8 +165,8 @@ endif()
167165
include(CPack)
168166

169167
# Convenience targets.
170-
add_custom_target(csim DEPENDS riscv_sim_rv32d riscv_sim_rv64d)
171-
add_custom_target(check DEPENDS generated_model_rv32d generated_model_rv64d)
168+
add_custom_target(csim DEPENDS riscv_sim_rv)
169+
add_custom_target(check DEPENDS generated_model_rv)
172170

173171
# TODO: Add `interpret` target.
174172
# TODO: Add hol4 target.

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ Install [Sail](https://github.com/rems-project/sail/). On Linux you can download
3434
$ ./build_simulators.sh
3535
```
3636

37-
will build the simulators in `build/c_emulator/riscv_sim_rv{32,64}d`.
37+
will build the simulator at `build/c_emulator/riscv_sim_rv`.
3838

3939
If you get an error message saying `sail: unknown option '--require-version'.` it's because your Sail compiler is too old. You need version 0.19 or later.
4040

41-
By default the RV32D and RV64D emulators are built, without RVFI-DII support.
42-
You can see a complete list of targets by running `make help` in the
43-
build directory, then e.g.
41+
By default the emulator is built without RVFI-DII support. For RVFI support run
4442

4543
```
46-
$ make -C build riscv_sim_rv64f_rvfi
44+
$ make -C build riscv_sim_rv_rvfi
4745
```
4846

4947
By default `build_simulators.sh` will download and build [libgmp](https://gmplib.org/).
@@ -54,7 +52,7 @@ To use a system installation of libgmp, run `env DOWNLOAD_GMP=FALSE ./build_simu
5452
The simulator can be used to execute small test binaries.
5553

5654
```
57-
$ build/c_emulator/riscv_sim_<arch> <elf-file>
55+
$ build/c_emulator/riscv_sim_rv <elf-file>
5856
```
5957

6058
A suite of RV32 and RV64 test programs derived from the
@@ -66,13 +64,13 @@ can be run using `make test` or `ctest` in the build directory.
6664

6765
The model is configured using a JSON file specifying various tunable
6866
options. The default configuration used for the model can be examined
69-
using `build/c_emulator/riscv_sim_<arch> --print-default-config`. To
67+
using `build/c_emulator/riscv_sim_rv --print-default-config`. To
7068
use a custom configuration, save the default configuration into a
7169
file, edit it as needed, and pass it to the simulator using the
7270
`--config` option.
7371

7472
Information on other options for the simulator is available from
75-
`build/c_emulator/riscv_sim_<arch> -h`.
73+
`build/c_emulator/riscv_sim_rv -h`.
7674

7775
### Booting OS images
7876

c_emulator/CMakeLists.txt

+45-58
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,57 @@ set(EMULATOR_COMMON_SRCS
1414
riscv_softfloat.h
1515
)
1616

17-
foreach (xlen IN ITEMS 32 64)
18-
foreach (flen IN ITEMS 32 64)
19-
foreach (variant IN ITEMS "" "rvfi")
20-
set(arch "rv${xlen}")
21-
if (flen EQUAL 32)
22-
string(APPEND arch "f")
23-
else()
24-
string(APPEND arch "d")
25-
endif()
26-
if (variant)
27-
string(APPEND arch "_${variant}")
28-
endif()
17+
foreach (variant IN ITEMS "" "rvfi")
18+
set(arch "rv")
19+
if (variant)
20+
string(APPEND arch "_${variant}")
21+
endif()
2922

30-
add_executable(riscv_sim_${arch}
31-
"${CMAKE_BINARY_DIR}/riscv_model_${arch}.c"
32-
${EMULATOR_COMMON_SRCS}
33-
)
34-
# The generated model is not warnings-clean, silence them.
35-
# -Wno-self-assing is needed for `zhtif_tohost = zhtif_tohost`
36-
# generated by the sail code to avoid optimizing the function out.
37-
set(_generated_c_warning_opt_out
38-
-Wno-extra
39-
-Wno-unused
40-
-Wno-uninitialized
41-
$<$<BOOL:${HAVE_WSELF_ASSIGN}>:-Wno-self-assign>
42-
)
43-
set_source_files_properties("${CMAKE_BINARY_DIR}/riscv_model_${arch}.c"
44-
PROPERTIES COMPILE_OPTIONS "${_generated_c_warning_opt_out}")
23+
add_executable(riscv_sim_${arch}
24+
"${CMAKE_BINARY_DIR}/riscv_model_${arch}.c"
25+
${EMULATOR_COMMON_SRCS}
26+
)
27+
# The generated model is not warnings-clean, silence them.
28+
# -Wno-self-assing is needed for `zhtif_tohost = zhtif_tohost`
29+
# generated by the sail code to avoid optimizing the function out.
30+
set(_generated_c_warning_opt_out
31+
-Wno-extra
32+
-Wno-unused
33+
-Wno-uninitialized
34+
$<$<BOOL:${HAVE_WSELF_ASSIGN}>:-Wno-self-assign>
35+
)
36+
set_source_files_properties("${CMAKE_BINARY_DIR}/riscv_model_${arch}.c"
37+
PROPERTIES COMPILE_OPTIONS "${_generated_c_warning_opt_out}")
4538

46-
if (NOT arch IN_LIST DEFAULT_ARCHITECTURES)
47-
set_target_properties(riscv_sim_${arch} PROPERTIES EXCLUDE_FROM_ALL TRUE)
48-
endif()
39+
add_dependencies(riscv_sim_${arch} generated_model_${arch})
4940

50-
add_dependencies(riscv_sim_${arch} generated_model_${arch})
41+
target_link_libraries(riscv_sim_${arch}
42+
PRIVATE softfloat sail_runtime default_config GMP::GMP
43+
)
5144

52-
target_link_libraries(riscv_sim_${arch}
53-
PRIVATE softfloat sail_runtime default_config GMP::GMP
54-
)
45+
target_include_directories(riscv_sim_${arch}
46+
# So the generated C can find riscv_platform/prelude.h"
47+
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}"
48+
)
5549

56-
target_include_directories(riscv_sim_${arch}
57-
# So the generated C can find riscv_platform/prelude.h"
58-
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}"
59-
)
50+
if (arch MATCHES "rvfi")
51+
target_compile_definitions(riscv_sim_${arch}
52+
PRIVATE RVFI_DII
53+
)
54+
endif()
6055

61-
if (arch MATCHES "rvfi")
62-
target_compile_definitions(riscv_sim_${arch}
63-
PRIVATE RVFI_DII
64-
)
65-
endif()
56+
# TODO: Enable warnings when we use the #include trick
57+
# to include the generated Sail code. Currently it
58+
# generates too many warnings to turn these on globally.
6659

67-
# TODO: Enable warnings when we use the #include trick
68-
# to include the generated Sail code. Currently it
69-
# generates too many warnings to turn these on globally.
60+
# target_compile_options(riscv_sim_${arch} PRIVATE
61+
# -Wall -Wextra
62+
# # Too annoying at the moment.
63+
# -Wno-unused-parameter
64+
# )
7065

71-
# target_compile_options(riscv_sim_${arch} PRIVATE
72-
# -Wall -Wextra
73-
# # Too annoying at the moment.
74-
# -Wno-unused-parameter
75-
# )
76-
77-
install(TARGETS riscv_sim_${arch}
78-
OPTIONAL
79-
RUNTIME DESTINATION "bin"
80-
)
81-
endforeach()
82-
endforeach()
66+
install(TARGETS riscv_sim_${arch}
67+
OPTIONAL
68+
RUNTIME DESTINATION "bin"
69+
)
8370
endforeach()

c_emulator/riscv_platform.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ mach_bits plat_get_16_random_bits(unit)
2424
return rv_16_random_bits();
2525
}
2626

27-
unit load_reservation(mach_bits addr)
27+
unit load_reservation(sbits addr)
2828
{
29-
reservation = addr;
29+
reservation = addr.bits;
3030
reservation_valid = true;
3131
RESERVATION_DBG("reservation <- %0" PRIx64 "\n", reservation);
3232
return UNIT;
@@ -42,10 +42,10 @@ static mach_bits check_mask()
4242
return (zxlen_val == 32) ? 0x00000000FFFFFFFF : -1;
4343
}
4444

45-
bool match_reservation(mach_bits addr)
45+
bool match_reservation(sbits addr)
4646
{
4747
mach_bits mask = check_mask();
48-
bool ret = reservation_valid && (reservation & mask) == (addr & mask);
48+
bool ret = reservation_valid && (reservation & mask) == (addr.bits & mask);
4949
RESERVATION_DBG("reservation(%c): %0" PRIx64 ", key=%0" PRIx64 ": %s\n",
5050
reservation_valid ? 'v' : 'i', reservation, addr,
5151
ret ? "ok" : "fail");

c_emulator/riscv_platform.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ extern "C" {
99
mach_bits plat_get_16_random_bits(unit);
1010

1111
bool speculate_conditional(unit);
12-
unit load_reservation(mach_bits);
13-
bool match_reservation(mach_bits);
12+
unit load_reservation(sbits);
13+
bool match_reservation(sbits);
1414
unit cancel_reservation(unit);
1515

1616
unit plat_term_write(mach_bits);

c_emulator/riscv_sail.h

+10-29
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ extern "C" {
88

99
typedef int unit;
1010
#define UNIT 0
11-
typedef uint64_t mach_bits;
12-
13-
struct zMisa {
14-
mach_bits zMisa_chunk_0;
15-
};
16-
extern struct zMisa zmisa;
1711

1812
void model_init(void);
1913
void model_fini(void);
@@ -24,9 +18,9 @@ unit ztick_clock(unit);
2418
unit ztick_platform(unit);
2519

2620
#ifdef RVFI_DII
27-
unit zrvfi_set_instr_packet(mach_bits);
28-
mach_bits zrvfi_get_cmd(unit);
29-
mach_bits zrvfi_get_insn(unit);
21+
unit zrvfi_set_instr_packet(uint64_t);
22+
uint64_t zrvfi_get_cmd(unit);
23+
uint64_t zrvfi_get_insn(unit);
3024
bool zrvfi_step(sail_int);
3125
unit zrvfi_zzero_exec_packet(unit);
3226
unit zrvfi_halt_exec_packet(unit);
@@ -43,33 +37,20 @@ unit zprint_rvfi_exec(unit);
4337
unit zprint_instr_packet(uint64_t);
4438
#endif
4539

46-
extern mach_bits zxlen_val;
40+
extern uint64_t zxlen_val;
4741
extern bool zhtif_done;
48-
extern mach_bits zhtif_exit_code;
42+
extern uint64_t zhtif_exit_code;
4943
extern bool have_exception;
5044

5145
/* machine state */
5246

53-
extern uint32_t zcur_privilege;
54-
55-
extern mach_bits zPC;
56-
57-
extern mach_bits zx1, zx2, zx3, zx4, zx5, zx6, zx7, zx8, zx9, zx10, zx11, zx12,
58-
zx13, zx14, zx15, zx16, zx17, zx18, zx19, zx20, zx21, zx22, zx23, zx24,
59-
zx25, zx26, zx27, zx28, zx29, zx30, zx31;
60-
61-
extern mach_bits zmstatus;
62-
extern mach_bits zmepc, zmtval;
63-
extern mach_bits zsepc, zstval;
64-
65-
extern mach_bits zfloat_result, zfloat_fflags;
47+
unit zforce_pc(uint64_t pc);
6648

67-
struct zMcause {
68-
mach_bits zMcause_chunk_0;
69-
};
70-
extern struct zMcause zmcause, zscause;
49+
extern uint64_t zfloat_result, zfloat_fflags;
7150

72-
extern mach_bits zminstret;
51+
// Initialise types based on config values.
52+
void sail_set_abstract_xlen(void);
53+
void sail_set_abstract_ext_d_supported(void);
7354

7455
#ifdef __cplusplus
7556
} // extern "C"

c_emulator/riscv_sim.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ void init_sail_reset_vector(uint64_t entry)
449449
}
450450

451451
/* boot at reset vector */
452-
zPC = rom_base;
452+
zforce_pc(rom_base);
453453
}
454454

455455
void init_sail(uint64_t elf_entry)
@@ -465,15 +465,18 @@ void init_sail(uint64_t elf_entry)
465465
rv_clint_size = UINT64_C(0);
466466
rv_htif_tohost = UINT64_C(0);
467467
*/
468-
zPC = elf_entry;
469-
} else
468+
zforce_pc(elf_entry);
469+
} else {
470470
init_sail_reset_vector(elf_entry);
471+
}
471472
}
472473

473474
/* reinitialize to clear state and memory, typically across tests runs */
474475
void reinit_sail(uint64_t elf_entry)
475476
{
476477
model_fini();
478+
sail_set_abstract_xlen();
479+
sail_set_abstract_ext_d_supported();
477480
model_init();
478481
init_sail(elf_entry);
479482
}
@@ -676,6 +679,8 @@ int main(int argc, char **argv)
676679
{
677680
int files_start = process_args(argc, argv);
678681

682+
sail_set_abstract_xlen();
683+
sail_set_abstract_ext_d_supported();
679684
model_init();
680685

681686
if (do_report_arch) {

config/default.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"base": {
3+
"xlen": 64,
34
"writable_misa": true,
45
"writable_fiom": true,
56
"writable_hpm_counters": {
@@ -49,7 +50,10 @@
4950
"A": {
5051
"supported": true
5152
},
52-
"FD": {
53+
"F": {
54+
"supported": true
55+
},
56+
"D": {
5357
"supported": true
5458
},
5559
"V": {

0 commit comments

Comments
 (0)