From 3b03972a81cae571a72eb99b4fd7a062c1958caa Mon Sep 17 00:00:00 2001 From: Clo91eaf Date: Sun, 28 Jul 2024 23:27:52 +0800 Subject: [PATCH] [difftest] refactor dpi_pre_link.cc to use std::shared_ptr for VerilatedContext and deal with chisel error --- difftest/online_drive/dpi_c/dpi_pre_link.cc | 17 ++++++++++------- difftest/online_drive/src/dpi.rs | 14 +++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/difftest/online_drive/dpi_c/dpi_pre_link.cc b/difftest/online_drive/dpi_c/dpi_pre_link.cc index 914a70997e..0d2565b2cc 100644 --- a/difftest/online_drive/dpi_c/dpi_pre_link.cc +++ b/difftest/online_drive/dpi_c/dpi_pre_link.cc @@ -1,15 +1,15 @@ #include #include +#include class VTestBench; -static VerilatedContext *contextp; -static VTestBench *topp; +static std::shared_ptr 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(); contextp->fatalOnError(false); contextp->commandArgs(argc, argv); #ifdef VM_TRACE @@ -17,7 +17,7 @@ extern "C" int verilator_main_c(int argc, char **argv) { #endif // Construct the Verilated model, from Vtop.h generated from Verilating - topp = new VTestBench(contextp); + std::shared_ptr topp = std::make_shared(contextp.get()); // Simulate until $finish while (!contextp->gotFinish()) { @@ -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; } diff --git a/difftest/online_drive/src/dpi.rs b/difftest/online_drive/src/dpi.rs index fb360386be..c5e6001ab0 100644 --- a/difftest/online_drive/src/dpi.rs +++ b/difftest/online_drive/src/dpi.rs @@ -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")]