Skip to content

Commit

Permalink
support verlator 5, fix djg#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Anillc committed Nov 7, 2023
1 parent a51e3c9 commit 63aedda
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 99 deletions.
2 changes: 1 addition & 1 deletion verilated/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
// cargo:rustc-cfg=KEY[="VALUE"]
let (major, minor) = verilator_version().unwrap();
println!("cargo:rustc-cfg=verilator_version=\"{}.{}\"", major, minor);
if major == 4 && minor >= 38 {
if (major >= 5) || (major == 4 && minor >= 38) {
println!("cargo:rustc-cfg=verilator=\"flush_and_exit_cb\"");
}

Expand Down
4 changes: 2 additions & 2 deletions verilated/src/verilated_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ verilated_fatal_on_vpi_error() {
return Verilated::fatalOnVpiError() ? 1 : 0;
}

#if VERILATOR_VERSION_MAJOR == 4 && VERILATOR_VERSION_MINOR >= 38
#if (VERILATOR_VERSION_MAJOR >= 5) || (VERILATOR_VERSION_MAJOR == 4 && VERILATOR_VERSION_MINOR >= 38)
typedef void (*voidp_cb)(void*); // Callback type for below

/// Callbacks to run on global flush
Expand Down Expand Up @@ -121,7 +121,7 @@ void
verilator_run_exit_callbacks() {
Verilated::runExitCallbacks();
}
#else // !(VERILATOR_VERSION_MAJOR == 4 && VERILATOR_VERSION_MINOR >= 38)
#else // !((VERILATOR_VERSION_MAJOR >= 5) || (VERILATOR_VERSION_MAJOR == 4 && VERILATOR_VERSION_MINOR >= 38))
/// Flush callback for VCD waves
void
verilated_flush_cb(VerilatedVoidCb cb) {
Expand Down
8 changes: 6 additions & 2 deletions verilated/src/verilatedvcdc_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ verilatedvcdc_open_next(VerilatedVcdC* vcd, int inc_filename) {

/// Set size in megabytes after which new file should be created
void
verilatedvcdc_rollover_mb(VerilatedVcdC* vcd, size_t rolloverMB) {
vcd->rolloverMB(rolloverMB);
verilatedvcdc_rollover_mb(VerilatedVcdC* vcd, size_t rolloverSize) {
#if (VERILATOR_VERSION_MAJOR >= 5) || (VERILATOR_VERSION_MAJOR == 4 && VERILATOR_VERSION_MINOR >= 226)
vcd->rolloverSize(rolloverSize);
#else
vcd->rolloverMB(rolloverSize);
#endif
}

/// Close dump
Expand Down
3 changes: 1 addition & 2 deletions verilator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ keywords = ["verilator", "verilog"]
categories = ["development-tools::build-utils","development-tools::ffi", "simulation"]

[dependencies]
cc = { version = "1.0", optional = true }
fnv = { version = "1.0", optional = true }
regex = "1.4"
syn = { version = "0.13", features = ["extra-traits", "full", "visit"], optional = true }

[features]
gen = ["cc"]
gen = []
module = ["fnv", "syn"]
default = ["gen"]
106 changes: 16 additions & 90 deletions verilator/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use verilator_version;
use cc;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -17,8 +15,6 @@ pub enum Standard {

/// Builder style configuration for running verilator.
pub struct Verilator {
target: Option<String>,
host: Option<String>,
out_dir: Option<PathBuf>,
root: Option<PathBuf>,
files: Vec<(PathBuf, Option<Standard>)>,
Expand Down Expand Up @@ -136,9 +132,6 @@ impl Verilator {
let mut cmd = Command::new(verilator_exe.clone());
cmd.arg("--getenv").arg("VERILATOR_ROOT");

// Determine the Verilator version
let (verilator_major, verilator_minor) = verilator_version().unwrap();

println!("running: {:?}", cmd);
let root = match cmd.output() {
Ok(output) => PathBuf::from(String::from_utf8_lossy(&output.stdout).trim()),
Expand All @@ -152,13 +145,16 @@ impl Verilator {
};
println!("verilator root: {:?}", root);

let lib_name = format!("_{top_module}");
// Generate .CPP from .V using verilator
let mut cmd = Command::new(verilator_exe.clone());
cmd.arg("--cc")
.arg("-Mdir")
.arg(&dst)
.arg("--top-module")
.arg(top_module);
.arg(top_module)
.arg("--lib-create")
.arg(lib_name.clone());

if self.coverage {
cmd.arg("--coverage");
Expand Down Expand Up @@ -197,90 +193,22 @@ impl Verilator {
}
}

cmd.arg(file);
// verilator will generate relative path in .d files
// which will cause make cannot find files.
cmd.arg(fs::canonicalize(file).unwrap());
}

run(&mut cmd, "verilator");

// Compile the .CPP into library.
let target = match self.target.clone() {
Some(t) => t,
None => {
let mut t = getenv_unwrap("TARGET");
if t.ends_with("-darwin") {
t += "11";
}
t
}
};
let host = self.host.clone().unwrap_or_else(|| getenv_unwrap("HOST"));

let mut cpp_cfg = cc::Build::new();
cpp_cfg
.cpp(true)
.target(&target)
.host(&host)
.out_dir(&dst)
.define("VL_PRINTF", "printf");

let tool = cpp_cfg.get_compiler();
if tool.is_like_clang() {
cpp_cfg
.flag("-faligned-new")
.flag("-fbracket-depth=4096")
.flag("-Qunused-arguments")
.flag("-Wno-parentheses-equality")
.flag("-Wno-sign-compare")
.flag("-Wno-uninitialized")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-variable")
.flag("-Wno-shadow");
}
if tool.is_like_gnu() {
cpp_cfg
.flag("-std=gnu++17")
.flag("-faligned-new")
.flag("-Wno-bool-operation")
.flag("-Wno-sign-compare")
.flag("-Wno-uninitialized")
.flag("-Wno-unused-but-set-variable")
.flag("-Wno-unused-parameter")
.flag("-Wno-unused-variable")
.flag("-Wno-shadow");
}
cpp_cfg
.include(root.join("include"))
.include(root.join("include/vltstd"))
.include(&dst)
.file(dst.join(format!("V{}.cpp", top_module)))
.file(dst.join(format!("V{}__Syms.cpp", top_module)));

if verilator_major > 4 || verilator_minor >= 38 {
cpp_cfg.file(dst.join(format!("V{}__Slow.cpp", top_module)));
}

for &(ref f, _) in &self.files {
match f.extension() {
Some(ext) if ext == "c" || ext == "cpp" => {
cpp_cfg.file(f);
}
_ => {}
};
}

if self.coverage {
cpp_cfg.define("VM_COVERAGE", "1");
}

if self.trace {
cpp_cfg
.define("VM_TRACE", "1")
.file(dst.join(format!("V{}__Trace.cpp", top_module)))
.file(dst.join(format!("V{}__Trace__Slow.cpp", top_module)));
}

cpp_cfg.compile(&format!("V{}__ALL", top_module));

Command::new("make")
.current_dir(dst.clone())
.args(["-f", &format!("V{}.mk", top_module)])
.spawn()
.unwrap()
.wait()
.unwrap();
println!("cargo:rustc-link-search={}", dst.to_str().unwrap());
println!("cargo:rustc-link-lib={lib_name}");
dst
}

Expand All @@ -307,8 +235,6 @@ impl Verilator {
impl Default for Verilator {
fn default() -> Verilator {
Verilator {
target: None,
host: None,
out_dir: None,
root: None,
files: Vec::new(),
Expand Down
2 changes: 0 additions & 2 deletions verilator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "gen")]
extern crate cc;
#[cfg(feature = "module")]
extern crate fnv;
extern crate regex;
Expand Down

0 comments on commit 63aedda

Please sign in to comment.