| From ac5b44270399e2bccd9d9d5f34c19494e1cfd833 Mon Sep 17 00:00:00 2001 |
| From: Alex Van Damme <atv@google.com> |
| Date: Wed, 5 Feb 2025 13:40:42 -0800 |
| Subject: [PATCH 1/2] Use systemc in verilator and support verilator in cocotb |
| |
| --- |
| BUILD | 11 +- |
| cocotb/cocotb.bzl | 32 ++++- |
| cocotb/cocotb_wrapper.py | 1 + |
| .../verilator/verilator.BUILD.bazel | 112 +++++++++++++++++- |
| verilator/defs.bzl | 22 ++-- |
| 5 files changed, 163 insertions(+), 15 deletions(-) |
| |
| diff --git a/BUILD b/BUILD |
| index bf28dd5..a6fe964 100644 |
| --- a/BUILD |
| +++ b/BUILD |
| @@ -13,15 +13,24 @@ |
| # limitations under the License. |
| |
| load("@rules_license//rules:license.bzl", "license") |
| +load("@rules_license//rules:license_kind.bzl", "license_kind") |
| |
| package( |
| default_applicable_licenses = ["//:package_license"], |
| - default_visibility = ["//visibility:private"], |
| + default_visibility = ["//visibility:public"], |
| +) |
| + |
| +license_kind( |
| + name = "unencumbered", |
| + conditions = [], |
| ) |
| |
| license( |
| name = "package_license", |
| package_name = "bazel_rules_hdl", |
| + license_kinds = [ |
| + ":unencumbered", |
| + ], |
| ) |
| |
| licenses(["notice"]) |
| diff --git a/cocotb/cocotb.bzl b/cocotb/cocotb.bzl |
| index 8ab664d..6cddd81 100644 |
| --- a/cocotb/cocotb.bzl |
| +++ b/cocotb/cocotb.bzl |
| @@ -15,16 +15,20 @@ |
| """Rules for running tests using Cocotb framework""" |
| |
| load("@rules_python//python:defs.bzl", "PyInfo") |
| +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") |
| load("//verilog:defs.bzl", "VerilogInfo") |
| |
| ## Helpers for parsing arguments |
| |
| def _list_to_argstring(data, argname, attr = None, operation = None): |
| - result = " --{}".format(argname) if data else "" |
| + result = " --{}=".format(argname) if data else "" |
| + elems = [] |
| for value in data: |
| elem = value if attr == None else getattr(value, attr) |
| elem = elem if operation == None else operation(elem) |
| - result += " {}".format(elem) |
| + elems.append(elem) |
| + elems = " ".join(elems) |
| + result += "\"{}\"".format(elems) |
| return result |
| |
| def _dict_to_argstring(data, argname): |
| @@ -159,6 +163,9 @@ def _get_test_command(ctx, verilog_files, vhdl_files): |
| test_module_args |
| ) |
| |
| + if ctx.attr.sim_name == "verilator": |
| + command = "VERILATOR_ROOT=$PWD/external/verilator " + command |
| + |
| return command |
| |
| def _cocotb_test_impl(ctx): |
| @@ -183,8 +190,18 @@ def _cocotb_test_impl(ctx): |
| _collect_transitive_runfiles(ctx), |
| ) |
| |
| + |
| # specify PYTHONPATH for the script |
| - env = {"PYTHONPATH": _get_pythonpath_to_set(ctx)} |
| + env = { |
| + "PYTHONPATH": _get_pythonpath_to_set(ctx), |
| + } |
| + |
| + if ctx.attr.sim_name == "verilator": |
| + cc_toolchain = find_cpp_toolchain(ctx) |
| + env = env | { |
| + "VERILATOR_AR": cc_toolchain.ar_executable, |
| + "VERILATOR_CXX": cc_toolchain.compiler_executable, |
| + } |
| |
| # return the information about testing script and its dependencies |
| return [ |
| @@ -292,11 +309,18 @@ _cocotb_test_attrs = { |
| doc = "Record signal traces", |
| default = True, |
| ), |
| + "_cc_toolchain": attr.label( |
| + doc = "CC compiler.", |
| + default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), |
| + ), |
| } |
| |
| cocotb_test = rule( |
| implementation = _cocotb_test_impl, |
| attrs = _cocotb_test_attrs, |
| - toolchains = ["@rules_python//python:toolchain_type"], |
| + toolchains = [ |
| + "@rules_cc//cc:toolchain_type", |
| + "@rules_python//python:toolchain_type", |
| + ], |
| test = True, |
| ) |
| diff --git a/cocotb/cocotb_wrapper.py b/cocotb/cocotb_wrapper.py |
| index ba37573..03d0dbc 100644 |
| --- a/cocotb/cocotb_wrapper.py |
| +++ b/cocotb/cocotb_wrapper.py |
| @@ -28,6 +28,7 @@ cocotb_build_flags = [ |
| "always", |
| "build_dir", |
| "verbose", |
| + "waves", |
| ] |
| |
| |
| diff --git a/dependency_support/verilator/verilator.BUILD.bazel b/dependency_support/verilator/verilator.BUILD.bazel |
| index f958490..3c6f975 100644 |
| --- a/dependency_support/verilator/verilator.BUILD.bazel |
| +++ b/dependency_support/verilator/verilator.BUILD.bazel |
| @@ -81,6 +81,45 @@ verilator_build_template( |
| version = ":version", |
| ) |
| |
| +verilator_build_template( |
| + name = "verilated_mk", |
| + out = "include/verilated.mk", |
| + substitutions = { |
| + "@AR@": "$${VERILATOR_AR}", |
| + "@CXX@": "$${VERILATOR_CXX}", |
| + "@PYTHON3@": "$${VERILATOR_PYTHON3}", |
| + "@OBJCACHE@": "", |
| + "@PERL@": "perl", |
| + "@CFG_WITH_CCWARN@": "no", |
| + "@CFG_WITH_LONGTESTS@": "no", |
| + "@CFG_CXXFLAGS_PROFILE@": "-pg", |
| + "@CFG_CXXFLAGS_STD@": "", |
| + "@CFG_CXXFLAGS_STD_NEWEST@": "-std=c++17", |
| + "@CFG_CXXFLAGS_NO_UNUSED@": " ".join([ |
| + "-faligned-new", |
| + "-fcf-protection=none", |
| + "-Wno-bool-operation", |
| + "-Wno-overloaded-virtual", |
| + "-Wno-shadow", |
| + "-Wno-sign-compare", |
| + "-Wno-uninitialized", |
| + "-Wno-unused-but-set-parameter", |
| + "-Wno-unused-but-set-variable", |
| + "-Wno-unused-parameter", |
| + "-Wno-unused-variable", |
| + ]), |
| + "@CFG_CXXFLAGS_WEXTRA@": "-Wextra -Wfloat-conversion -Wlogical-op", |
| + "@CFG_CXXFLAGS_COROUTINES@": "-fcoroutines", |
| + "@CFG_CXXFLAGS_PCH@": "-x c++-header", |
| + "@CFG_CXXFLAGS_PCH_I@": "-include", |
| + "@CFG_GCH_IF_CLANG@": "", |
| + "@CFG_LDFLAGS_VERILATED@": "", |
| + "@CFG_LDLIBS_THREADS@": "-lpthread -latomic -lstdc++ -lm", |
| + }, |
| + template = "include/verilated.mk.in", |
| + version = ":version", |
| +) |
| + |
| verilator_astgen( |
| name = "verilator_astgen", |
| srcs = [ |
| @@ -287,7 +326,7 @@ cc_library( |
| ) |
| |
| cc_library( |
| - name = "verilator", |
| + name = "verilator_lib", |
| srcs = [ |
| "include/gtkwave/fastlz.h", |
| "include/gtkwave/fst_config.h", |
| @@ -295,10 +334,12 @@ cc_library( |
| "include/gtkwave/lz4.h", |
| "include/gtkwave/wavealloca.h", |
| "include/verilated_fst_c.cpp", |
| + "include/verilated_fst_sc.cpp", |
| "include/verilated_imp.h", |
| "include/verilated_syms.h", |
| "include/verilated_threads.cpp", |
| "include/verilated_vcd_c.cpp", |
| + "include/verilated_vcd_sc.cpp", |
| "include/verilated_vpi.cpp", |
| "include/verilated.cpp", |
| ], |
| @@ -309,14 +350,17 @@ cc_library( |
| "include/verilated_funcs.h", |
| "include/verilated_intrinsics.h", |
| "include/verilated_sc.h", |
| + "include/verilated_sc_trace.h", |
| "include/verilated_sym_props.h", |
| "include/verilated_threads.h", |
| "include/verilated_timing.h", |
| # Needed for verilated_vcd_c.cpp and verilated_fst_c.cpp |
| "include/verilated_trace_imp.h", |
| + "include/verilated_fst_sc.h", |
| "include/verilated_trace.h", |
| "include/verilated_types.h", |
| "include/verilated_vcd_c.h", |
| + "include/verilated_vcd_sc.h", |
| "include/verilated_vpi.h", |
| "include/verilated.h", |
| "include/verilatedos.h", |
| @@ -327,7 +371,12 @@ cc_library( |
| "-std=c++17", |
| "-ffp-contract=off", |
| ], |
| - includes = ["include"], |
| + includes = [ |
| + "include", |
| + # include systemc header |
| + "systemc/include", |
| + ], |
| + defines = ["VM_SC"], |
| linkopts = [ |
| "-lpthread", |
| ], |
| @@ -339,6 +388,7 @@ cc_library( |
| ], |
| visibility = ["//visibility:public"], |
| deps = [ |
| + "@accellera_systemc//:systemc", |
| "@net_zlib//:zlib", |
| ":vltstd", |
| ], |
| @@ -349,7 +399,7 @@ cc_library( |
| # output name. |
| alias( |
| name = "libverilator", |
| - actual = "verilator", |
| + actual = "verilator_lib", |
| visibility = ["//visibility:public"], |
| ) |
| |
| @@ -376,3 +426,59 @@ cc_binary( |
| visibility = ["//visibility:public"], |
| deps = [":verilator_libV3"], |
| ) |
| + |
| +cc_binary( |
| + name = "verilator_bin", |
| + srcs = ["src/Verilator.cpp"], |
| + copts = [ |
| + "-std=c++17", |
| + "-ffp-contract=off", |
| + ], |
| + linkopts = [ |
| + "-lpthread", |
| + "-latomic", |
| + ], |
| + visibility = ["//visibility:public"], |
| + deps = [":verilator_libV3"], |
| + data = [ |
| + "bin/verilator_includer", |
| + "include/verilated.cpp", |
| + "include/verilated_dpi.cpp", |
| + "include/verilated_dpi.h", |
| + "include/verilated_std.sv", |
| + "include/verilated.mk", |
| + "include/verilated_config.h", |
| + "include/verilated_funcs.h", |
| + "include/verilated_imp.h", |
| + "include/verilated_syms.h", |
| + "include/verilated_sym_props.h", |
| + "include/verilated_threads.cpp", |
| + "include/verilated_threads.h", |
| + "include/verilated_trace.h", |
| + "include/verilated_types.h", |
| + "include/verilated_vpi.cpp", |
| + "include/verilated_vpi.h", |
| + "include/verilated.h", |
| + "include/verilatedos.h", |
| + "include/vltstd/svdpi.h", |
| + "include/vltstd/sv_vpi_user.h", |
| + "include/vltstd/vpi_user.h", |
| + # Tracing support |
| + "include/verilated_fst_c.cpp", |
| + "include/verilated_fst_c.h", |
| + "include/verilated_fst_sc.cpp", |
| + "include/verilated_fst_sc.h", |
| + "include/verilated_vcd_c.cpp", |
| + "include/verilated_vcd_c.h", |
| + "include/verilated_vcd_sc.cpp", |
| + "include/verilated_vcd_sc.h", |
| + "include/verilated_trace_imp.h", |
| + "include/verilated_intrinsics.h", |
| + ], |
| +) |
| + |
| +sh_binary( |
| + name = "verilator", |
| + srcs = ["bin/verilator"], |
| + visibility = ["//visibility:public"], |
| +) |
| diff --git a/verilator/defs.bzl b/verilator/defs.bzl |
| index c3e26d5..fc8af53 100644 |
| --- a/verilator/defs.bzl |
| +++ b/verilator/defs.bzl |
| @@ -16,7 +16,6 @@ |
| """Functions for verilator.""" |
| |
| load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") |
| -load("@rules_cc//cc:defs.bzl", "CcInfo") |
| load("//verilog:defs.bzl", "VerilogInfo") |
| |
| def cc_compile_and_link_static_library(ctx, srcs, hdrs, deps, runfiles, includes = [], defines = []): |
| @@ -104,9 +103,10 @@ def _verilator_cc_library(ctx): |
| verilator_toolchain = ctx.toolchains["@rules_hdl//verilator:toolchain_type"] |
| |
| transitive_srcs = depset([], transitive = [ctx.attr.module[VerilogInfo].dag]) |
| + all_hdrs = [verilog_info_struct.hdrs for verilog_info_struct in transitive_srcs.to_list()] |
| all_srcs = [verilog_info_struct.srcs for verilog_info_struct in transitive_srcs.to_list()] |
| all_data = [verilog_info_struct.data for verilog_info_struct in transitive_srcs.to_list()] |
| - all_files = [src for sub_tuple in (all_srcs + all_data) for src in sub_tuple] |
| + all_files = [src for sub_tuple in (all_srcs + all_data + all_hdrs) for src in sub_tuple] |
| |
| # Filter out .dat files. |
| runfiles = [] |
| @@ -123,16 +123,20 @@ def _verilator_cc_library(ctx): |
| |
| args = ctx.actions.args() |
| args.add("--no-std") |
| - args.add("--cc") |
| + if ctx.attr.systemc: |
| + args.add("--sc") |
| + else: |
| + args.add("--cc") |
| args.add("--Mdir", verilator_output.path) |
| args.add("--top-module", ctx.attr.module_top) |
| args.add("--prefix", prefix) |
| + |
| if ctx.attr.trace: |
| - args.add("--trace") |
| - for verilog_file in verilog_files: |
| - args.add(verilog_file.path) |
| + args.add("--trace-fst") |
| args.add_all(verilator_toolchain.extra_vopts) |
| args.add_all(ctx.attr.vopts, expand_directories = False) |
| + for verilog_file in verilog_files: |
| + args.add(verilog_file.path) |
| |
| ctx.actions.run( |
| arguments = [args], |
| @@ -192,12 +196,16 @@ verilator_cc_library = rule( |
| ), |
| "trace": attr.bool( |
| doc = "Enable tracing for Verilator", |
| - default = False, |
| + default = True, |
| ), |
| "vopts": attr.string_list( |
| doc = "Additional command line options to pass to Verilator", |
| default = ["-Wall"], |
| ), |
| + "systemc": attr.bool( |
| + doc = "Enable SystemC support", |
| + default = True, |
| + ), |
| "_cc_toolchain": attr.label( |
| doc = "CC compiler.", |
| default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), |
| -- |
| 2.50.0.rc0.642.g800a2b2222-goog |
| |