Skip to content

Commit

Permalink
[difftest] add rtl unexcepted quit check in difftest
Browse files Browse the repository at this point in the history
[difftest] refactor dpi_pre_link.cc to use std::shared_ptr for VerilatedContext and deal with chisel error

[difftest] use return code instead of panic when exiting offline difftest

[difftest] fix wrong unique ptr usage
  • Loading branch information
Clo91eaf authored and sequencer committed Jul 30, 2024
1 parent 1eb32c4 commit c4d5f56
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion difftest/offline/src/difftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Difftest {
JsonEvents::SimulationStop { reason, cycle } => {
info!("simulation stopped at cycle {}, reason {}", cycle, reason);
self.runner.cycle = *cycle;
Ok(())
Err(anyhow::anyhow!("quit: no more events"))
}
JsonEvents::Issue { idx, cycle } => {
self.runner.cycle = *cycle;
Expand Down
2 changes: 1 addition & 1 deletion difftest/offline/src/dut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Dut {
pub fn step(&mut self) -> anyhow::Result<&JsonEvents> {
let event = match self.events.get(self.idx as usize) {
Some(event) => event,
None => return Err(anyhow::anyhow!("no more events")),
None => return Err(anyhow::anyhow!("error: rtl quit unexpectedly")),
};
self.idx += 1;

Expand Down
18 changes: 11 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,24 @@
#include <VTestBench.h>
#include <VTestBench__Dpi.h>
#include <memory>

class VTestBench;

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

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_unique<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);
topp = std::make_unique<VTestBench>(contextp.get());

// Simulate until $finish
while (!contextp->gotFinish()) {
Expand All @@ -31,14 +32,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
16 changes: 10 additions & 6 deletions difftest/online_drive/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
use std::ffi::{c_char, c_int, c_longlong, CString};
use std::ptr;
use std::sync::Mutex;
use tracing::debug;
use tracing::{debug, error};

use crate::drive::Driver;
use crate::OfflineArgs;
Expand Down Expand Up @@ -280,7 +280,7 @@ pub(crate) fn get_t() -> u64 {
unsafe { get_t_c() / 20 }
}

pub(crate) fn verilator_main() {
pub(crate) fn verilator_main() -> i32 {
let c_args: Vec<CString> = std::env::args().map(|arg| CString::new(arg).unwrap()).collect();

let mut c_args_ptr: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect();
Expand All @@ -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 {
error!("verilator_main_c return unexpectedly");
}

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

#[cfg(feature = "trace")]
Expand Down
2 changes: 1 addition & 1 deletion difftest/online_drive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub(crate) struct OfflineArgs {
}

fn main() {
verilator_main();
std::process::exit(verilator_main());
}

0 comments on commit c4d5f56

Please sign in to comment.