Skip to content

Commit

Permalink
[difftest] refactor dpi_pre_link.cc to use std::shared_ptr for Verila…
Browse files Browse the repository at this point in the history
…tedContext and deal with chisel error
  • Loading branch information
Clo91eaf committed Jul 28, 2024
1 parent c79227a commit 3b03972
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 10 additions & 7 deletions difftest/online_drive/dpi_c/dpi_pre_link.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include <VTestBench.h>
#include <VTestBench__Dpi.h>
#include <memory>

class VTestBench;

static VerilatedContext *contextp;
static VTestBench *topp;
static std::shared_ptr<VerilatedContext> contextp;

extern "C" int verilator_main_c(int argc, char **argv) {
// Setup context, defaults, and parse command line
Verilated::debug(0);
contextp = new VerilatedContext();
contextp = std::make_shared<VerilatedContext>();
contextp->fatalOnError(false);
contextp->commandArgs(argc, argv);
#ifdef VM_TRACE
contextp->traceEverOn(true);
#endif

// Construct the Verilated model, from Vtop.h generated from Verilating
topp = new VTestBench(contextp);
std::shared_ptr<VTestBench> topp = std::make_shared<VTestBench>(contextp.get());

// Simulate until $finish
while (!contextp->gotFinish()) {
Expand All @@ -31,14 +31,17 @@ extern "C" int verilator_main_c(int argc, char **argv) {

if (!contextp->gotFinish()) {
VL_DEBUG_IF(VL_PRINTF("+ Exiting without $finish; no events left\n"););
return 1;
}

if (contextp->gotError()) {
VL_DEBUG_IF(VL_PRINTF("+ Exiting due to errors\n"););
return 1;
}

// Final model cleanup
topp->final();

delete topp;
delete contextp;

return 0;
}

Expand Down
14 changes: 9 additions & 5 deletions difftest/online_drive/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,16 @@ pub(crate) fn verilator_main() {
let argc = c_args.len() as c_int;
let argv = c_args_ptr.as_ptr() as *mut *mut c_char;

unsafe {
verilator_main_c(argc, argv);
let verilator_ret = unsafe {
verilator_main_c(argc, argv)
};

if verilator_ret == 0 {
std::fs::write("perf.txt", format!("total_cycles: {}", get_t()))
.expect("fail to write into perf.txt");
} else {
panic!("verilator_main_c return unexpectedly");
}

std::fs::write("perf.txt", format!("total_cycles: {}", get_t()))
.expect("fail to write into perf.txt");
}

#[cfg(feature = "trace")]
Expand Down

0 comments on commit 3b03972

Please sign in to comment.