From d0d6be398892303c8df94560cae7ef01195da683 Mon Sep 17 00:00:00 2001 From: Wajahat Riaz Date: Sat, 17 May 2025 21:31:23 +0500 Subject: [PATCH 1/5] build: Enable C++ exceptions in verilator build Add -fexceptions flag to verilator CFLAGS to support C++ exception handling in the simple system simulation environment. --- examples/simple_system/ibex_simple_system.core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_system/ibex_simple_system.core b/examples/simple_system/ibex_simple_system.core index 11818137a..e919e14ee 100644 --- a/examples/simple_system/ibex_simple_system.core +++ b/examples/simple_system/ibex_simple_system.core @@ -175,7 +175,7 @@ targets: - '--trace-structs' - '--trace-params' - '--trace-max-array 1024' - - '-CFLAGS "-std=c++14 -Wall -DVM_TRACE_FMT_FST -DTOPLEVEL_NAME=ibex_simple_system -g"' + - '-CFLAGS "-std=c++14 -Wall -fexceptions -DVM_TRACE_FMT_FST -DTOPLEVEL_NAME=ibex_simple_system -g"' - '-LDFLAGS "-pthread -lutil -lelf"' - "-Wall" # RAM primitives wider than 64bit (required for ECC) fail to build in From 67f85c66119546653c64c87583bf4a904f719c34 Mon Sep 17 00:00:00 2001 From: Wajahat Riaz Date: Sat, 17 May 2025 21:38:56 +0500 Subject: [PATCH 2/5] modify the CSR testbench Makefile to include the Verilator include path automatically --- dv/cs_registers/Makefile | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dv/cs_registers/Makefile b/dv/cs_registers/Makefile index 3a2f2658d..0ffaf653e 100644 --- a/dv/cs_registers/Makefile +++ b/dv/cs_registers/Makefile @@ -23,11 +23,21 @@ LDFLAGS = -shared CC = $(CXX) # Add svdpi include -TOOLDIR = $(subst bin/$(TOOL),,$(shell which $(TOOL))) ifeq ($(TOOL),vcs) + # For VCS, add include path + TOOLDIR = $(subst bin/$(TOOL),,$(shell which $(TOOL))) INCLUDES += -I$(TOOLDIR)include else - INCLUDES += -I$(TOOLDIR)share/verilator/include/vltstd + # For Verilator, use verilator command to find root directory + ifeq ($(shell which verilator),) + $(error "Verilator not found in PATH. Please install Verilator or add it to your PATH") + endif + VERILATOR_ROOT = $(shell verilator --getenv VERILATOR_ROOT 2>/dev/null) + ifeq ($(VERILATOR_ROOT),) + $(error "Could not determine VERILATOR_ROOT. Please make sure Verilator is properly installed") + endif + # Add both main include directory and vltstd directory where svdpi.h is located + INCLUDES += -I$(VERILATOR_ROOT)/include -I$(VERILATOR_ROOT)/include/vltstd endif .PHONY: all clean From 7ad1a9b9720014339ef031b199817acea2fe74ff Mon Sep 17 00:00:00 2001 From: Wajahat Riaz Date: Sat, 17 May 2025 21:52:20 +0500 Subject: [PATCH 3/5] Adding changes for targes to run on verilator --- dv/cs_registers/Makefile | 44 ++++++++++++++----- dv/cs_registers/tb_cs_registers.core | 4 +- examples/sw/simple_system/common/common.mk | 2 +- .../lowrisc_ip/dv/verilator/memutil_dpi.core | 6 +++ 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/dv/cs_registers/Makefile b/dv/cs_registers/Makefile index 0ffaf653e..c70ec917e 100644 --- a/dv/cs_registers/Makefile +++ b/dv/cs_registers/Makefile @@ -23,21 +23,41 @@ LDFLAGS = -shared CC = $(CXX) # Add svdpi include -ifeq ($(TOOL),vcs) - # For VCS, add include path - TOOLDIR = $(subst bin/$(TOOL),,$(shell which $(TOOL))) - INCLUDES += -I$(TOOLDIR)include +# Check if VERILATOR_ROOT is set, use it first if available +ifdef VERILATOR_ROOT + VERILATOR_INCLUDE = $(VERILATOR_ROOT)/include else - # For Verilator, use verilator command to find root directory - ifeq ($(shell which verilator),) - $(error "Verilator not found in PATH. Please install Verilator or add it to your PATH") + # Try to find Verilator installation directory + TOOLDIR = $(subst bin/$(TOOL),,$(shell which $(TOOL) 2>/dev/null)) + + # Define list of possible Verilator include paths to check + VERILATOR_INCLUDE_PATHS := \ + $(TOOLDIR)share/verilator/include \ + $(TOOLDIR)share/verilator/include/vltstd \ + /usr/share/verilator/include \ + /usr/local/share/verilator/include \ + /opt/verilator/share/verilator/include \ + $(HOME)/verilator/include + + # Find first valid include path + VERILATOR_INCLUDE := $(firstword $(foreach dir,$(VERILATOR_INCLUDE_PATHS),$(if $(wildcard $(dir)/svdpi.h),$(dir),))) + + # If include path not found, provide error messages + ifeq ($(VERILATOR_INCLUDE),) + $(warning WARNING: Could not find Verilator include directory with svdpi.h) + $(warning Searched in: $(VERILATOR_INCLUDE_PATHS)) + $(warning You can manually specify it using: make build-csr-test INCLUDES="-I/path/to/verilator/include -I./env -I./rst_driver -I./reg_driver -I./model") + $(warning Or set VERILATOR_ROOT environment variable pointing to your Verilator installation) endif - VERILATOR_ROOT = $(shell verilator --getenv VERILATOR_ROOT 2>/dev/null) - ifeq ($(VERILATOR_ROOT),) - $(error "Could not determine VERILATOR_ROOT. Please make sure Verilator is properly installed") +endif + +# Add appropriate includes based on tool +ifeq ($(TOOL),vcs) + INCLUDES += -I$(TOOLDIR)include +else ifeq ($(TOOL),verilator) + ifdef VERILATOR_INCLUDE + INCLUDES += -I$(VERILATOR_INCLUDE) -I$(VERILATOR_INCLUDE)/vltstd endif - # Add both main include directory and vltstd directory where svdpi.h is located - INCLUDES += -I$(VERILATOR_ROOT)/include -I$(VERILATOR_ROOT)/include/vltstd endif .PHONY: all clean diff --git a/dv/cs_registers/tb_cs_registers.core b/dv/cs_registers/tb_cs_registers.core index 7f109d9b6..0468bf0c4 100644 --- a/dv/cs_registers/tb_cs_registers.core +++ b/dv/cs_registers/tb_cs_registers.core @@ -121,7 +121,7 @@ targets: - '--trace-structs' - '--trace-params' - '--trace-max-array 1024' - - '-CFLAGS "-std=c++14 -Wall -DTOPLEVEL_NAME=tb_cs_registers -DVM_TRACE_FMT_FST -g"' - - '-LDFLAGS "-pthread -lutil -lelf"' + - '-CFLAGS "-std=c++14 -Wall -DTOPLEVEL_NAME=tb_cs_registers -DVM_TRACE_FMT_FST -g -fexceptions -I$(shell verilator --getenv VERILATOR_ROOT)/include -I$(shell verilator --getenv VERILATOR_ROOT)/include/vltstd"' + - '-LDFLAGS "-pthread -lutil -lelf -fexceptions"' - "-Wall" - '-Wno-fatal' # Do not fail on (style) issues, only warn about them. diff --git a/examples/sw/simple_system/common/common.mk b/examples/sw/simple_system/common/common.mk index 9c4946e6d..c25c68dc4 100644 --- a/examples/sw/simple_system/common/common.mk +++ b/examples/sw/simple_system/common/common.mk @@ -8,7 +8,7 @@ COMMON_SRCS = $(wildcard $(COMMON_DIR)/*.c) INCS := -I$(COMMON_DIR) # ARCH = rv32im # to disable compressed instructions -ARCH ?= rv32imc +ARCH ?= rv32imc_zicsr ifdef PROGRAM PROGRAM_C := $(PROGRAM).c diff --git a/vendor/lowrisc_ip/dv/verilator/memutil_dpi.core b/vendor/lowrisc_ip/dv/verilator/memutil_dpi.core index a8957e16a..ff6d97acb 100644 --- a/vendor/lowrisc_ip/dv/verilator/memutil_dpi.core +++ b/vendor/lowrisc_ip/dv/verilator/memutil_dpi.core @@ -30,3 +30,9 @@ targets: vcs_options: - '-CFLAGS -I../../src/lowrisc_dv_verilator_memutil_dpi_0/cpp' - '-lelf' + verilator: + verilator_options: + - '-CFLAGS' + - '-fexceptions' + - '-LDFLAGS' + - '-fexceptions' From 950583d356fccba71a25ec1d6761fbfc87793709 Mon Sep 17 00:00:00 2001 From: Wajahat Riaz Date: Mon, 19 May 2025 22:54:59 +0500 Subject: [PATCH 4/5] Fixing march flags as per lowrisc-toolchain --- examples/sw/simple_system/common/common.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sw/simple_system/common/common.mk b/examples/sw/simple_system/common/common.mk index c25c68dc4..9c4946e6d 100644 --- a/examples/sw/simple_system/common/common.mk +++ b/examples/sw/simple_system/common/common.mk @@ -8,7 +8,7 @@ COMMON_SRCS = $(wildcard $(COMMON_DIR)/*.c) INCS := -I$(COMMON_DIR) # ARCH = rv32im # to disable compressed instructions -ARCH ?= rv32imc_zicsr +ARCH ?= rv32imc ifdef PROGRAM PROGRAM_C := $(PROGRAM).c From 00833d8d8f727d4a874119920199d01fbb22a18b Mon Sep 17 00:00:00 2001 From: Wajahat Riaz Date: Mon, 19 May 2025 22:55:56 +0500 Subject: [PATCH 5/5] Adding fix for possible paths for verilator include directory and header files --- dv/cs_registers/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dv/cs_registers/Makefile b/dv/cs_registers/Makefile index c70ec917e..eaea29570 100644 --- a/dv/cs_registers/Makefile +++ b/dv/cs_registers/Makefile @@ -35,9 +35,13 @@ else $(TOOLDIR)share/verilator/include \ $(TOOLDIR)share/verilator/include/vltstd \ /usr/share/verilator/include \ + /usr/share/verilator/include/vltstd \ /usr/local/share/verilator/include \ + /usr/local/share/verilator/include/vltstd \ /opt/verilator/share/verilator/include \ - $(HOME)/verilator/include + /opt/verilator/share/verilator/include/vltstd \ + $(HOME)/verilator/include \ + $(HOME)/verilator/include/vltstd # Find first valid include path VERILATOR_INCLUDE := $(firstword $(foreach dir,$(VERILATOR_INCLUDE_PATHS),$(if $(wildcard $(dir)/svdpi.h),$(dir),))) @@ -48,6 +52,7 @@ else $(warning Searched in: $(VERILATOR_INCLUDE_PATHS)) $(warning You can manually specify it using: make build-csr-test INCLUDES="-I/path/to/verilator/include -I./env -I./rst_driver -I./reg_driver -I./model") $(warning Or set VERILATOR_ROOT environment variable pointing to your Verilator installation) + VERILATOR_INCLUDE = # Leave empty or set a default fallback path if applicable endif endif