Skip to content

Commit

Permalink
Iverilog verilator refactor (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Risto97 authored Sep 15, 2024
1 parent a507e98 commit 3cbbd2a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 48 deletions.
2 changes: 1 addition & 1 deletion SoCMakeConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/cmake/utils/copy_rtl_files/vhier.cmake")
# ====================================

# ----- Verilator ------
include("${CMAKE_CURRENT_LIST_DIR}/cmake/sim/verilator/verilate.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/cmake/sim/verilator/verilator.cmake")

# ----- iverilog ------
include("${CMAKE_CURRENT_LIST_DIR}/cmake/sim/iverilog/iverilog.cmake")
Expand Down
44 changes: 19 additions & 25 deletions cmake/sim/iverilog/iverilog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,8 @@ function(iverilog IP_LIB)
# Get the binary directory of the IP library
get_target_property(BINARY_DIR ${IP_LIB} BINARY_DIR)

# Set the output directory for iverilog results
if(NOT ARG_OUTDIR)
set(OUTDIR ${BINARY_DIR})
else()
set(OUTDIR ${ARG_OUTDIR})
endif()

if(ARG_TOP_MODULE)
set(TOP_MODULE "-s${ARG_TOP_MODULE}")
endif()

# Get the IP RTL sources
get_ip_sources(SOURCES ${IP_LIB} SYSTEMVERILOG VERILOG)
# Where is defined V_SOURCES (if it's defined)?
list(PREPEND SOURCES ${V_SOURCES})
# Get IP include directories
get_ip_include_directories(INC_DIRS ${IP_LIB} SYSTEMVERILOG VERILOG)
# Prepare include directories arguments for iverilog
Expand All @@ -61,41 +48,48 @@ function(iverilog IP_LIB)
list(APPEND CMP_DEFS_ARG -D${def})
endforeach()

# Generator expression for OUTDIR = defined(ARG_OUTDIR) ? ARG_OUTDIR : BINARY_DIR
set(OUTDIR $<IF:$<BOOL:${ARG_OUTDIR}>,${ARG_OUTDIR},${BINARY_DIR}>)
# Set the output executable name
if(NOT ARG_EXECUTABLE)
set(ARG_EXECUTABLE "${OUTDIR}/${IP_LIB}_iv")
endif()
set(ARG_EXECUTABLE $<IF:$<BOOL:${ARG_EXECUTABLE}>,${ARG_EXECUTABLE},${OUTDIR}/${IP_LIB}_iverilog>)

# Set the stamp file path (is the stamp file really needed?)
set(STAMP_FILE "${BINARY_DIR}/${IP_LIB}_${CMAKE_CURRENT_FUNCTION}.stamp")
set(DESCRIPTION "Compile ${IP_LIB} with ${CMAKE_CURRENT_FUNCTION}")

# Add a custom command to run iverilog
add_custom_command(
OUTPUT ${ARG_EXECUTABLE} ${STAMP_FILE}
COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTDIR}
COMMAND iverilog
${TOP_MODULE}
${ARG_INCDIRS}
${CMP_DEFS_ARG}
${ARG_CLI_FLAGS}
-o ${ARG_EXECUTABLE}
${SOURCES}
$<$<BOOL:${ARG_TOP_MODULE}>:-s${ARG_TOP_MODULE}>
${ARG_INCDIRS}
${CMP_DEFS_ARG}
${ARG_CLI_FLAGS}
-o ${ARG_EXECUTABLE}
${SOURCES}
COMMAND touch ${STAMP_FILE}
DEPENDS ${SOURCES}
COMMENT "Running iverilog on ${IP_LIB}"
COMMENT ${DESCRIPTION}
)

# Add a custom target that depends on the executable and stamp file
add_custom_target(
${IP_LIB}_${CMAKE_CURRENT_FUNCTION}
DEPENDS ${ARG_EXECUTABLE} ${STAMP_FILE} ${IP_LIB}
COMMENT ${DESCRIPTION}
)
set_property(TARGET ${IP_LIB}_${CMAKE_CURRENT_FUNCTION} PROPERTY DESCRIPTION ${DESCRIPTION})

set(DESCRIPTION "Run ${CMAKE_CURRENT_FUNCTION} testbench compiled from ${IP_LIB}")
# Add a custom target to run the generated executable
add_custom_target(
run_${IP_LIB}_iv
COMMAND exec ${ARG_EXECUTABLE}
run_${IP_LIB}_${CMAKE_CURRENT_FUNCTION}
COMMAND ${ARG_EXECUTABLE}
DEPENDS ${ARG_EXECUTABLE} ${STAMP_FILE} ${SOURCES} ${IP_LIB}_${CMAKE_CURRENT_FUNCTION}
COMMENT ${DESCRIPTION}
)
set_property(TARGET run_${IP_LIB}_${CMAKE_CURRENT_FUNCTION} PROPERTY DESCRIPTION ${DESCRIPTION})

endfunction()

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(verilate IP_LIB)
function(verilator IP_LIB)
set(OPTIONS "COVERAGE;TRACE;TRACE_FST;SYSTEMC;TRACE_STRUCTS;MAIN")
set(ONE_PARAM_ARGS "PREFIX;TOP_MODULE;THREADS;TRACE_THREADS;DIRECTORY;EXECUTABLE_NAME")
set(MULTI_PARAM_ARGS "VERILATOR_ARGS;OPT_SLOW;OPT_FAST;OPT_GLOBAL")
Expand All @@ -21,7 +21,7 @@ function(verilate IP_LIB)
get_target_property(BINARY_DIR ${IP_LIB} BINARY_DIR)

if(NOT ARG_DIRECTORY)
set(DIRECTORY "${BINARY_DIR}/${IP_LIB}_verilate")
set(DIRECTORY "${BINARY_DIR}/${IP_LIB}_verilator")
else()
set(DIRECTORY ${ARG_DIRECTORY})
endif()
Expand Down Expand Up @@ -58,8 +58,10 @@ function(verilate IP_LIB)
set(PREFIX V${ARG_TOP_MODULE})
endif()

## TODO deprecate
get_ip_property(VERILATOR_ARGS ${IP_LIB} VERILATOR_ARGS)
list(APPEND ARG_VERILATOR_ARGS ${VERILATOR_ARGS})
##

get_ip_compile_definitions(COMP_DEFS ${IP_LIB} SYSTEMVERILOG VERILOG)
foreach(def ${COMP_DEFS})
Expand All @@ -76,7 +78,7 @@ function(verilate IP_LIB)
if(ARG_MAIN)
list(APPEND ARG_VERILATOR_ARGS --main)
if(NOT ARG_EXECUTABLE_NAME)
set(ARG_EXECUTABLE_NAME ${IP_LIB}_verilator_tb)
set(ARG_EXECUTABLE_NAME ${IP_LIB}_verilator_exec)
endif()
set(EXECUTABLE_PATH ${BINARY_DIR}/${ARG_EXECUTABLE_NAME})
unset(ARG_MAIN)
Expand Down Expand Up @@ -111,7 +113,6 @@ function(verilate IP_LIB)
endforeach()
string(REPLACE ";" "|" ARGUMENTS_LIST "${ARGUMENTS_LIST}")


if(ARG_SYSTEMC)
if(NOT SYSTEMC_HOME)
find_package(SystemCLanguage REQUIRED
Expand All @@ -125,6 +126,17 @@ function(verilate IP_LIB)
set(ARG_CMAKE_CXX_STANDARD "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
endif()

##################################
## Prepare help message ##########
##################################
if(EXECUTABLE_PATH)
set(OUTPUT_TYPE "EXECUTABLE")
else()
set(OUTPUT_TYPE "STATIC_LIBRARY")
endif()
set(DESCRIPTION "Compiling ${IP_LIB} with verilator as ${OUTPUT_TYPE}")
###

set(VERILATE_TARGET ${IP_LIB}_verilate)
include(ExternalProject)
ExternalProject_Add(${VERILATE_TARGET}
Expand Down Expand Up @@ -152,6 +164,7 @@ function(verilate IP_LIB)
INSTALL_COMMAND ""
DEPENDS ${IP_LIB}
EXCLUDE_FROM_ALL 1
COMMENT ${DESCRIPTION}
)

set_property(
Expand All @@ -160,6 +173,7 @@ function(verilate IP_LIB)
${DIRECTORY}
${EXECUTABLE_PATH}
)
set_property(TARGET ${VERILATE_TARGET} PROPERTY DESCRIPTION ${DESCRIPTION})

set(VLT_STATIC_LIB "${DIRECTORY}/lib${ARG_TOP_MODULE}.a")
set(INC_DIR ${DIRECTORY})
Expand All @@ -181,4 +195,16 @@ function(verilate IP_LIB)

string(REPLACE "__" "::" ALIAS_NAME "${VERILATED_LIB}")
add_library(${ALIAS_NAME} ALIAS ${VERILATED_LIB})

if(EXECUTABLE_PATH)
set(DESCRIPTION "Run ${CMAKE_CURRENT_FUNCTION} testbench compiled from ${IP_LIB}")
# Add a custom target to run the generated executable
add_custom_target(
run_${IP_LIB}_${CMAKE_CURRENT_FUNCTION}
COMMAND ${EXECUTABLE_PATH}
DEPENDS ${EXECUTABLE_PATH} ${STAMP_FILE} ${VERILATE_TARGET}
COMMENT ${DESCRIPTION}
)
set_property(TARGET run_${IP_LIB}_${CMAKE_CURRENT_FUNCTION} PROPERTY DESCRIPTION ${DESCRIPTION})
endif()
endfunction()
6 changes: 5 additions & 1 deletion cmake/utils/copy_rtl_files/vhier.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ function(vhier IP_LIB)
$<$<BOOL:${ARG_FOREST}>:--forest>
)

set(DESCRIPTION "Extract verilog hierarchy of ${IP_LIB} with ${CMAKE_CURRENT_FUNCTION}")

set(OUT_FILE ${CMAKE_BINARY_DIR}/${IP_LIB}_${CMAKE_CURRENT_FUNCTION}.$<IF:$<BOOL:${ARG_XML}>,xml,txt>)
set(STAMP_FILE "${CMAKE_BINARY_DIR}/${IP_LIB}_${CMAKE_CURRENT_FUNCTION}.stamp")
add_custom_command(
OUTPUT ${STAMP_FILE} ${OUT_FILE}
COMMAND touch ${STAMP_FILE}
COMMAND ${__CMD} | tee ${OUT_FILE}
DEPENDS ${RTL_SOURCES} ${IP_LIB}
COMMENT "Printing verilog hierarchy of ${IP_LIB} with ${CMAKE_CURRENT_FUNCTION}"
COMMENT ${DESCRIPTION}
)

add_custom_target(
${IP_LIB}_${CMAKE_CURRENT_FUNCTION}
DEPENDS ${IP_LIB} ${STAMP_FILE} ${OUT_FILE}
)

set_property(TARGET ${IP_LIB}_${CMAKE_CURRENT_FUNCTION} PROPERTY DESCRIPTION ${DESCRIPTION})
endfunction()

17 changes: 8 additions & 9 deletions examples/sim_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ project(example NONE)

include("deps/deps.cmake")

add_ip(tb # Name of the IP block
VENDOR cern # Vendor name (can be ommited, but not recommended)
LIBRARY ip # Library name (can be ommited, but not recommended)
VERSION 0.0.1 # Version Number (can be ommited, but not recommended)
add_ip(cern::ip::tb::0.0.1
DESCRIPTION "Simple verilog testbench"
)

ip_sources(tb VERILOG # Add source files to the VERILOG file set
ip_sources(${IP} VERILOG # Add source files to the VERILOG file set
${PROJECT_SOURCE_DIR}/tb.v
)

iverilog(tb) # Create iverilog target
iverilog(${IP})

verilate(tb # Create verilate target
MAIN # Let Verilator create a main.cpp testbench
)
verilator(${IP} # Create verilate target
MAIN) # Let Verilator create a main.cpp testbench

help()
2 changes: 1 addition & 1 deletion examples/sim_example/deps/deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake) # Include the CPM.cmake downloader

CPMAddPackage( # Add SoCMake as a package
NAME SoCMake
GIT_TAG master # You can define GIT_TAG or VERSION for versioning
GIT_TAG develop # You can define GIT_TAG or VERSION for versioning
GIT_REPOSITORY "https://github.com/HEP-SoC/SoCMake.git" # GIT_REPOSITORY or URL
)
2 changes: 1 addition & 1 deletion examples/sim_example/tb.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tb;
initial begin
$display("Simulated with Iverilog, from SoCMake build system\n");
$display("Hello world, from SoCMake build system\n");
$finish();
end
endmodule
4 changes: 3 additions & 1 deletion examples/verilator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ project(example CXX C)
include("deps/deps.cmake")
add_subdirectory(adder)

verilate(cern::ip::adder::0.0.1 TRACE)
verilator(cern::ip::adder::0.0.1 TRACE)

add_executable(testbench main.cpp)

target_link_libraries(testbench cern::ip::adder::0.0.1::vlt)

help()
6 changes: 2 additions & 4 deletions examples/verilator/adder/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
cmake_minimum_required(VERSION 3.25)
project(adder NONE)

add_ip(adder
VENDOR cern
LIBRARY ip
VERSION 0.0.1
add_ip(cern::ip::adder::0.0.1
DESCRIPTION "Just a simple adder"
)

ip_sources(adder VERILOG
Expand Down
2 changes: 1 addition & 1 deletion examples/verilator/deps/deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake) # Include the CPM.cmake downloader

CPMAddPackage( # Add SoCMake as a package
NAME SoCMake
GIT_TAG master # You can define GIT_TAG or VERSION for versioning
GIT_TAG develop # You can define GIT_TAG or VERSION for versioning
GIT_REPOSITORY "https://github.com/HEP-SoC/SoCMake.git" # GIT_REPOSITORY or URL
)

0 comments on commit 3cbbd2a

Please sign in to comment.