Skip to content

Commit

Permalink
[difftest] print cycles at end of simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
FanShupei authored and Avimitin committed Sep 4, 2024
1 parent c1d7ba8 commit 6f484a7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions difftest/dpi_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod dpi_target;
pub mod dump;
pub mod plusarg;
pub mod util;

pub use dpi_target::DpiTarget;

Expand Down
15 changes: 15 additions & 0 deletions difftest/dpi_common/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! utility functions

pub fn write_perf_json(cycle: u64) {
let mut content = String::new();
content += "{\n";
content += &format!(" \"total_cycles\": {cycle}\n");
content += "}\n";

match std::fs::write("perf.json", &content) {
Ok(()) => {}
Err(e) => {
tracing::error!("failed to write 'perf.json': {e}");
}
}
}
5 changes: 5 additions & 0 deletions difftest/dpi_t1/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ unsafe extern "C" fn t1_cosim_init() {
TARGET.init(|| Driver::new(scope, dump_control, &args));
}

#[no_mangle]
unsafe extern "C" fn t1_cosim_final() {
dpi_common::util::write_perf_json(crate::get_t());
}

/// evaluate at every 1024 cycles, return reason = 0 to continue simulation,
/// other value is used as error code.
#[no_mangle]
Expand Down
3 changes: 2 additions & 1 deletion difftest/dpi_t1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl OnlineArgs {
}

// keep in sync with TestBench.ClockGen
pub const CYCLE_PERIOD: u64 = 20;
// the value is measured in simulation time unit
pub const CYCLE_PERIOD: u64 = 20000;

/// get cycle
pub fn get_t() -> u64 {
Expand Down
5 changes: 5 additions & 0 deletions difftest/dpi_t1rocket/src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ unsafe extern "C" fn t1rocket_cosim_init() {
TARGET.init(|| Driver::new(scope, dump_control, &args));
}

#[no_mangle]
unsafe extern "C" fn t1rocket_cosim_final() {
dpi_common::util::write_perf_json(crate::get_t());
}

/// evaluate at every 1024 cycles, return reason = 0 to continue simulation,
/// other value is used as error code.
#[no_mangle]
Expand Down
3 changes: 2 additions & 1 deletion difftest/dpi_t1rocket/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const EXIT_POS: u32 = 0x4000_0000;
const EXIT_CODE: u32 = 0xdead_beef;

// keep in sync with TestBench.ClockGen
pub const CYCLE_PERIOD: u64 = 20;
// the value is measured in simulation time unit
pub const CYCLE_PERIOD: u64 = 20000;

/// get cycle
pub fn get_t() -> u64 {
Expand Down
4 changes: 4 additions & 0 deletions ipemu/src/TestBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ class TestBench(generator: SerializableModuleGenerator[T1, T1Parameter])
|`endif
|
| import "DPI-C" context function void t1_cosim_init();
| import "DPI-C" context function void t1_cosim_final();
| initial begin
| t1_cosim_init();
| clock = 1'b0;
| reset = 1'b1;
| end
| final begin
| t1_cosim_final();
| end
| initial #(11) reset = 1'b0;
| always #10 clock = ~clock;
|endmodule
Expand Down
5 changes: 5 additions & 0 deletions t1rocketemu/src/TestBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ class TestBench(generator: SerializableModuleGenerator[T1RocketTile, T1RocketTil
| endfunction;
|
| import "DPI-C" context function void t1rocket_cosim_init();
| import "DPI-C" context function void t1rocket_cosim_final();
| initial begin
| t1rocket_cosim_init();
| clock = 1'b0;
| reset = 1'b1;
| end
| final begin
| t1rocket_cosim_final();
| end
|
| initial #(100) reset = 1'b0;
| always #10 clock = ~clock;
|endmodule
Expand Down

0 comments on commit 6f484a7

Please sign in to comment.