Skip to content

Commit

Permalink
[difftest] remember cosim_init scope
Browse files Browse the repository at this point in the history
  • Loading branch information
FanShupei authored and sequencer committed Jul 28, 2024
1 parent 19d0999 commit add2391
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
9 changes: 6 additions & 3 deletions difftest/online_dpi/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Mutex;
use tracing::debug;

use crate::drive::Driver;
use crate::svdpi::SvScope;
use crate::OfflineArgs;

pub type SvBitVecVal = u32;
Expand Down Expand Up @@ -219,7 +220,9 @@ unsafe extern "C" fn t1_cosim_init() {
let args = OfflineArgs::parse();
args.common_args.setup_logger().unwrap();

let driver = Box::new(Driver::new(&args));
let scope = SvScope::get_current().expect("failed to get scope in t1_cosim_init");

let driver = Box::new(Driver::new(scope, &args));
let mut dpi_target = DPI_TARGET.lock().unwrap();
assert!(
dpi_target.is_none(),
Expand Down Expand Up @@ -278,11 +281,11 @@ mod dpi_export {
}

#[cfg(feature = "trace")]
pub(crate) fn dump_wave(path: &str) {
pub(crate) fn dump_wave(scope: crate::svdpi::SvScope, path: &str) {
use crate::svdpi;
let path_cstring = CString::new(path).unwrap();

svdpi::set_scope_by_name("TOP.TestBench.clockGen");
svdpi::set_scope(scope);
unsafe {
dpi_export::dump_wave(path_cstring.as_ptr());
}
Expand Down
9 changes: 7 additions & 2 deletions difftest/online_dpi/src/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::{debug, error, info, trace};

use crate::dpi::*;
use crate::get_t;
use crate::svdpi::SvScope;
use crate::OfflineArgs;

struct ShadowMem {
Expand Down Expand Up @@ -97,6 +98,9 @@ impl ShadowMem {
pub(crate) struct Driver {
spike_runner: SpikeRunner,

// SvScope from t1_cosim_init
scope: SvScope,

#[cfg(feature = "trace")]
wave_path: String,
#[cfg(feature = "trace")]
Expand Down Expand Up @@ -151,12 +155,13 @@ fn parse_range(input: &str) -> (u64, u64) {
}

impl Driver {
pub(crate) fn new(args: &OfflineArgs) -> Self {
pub(crate) fn new(scope: SvScope, args: &OfflineArgs) -> Self {
#[cfg(feature = "trace")]
let (dump_start, dump_end) = parse_range(&args.dump_range);

let mut self_ = Self {
spike_runner: SpikeRunner::new(&args.common_args, false),
scope,

#[cfg(feature = "trace")]
wave_path: args.wave_path.to_owned(),
Expand Down Expand Up @@ -269,7 +274,7 @@ impl Driver {

#[cfg(feature = "trace")]
fn start_dump_wave(&mut self) {
dump_wave(&self.wave_path);
dump_wave(self.scope, &self.wave_path);
}

pub(crate) fn step(&mut self) -> SpikeEvent {
Expand Down
23 changes: 22 additions & 1 deletion difftest/online_dpi/src/svdpi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{ffi::CString, ptr};
use std::{ffi::{c_void, CString}, ptr::{self, NonNull}};

#[rustfmt::skip]
pub mod sys;

/// get current simulation time in _simulation time unit_
Expand Down Expand Up @@ -27,3 +28,23 @@ pub fn set_scope_by_name(name: &str) {
sys::svSetScope(scope);
}
}

pub fn set_scope(scope: SvScope) {
unsafe {
sys::svSetScope(scope.ptr.as_ptr());
}
}

#[derive(Debug, Clone, Copy)]
pub struct SvScope {
ptr: NonNull<c_void>,
}

unsafe impl Send for SvScope {}

impl SvScope {
pub fn get_current() -> Option<Self> {
let ptr = unsafe { sys::svGetScope() };
NonNull::new(ptr).map(|ptr| Self { ptr })
}
}
1 change: 1 addition & 0 deletions difftest/online_dpi/src/svvpi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[rustfmt::skip]
pub mod sys;

use std::ptr;
Expand Down
Empty file added difftest/vcs.nix
Empty file.

0 comments on commit add2391

Please sign in to comment.